From 70e2aadc18fce9f6e3b9c06612c8314bf4f710b1 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 23 Apr 2004 10:37:52 +0000 Subject: Replaced Curl_FormReadOneLine with Curl_formpostheader as that is the only use for it. It saves one extra copy of the header. I also added comments for several functions in formdata.c --- lib/formdata.c | 98 ++++++++++++++++++++++++++++++++-------------------------- lib/formdata.h | 11 ++++--- lib/http.c | 12 +++---- 3 files changed, 65 insertions(+), 56 deletions(-) diff --git a/lib/formdata.c b/lib/formdata.c index 58d1547af..57ad283af 100644 --- a/lib/formdata.c +++ b/lib/formdata.c @@ -176,7 +176,7 @@ int FormParse(char *input, struct curl_httppost **httppost, struct curl_httppost **last_post) { - /* nextarg MUST be a string in the format 'name=contents' and we'll + /* 'input' MUST be a string in the format 'name=contents' and we'll build a linked list with the info */ char name[256]; char *contents; @@ -975,9 +975,13 @@ CURLFORMcode FormAdd(struct curl_httppost **httppost, return return_value; } +/* + * curl_formadd() is a public API to add a section to the multipart formpost. + */ + CURLFORMcode curl_formadd(struct curl_httppost **httppost, - struct curl_httppost **last_post, - ...) + struct curl_httppost **last_post, + ...) { va_list arg; CURLFORMcode result; @@ -987,6 +991,9 @@ CURLFORMcode curl_formadd(struct curl_httppost **httppost, return result; } +/* + * AddFormData() adds a chunk of data to the FormData linked list. + */ static size_t AddFormData(struct FormData **formp, const void *line, size_t length) @@ -1014,9 +1021,12 @@ static size_t AddFormData(struct FormData **formp, return length; } +/* + * AddFormDataf() adds printf()-style formatted data to the formdata chain. + */ static size_t AddFormDataf(struct FormData **formp, - const char *fmt, ...) + const char *fmt, ...) { char s[4096]; va_list ap; @@ -1027,7 +1037,10 @@ static size_t AddFormDataf(struct FormData **formp, return AddFormData(formp, s, 0); } - +/* + * Curl_FormBoundary() creates a suitable boundary string and returns an + * allocated one. + */ char *Curl_FormBoundary(void) { char *retstring; @@ -1056,7 +1069,10 @@ char *Curl_FormBoundary(void) return retstring; } -/* Used from http.c, this cleans a built FormData linked list */ +/* + * Curl_formclean() is used from http.c, this cleans a built FormData linked + * list + */ void Curl_formclean(struct FormData *form) { struct FormData *next; @@ -1069,7 +1085,10 @@ void Curl_formclean(struct FormData *form) } while((form=next)); /* continue */ } -/* external function to free up a whole form post chain */ +/* + * curl_formfree() is an external function to free up a whole form post + * chain + */ void curl_formfree(struct curl_httppost *form) { struct curl_httppost *next; @@ -1098,6 +1117,13 @@ void curl_formfree(struct curl_httppost *form) } while((form=next)); /* continue */ } +/* + * Curl_getFormData() converts a linked list of "meta data" into a complete + * (possibly huge) multipart formdata. The input list is in 'post', while the + * output resulting linked lists gets stored in '*finalform'. *sizep will get + * the total size of the whole POST. + */ + CURLcode Curl_getFormData(struct FormData **finalform, struct curl_httppost *post, curl_off_t *sizep) @@ -1278,6 +1304,10 @@ CURLcode Curl_getFormData(struct FormData **finalform, return result; } +/* + * Curl_FormInit() inits the struct 'form' points to with the 'formdata' + * and resets the 'sent' counter. + */ int Curl_FormInit(struct Form *form, struct FormData *formdata ) { if(!formdata) @@ -1289,7 +1319,10 @@ int Curl_FormInit(struct Form *form, struct FormData *formdata ) return 0; } -/* fread() emulation */ +/* + * Curl_FormReader() is the fread() emulation function that will be used to + * deliver the formdata to the transfer loop and then sent away to the peer. + */ size_t Curl_FormReader(char *buffer, size_t size, size_t nitems, @@ -1335,48 +1368,25 @@ size_t Curl_FormReader(char *buffer, return gotsize; } -/* possible (old) fread() emulation that copies at most one line */ -size_t Curl_FormReadOneLine(char *buffer, - size_t size, - size_t nitems, - FILE *mydata) +/* + * Curl_formpostheader() returns the first line of the formpost, the + * request-header part (which is not part of the request-body like the rest of + * the post). + */ +char *Curl_formpostheader(void *formp, size_t *len) { - struct Form *form; - size_t wantedsize; - size_t gotsize; - - form=(struct Form *)mydata; - - wantedsize = size * nitems; + char *header; + struct Form *form=(struct Form *)formp; if(!form->data) - return 0; /* nothing, error, empty */ - - do { - - if( (form->data->length - form->sent ) > wantedsize ) { - - memcpy(buffer, form->data->line + form->sent, wantedsize); - - form->sent += wantedsize; + return 0; /* nothing, ERROR! */ - return wantedsize; - } - - memcpy(buffer, - form->data->line + form->sent, - gotsize = (form->data->length - form->sent) ); - - form->sent = 0; + header = form->data->line; + *len = form->data->length; - form->data = form->data->next; /* advance */ - - } while(!gotsize && form->data); - /* 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) */ + form->data = form->data->next; /* advance */ - return gotsize; + return header; } diff --git a/lib/formdata.h b/lib/formdata.h index df6ae3ac0..d087fa7ca 100644 --- a/lib/formdata.h +++ b/lib/formdata.h @@ -65,11 +65,12 @@ size_t Curl_FormReader(char *buffer, size_t nitems, FILE *mydata); -/* possible (old) fread() emulation that copies at most one line */ -size_t Curl_FormReadOneLine(char *buffer, - size_t size, - size_t nitems, - FILE *mydata); +/* + * Curl_formpostheader() returns the first line of the formpost, the + * request-header part (which is not part of the request-body like the rest of + * the post). + */ +char *Curl_formpostheader(void *formp, size_t *len); char *Curl_FormBoundary(void); diff --git a/lib/http.c b/lib/http.c index 0c81752d3..1e95e1083 100644 --- a/lib/http.c +++ b/lib/http.c @@ -1686,7 +1686,7 @@ CURLcode Curl_http(struct connectdata *conn) } if(!checkheaders(data, "Content-Type:")) { - /* Get Content-Type: line from Curl_FormReadOneLine, which happens + /* Get Content-Type: line from Curl_formpostheader, which happens to always be the first line. We can know this for sure since we always build the formpost linked list the same way! @@ -1694,13 +1694,11 @@ CURLcode Curl_http(struct connectdata *conn) string etc why disabling this header is likely to not make things work, but we support it anyway. */ - char contentType[256]; + char *contentType; size_t linelength=0; - linelength = Curl_FormReadOneLine(contentType, - sizeof(contentType), - 1, - (FILE *)&http->form); - if(!linelength) { + contentType = Curl_formpostheader((void *)&http->form, + &linelength); + if(!contentType) { failf(data, "Could not get Content-Type header line!"); return CURLE_HTTP_POST_ERROR; } -- cgit v1.2.3