From a2314225e02ea2f3bd49dc8557f2452846e49b19 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 31 Mar 2008 10:02:23 +0000 Subject: [PATCH] - Added CURLFORM_STREAM as a supported option to curl_formadd() to allow an application to provide data for a multipart with the read callback. Note that the size needs to be provided with CURLFORM_CONTENTSLENGTH when the stream option is used. This feature is verified by the new test case 554. This feature was sponsored by Xponaut. --- CHANGES | 7 ++ RELEASE-NOTES | 4 +- docs/libcurl/curl_formadd.3 | 29 +++---- include/curl/curl.h | 12 ++- lib/formdata.c | 152 ++++++++++++++++++++++++------------ lib/formdata.h | 10 ++- lib/http.c | 2 + tests/data/test554 | 68 ++++++++++++++++ tests/libtest/Makefile.am | 4 +- tests/libtest/lib554.c | 130 ++++++++++++++++++++++++++++++ 10 files changed, 340 insertions(+), 78 deletions(-) create mode 100644 tests/data/test554 create mode 100644 tests/libtest/lib554.c diff --git a/CHANGES b/CHANGES index e1bcb9456..7efb8267b 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,13 @@ Changelog +Daniel Stenberg (31 Mar 2008) +- Added CURLFORM_STREAM as a supported option to curl_formadd() to allow an + application to provide data for a multipart with the read callback. Note + that the size needs to be provided with CURLFORM_CONTENTSLENGTH when the + stream option is used. This feature is verified by the new test case + 554. This feature was sponsored by Xponaut. + Daniel Fandrich (30 Mar 2008) - Changed the makefile so the doc/examples/ programs are never built in a normal build/install (only with the 'make check' target), so that a diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 5eda86711..e97294987 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -6,11 +6,11 @@ Curl and libcurl 7.18.2 Public functions in libcurl: 56 Public web site mirrors: 39 Known libcurl bindings: 36 - Contributors: 621 + Contributors: 636 This release includes the following changes: - o + o CURLFORM_STREAM was added This release includes the following bugfixes: diff --git a/docs/libcurl/curl_formadd.3 b/docs/libcurl/curl_formadd.3 index 8508dd411..f3cea0054 100644 --- a/docs/libcurl/curl_formadd.3 +++ b/docs/libcurl/curl_formadd.3 @@ -38,9 +38,7 @@ parts you want to add to your post. The options listed first are for making normal parts. The options from \fICURLFORM_FILE\fP through \fICURLFORM_BUFFERLENGTH\fP are for file upload parts. - .SH OPTIONS - .IP CURLFORM_COPYNAME followed by a string which provides the \fIname\fP of this part. libcurl copies the string so your application doesn't need to keep it around after @@ -48,14 +46,12 @@ this function call. If the name isn't NUL-terminated, or if you'd like it to contain zero bytes, you must set its length with \fBCURLFORM_NAMELENGTH\fP. The copied data will be freed by \fIcurl_formfree(3)\fP. - .IP CURLFORM_PTRNAME followed by a string which provides the \fIname\fP of this part. libcurl will use the pointer and refer to the data in your application, so you must make sure it remains until curl no longer needs it. If the name isn't NUL-terminated, or if you'd like it to contain zero bytes, you must set its length with \fBCURLFORM_NAMELENGTH\fP. - .IP CURLFORM_COPYCONTENTS followed by a pointer to the contents of this part, the actual data to send away. libcurl copies the provided data, so your application doesn't @@ -63,57 +59,55 @@ need to keep it around after this function call. If the data isn't null terminated, or if you'd like it to contain zero bytes, you must set the length of the name with \fBCURLFORM_CONTENTSLENGTH\fP. The copied data will be freed by \fIcurl_formfree(3)\fP. - .IP CURLFORM_PTRCONTENTS followed by a pointer to the contents of this part, the actual data to send away. libcurl will use the pointer and refer to the data in your application, so you must make sure it remains until curl no longer needs it. If the data isn't NUL-terminated, or if you'd like it to contain zero bytes, you must set its length with \fBCURLFORM_CONTENTSLENGTH\fP. - .IP CURLFORM_CONTENTSLENGTH -followed by a long giving the length of the contents. - +followed by a long giving the length of the contents. Note that for +\fICURLFORM_STREAM\fP contents, this option is mandatory. .IP CURLFORM_FILECONTENT followed by a filename, causes that file to be read and its contents used as data in this part. This part does \fInot\fP automatically become a file upload part simply because its data was read from a file. - .IP CURLFORM_FILE followed by a filename, makes this part a file upload part. It sets the \fIfilename\fP field to the basename of the provided filename, it reads the contents of the file and passes them as data and sets the content-type if the given file match one of the internally known file extensions. For \fBCURLFORM_FILE\fP the user may send one or more files in one part by -providing multiple \fBCURLFORM_FILE\fP arguments each followed by the -filename (and each CURLFORM_FILE is allowed to have a CURLFORM_CONTENTTYPE). - +providing multiple \fBCURLFORM_FILE\fP arguments each followed by the filename +(and each \fICURLFORM_FILE\fP is allowed to have a +\fICURLFORM_CONTENTTYPE\fP). .IP CURLFORM_CONTENTTYPE is used in combination with \fICURLFORM_FILE\fP. Followed by a pointer to a string which provides the content-type for this part, possibly instead of an internally chosen one. - .IP CURLFORM_FILENAME is used in combination with \fICURLFORM_FILE\fP. Followed by a pointer to a string, it tells libcurl to use the given string as the \fIfilename\fP in the file upload part instead of the actual file name. - .IP CURLFORM_BUFFER is used for custom file upload parts without use of \fICURLFORM_FILE\fP. It tells libcurl that the file contents are already present in a buffer. The parameter is a string which provides the \fIfilename\fP field in the content header. - .IP CURLFORM_BUFFERPTR is used in combination with \fICURLFORM_BUFFER\fP. The parameter is a pointer to the buffer to be uploaded. This buffer must not be freed until after \fIcurl_easy_cleanup(3)\fP is called. You must also use \fICURLFORM_BUFFERLENGTH\fP to set the number of bytes in the buffer. - .IP CURLFORM_BUFFERLENGTH is used in combination with \fICURLFORM_BUFFER\fP. The parameter is a long which gives the length of the buffer. - +.IP CURLFORM_STREAM +Tells libcurl to use the \fICURLOPT_READFUNCTION\fP callback to get data. The +parameter you pass to \fICURLFORM_STREAM\fP is the pointer passed on to the +read callback's fourth argument. If you want the part to look like a file +upload one, set the \fICURLFORM_FILENAME\fP parameter as well. (Option added +in libcurl 7.18.2) .IP CURLFORM_ARRAY Another possibility to send options to curl_formadd() is the \fBCURLFORM_ARRAY\fP option, that passes a struct curl_forms array pointer as @@ -121,7 +115,6 @@ its value. Each curl_forms structure element has a CURLformoption and a char pointer. The final element in the array must be a CURLFORM_END. All available options can be used in an array, except the CURLFORM_ARRAY option itself! The last argument in such an array must always be \fBCURLFORM_END\fP. - .IP CURLFORM_CONTENTHEADER specifies extra headers for the form POST section. This takes a curl_slist prepared in the usual way using \fBcurl_slist_append\fP and appends the list diff --git a/include/curl/curl.h b/include/curl/curl.h index b4ed5d0f0..bd188cdd1 100644 --- a/include/curl/curl.h +++ b/include/curl/curl.h @@ -211,10 +211,16 @@ struct curl_httppost { do not free in formfree */ #define HTTPPOST_BUFFER (1<<4) /* upload file from buffer */ #define HTTPPOST_PTRBUFFER (1<<5) /* upload file from pointer contents */ +#define HTTPPOST_CALLBACK (1<<6) /* upload fiel contents by using the + regular read callback to get the data + and pass the given pointer as custom + pointer */ char *showfilename; /* The file name to show. If not set, the actual file name will be used (if this is a file part) */ + void *userp; /* custom pointer used for + HTTPPOST_CALLBACK posts */ }; typedef int (*curl_progress_callback)(void *clientp, @@ -246,7 +252,7 @@ typedef size_t (*curl_write_callback)(char *buffer, #define CURL_READFUNC_PAUSE 0x10000001 typedef int (*curl_seek_callback)(void *instream, curl_off_t offset, - int origin); /* 'whence' */ + int origin); /* 'whence' */ typedef size_t (*curl_read_callback)(char *buffer, size_t size, @@ -1313,6 +1319,8 @@ typedef enum { CFINIT(END), CFINIT(OBSOLETE2), + CFINIT(STREAM), + CURLFORM_LASTENTRY /* the last unusued */ } CURLformoption; @@ -1796,7 +1804,7 @@ CURL_EXTERN CURLcode curl_easy_pause(CURL *handle, int bitmask); !defined(__cplusplus) #include "typecheck-gcc.h" #else -#if defined(__STDC__) && (__STDC__ >= 1) +#if defined(__STDC__) && (__STDC__ >= 1) /* This preprocessor magic that replaces a call with the exact same call is only done to make sure application authors pass exactly three arguments to these functions. */ diff --git a/lib/formdata.c b/lib/formdata.c index d80dc3d14..420f85f17 100644 --- a/lib/formdata.c +++ b/lib/formdata.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2007, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2008, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -159,13 +159,13 @@ static size_t readfromfile(struct Form *form, char *buffer, size_t size); * ***************************************************************************/ static struct curl_httppost * -AddHttpPost(char * name, size_t namelength, - char * value, size_t contentslength, - char * buffer, size_t bufferlength, +AddHttpPost(char *name, size_t namelength, + char *value, size_t contentslength, + char *buffer, size_t bufferlength, char *contenttype, long flags, struct curl_slist* contentHeader, - char *showfilename, + char *showfilename, char *userp, struct curl_httppost *parent_post, struct curl_httppost **httppost, struct curl_httppost **last_post) @@ -182,6 +182,7 @@ AddHttpPost(char * name, size_t namelength, post->contenttype = contenttype; post->contentheader = contentHeader; post->showfilename = showfilename; + post->userp = userp, post->flags = flags; } else @@ -597,7 +598,7 @@ CURLFORMcode FormAdd(struct curl_httppost **httppost, } case CURLFORM_BUFFERPTR: - current_form->flags |= HTTPPOST_PTRBUFFER; + current_form->flags |= HTTPPOST_PTRBUFFER; if(current_form->buffer) return_value = CURL_FORMADD_OPTION_TWICE; else { @@ -618,6 +619,25 @@ CURLFORMcode FormAdd(struct curl_httppost **httppost, array_state?(size_t)array_value:(size_t)va_arg(params, long); break; + case CURLFORM_STREAM: + current_form->flags |= HTTPPOST_CALLBACK; + if(current_form->userp) + return_value = CURL_FORMADD_OPTION_TWICE; + else { + char *userp = + array_state?array_value:va_arg(params, char *); + if(userp) { + current_form->userp = userp; + current_form->value = userp; /* this isn't strictly true but we + derive a value from this later on + and we need this non-NULL to be + accepted as a fine form part */ + } + else + return_value = CURL_FORMADD_NULL; + } + break; + case CURLFORM_CONTENTTYPE: { const char *contenttype = @@ -693,18 +713,18 @@ CURLFORMcode FormAdd(struct curl_httppost **httppost, form != NULL; form = form->more) { if( ((!form->name || !form->value) && !post) || - ( (form->contentslength) && - (form->flags & HTTPPOST_FILENAME) ) || - ( (form->flags & HTTPPOST_FILENAME) && - (form->flags & HTTPPOST_PTRCONTENTS) ) || + ( (form->contentslength) && + (form->flags & HTTPPOST_FILENAME) ) || + ( (form->flags & HTTPPOST_FILENAME) && + (form->flags & HTTPPOST_PTRCONTENTS) ) || - ( (!form->buffer) && - (form->flags & HTTPPOST_BUFFER) && - (form->flags & HTTPPOST_PTRBUFFER) ) || + ( (!form->buffer) && + (form->flags & HTTPPOST_BUFFER) && + (form->flags & HTTPPOST_PTRBUFFER) ) || - ( (form->flags & HTTPPOST_READFILE) && - (form->flags & HTTPPOST_PTRCONTENTS) ) - ) { + ( (form->flags & HTTPPOST_READFILE) && + (form->flags & HTTPPOST_PTRCONTENTS) ) + ) { return_value = CURL_FORMADD_INCOMPLETE; break; } @@ -731,10 +751,9 @@ CURLFORMcode FormAdd(struct curl_httppost **httppost, } form->name_alloc = TRUE; } - if( !(form->flags & HTTPPOST_FILENAME) && - !(form->flags & HTTPPOST_READFILE) && - !(form->flags & HTTPPOST_PTRCONTENTS) && - !(form->flags & HTTPPOST_PTRBUFFER) ) { + if( !(form->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE | + HTTPPOST_PTRCONTENTS | HTTPPOST_PTRBUFFER | + HTTPPOST_CALLBACK)) ) { /* copy value (without strdup; possibly contains null characters) */ form->value = memdup(form->value, form->contentslength); if(!form->value) { @@ -748,6 +767,7 @@ CURLFORMcode FormAdd(struct curl_httppost **httppost, form->buffer, form->bufferlength, form->contenttype, form->flags, form->contentheader, form->showfilename, + form->userp, post, httppost, last_post); @@ -824,18 +844,25 @@ static CURLcode AddFormData(struct FormData **formp, return CURLE_OUT_OF_MEMORY; newform->next = NULL; - /* we make it easier for plain strings: */ - if(!length) - length = strlen((char *)line); + if(type <= FORM_CONTENT) { + /* we make it easier for plain strings: */ + if(!length) + length = strlen((char *)line); - newform->line = (char *)malloc(length+1); - if(!newform->line) { - free(newform); - return CURLE_OUT_OF_MEMORY; + newform->line = (char *)malloc(length+1); + if(!newform->line) { + free(newform); + return CURLE_OUT_OF_MEMORY; + } + memcpy(newform->line, line, length); + newform->length = length; + newform->line[length]=0; /* zero terminate for easier debugging */ } - memcpy(newform->line, line, length); - newform->length = length; - newform->line[length]=0; /* zero terminate for easier debugging */ + else + /* For callbacks and files we don't have any actual data so we just keep a + pointer to whatever this points to */ + newform->line = (char *)line; + newform->type = type; if(*formp) { @@ -846,7 +873,9 @@ static CURLcode AddFormData(struct FormData **formp, *formp = newform; if(size) { - if((type == FORM_DATA) || (type == FORM_CONTENT)) + if(type != FORM_FILE) + /* for static content as well as callback data we add the size given + as input argument */ *size += length; else { /* Since this is a file to be uploaded here, add the size of the actual @@ -893,7 +922,8 @@ void Curl_formclean(struct FormData **form_ptr) do { next=form->next; /* the following form line */ - free(form->line); /* free the line */ + if(form->type <= FORM_CONTENT) + free(form->line); /* free the line */ free(form); /* free the struct */ } while((form = next) != NULL); /* continue */ @@ -997,7 +1027,8 @@ void curl_formfree(struct curl_httppost *form) if( !(form->flags & HTTPPOST_PTRNAME) && form->name) free(form->name); /* free the name */ - if( !(form->flags & HTTPPOST_PTRCONTENTS) && form->contents) + if( !(form->flags & (HTTPPOST_PTRCONTENTS|HTTPPOST_CALLBACK)) && + form->contents) free(form->contents); /* free the contents */ if(form->contenttype) free(form->contenttype); /* free the content type */ @@ -1188,9 +1219,11 @@ CURLcode Curl_getFormData(struct FormData **finalform, if(result) break; } - else if((post->flags & HTTPPOST_FILENAME) || - (post->flags & HTTPPOST_BUFFER)) { - + else if(post->flags & (HTTPPOST_FILENAME|HTTPPOST_BUFFER| + HTTPPOST_CALLBACK)) { + /* it should be noted that for the HTTPPOST_FILENAME and + HTTPPOST_CALLBACK cases the ->showfilename struct member is always + assigned at this point */ char *filebasename= (!post->showfilename)?strippath(post->contents):NULL; @@ -1312,7 +1345,14 @@ CURLcode Curl_getFormData(struct FormData **finalform, if(result) break; } - + else if(post->flags & HTTPPOST_CALLBACK) { + /* the contents should be read with the callback and the size + is set with the contentslength */ + result = AddFormData(&form, FORM_CALLBACK, post->userp, + post->contentslength, &size); + if(result) + break; + } else { /* include the contents we got */ result = AddFormData(&form, FORM_CONTENT, post->contents, @@ -1380,21 +1420,29 @@ int Curl_FormInit(struct Form *form, struct FormData *formdata ) return 0; } -static size_t readfromfile(struct Form *form, char *buffer, size_t size) +static size_t readfromfile(struct Form *form, char *buffer, + size_t size) { size_t nread; - if(!form->fp) { - /* this file hasn't yet been opened */ - form->fp = fopen(form->data->line, "rb"); /* b is for binary */ - if(!form->fp) - return (size_t)-1; /* failure */ - } - nread = fread(buffer, 1, size, form->fp); + bool callback = (bool)(form->data->type == FORM_CALLBACK); - if(nread != size) { + if(callback) + nread = form->fread_func(buffer, 1, size, form->data->line); + else { + if(!form->fp) { + /* this file hasn't yet been opened */ + form->fp = fopen(form->data->line, "rb"); /* b is for binary */ + if(!form->fp) + return (size_t)-1; /* failure */ + } + nread = fread(buffer, 1, size, form->fp); + } + if(!nread || nread > size) { /* this is the last chunk from the file, move on */ - fclose(form->fp); - form->fp = NULL; + if(!callback) { + fclose(form->fp); + form->fp = NULL; + } form->data = form->data->next; } @@ -1421,7 +1469,8 @@ size_t Curl_FormReader(char *buffer, if(!form->data) return 0; /* nothing, error, empty */ - if(form->data->type == FORM_FILE) { + if((form->data->type == FORM_FILE) || + (form->data->type == FORM_CALLBACK)) { gotsize = readfromfile(form, buffer, wantedsize); if(gotsize) @@ -1449,10 +1498,9 @@ size_t Curl_FormReader(char *buffer, form->data = form->data->next; /* advance */ - } while(form->data && (form->data->type != FORM_FILE)); + } while(form->data && (form->data->type < FORM_CALLBACK)); /* If we got an empty line and we have more data, we proceed to the next - line immediately to avoid returning zero before we've reached the end. - This is the bug reported November 22 1999 on curl 6.3. (Daniel) */ + line immediately to avoid returning zero before we've reached the end. */ return gotsize; } diff --git a/lib/formdata.h b/lib/formdata.h index 4ca0f3c5c..04f139322 100644 --- a/lib/formdata.h +++ b/lib/formdata.h @@ -8,7 +8,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2007, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2008, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -27,8 +27,10 @@ enum formtype { FORM_DATA, /* form metadata (convert to network encoding if necessary) */ FORM_CONTENT, /* form content (never convert) */ - FORM_FILE /* 'line' points to a file name we should read from - to create the form data (never convert) */ + FORM_CALLBACK, /* 'line' points to the custom pointer we pass to the callback + */ + FORM_FILE /* 'line' points to a file name we should read from + to create the form data (never convert) */ }; /* plain and simple linked list with lines to send */ @@ -44,6 +46,7 @@ struct Form { size_t sent; /* number of bytes of the current line that has already been sent in a previous invoke */ FILE *fp; /* file to read from */ + curl_read_callback fread_func; /* fread callback pointer */ }; /* used by FormAdd for temporary storage */ @@ -62,6 +65,7 @@ typedef struct FormInfo { char *showfilename; /* The file name to show. If not set, the actual file name will be used */ bool showfilename_alloc; + char *userp; /* pointer for the read callback */ struct curl_slist* contentheader; struct FormInfo *more; } FormInfo; diff --git a/lib/http.c b/lib/http.c index a76c16053..356741fe8 100644 --- a/lib/http.c +++ b/lib/http.c @@ -2507,6 +2507,8 @@ CURLcode Curl_http(struct connectdata *conn, bool *done) } /* set the read function to read from the generated form data */ + http->form.fread_func = conn->fread_func; /* get the previously set callback + function pointer */ conn->fread_func = (curl_read_callback)Curl_FormReader; conn->fread_in = &http->form; diff --git a/tests/data/test554 b/tests/data/test554 new file mode 100644 index 000000000..134f7ef97 --- /dev/null +++ b/tests/data/test554 @@ -0,0 +1,68 @@ + +# +# Server-side + + +HTTP/1.1 200 OK +Date: Thu, 09 Nov 2010 14:49:00 GMT +Server: test-server/fake swsclose +Connection: close +Content-Type: text/html + +hello + + + +# Client-side + + +http + +# tool is what to use instead of 'curl' + +lib554 + + + +HTTP multi-part formpost using read callback for the file part + + +http://%HOSTIP:%HTTPPORT/554 + + + +# +# Verify data after the test has been "shot" + + +s/^------------------------------[a-z0-9]*/------------------------------/ +s/boundary=----------------------------[a-z0-9]*/boundary=----------------------------/ + +# Note that the stripping above removes 12 bytes from every occurance of the +# boundary string and since 4 of them are in the body contents, we see +# 415 - (4*12) here == 367 bytes. + +POST /554 HTTP/1.1 +Host: %HOSTIP:%HTTPPORT +Accept: */* +Content-Length: 415 +Expect: 100-continue +Content-Type: multipart/form-data; boundary=---------------------------- + +------------------------------ +Content-Disposition: form-data; name="sendfile"; filename="postit2.c" + +this is what we post to the silly web server + +------------------------------ +Content-Disposition: form-data; name="filename" + +postit2.c +------------------------------ +Content-Disposition: form-data; name="submit" + +send +-------------------------------- + + + diff --git a/tests/libtest/Makefile.am b/tests/libtest/Makefile.am index 8a9e3a42e..c6e6910cf 100644 --- a/tests/libtest/Makefile.am +++ b/tests/libtest/Makefile.am @@ -48,7 +48,7 @@ noinst_PROGRAMS = lib500 lib501 lib502 lib503 lib504 lib505 lib506 \ lib507 lib508 lib510 lib511 lib512 lib513 lib514 lib515 lib516 \ lib517 lib518 lib519 lib520 lib521 lib523 lib524 lib525 lib526 lib527 \ lib529 lib530 lib532 lib533 lib536 lib537 lib540 lib541 lib542 lib543 \ - lib544 lib545 lib547 lib548 lib549 lib552 lib553 + lib544 lib545 lib547 lib548 lib549 lib552 lib553 lib554 # Dependencies (may need to be overriden) LDADD = $(LIBDIR)/libcurl.la @@ -147,3 +147,5 @@ lib549_SOURCES = lib549.c $(SUPPORTFILES) lib552_SOURCES = lib552.c $(SUPPORTFILES) lib553_SOURCES = lib553.c $(SUPPORTFILES) + +lib554_SOURCES = lib554.c $(SUPPORTFILES) diff --git a/tests/libtest/lib554.c b/tests/libtest/lib554.c new file mode 100644 index 000000000..f04bc811a --- /dev/null +++ b/tests/libtest/lib554.c @@ -0,0 +1,130 @@ +/***************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * $Id$ + */ + +#include "test.h" + +static char data[]="this is what we post to the silly web server\n"; + +struct WriteThis { + char *readptr; + size_t sizeleft; +}; + +static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp) +{ + struct WriteThis *pooh = (struct WriteThis *)userp; + + if(size*nmemb < 1) + return 0; + + if(pooh->sizeleft) { + *(char *)ptr = pooh->readptr[0]; /* copy one single byte */ + pooh->readptr++; /* advance pointer */ + pooh->sizeleft--; /* less data left */ + return 1; /* we return 1 byte at a time! */ + } + + return 0; /* no more data left to deliver */ +} + +int test(char *URL) +{ + CURL *curl; + CURLcode res=CURLE_OK; + CURLFORMcode formrc; + + struct curl_httppost *formpost=NULL; + struct curl_httppost *lastptr=NULL; + struct WriteThis pooh; + + pooh.readptr = data; + pooh.sizeleft = strlen(data); + + curl_global_init(CURL_GLOBAL_ALL); + + /* Fill in the file upload field */ + formrc = curl_formadd(&formpost, + &lastptr, + CURLFORM_COPYNAME, "sendfile", + CURLFORM_STREAM, &pooh, + CURLFORM_CONTENTSLENGTH, pooh.sizeleft, + CURLFORM_FILENAME, "postit2.c", + CURLFORM_END); + + if(formrc) + printf("curl_formadd(1) = %d\n", formrc); + + /* Fill in the filename field */ + formrc = curl_formadd(&formpost, + &lastptr, + CURLFORM_COPYNAME, "filename", + CURLFORM_COPYCONTENTS, "postit2.c", + CURLFORM_END); + + if(formrc) + printf("curl_formadd(2) = %d\n", formrc); + + /* Fill in a submit field too */ + formrc = curl_formadd(&formpost, + &lastptr, + CURLFORM_COPYNAME, "submit", + CURLFORM_COPYCONTENTS, "send", + CURLFORM_END); + + if(formrc) + printf("curl_formadd(3) = %d\n", formrc); + + if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { + fprintf(stderr, "curl_global_init() failed\n"); + return TEST_ERR_MAJOR_BAD; + } + + if ((curl = curl_easy_init()) == NULL) { + fprintf(stderr, "curl_easy_init() failed\n"); + curl_global_cleanup(); + return TEST_ERR_MAJOR_BAD; + } + + /* First set the URL that is about to receive our POST. */ + curl_easy_setopt(curl, CURLOPT_URL, URL); + + /* Now specify we want to POST data */ + curl_easy_setopt(curl, CURLOPT_POST, TRUE); + + /* Set the expected POST size */ + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft); + + /* we want to use our own read function */ + curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); + + /* pointer to pass to our read function */ + curl_easy_setopt(curl, CURLOPT_READDATA, &pooh); + + /* send a multi-part formpost */ + curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); + + /* get verbose debug output please */ + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); + + /* include headers in the output */ + curl_easy_setopt(curl, CURLOPT_HEADER, TRUE); + + /* Perform the request, res will get the return code */ + res = curl_easy_perform(curl); + + /* always cleanup */ + curl_easy_cleanup(curl); + curl_global_cleanup(); + + /* now cleanup the formpost chain */ + curl_formfree(formpost); + + return res; +}