forked from zhangyuanqiao/curl-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curljava.c
362 lines (291 loc) · 9.23 KB
/
curljava.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*****************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |
* \___|\___/|_| \_\_____|
*
* Copyright (C) 2001 - 2008 Daniel Stenberg, <[email protected]>, et al.
*
* In order to be useful for every potential user, curl and libcurl are
* dual-licensed under the MPL and the MIT/X-derivate licenses.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the MPL or the MIT/X-derivate
* licenses. You may pick one of these licenses.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* $Id: curljava.c 42 2008-10-20 09:27:21Z patrick $
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h> /* libcurl header */
#include "CurlGlue.h" /* the JNI-generated glue header file */
/*
* This is a private struct allocated for every 'CurlGlue' object.
*/
struct jcallback {
jmethodID mid;
JNIEnv *java;
jclass cls; /* global reference */
jobject object;
};
struct curljava {
void *libcurl;
void *whatever;
struct jcallback write;
struct jcallback read;
};
/*
* CurlGlue.version()
*/
JNIEXPORT jstring JNICALL Java_CurlGlue_version(JNIEnv * java, jclass myclass)
{
return (*java)->NewStringUTF(java, curl_version());
}
JNIEXPORT jint JNICALL Java_CurlGlue_jni_1init(JNIEnv *java,
jobject myself)
{
void *libhandle;
struct curljava *jcurl=NULL;
libhandle = curl_easy_init();
if(libhandle) {
jcurl=(struct curljava *)malloc(sizeof(struct curljava));
if(jcurl) {
memset(jcurl, 0, sizeof(struct curljava));
jcurl->libcurl = libhandle;
}
else {
curl_easy_cleanup(libhandle);
return (jint)0;
}
}
return (jint) jcurl; /* nasty typecast */
}
JNIEXPORT void JNICALL Java_CurlGlue_jni_1cleanup(JNIEnv *java,
jobject myself,
jint jcurl)
{
struct curljava *curl = (struct curljava*)jcurl;
if(curl->write.cls) {
/* a global reference we must delete */
(*java)->DeleteGlobalRef(java, curl->write.cls);
(*java)->DeleteGlobalRef(java, curl->write.object);
}
if(curl->read.cls) {
/* a global reference we must delete */
(*java)->DeleteGlobalRef(java, curl->read.cls);
(*java)->DeleteGlobalRef(java, curl->read.object);
}
curl_easy_cleanup(curl->libcurl); /* cleanup libcurl stuff */
free((void *)curl); /* free the struct too */
}
/*
* setopt() int + string
*/
JNIEXPORT jint JNICALL Java_CurlGlue_jni_1setopt__IILjava_lang_String_2
(JNIEnv *java, jobject myself, jint jcurl, jint option, jstring value)
{
/* get the actual string C-style */
const char *str = (*java)->GetStringUTFChars(java, value, 0);
void *handle = (void *)((struct curljava*)jcurl)->libcurl;
#ifdef DEBUG
puts("setopt int + string");
#endif
return (jint)curl_easy_setopt(handle, (CURLoption)option, str);
}
/*
* setopt() int + int
*/
JNIEXPORT jint JNICALL Java_CurlGlue_jni_1setopt__III
(JNIEnv *java, jobject myself, jint jcurl, jint option, jint value)
{
void *handle = (void *)((struct curljava*)jcurl)->libcurl;
CURLoption opt = (CURLoption)option;
#ifdef DEBUG
puts("setopt int + int");
#endif
switch(opt) {
case CURLOPT_WRITEDATA:
case CURLOPT_READDATA:
/* silently ignored, we don't need user-specified callback data when
we have an object, and besides the CURLOPT_WRITEDATA and CURLOPT_READDATA
are not exported to the java interface */
return 0;
}
return (jint) curl_easy_setopt(handle, (CURLoption) option, value);
}
static int curljava_write_callback(void *ptr,
size_t size,
size_t nmemb,
FILE *stream)
{
struct curljava *curl = (struct curljava *)stream;
size_t realsize = size * nmemb;
JNIEnv *java = curl->write.java;
jbyteArray jb=NULL;
int ret=0;
#ifdef DEBUG
fprintf(stderr, "%d bytes data received in callback:\n"
"ptr=%p, java=%p cls=%p\n",
realsize, curl, java, curl->write.cls);
#endif
jb=(*java)->NewByteArray(java, realsize);
(*java)->SetByteArrayRegion(java, jb, 0,
realsize, (jbyte *)ptr);
#ifdef DEBUG
fprintf(stderr, "created byte-array\n");
#endif
ret = (*java)->CallIntMethod(java,
curl->write.object,
curl->write.mid,
jb);
#ifdef DEBUG
fprintf(stderr, "java-method returned %d\n", ret);
#endif
return realsize;
}
/*
* setopt() int + object
*/
JNIEXPORT jint JNICALL Java_CurlGlue_jni_1setopt__IILCurlWrite_2
(JNIEnv *java, jobject myself, jint jcurl, jint option, jobject object)
{
jclass cls_local = (*java)->GetObjectClass(java, object);
jmethodID mid;
struct curljava *curl = (struct curljava *)jcurl;
jclass cls;
jobject obj_global;
switch(option) {
case CURLOPT_WRITEFUNCTION:
/* this makes a reference that'll be alive until we kill it! */
cls = (*java)->NewGlobalRef(java, cls_local);
#ifdef DEBUG
printf("setopt int + object, option = %d cls= %p\n",
option, cls);
#endif
if(!cls) {
fputs("couldn't make local reference global\n", stderr);
return 0;
}
/* this is the write callback */
mid = (*java)->GetMethodID(java, cls, "handleString", "([B)I");
if(!mid) {
fputs("no callback method found\n", stderr);
return 0;
}
obj_global = (*java)->NewGlobalRef(java, object);
curl->write.mid = mid;
curl->write.cls = cls;
curl->write.object = obj_global;
/*curl->write.java = java; stored on perform */
#ifdef DEBUG
fprintf(stderr,
"setopt write callback and write file pointer %p, java = %p\n",
curl, java);
#endif
curl_easy_setopt(curl->libcurl, CURLOPT_WRITEFUNCTION,
curljava_write_callback);
curl_easy_setopt(curl->libcurl, CURLOPT_FILE,
curl);
break;
}
return 0;
}
static int curljava_read_callback(void *ptr,
size_t size,
size_t nmemb,
FILE *stream)
{
struct curljava *curl = (struct curljava *)stream;
size_t realsize = size * nmemb;
JNIEnv *java = curl->read.java;
jbyteArray jb;
int ret;
jb = (*java)->NewByteArray(java, realsize);
ret = (*java)->CallIntMethod(java, curl->read.object, curl->read.mid, jb);
if (ret > 0) {
if (ret > realsize)
ret = realsize;
(*java)->GetByteArrayRegion(java, jb, 0, ret, ptr);
}
return ret;
}
/*
* setopt() int + object
*/
JNIEXPORT jint JNICALL Java_CurlGlue_jni_1setopt__IILCurlRead_2
(JNIEnv *java, jobject myself, jint jcurl, jint option, jobject object)
{
jclass cls_local = (*java)->GetObjectClass(java, object);
jmethodID mid;
struct curljava *curl = (struct curljava *)jcurl;
jclass cls;
jobject obj_global;
switch(option) {
case CURLOPT_READFUNCTION:
/* this makes a reference that'll be alive until we kill it! */
cls = (*java)->NewGlobalRef(java, cls_local);
#ifdef DEBUG
printf("setopt int + object, option = %d cls= %p\n",
option, cls);
#endif
if(!cls) {
fputs("couldn't make local reference global\n", stderr);
return 0;
}
/* this is the read callback */
mid = (*java)->GetMethodID(java, cls, "retrieveString", "([B)I");
if(!mid) {
fputs("no callback method found\n", stderr);
return 0;
}
obj_global = (*java)->NewGlobalRef(java, object);
curl->read.mid = mid;
curl->read.cls = cls;
curl->read.object = obj_global;
#ifdef DEBUG
fprintf(stderr,
"setopt read callback and read file pointer %p, java = %p\n",
curl, java);
#endif
curl_easy_setopt(curl->libcurl, CURLOPT_READFUNCTION,
curljava_read_callback);
curl_easy_setopt(curl->libcurl, CURLOPT_READDATA,
curl);
break;
}
return 0;
}
/*
* setopt() int + object
*/
JNIEXPORT jint JNICALL Java_CurlGlue_jni_1setopt__IILCurlIO_2
(JNIEnv *java, jobject myself, jint jcurl, jint option, jobject object)
{
switch(option) {
case CURLOPT_WRITEFUNCTION:
return Java_CurlGlue_jni_1setopt__IILCurlWrite_2(java, myself,
jcurl, option, object);
case CURLOPT_READFUNCTION:
return Java_CurlGlue_jni_1setopt__IILCurlRead_2(java, myself,
jcurl, option, object);
}
return 0;
}
JNIEXPORT jint JNICALL Java_CurlGlue_getinfo
(JNIEnv *java, jobject value)
{
return 0;
}
JNIEXPORT jint JNICALL Java_CurlGlue_jni_1perform
(JNIEnv *java, jobject myself, jint jcurl)
{
struct curljava *curl=(struct curljava*)jcurl;
curl->write.java = java;
curl->read.java = java;
return (jint)curl_easy_perform(curl->libcurl);
}