2001-04-10 11:29:32 -04:00
|
|
|
|
|
|
|
#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 javacurl {
|
|
|
|
void *libcurl;
|
|
|
|
void *whatever;
|
|
|
|
struct writecallback {
|
|
|
|
jmethodID mid;
|
|
|
|
JNIEnv *java;
|
2001-04-11 09:45:55 -04:00
|
|
|
jclass cls; /* global reference */
|
2001-04-11 06:06:28 -04:00
|
|
|
jobject object;
|
2001-04-10 11:29:32 -04:00
|
|
|
} write;
|
|
|
|
};
|
|
|
|
|
|
|
|
JNIEXPORT jint JNICALL Java_CurlGlue_jni_1init(JNIEnv *java,
|
|
|
|
jobject myself)
|
|
|
|
{
|
|
|
|
void *libhandle;
|
|
|
|
struct javacurl *jcurl=NULL;
|
|
|
|
|
|
|
|
libhandle = curl_easy_init();
|
|
|
|
|
|
|
|
if(libhandle) {
|
|
|
|
jcurl=(struct javacurl *)malloc(sizeof(struct javacurl));
|
|
|
|
if(jcurl) {
|
|
|
|
memset(jcurl, 0, sizeof(struct javacurl));
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
|
2001-04-11 09:45:55 -04:00
|
|
|
struct javacurl *curl = (struct javacurl*)jcurl;
|
2001-04-10 11:29:32 -04:00
|
|
|
|
2001-04-11 09:45:55 -04:00
|
|
|
if(curl->write.cls) {
|
|
|
|
/* a global reference we must delete */
|
|
|
|
(*java)->DeleteGlobalRef(java, curl->write.cls);
|
|
|
|
(*java)->DeleteGlobalRef(java, curl->write.object);
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_easy_cleanup(curl->libcurl); /* cleanup libcurl stuff */
|
|
|
|
|
|
|
|
free((void *)curl); /* free the struct too */
|
2001-04-10 11:29:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 javacurl*)jcurl)->libcurl;
|
|
|
|
|
|
|
|
puts("setopt int + string");
|
|
|
|
|
|
|
|
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 javacurl*)jcurl)->libcurl;
|
|
|
|
|
|
|
|
puts("setopt int + int");
|
|
|
|
|
|
|
|
return (jint)curl_easy_setopt(handle, (CURLoption)option, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int javacurl_write_callback(void *ptr,
|
|
|
|
size_t size,
|
|
|
|
size_t nmemb,
|
|
|
|
FILE *stream)
|
|
|
|
{
|
|
|
|
struct javacurl *curl = (struct javacurl *)stream;
|
|
|
|
size_t realsize = size * nmemb;
|
|
|
|
JNIEnv *java = curl->write.java;
|
2001-04-11 06:06:28 -04:00
|
|
|
jbyteArray jb=NULL;
|
|
|
|
int ret=0;
|
|
|
|
|
2001-04-11 09:45:55 -04:00
|
|
|
fprintf(stderr, "%d bytes data received in callback:\n"
|
|
|
|
"ptr=%p, java=%p cls=%p\n",
|
|
|
|
realsize, curl, java, curl->write.cls);
|
2001-04-10 11:29:32 -04:00
|
|
|
|
|
|
|
jb=(*java)->NewByteArray(java, realsize);
|
|
|
|
(*java)->SetByteArrayRegion(java, jb, 0,
|
|
|
|
realsize, (jbyte *)ptr);
|
|
|
|
|
2001-04-11 06:06:28 -04:00
|
|
|
fprintf(stderr, "created byte-array\n");
|
|
|
|
|
2001-04-10 11:29:32 -04:00
|
|
|
ret = (*java)->CallIntMethod(java,
|
2001-04-11 06:06:28 -04:00
|
|
|
curl->write.object,
|
2001-04-10 11:29:32 -04:00
|
|
|
curl->write.mid,
|
|
|
|
jb);
|
|
|
|
|
2001-04-11 06:06:28 -04:00
|
|
|
fprintf(stderr, "java-method returned %d\n", ret);
|
|
|
|
|
2001-04-10 11:29:32 -04:00
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* setopt() int + object
|
|
|
|
*/
|
|
|
|
|
|
|
|
JNIEXPORT jint JNICALL Java_CurlGlue_jni_1setopt__IILCurlWrite_2
|
2001-04-11 06:06:28 -04:00
|
|
|
(JNIEnv *java, jobject myself, jint jcurl, jint option, jobject object)
|
2001-04-10 11:29:32 -04:00
|
|
|
{
|
2001-04-11 09:45:55 -04:00
|
|
|
jclass cls_local = (*java)->GetObjectClass(java, object);
|
2001-04-10 11:29:32 -04:00
|
|
|
jmethodID mid;
|
|
|
|
struct javacurl *curl = (struct javacurl *)jcurl;
|
2001-04-11 09:45:55 -04:00
|
|
|
jclass cls;
|
|
|
|
jobject obj_global;
|
2001-04-10 11:29:32 -04:00
|
|
|
|
|
|
|
switch(option) {
|
|
|
|
case CURLOPT_WRITEFUNCTION:
|
2001-04-11 09:45:55 -04:00
|
|
|
/* this makes a reference that'll be alive until we kill it! */
|
|
|
|
cls = (*java)->NewGlobalRef(java, cls_local);
|
|
|
|
|
|
|
|
printf("setopt int + object, option = %d cls= %p\n",
|
|
|
|
option, cls);
|
|
|
|
|
|
|
|
if(!cls) {
|
|
|
|
puts("couldn't make local reference global");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-04-10 11:29:32 -04:00
|
|
|
/* this is the write callback */
|
2001-04-11 09:45:55 -04:00
|
|
|
mid = (*java)->GetMethodID(java, cls, "handleString", "([B)I");
|
2001-04-10 11:29:32 -04:00
|
|
|
if(!mid) {
|
|
|
|
puts("no callback method found");
|
|
|
|
return 0;
|
|
|
|
}
|
2001-04-11 09:45:55 -04:00
|
|
|
|
|
|
|
obj_global = (*java)->NewGlobalRef(java, object);
|
|
|
|
|
2001-04-10 11:29:32 -04:00
|
|
|
curl->write.mid = mid;
|
2001-04-11 09:45:55 -04:00
|
|
|
curl->write.cls = cls;
|
|
|
|
curl->write.object = obj_global;
|
|
|
|
/*curl->write.java = java; stored on perform */
|
2001-04-10 11:29:32 -04:00
|
|
|
|
2001-04-11 06:06:28 -04:00
|
|
|
fprintf(stderr, "setopt write callback and write file pointer %p, java = %p\n",
|
|
|
|
curl, java);
|
2001-04-10 11:29:32 -04:00
|
|
|
|
|
|
|
curl_easy_setopt(curl->libcurl, CURLOPT_WRITEFUNCTION,
|
|
|
|
javacurl_write_callback);
|
|
|
|
curl_easy_setopt(curl->libcurl, CURLOPT_FILE,
|
|
|
|
curl);
|
2001-04-11 09:45:55 -04:00
|
|
|
|
2001-04-10 11:29:32 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2001-04-11 09:45:55 -04:00
|
|
|
struct javacurl *curl=(struct javacurl*)jcurl;
|
|
|
|
curl->write.java = java;
|
|
|
|
return (jint)curl_easy_perform(curl->libcurl);
|
2001-04-10 11:29:32 -04:00
|
|
|
}
|