112 #ifndef CURL_DISABLE_HTTP
121 #ifndef CURL_OLDSTYLE
122 #define CURL_OLDSTYLE 1
135 #define BOUNDARY_LENGTH 40
139 #define HTTPPOST_CONTENTTYPE_DEFAULT "application/octet-stream"
143 static void GetStr(
char **
string,
171 #define FORM_FILE_SEPARATOR ','
172 #define FORM_TYPE_SEPARATOR ';'
175 int FormParse(
char *
input,
188 char *prevtype =
NULL;
197 contents = malloc(strlen(input));
198 contents[0] =
'\000';
200 if(1 <= sscanf(input,
"%255[^=]=%[^\n]", name, contents)) {
204 if(
'@' == contp[0]) {
220 if(sep2 && (sep2 < sep)) {
229 type = strstr(sep+1,
"type=");
236 type += strlen(
"type=");
238 if(2 != sscanf(type,
"%127[^/]/%127[^,\n]",
244 sep = (
char *)type + strlen(major)+strlen(minor)+1;
265 const char *extension;
268 static struct ContentType ctts[]={
269 {
".gif",
"image/gif"},
270 {
".jpg",
"image/jpeg"},
271 {
".jpeg",
"image/jpeg"},
272 {
".txt",
"text/plain"},
273 {
".html",
"text/html"}
284 for(i=0; i<
sizeof(ctts)/
sizeof(ctts[0]); i++) {
285 if(strlen(contp) >= strlen(ctts[i].extension)) {
287 strlen(contp) - strlen(ctts[i].extension),
288 ctts[i].extension)) {
304 GetStr(&post->
name, name);
314 (*last_post)->next = post;
329 GetStr(&subpost->
name, name);
341 post->
more = subpost;
345 }
while(sep && *sep);
351 GetStr(&post->
name, name);
352 if( contp[0]==
'<' ) {
365 (*last_post)->next = post;
387 return FormParse(input, httppost, last_post);
417 post->
namelength = (long)(name?(namelength?namelength:strlen(name)):0);
435 parent_post->
more = post;
440 (*last_post)->
next = post;
459 static FormInfo * AddFormInfo(
char *value,
466 memset(form_info, 0,
sizeof(
FormInfo));
476 if (parent_form_info) {
478 form_info->
more = parent_form_info->
more;
481 parent_form_info->
more = form_info;
499 static const char * ContentTypeForFilename (
const char *filename,
500 const char *prevtype)
502 const char *contenttype =
NULL;
509 const char *extension;
512 static struct ContentType ctts[]={
513 {
".gif",
"image/gif"},
514 {
".jpg",
"image/jpeg"},
515 {
".jpeg",
"image/jpeg"},
516 {
".txt",
"text/plain"},
517 {
".html",
"text/html"}
522 contenttype = prevtype;
528 for(i=0; i<
sizeof(ctts)/
sizeof(ctts[0]); i++) {
529 if(strlen(filename) >= strlen(ctts[i].extension)) {
531 strlen(filename) - strlen(ctts[i].extension),
532 ctts[i].extension)) {
533 contenttype = ctts[
i].type;
553 static int AllocAndCopy(
char **buffer,
size_t buffer_length)
559 length = buffer_length;
561 length = strlen(*buffer);
564 *buffer = (
char*)malloc(length+add);
567 memcpy(*buffer, src, length);
629 FormInfo *first_form, *current_form, *form;
631 const char *prevtype =
NULL;
635 char *array_value=
NULL;
640 bool array_state =
FALSE;
647 memset(first_form, 0,
sizeof(
FormInfo));
648 current_form = first_form;
666 array_value = (
char *)forms->
value;
669 if (CURLFORM_END == option) {
678 if (CURLFORM_END == option)
699 case CURLFORM_PTRNAME:
701 case CURLFORM_COPYNAME:
702 if (current_form->
name)
705 char *name = array_state?
706 array_value:va_arg(params,
char *);
713 case CURLFORM_NAMELENGTH:
718 array_state?(long)array_value:va_arg(params,
long);
724 case CURLFORM_PTRCONTENTS:
726 case CURLFORM_COPYCONTENTS:
727 if (current_form->
value)
731 array_state?array_value:va_arg(params,
char *);
738 case CURLFORM_CONTENTSLENGTH:
743 array_state?(long)array_value:va_arg(params,
long);
747 case CURLFORM_FILECONTENT:
748 if (current_form->
flags != 0)
751 char *filename = array_state?
752 array_value:va_arg(params,
char *);
765 char *filename = array_state?array_value:
766 va_arg(params,
char *);
768 if (current_form->
value) {
771 if (!(current_form = AddFormInfo(
strdup(filename),
772 NULL, current_form)))
791 case CURLFORM_BUFFER:
793 char *filename = array_state?array_value:
794 va_arg(params,
char *);
796 if (current_form->
value) {
799 if (!(current_form = AddFormInfo(
strdup(filename),
800 NULL, current_form)))
819 case CURLFORM_BUFFERPTR:
825 array_state?array_value:va_arg(params,
char *);
833 case CURLFORM_BUFFERLENGTH:
838 array_state?(long)array_value:va_arg(params,
long);
841 case CURLFORM_CONTENTTYPE:
844 array_state?array_value:va_arg(params,
char *);
848 if (!(current_form = AddFormInfo(
NULL,
867 case CURLFORM_CONTENTHEADER:
882 case CURLFORM_FILENAME:
884 char *filename = array_state?array_value:
885 va_arg(params,
char *);
902 for(form = first_form;
905 if ( ((!form->
name || !form->
value) && !post) ||
927 =
strdup(ContentTypeForFilename(form->
value, prevtype));
930 (form == first_form) ) {
967 while (form !=
NULL) {
984 va_start(arg, last_post);
985 result = FormAdd(httppost, last_post, arg);
990 static size_t AddFormData(
struct FormData **formp,
1000 length = strlen((
char *)line);
1002 newform->
line = (
char *)malloc(length+1);
1003 memcpy(newform->
line, line, length);
1008 (*formp)->next = newform;
1018 static size_t AddFormDataf(
struct FormData **formp,
1019 const char *fmt, ...)
1027 return AddFormData(formp, s, 0);
1034 static int randomizer=0;
1038 static char table16[]=
"abcdef0123456789";
1045 srand(time(
NULL)+randomizer++);
1047 strcpy(retstring,
"----------------------------");
1050 retstring[i] = table16[rand()%16];
1069 }
while((form=next));
1098 }
while((form=next));
1112 char *fileboundary=
NULL;
1124 "Content-Type: multipart/form-data;"
1134 size += AddFormDataf(&form,
"\r\n");
1137 size += AddFormDataf(&form,
"--%s\r\n", boundary);
1139 size += AddFormData(&form,
1140 "Content-Disposition: form-data; name=\"", 0);
1144 size += AddFormData(&form,
"\"", 0);
1152 size += AddFormDataf(&form,
1153 "\r\nContent-Type: multipart/mixed,"
1168 size += AddFormDataf(&form,
1169 "\r\n--%s\r\nContent-Disposition: "
1170 "attachment; filename=\"%s\"",
1178 size += AddFormDataf(&form,
1179 "; filename=\"%s\"",
1186 size += AddFormDataf(&form,
1187 "\r\nContent-Type: %s",
1194 size += AddFormDataf( &form,
"\r\n%s", curList->
data );
1195 curList = curList->
next;
1208 size += AddFormData(&form,
"\r\nContent-Transfer-Encoding: binary", 0);
1212 size += AddFormData(&form,
"\r\n\r\n", 0);
1227 while((nread =
fread(buffer, 1, 1024, fileread)))
1228 size += AddFormData(&form, buffer, nread);
1230 if(fileread != stdin)
1236 size += AddFormData(&form,
"", 0);
1254 }
while((file = file->
more));
1259 size += AddFormDataf(&form,
1265 }
while((post=post->
next));
1268 size += AddFormDataf(&form,
1276 *finalform=firstform;
1286 form->
data = formdata;
1302 form=(
struct Form *)mydata;
1304 wantedsize = size * nitems;
1313 memcpy(buffer + gotsize , form->
data->
line + form->
sent,
1314 wantedsize - gotsize);
1316 form->
sent += wantedsize-gotsize;
1321 memcpy(buffer+gotsize,
1330 }
while(form->
data);
1348 form=(
struct Form *)mydata;
1350 wantedsize = size * nitems;
1359 memcpy(buffer, form->
data->
line + form->
sent, wantedsize);
1361 form->
sent += wantedsize;
1374 }
while(!gotsize && form->
data);
1384 int FormAddTest(
const char * errormsg,
1391 va_start(arg, last_post);
1392 if ((result = FormAdd(httppost, last_post, arg)))
1393 fprintf (stderr,
"ERROR doing FormAdd ret: %d action: %s\n", result,
1402 char name1[] =
"simple_COPYCONTENTS";
1403 char name2[] =
"COPYCONTENTS_+_CONTENTTYPE";
1404 char name3[] =
"PTRNAME_+_NAMELENGTH_+_COPYNAME_+_CONTENTSLENGTH";
1405 char name4[] =
"simple_PTRCONTENTS";
1406 char name5[] =
"PTRCONTENTS_+_CONTENTSLENGTH";
1407 char name6[] =
"PTRCONTENTS_+_CONTENTSLENGTH_+_CONTENTTYPE";
1408 char name7[] =
"FILE1_+_CONTENTTYPE";
1409 char name8[] =
"FILE1_+_FILE2";
1410 char name9[] =
"FILE1_+_FILE2_+_FILE3";
1411 char name10[] =
"ARRAY: FILE1_+_FILE2_+_FILE3";
1412 char name11[] =
"FILECONTENT";
1413 char value1[] =
"value for simple COPYCONTENTS";
1414 char value2[] =
"value for COPYCONTENTS + CONTENTTYPE";
1415 char value3[] =
"value for PTRNAME + NAMELENGTH + COPYNAME + CONTENTSLENGTH";
1416 char value4[] =
"value for simple PTRCONTENTS";
1417 char value5[] =
"value for PTRCONTENTS + CONTENTSLENGTH";
1418 char value6[] =
"value for PTRCOTNENTS + CONTENTSLENGTH + CONTENTTYPE";
1419 char value7[] =
"inet_ntoa_r.h";
1420 char value8[] =
"Makefile.b32.resp";
1421 char type2[] =
"image/gif";
1422 char type6[] =
"text/plain";
1423 char type7[] =
"text/html";
1424 int name3length = strlen(name3);
1425 int value3length = strlen(value3);
1426 int value5length = strlen(value4);
1427 int value6length = strlen(value5);
1437 struct Form formread;
1439 if (FormAddTest(
"simple COPYCONTENTS test", &httppost, &last_post,
1440 CURLFORM_COPYNAME, name1, CURLFORM_COPYCONTENTS, value1,
1443 if (FormAddTest(
"COPYCONTENTS + CONTENTTYPE test", &httppost, &last_post,
1444 CURLFORM_COPYNAME, name2, CURLFORM_COPYCONTENTS, value2,
1445 CURLFORM_CONTENTTYPE, type2, CURLFORM_END))
1451 if (FormAddTest(
"PTRNAME + NAMELENGTH + COPYNAME + CONTENTSLENGTH test",
1452 &httppost, &last_post,
1453 CURLFORM_PTRNAME, name3, CURLFORM_COPYCONTENTS, value3,
1454 CURLFORM_CONTENTSLENGTH, value3length,
1455 CURLFORM_NAMELENGTH, name3length, CURLFORM_END))
1457 if (FormAddTest(
"simple PTRCONTENTS test", &httppost, &last_post,
1458 CURLFORM_COPYNAME, name4, CURLFORM_PTRCONTENTS, value4,
1464 if (FormAddTest(
"PTRCONTENTS + CONTENTSLENGTH test", &httppost, &last_post,
1465 CURLFORM_COPYNAME, name5, CURLFORM_PTRCONTENTS, value5,
1466 CURLFORM_CONTENTSLENGTH, value5length, CURLFORM_END))
1471 if (FormAddTest(
"PTRCONTENTS + CONTENTSLENGTH + CONTENTTYPE test",
1472 &httppost, &last_post,
1473 CURLFORM_COPYNAME, name6, CURLFORM_PTRCONTENTS, value6,
1474 CURLFORM_CONTENTSLENGTH, value6length,
1475 CURLFORM_CONTENTTYPE, type6, CURLFORM_END))
1477 if (FormAddTest(
"FILE + CONTENTTYPE test", &httppost, &last_post,
1478 CURLFORM_COPYNAME, name7, CURLFORM_FILE, value7,
1479 CURLFORM_CONTENTTYPE, type7, CURLFORM_END))
1481 if (FormAddTest(
"FILE1 + FILE2 test", &httppost, &last_post,
1482 CURLFORM_COPYNAME, name8, CURLFORM_FILE, value7,
1483 CURLFORM_FILE, value8, CURLFORM_END))
1485 if (FormAddTest(
"FILE1 + FILE2 + FILE3 test", &httppost, &last_post,
1486 CURLFORM_COPYNAME, name9, CURLFORM_FILE, value7,
1487 CURLFORM_FILE, value8, CURLFORM_FILE, value7, CURLFORM_END))
1489 forms[0].
option = CURLFORM_FILE;
1490 forms[0].
value = value7;
1491 forms[1].
option = CURLFORM_FILE;
1492 forms[1].
value = value8;
1493 forms[2].
option = CURLFORM_FILE;
1494 forms[2].
value = value7;
1495 forms[3].
option = CURLFORM_END;
1496 if (FormAddTest(
"FILE1 + FILE2 + FILE3 ARRAY test", &httppost, &last_post,
1497 CURLFORM_COPYNAME, name10, CURLFORM_ARRAY, forms,
1500 if (FormAddTest(
"FILECONTENT test", &httppost, &last_post,
1501 CURLFORM_COPYNAME, name11, CURLFORM_FILECONTENT, value7,
1515 fwrite(buffer, nread, 1, stdout);
1518 fprintf(stdout,
"size: %d\n", size);
1520 fprintf(stdout,
"\n==> %d Test(s) failed!\n", errors);
1522 fprintf(stdout,
"\nAll Tests seem to have worked (please check output)\n");
1529 #ifdef _OLD_FORM_DEBUG
1531 int main(
int argc,
char **argv)
1535 "name1 = data in number one",
1536 "name2 = number two data",
1550 struct Form formread;
1552 for(i=1; i<argc; i++) {
1554 if( FormParse( argv[i],
1557 fprintf(stderr,
"Illegally formatted input field: '%s'!\n",
1573 fwrite(buffer, nread, 1, stderr);
1576 fprintf(stderr,
"size: %d\n", size);
GLsizei const GLfloat * value
#define HTTPPOST_READFILE
GLuint GLuint GLsizei GLenum type
#define HTTPPOST_PTRCONTENTS
#define HTTPPOST_FILENAME
struct curl_httppost * more
GLenum GLenum GLenum input
#define checkprefix(a, b)
#define HTTPPOST_PTRBUFFER
struct curl_slist * contentheader
GLsizei const GLcharARB const GLint * length
int vsprintf(idStr &string, const char *fmt, va_list argptr)
struct curl_httppost * next
if(!ValidDisplayID(prefInfo.prefDisplayID)) prefInfo.prefDisplayID
GLenum const GLfloat * params