From 6012a3374081d92eaade74b15f1c072aaa6ec09f Mon Sep 17 00:00:00 2001 From: Paul Joseph Davis Date: Wed, 8 Jul 2009 01:26:04 +0000 Subject: Fixes COUCHDB-402 Thanks Mark Hammond git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@792010 13f79535-47bb-0310-9956-ffa450edef68 --- THANKS | 1 + src/couchdb/couch_js.c | 103 +++++++++++++++++++++++------------------------ src/couchdb/curlhelper.c | 22 ++++------ 3 files changed, 60 insertions(+), 66 deletions(-) diff --git a/THANKS b/THANKS index 5304615e..b10bd9ea 100644 --- a/THANKS +++ b/THANKS @@ -31,5 +31,6 @@ suggesting improvements or submitting changes. Some of these people are: * Nick Gerakines * Robert Newson * Bob Dionne + * Mark Hammond For a list of authors see the `AUTHORS` file. diff --git a/src/couchdb/couch_js.c b/src/couchdb/couch_js.c index 43f2da12..9a4a3941 100644 --- a/src/couchdb/couch_js.c +++ b/src/couchdb/couch_js.c @@ -434,21 +434,21 @@ typedef struct buffer_counter { }* BufferCount; size_t curl_read(void *ptr, size_t size, size_t nmemb, void *stream) { - if( size == 0 || nmemb == 0) { - return 0; - } - + int readlength, spaceleft, i; char* databuffer = (char*)ptr; Buffer b = ((BufferCount)stream)->buffer; int* pos = &(((BufferCount)stream)->pos); + if( size == 0 || nmemb == 0) { + return 0; + } + if((b->count - *pos) == 0) { return 0; } - int readlength = size*nmemb; - int spaceleft = b->count - *pos; - int i; + readlength = size*nmemb; + spaceleft = b->count - *pos; if(readlength < spaceleft) { copy_Buffer(b,databuffer,*pos,readlength); @@ -462,11 +462,10 @@ size_t curl_read(void *ptr, size_t size, size_t nmemb, void *stream) { } size_t curl_write(void *ptr, size_t size, size_t nmemb, void *stream) { - if( size == 0 || nmemb == 0 ) - return 0; - char *data, *tmp; Buffer b; + if( size == 0 || nmemb == 0 ) + return 0; data = (char *)ptr; b = (Buffer)stream; @@ -478,13 +477,13 @@ size_t curl_write(void *ptr, size_t size, size_t nmemb, void *stream) { // This uses MALLOC dont forget to free char* JSValToChar(JSContext* context, jsval* arg) { - if(!JSVAL_IS_STRING(*arg)) { - return NULL; - } - char *c, *tmp; JSString *jsmsg; size_t len; + int i; + if(!JSVAL_IS_STRING(*arg)) { + return NULL; + } jsmsg = JS_ValueToString(context,*arg); len = JS_GetStringLength(jsmsg); @@ -493,8 +492,6 @@ char* JSValToChar(JSContext* context, jsval* arg) { c = (char*)malloc(len+1); c[len] = '\0'; - int i; - for(i = 0;i < len;i++) { c[i] = tmp[i]; } @@ -534,17 +531,23 @@ struct curl_slist* generateCurlHeaders(JSContext* context,jsval* arg) { struct curl_slist *slist = NULL; JSObject* header_obj; + JSObject* iterator; + jsval *jsProperty; + jsval *jsValue; + jsid *jsId; + Buffer bTmp; + char* jsPropertyName, *jsPropertyValue; // If we fail to convert arg2 to an object. Error! if(!JS_ValueToObject(context,*arg,&header_obj)) { return NULL; } - JSObject* iterator = JS_NewPropertyIterator(context,header_obj); + iterator = JS_NewPropertyIterator(context,header_obj); - jsval *jsProperty = JS_malloc(context,sizeof(jsval)); - jsval *jsValue = JS_malloc(context,sizeof(jsval)); - jsid *jsId = JS_malloc(context,sizeof(jsid)); + jsProperty = JS_malloc(context,sizeof(jsval)); + jsValue = JS_malloc(context,sizeof(jsval)); + jsId = JS_malloc(context,sizeof(jsid)); while(JS_NextProperty(context,iterator,jsId) == JS_TRUE) { @@ -554,16 +557,16 @@ struct curl_slist* generateCurlHeaders(JSContext* context,jsval* arg) { // TODO: Refactor this maybe make a JSValAppendBuffer method b/c that is what you really want to do. - Buffer bTmp = init_Buffer(); + bTmp = init_Buffer(); JS_IdToValue(context,*jsId,jsProperty); - char* jsPropertyName = JSValToChar(context,jsProperty); + jsPropertyName = JSValToChar(context,jsProperty); // TODO: Remove strlen =/ append_Buffer(bTmp,jsPropertyName,strlen(jsPropertyName)); append_Buffer(bTmp,": ",2); JS_GetProperty(context,header_obj,jsPropertyName,jsValue); - char* jsPropertyValue = JSValToChar(context,jsValue); + jsPropertyValue = JSValToChar(context,jsValue); // TODO: Remove strlen =/ append_Buffer(bTmp,jsPropertyValue,strlen(jsPropertyValue)); append_Buffer(bTmp,"",1); @@ -592,6 +595,8 @@ GetHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) Buffer b; char *url; size_t charslen, readlen; + struct curl_slist *slist; + int exitcode; // Run GC JS_MaybeGC(context); @@ -620,14 +625,12 @@ GetHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) curl_easy_setopt(handle,CURLOPT_NOPROGRESS,1); curl_easy_setopt(handle,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4); - struct curl_slist *slist = generateCurlHeaders(context,argv+1); + slist = generateCurlHeaders(context,argv+1); if(slist != NULL) { curl_easy_setopt(handle,CURLOPT_HTTPHEADER,slist); } // Perform - int exitcode; - if((exitcode = curl_easy_perform(handle)) != 0) { if(slist != NULL) { curl_slist_free_all(slist); @@ -676,6 +679,8 @@ HeadHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval Buffer b; char *url; size_t charslen, readlen; + struct curl_slist *slist; + int exitcode; // Run GC JS_MaybeGC(context); @@ -704,7 +709,7 @@ HeadHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval curl_easy_setopt(handle,CURLOPT_NOPROGRESS,1); curl_easy_setopt(handle,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4); - struct curl_slist *slist = generateCurlHeaders(context,argv+1); + slist = generateCurlHeaders(context,argv+1); if(slist != NULL) { curl_easy_setopt(handle,CURLOPT_HTTPHEADER,slist); } @@ -712,8 +717,6 @@ HeadHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval // fprintf(stderr, "about to run HEAD request\n"); // Perform - int exitcode; - if((exitcode = curl_easy_perform(handle)) != 0) { if(slist != NULL) { curl_slist_free_all(slist); @@ -764,6 +767,8 @@ PostHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval Buffer b; char *url, *body; size_t charslen, readlen; + struct curl_slist *slist; + int exitcode; // Run GC JS_MaybeGC(context); @@ -801,13 +806,11 @@ PostHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval curl_easy_setopt(handle,CURLOPT_POSTFIELDSIZE,strlen(body)); curl_easy_setopt(handle,CURLOPT_POSTFIELDS,body); // Curl wants '\0' terminated, we oblige - struct curl_slist *slist = generateCurlHeaders(context,argv+2); // Initialize Headers + slist = generateCurlHeaders(context,argv+2); // Initialize Headers if(slist != NULL) { curl_easy_setopt(handle,CURLOPT_HTTPHEADER,slist); } - int exitcode; - if((exitcode = curl_easy_perform(handle)) != 0) { // Perform curl_slist_free_all(slist); free(body); @@ -849,6 +852,9 @@ PutHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) char *url, *data; size_t charslen, readlen; JSObject* header_obj; + CURL* handle; + struct curl_slist *slist; + int exitcode; // Run GC JS_MaybeGC(context); @@ -874,8 +880,6 @@ PutHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) free(data); - CURL* handle; - // Init Curl if((handle = curl_easy_init()) == NULL) { @@ -897,7 +901,7 @@ PutHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) // Curl structure - struct curl_slist *slist = generateCurlHeaders(context,argv+2); + slist = generateCurlHeaders(context,argv+2); if(slist != NULL) { curl_easy_setopt(handle,CURLOPT_HTTPHEADER,slist); } @@ -911,8 +915,6 @@ PutHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) // Perform - int exitcode; - if((exitcode = curl_easy_perform(handle)) != 0) { if(slist != NULL) curl_slist_free_all(slist); @@ -960,6 +962,10 @@ DelHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) char *url; size_t charslen, readlen; char header_name[7]; + CURL* handle; + int exitcode; + struct curl_slist *slist = NULL; + strcpy(header_name,"DELETE"); // Run GC @@ -971,8 +977,6 @@ DelHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) // Allocate buffer that will store the del resultant b = init_Buffer(); - CURL* handle; - // Init Curl if((handle = curl_easy_init()) == NULL) { free_Buffer(b); @@ -990,14 +994,11 @@ DelHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval) curl_easy_setopt(handle,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4); // Curl structure - struct curl_slist *slist = NULL; if((slist = generateCurlHeaders(context,argv+1)) != NULL) { curl_easy_setopt(handle,CURLOPT_HTTPHEADER,slist); } // Perform - int exitcode; - if((exitcode = curl_easy_perform(handle)) != 0) { if(slist != NULL) curl_slist_free_all(slist); @@ -1042,6 +1043,10 @@ CopyHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval char *url; size_t charslen, readlen; char header_name[5]; + CURL* handle; + int exitcode; + struct curl_slist *slist = NULL; + strcpy(header_name,"COPY"); // Run GC @@ -1053,8 +1058,6 @@ CopyHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval // Allocate buffer that will store the del resultant b = init_Buffer(); - CURL* handle; - // Init Curl if((handle = curl_easy_init()) == NULL) { free_Buffer(b); @@ -1072,14 +1075,11 @@ CopyHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval curl_easy_setopt(handle,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4); // Curl structure - struct curl_slist *slist = NULL; if((slist = generateCurlHeaders(context,argv+1)) != NULL) { curl_easy_setopt(handle,CURLOPT_HTTPHEADER,slist); } // Perform - int exitcode; - if((exitcode = curl_easy_perform(handle)) != 0) { if(slist != NULL) curl_slist_free_all(slist); @@ -1124,6 +1124,10 @@ MoveHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval char *url; size_t charslen, readlen; char header_name[5]; + CURL* handle; + struct curl_slist *slist = NULL; + int exitcode; + strcpy(header_name,"MOVE"); // Run GC @@ -1135,8 +1139,6 @@ MoveHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval // Allocate buffer that will store the del resultant b = init_Buffer(); - CURL* handle; - // Init Curl if((handle = curl_easy_init()) == NULL) { free_Buffer(b); @@ -1154,14 +1156,11 @@ MoveHttp(JSContext *context, JSObject *obj, uintN argc, jsval *argv, jsval *rval curl_easy_setopt(handle,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4); // Curl structure - struct curl_slist *slist = NULL; if((slist = generateCurlHeaders(context,argv+1)) != NULL) { curl_easy_setopt(handle,CURLOPT_HTTPHEADER,slist); } // Perform - int exitcode; - if((exitcode = curl_easy_perform(handle)) != 0) { if(slist != NULL) curl_slist_free_all(slist); diff --git a/src/couchdb/curlhelper.c b/src/couchdb/curlhelper.c index 116612cd..9fb8a93f 100644 --- a/src/couchdb/curlhelper.c +++ b/src/couchdb/curlhelper.c @@ -48,6 +48,7 @@ void free_Buffer(Buffer b) { int append_Buffer(Buffer b, char* c, int length) { int capacity_changed; int new_count; + int i; capacity_changed = FALSE; new_count = b->count + length; @@ -67,8 +68,6 @@ int append_Buffer(Buffer b, char* c, int length) { } } - int i; - for(i = 0;i < length;i++) { *(b->data + b->count + i) = *(c + i); } @@ -80,6 +79,7 @@ int append_Buffer(Buffer b, char* c, int length) { char* toString_Buffer(Buffer b) { char* result; + int i; if((result = (char*)malloc(sizeof(char)*(b->count+1))) == NULL) { return NULL; @@ -87,8 +87,6 @@ char* toString_Buffer(Buffer b) { result[b->count] = '\0'; - int i; - for(i = 0;i < b->count;i++) { result[i] = b->data[i]; } @@ -122,11 +120,10 @@ int shrink_Buffer(Buffer b) { } void copy_Buffer(Buffer b, char* c, int pos, int length) { + int i; if((pos + length) > b->count) return; - int i; - for(i = 0; i < length;i++) { *(c + i) = *(b->data + pos + i); } @@ -134,11 +131,10 @@ void copy_Buffer(Buffer b, char* c, int pos, int length) { List init_List(int capacity) { + List l; if(capacity < 5) capacity = 5; - List l; - if((l = (List)malloc(sizeof(void**)+sizeof(int)*2)) == NULL) { return NULL; } @@ -193,6 +189,7 @@ int set_List(List l, int pos, void* ptr) { int append_List(List l, void* ptr, int length) { int capacity_changed; int new_count; + int i; capacity_changed = FALSE; new_count = l->count + length; @@ -212,10 +209,8 @@ int append_List(List l, void* ptr, int length) { } } - int i; - for(i = 0;i < length;i++) { - *(l->elements + l->count + i) = ptr + i; + *(l->elements + l->count + i) = (void *)((char *)ptr + i); } l->count = new_count; @@ -226,6 +221,7 @@ int append_List(List l, void* ptr, int length) { int push_List(List l, void* ptr, int length) { int capacity_changed; int new_count; + int i; capacity_changed = FALSE; new_count = l->count + length; @@ -245,14 +241,12 @@ int push_List(List l, void* ptr, int length) { } } - int i; - for(i = 0;i < length;i++) { *(l->elements + l->count + i) = *(l->elements + i); } for(i = 0;i < length;i++) { - *(l->elements + i) = ptr+i; + *(l->elements + i) = (void *)((char *)ptr+i); } l->count = new_count; -- cgit v1.2.3