mirror of
https://github.com/moparisthebest/curl
synced 2024-12-22 08:08:50 -05:00
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
This commit is contained in:
parent
a1c8aaf666
commit
70e2aadc18
@ -176,7 +176,7 @@ int FormParse(char *input,
|
|||||||
struct curl_httppost **httppost,
|
struct curl_httppost **httppost,
|
||||||
struct curl_httppost **last_post)
|
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 */
|
build a linked list with the info */
|
||||||
char name[256];
|
char name[256];
|
||||||
char *contents;
|
char *contents;
|
||||||
@ -975,6 +975,10 @@ CURLFORMcode FormAdd(struct curl_httppost **httppost,
|
|||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* curl_formadd() is a public API to add a section to the multipart formpost.
|
||||||
|
*/
|
||||||
|
|
||||||
CURLFORMcode curl_formadd(struct curl_httppost **httppost,
|
CURLFORMcode curl_formadd(struct curl_httppost **httppost,
|
||||||
struct curl_httppost **last_post,
|
struct curl_httppost **last_post,
|
||||||
...)
|
...)
|
||||||
@ -987,6 +991,9 @@ CURLFORMcode curl_formadd(struct curl_httppost **httppost,
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* AddFormData() adds a chunk of data to the FormData linked list.
|
||||||
|
*/
|
||||||
static size_t AddFormData(struct FormData **formp,
|
static size_t AddFormData(struct FormData **formp,
|
||||||
const void *line,
|
const void *line,
|
||||||
size_t length)
|
size_t length)
|
||||||
@ -1014,6 +1021,9 @@ static size_t AddFormData(struct FormData **formp,
|
|||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* AddFormDataf() adds printf()-style formatted data to the formdata chain.
|
||||||
|
*/
|
||||||
|
|
||||||
static size_t AddFormDataf(struct FormData **formp,
|
static size_t AddFormDataf(struct FormData **formp,
|
||||||
const char *fmt, ...)
|
const char *fmt, ...)
|
||||||
@ -1027,7 +1037,10 @@ static size_t AddFormDataf(struct FormData **formp,
|
|||||||
return AddFormData(formp, s, 0);
|
return AddFormData(formp, s, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Curl_FormBoundary() creates a suitable boundary string and returns an
|
||||||
|
* allocated one.
|
||||||
|
*/
|
||||||
char *Curl_FormBoundary(void)
|
char *Curl_FormBoundary(void)
|
||||||
{
|
{
|
||||||
char *retstring;
|
char *retstring;
|
||||||
@ -1056,7 +1069,10 @@ char *Curl_FormBoundary(void)
|
|||||||
return retstring;
|
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)
|
void Curl_formclean(struct FormData *form)
|
||||||
{
|
{
|
||||||
struct FormData *next;
|
struct FormData *next;
|
||||||
@ -1069,7 +1085,10 @@ void Curl_formclean(struct FormData *form)
|
|||||||
} while((form=next)); /* continue */
|
} 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)
|
void curl_formfree(struct curl_httppost *form)
|
||||||
{
|
{
|
||||||
struct curl_httppost *next;
|
struct curl_httppost *next;
|
||||||
@ -1098,6 +1117,13 @@ void curl_formfree(struct curl_httppost *form)
|
|||||||
} while((form=next)); /* continue */
|
} 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,
|
CURLcode Curl_getFormData(struct FormData **finalform,
|
||||||
struct curl_httppost *post,
|
struct curl_httppost *post,
|
||||||
curl_off_t *sizep)
|
curl_off_t *sizep)
|
||||||
@ -1278,6 +1304,10 @@ CURLcode Curl_getFormData(struct FormData **finalform,
|
|||||||
return result;
|
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 )
|
int Curl_FormInit(struct Form *form, struct FormData *formdata )
|
||||||
{
|
{
|
||||||
if(!formdata)
|
if(!formdata)
|
||||||
@ -1289,7 +1319,10 @@ int Curl_FormInit(struct Form *form, struct FormData *formdata )
|
|||||||
return 0;
|
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 Curl_FormReader(char *buffer,
|
||||||
size_t size,
|
size_t size,
|
||||||
size_t nitems,
|
size_t nitems,
|
||||||
@ -1335,48 +1368,25 @@ size_t Curl_FormReader(char *buffer,
|
|||||||
return gotsize;
|
return gotsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* possible (old) fread() emulation that copies at most one line */
|
/*
|
||||||
size_t Curl_FormReadOneLine(char *buffer,
|
* Curl_formpostheader() returns the first line of the formpost, the
|
||||||
size_t size,
|
* request-header part (which is not part of the request-body like the rest of
|
||||||
size_t nitems,
|
* the post).
|
||||||
FILE *mydata)
|
*/
|
||||||
|
char *Curl_formpostheader(void *formp, size_t *len)
|
||||||
{
|
{
|
||||||
struct Form *form;
|
char *header;
|
||||||
size_t wantedsize;
|
struct Form *form=(struct Form *)formp;
|
||||||
size_t gotsize;
|
|
||||||
|
|
||||||
form=(struct Form *)mydata;
|
|
||||||
|
|
||||||
wantedsize = size * nitems;
|
|
||||||
|
|
||||||
if(!form->data)
|
if(!form->data)
|
||||||
return 0; /* nothing, error, empty */
|
return 0; /* nothing, ERROR! */
|
||||||
|
|
||||||
do {
|
header = form->data->line;
|
||||||
|
*len = form->data->length;
|
||||||
if( (form->data->length - form->sent ) > wantedsize ) {
|
|
||||||
|
|
||||||
memcpy(buffer, form->data->line + form->sent, wantedsize);
|
|
||||||
|
|
||||||
form->sent += wantedsize;
|
|
||||||
|
|
||||||
return wantedsize;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(buffer,
|
|
||||||
form->data->line + form->sent,
|
|
||||||
gotsize = (form->data->length - form->sent) );
|
|
||||||
|
|
||||||
form->sent = 0;
|
|
||||||
|
|
||||||
form->data = form->data->next; /* advance */
|
form->data = form->data->next; /* advance */
|
||||||
|
|
||||||
} while(!gotsize && form->data);
|
return header;
|
||||||
/* 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) */
|
|
||||||
|
|
||||||
return gotsize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -65,11 +65,12 @@ size_t Curl_FormReader(char *buffer,
|
|||||||
size_t nitems,
|
size_t nitems,
|
||||||
FILE *mydata);
|
FILE *mydata);
|
||||||
|
|
||||||
/* possible (old) fread() emulation that copies at most one line */
|
/*
|
||||||
size_t Curl_FormReadOneLine(char *buffer,
|
* Curl_formpostheader() returns the first line of the formpost, the
|
||||||
size_t size,
|
* request-header part (which is not part of the request-body like the rest of
|
||||||
size_t nitems,
|
* the post).
|
||||||
FILE *mydata);
|
*/
|
||||||
|
char *Curl_formpostheader(void *formp, size_t *len);
|
||||||
|
|
||||||
char *Curl_FormBoundary(void);
|
char *Curl_FormBoundary(void);
|
||||||
|
|
||||||
|
12
lib/http.c
12
lib/http.c
@ -1686,7 +1686,7 @@ CURLcode Curl_http(struct connectdata *conn)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(!checkheaders(data, "Content-Type:")) {
|
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
|
to always be the first line. We can know this for sure since
|
||||||
we always build the formpost linked list the same way!
|
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
|
string etc why disabling this header is likely to not make things
|
||||||
work, but we support it anyway.
|
work, but we support it anyway.
|
||||||
*/
|
*/
|
||||||
char contentType[256];
|
char *contentType;
|
||||||
size_t linelength=0;
|
size_t linelength=0;
|
||||||
linelength = Curl_FormReadOneLine(contentType,
|
contentType = Curl_formpostheader((void *)&http->form,
|
||||||
sizeof(contentType),
|
&linelength);
|
||||||
1,
|
if(!contentType) {
|
||||||
(FILE *)&http->form);
|
|
||||||
if(!linelength) {
|
|
||||||
failf(data, "Could not get Content-Type header line!");
|
failf(data, "Could not get Content-Type header line!");
|
||||||
return CURLE_HTTP_POST_ERROR;
|
return CURLE_HTTP_POST_ERROR;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user