📄 formdata.c
字号:
file->contenttype); } curList = file->contentheader; while( curList ) { /* Process the additional headers specified for this form */ size += AddFormDataf( &form, "\r\n%s", curList->data ); curList = curList->next; }#if 0 /* The header Content-Transfer-Encoding: seems to confuse some receivers * (like the built-in PHP engine). While I can't see any reason why it * should, I can just as well skip this to the benefit of the users who * are using such confused receivers. */ if(file->contenttype && !checkprefix("text/", file->contenttype)) { /* this is not a text content, mention our binary encoding */ size += AddFormData(&form, "\r\nContent-Transfer-Encoding: binary", 0); }#endif size += AddFormData(&form, "\r\n\r\n", 0); if((post->flags & HTTPPOST_FILENAME) || (post->flags & HTTPPOST_READFILE)) { /* we should include the contents from the specified file */ FILE *fileread; char buffer[1024]; int nread; fileread = strequal("-", file->contents)?stdin: /* binary read for win32 crap */ /*VMS??*/ fopen(file->contents, "rb"); /* ONLY ALLOWS FOR STREAM FILES ON VMS */ /*VMS?? Stream files are OK, as are FIXED & VAR files WITHOUT implied CC */ /*VMS?? For implied CC, every record needs to have a \n appended & 1 added to SIZE */ if(fileread) { while((nread = fread(buffer, 1, 1024, fileread))) size += AddFormData(&form, buffer, nread); if(fileread != stdin) fclose(fileread); } else {#if 0 /* File wasn't found, add a nothing field! */ size += AddFormData(&form, "", 0);#endif Curl_formclean(firstform); free(boundary); *finalform = NULL; return CURLE_READ_ERROR; } /* CMC: Added support for buffer uploads */ } else if (post->flags & HTTPPOST_BUFFER) { /* include contents of buffer */ size += AddFormData(&form, post->buffer, post->bufferlength); } else { /* include the contents we got */ size += AddFormData(&form, post->contents, post->contentslength); } } while((file = file->more)); /* for each specified file for this field */ if(post->more) { /* this was a multiple-file inclusion, make a termination file boundary: */ size += AddFormDataf(&form, "\r\n--%s--", fileboundary); free(fileboundary); } } while((post=post->next)); /* for each field */ /* end-boundary for everything */ size += AddFormDataf(&form, "\r\n--%s--\r\n", boundary); *sizep = size; free(boundary); *finalform=firstform; return result;}int Curl_FormInit(struct Form *form, struct FormData *formdata ){ if(!formdata) return 1; /* error */ form->data = formdata; form->sent = 0; return 0;}/* fread() emulation */int Curl_FormReader(char *buffer, size_t size, size_t nitems, FILE *mydata){ struct Form *form; int wantedsize; int gotsize = 0; form=(struct Form *)mydata; wantedsize = size * nitems; if(!form->data) return 0; /* nothing, error, empty */ do { if( (form->data->length - form->sent ) > wantedsize - gotsize) { memcpy(buffer + gotsize , form->data->line + form->sent, wantedsize - gotsize); form->sent += wantedsize-gotsize; return wantedsize; } memcpy(buffer+gotsize, form->data->line + form->sent, (form->data->length - form->sent) ); gotsize += form->data->length - form->sent; form->sent = 0; form->data = form->data->next; /* advance */ } while(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) */ return gotsize;}/* possible (old) fread() emulation that copies at most one line */int Curl_FormReadOneLine(char *buffer, size_t size, size_t nitems, FILE *mydata){ struct Form *form; int wantedsize; int gotsize; form=(struct Form *)mydata; wantedsize = size * nitems; if(!form->data) return -1; /* nothing, error, empty */ do { 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 */ } 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) */ return gotsize;}#ifdef _FORM_DEBUGint FormAddTest(const char * errormsg, struct curl_httppost **httppost, struct curl_httppost **last_post, ...){ int result; va_list arg; va_start(arg, last_post); if ((result = FormAdd(httppost, last_post, arg))) fprintf (stderr, "ERROR doing FormAdd ret: %d action: %s\n", result, errormsg); va_end(arg); return result;}int main(){ char name1[] = "simple_COPYCONTENTS"; char name2[] = "COPYCONTENTS_+_CONTENTTYPE"; char name3[] = "PTRNAME_+_NAMELENGTH_+_COPYNAME_+_CONTENTSLENGTH"; char name4[] = "simple_PTRCONTENTS"; char name5[] = "PTRCONTENTS_+_CONTENTSLENGTH"; char name6[] = "PTRCONTENTS_+_CONTENTSLENGTH_+_CONTENTTYPE"; char name7[] = "FILE1_+_CONTENTTYPE"; char name8[] = "FILE1_+_FILE2"; char name9[] = "FILE1_+_FILE2_+_FILE3"; char name10[] = "ARRAY: FILE1_+_FILE2_+_FILE3"; char name11[] = "FILECONTENT"; char value1[] = "value for simple COPYCONTENTS"; char value2[] = "value for COPYCONTENTS + CONTENTTYPE"; char value3[] = "value for PTRNAME + NAMELENGTH + COPYNAME + CONTENTSLENGTH"; char value4[] = "value for simple PTRCONTENTS"; char value5[] = "value for PTRCONTENTS + CONTENTSLENGTH"; char value6[] = "value for PTRCOTNENTS + CONTENTSLENGTH + CONTENTTYPE"; char value7[] = "inet_ntoa_r.h"; char value8[] = "Makefile.b32.resp"; char type2[] = "image/gif"; char type6[] = "text/plain"; char type7[] = "text/html"; int name3length = strlen(name3); int value3length = strlen(value3); int value5length = strlen(value4); int value6length = strlen(value5); int errors = 0; int size; int nread; char buffer[4096]; struct curl_httppost *httppost=NULL; struct curl_httppost *last_post=NULL; struct curl_forms forms[4]; struct FormData *form; struct Form formread; if (FormAddTest("simple COPYCONTENTS test", &httppost, &last_post, CURLFORM_COPYNAME, name1, CURLFORM_COPYCONTENTS, value1, CURLFORM_END)) ++errors; if (FormAddTest("COPYCONTENTS + CONTENTTYPE test", &httppost, &last_post, CURLFORM_COPYNAME, name2, CURLFORM_COPYCONTENTS, value2, CURLFORM_CONTENTTYPE, type2, CURLFORM_END)) ++errors; /* make null character at start to check that contentslength works correctly */ name3[1] = '\0'; value3[1] = '\0'; if (FormAddTest("PTRNAME + NAMELENGTH + COPYNAME + CONTENTSLENGTH test", &httppost, &last_post, CURLFORM_PTRNAME, name3, CURLFORM_COPYCONTENTS, value3, CURLFORM_CONTENTSLENGTH, value3length, CURLFORM_NAMELENGTH, name3length, CURLFORM_END)) ++errors; if (FormAddTest("simple PTRCONTENTS test", &httppost, &last_post, CURLFORM_COPYNAME, name4, CURLFORM_PTRCONTENTS, value4, CURLFORM_END)) ++errors; /* make null character at start to check that contentslength works correctly */ value5[1] = '\0'; if (FormAddTest("PTRCONTENTS + CONTENTSLENGTH test", &httppost, &last_post, CURLFORM_COPYNAME, name5, CURLFORM_PTRCONTENTS, value5, CURLFORM_CONTENTSLENGTH, value5length, CURLFORM_END)) ++errors; /* make null character at start to check that contentslength works correctly */ value6[1] = '\0'; if (FormAddTest("PTRCONTENTS + CONTENTSLENGTH + CONTENTTYPE test", &httppost, &last_post, CURLFORM_COPYNAME, name6, CURLFORM_PTRCONTENTS, value6, CURLFORM_CONTENTSLENGTH, value6length, CURLFORM_CONTENTTYPE, type6, CURLFORM_END)) ++errors; if (FormAddTest("FILE + CONTENTTYPE test", &httppost, &last_post, CURLFORM_COPYNAME, name7, CURLFORM_FILE, value7, CURLFORM_CONTENTTYPE, type7, CURLFORM_END)) ++errors; if (FormAddTest("FILE1 + FILE2 test", &httppost, &last_post, CURLFORM_COPYNAME, name8, CURLFORM_FILE, value7, CURLFORM_FILE, value8, CURLFORM_END)) ++errors; if (FormAddTest("FILE1 + FILE2 + FILE3 test", &httppost, &last_post, CURLFORM_COPYNAME, name9, CURLFORM_FILE, value7, CURLFORM_FILE, value8, CURLFORM_FILE, value7, CURLFORM_END)) ++errors; forms[0].option = CURLFORM_FILE; forms[0].value = value7; forms[1].option = CURLFORM_FILE; forms[1].value = value8; forms[2].option = CURLFORM_FILE; forms[2].value = value7; forms[3].option = CURLFORM_END; if (FormAddTest("FILE1 + FILE2 + FILE3 ARRAY test", &httppost, &last_post, CURLFORM_COPYNAME, name10, CURLFORM_ARRAY, forms, CURLFORM_END)) ++errors; if (FormAddTest("FILECONTENT test", &httppost, &last_post, CURLFORM_COPYNAME, name11, CURLFORM_FILECONTENT, value7, CURLFORM_END)) ++errors; form=Curl_getFormData(httppost, &size); Curl_FormInit(&formread, form); do { nread = Curl_FormReader(buffer, 1, sizeof(buffer), (FILE *)&formread); if(-1 == nread) break; fwrite(buffer, nread, 1, stdout); } while(1); fprintf(stdout, "size: %d\n", size); if (errors) fprintf(stdout, "\n==> %d Test(s) failed!\n", errors); else fprintf(stdout, "\nAll Tests seem to have worked (please check output)\n"); return 0;}#endif#ifdef _OLD_FORM_DEBUGint main(int argc, char **argv){#if 0 char *testargs[]={ "name1 = data in number one", "name2 = number two data", "test = @upload" };#endif int i; char *nextarg; struct curl_httppost *httppost=NULL; struct curl_httppost *last_post=NULL; struct curl_httppost *post; int size; int nread; char buffer[4096]; struct FormData *form; struct Form formread; for(i=1; i<argc; i++) { if( FormParse( argv[i], &httppost, &last_post)) { fprintf(stderr, "Illegally formatted input field: '%s'!\n", argv[i]); return 1; } } form=Curl_getFormData(httppost, &size); Curl_FormInit(&formread, form); do { nread = Curl_FormReader(buffer, 1, sizeof(buffer), (FILE *)&formread); if(-1 == nread) break; fwrite(buffer, nread, 1, stderr); } while(1); fprintf(stderr, "size: %d\n", size); return 0;}#endif#endif /* CURL_DISABLE_HTTP */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -