formdata.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id: formdata.c,v 1.56 2004/03/12 14:22:16 bagder Exp $
  22. ***************************************************************************/
  23. /*
  24. Debug the form generator stand-alone by compiling this source file with:
  25. gcc -DHAVE_CONFIG_H -I../ -g -D_FORM_DEBUG -o formdata -I../include formdata.c strequal.c
  26. run the 'formdata' executable the output should end with:
  27. All Tests seem to have worked ...
  28. and the following parts should be there:
  29. Content-Disposition: form-data; name="simple_COPYCONTENTS"
  30. value for simple COPYCONTENTS
  31. Content-Disposition: form-data; name="COPYCONTENTS_+_CONTENTTYPE"
  32. Content-Type: image/gif
  33. value for COPYCONTENTS + CONTENTTYPE
  34. Content-Disposition: form-data; name="PRNAME_+_NAMELENGTH_+_COPYNAME_+_CONTENTSLENGTH"
  35. vlue for PTRNAME + NAMELENGTH + COPYNAME + CONTENTSLENGTH
  36. (or you might see P^@RNAME and v^@lue at the start)
  37. Content-Disposition: form-data; name="simple_PTRCONTENTS"
  38. value for simple PTRCONTENTS
  39. Content-Disposition: form-data; name="PTRCONTENTS_+_CONTENTSLENGTH"
  40. vlue for PTRCONTENTS + CONTENTSLENGTH
  41. (or you might see v^@lue at the start)
  42. Content-Disposition: form-data; name="PTRCONTENTS_+_CONTENTSLENGTH_+_CONTENTTYPE"
  43. Content-Type: text/plain
  44. vlue for PTRCOTNENTS + CONTENTSLENGTH + CONTENTTYPE
  45. (or you might see v^@lue at the start)
  46. Content-Disposition: form-data; name="FILE1_+_CONTENTTYPE"; filename="inet_ntoa_r.h"
  47. Content-Type: text/html
  48. ...
  49. Content-Disposition: form-data; name="FILE1_+_FILE2"
  50. Content-Type: multipart/mixed, boundary=curlz1s0dkticx49MV1KGcYP5cvfSsz
  51. ...
  52. Content-Disposition: attachment; filename="inet_ntoa_r.h"
  53. Content-Type: text/plain
  54. ...
  55. Content-Disposition: attachment; filename="Makefile.b32.resp"
  56. Content-Type: text/plain
  57. ...
  58. Content-Disposition: form-data; name="FILE1_+_FILE2_+_FILE3"
  59. Content-Type: multipart/mixed, boundary=curlirkYPmPwu6FrJ1vJ1u1BmtIufh1
  60. ...
  61. Content-Disposition: attachment; filename="inet_ntoa_r.h"
  62. Content-Type: text/plain
  63. ...
  64. Content-Disposition: attachment; filename="Makefile.b32.resp"
  65. Content-Type: text/plain
  66. ...
  67. Content-Disposition: attachment; filename="inet_ntoa_r.h"
  68. Content-Type: text/plain
  69. ...
  70. Content-Disposition: form-data; name="ARRAY: FILE1_+_FILE2_+_FILE3"
  71. Content-Type: multipart/mixed, boundary=curlirkYPmPwu6FrJ1vJ1u1BmtIufh1
  72. ...
  73. Content-Disposition: attachment; filename="inet_ntoa_r.h"
  74. Content-Type: text/plain
  75. ...
  76. Content-Disposition: attachment; filename="Makefile.b32.resp"
  77. Content-Type: text/plain
  78. ...
  79. Content-Disposition: attachment; filename="inet_ntoa_r.h"
  80. Content-Type: text/plain
  81. ...
  82. Content-Disposition: form-data; name="FILECONTENT"
  83. ...
  84. For the old FormParse used by curl_formparse use:
  85. gcc -DHAVE_CONFIG_H -I../ -g -D_OLD_FORM_DEBUG -o formdata -I../include formdata.c strequal.c
  86. run the 'formdata' executable and make sure the output is ok!
  87. try './formdata "name=Daniel" "poo=noo" "foo=bar"' and similarly
  88. */
  89. #include "setup.h"
  90. #ifndef CURL_DISABLE_HTTP
  91. #include <stdio.h>
  92. #include <stdlib.h>
  93. #include <string.h>
  94. #include <stdarg.h>
  95. #include <time.h>
  96. #ifndef CURL_OLDSTYLE
  97. #define CURL_OLDSTYLE 1 /* enable deprecated prototype for curl_formparse */
  98. #endif
  99. #include <curl/curl.h>
  100. #include "formdata.h"
  101. #include "strequal.h"
  102. /* The last #include file should be: */
  103. #ifdef CURLDEBUG
  104. #include "memdebug.h"
  105. #endif
  106. /* Length of the random boundary string. */
  107. #define BOUNDARY_LENGTH 40
  108. /* What kind of Content-Type to use on un-specified files with unrecognized
  109. extensions. */
  110. #define HTTPPOST_CONTENTTYPE_DEFAULT "application/octet-stream"
  111. /* This is a silly duplicate of the function in main.c to enable this source
  112. to compile stand-alone for better debugging */
  113. static void GetStr(char **string,
  114. const char *value)
  115. {
  116. if(*string)
  117. free(*string);
  118. *string = strdup(value);
  119. }
  120. /***************************************************************************
  121. *
  122. * FormParse()
  123. *
  124. * Reads a 'name=value' paramter and builds the appropriate linked list.
  125. *
  126. * Specify files to upload with 'name=@filename'. Supports specified
  127. * given Content-Type of the files. Such as ';type=<content-type>'.
  128. *
  129. * You may specify more than one file for a single name (field). Specify
  130. * multiple files by writing it like:
  131. *
  132. * 'name=@filename,filename2,filename3'
  133. *
  134. * If you want content-types specified for each too, write them like:
  135. *
  136. * 'name=@filename;type=image/gif,filename2,filename3'
  137. *
  138. ***************************************************************************/
  139. #define FORM_FILE_SEPARATOR ','
  140. #define FORM_TYPE_SEPARATOR ';'
  141. static
  142. int FormParse(char *input,
  143. struct curl_httppost **httppost,
  144. struct curl_httppost **last_post)
  145. {
  146. /* nextarg MUST be a string in the format 'name=contents' and we'll
  147. build a linked list with the info */
  148. char name[256];
  149. char *contents;
  150. char major[128];
  151. char minor[128];
  152. long flags = 0;
  153. char *contp;
  154. const char *type = NULL;
  155. char *prevtype = NULL;
  156. char *sep;
  157. char *sep2;
  158. struct curl_httppost *post;
  159. struct curl_httppost *subpost; /* a sub-node */
  160. unsigned int i;
  161. /* Preallocate contents to the length of input to make sure we don't
  162. overwrite anything. */
  163. contents = malloc(strlen(input));
  164. contents[0] = '\000';
  165. if(1 <= sscanf(input, "%255[^=]=%[^\n]", name, contents)) {
  166. /* the input was using the correct format */
  167. contp = contents;
  168. if('@' == contp[0]) {
  169. /* we use the @-letter to indicate file name(s) */
  170. flags = HTTPPOST_FILENAME;
  171. contp++;
  172. post=NULL;
  173. do {
  174. /* since this was a file, it may have a content-type specifier
  175. at the end too */
  176. sep=strchr(contp, FORM_TYPE_SEPARATOR);
  177. sep2=strchr(contp, FORM_FILE_SEPARATOR);
  178. /* pick the closest */
  179. if(sep2 && (sep2 < sep)) {
  180. sep = sep2;
  181. /* no type was specified! */
  182. }
  183. if(sep) {
  184. /* if we got here on a comma, don't do much */
  185. if(FORM_FILE_SEPARATOR != *sep)
  186. type = strstr(sep+1, "type=");
  187. else
  188. type=NULL;
  189. *sep=0; /* terminate file name at separator */
  190. if(type) {
  191. type += strlen("type=");
  192. if(2 != sscanf(type, "%127[^/]/%127[^,\n]",
  193. major, minor)) {
  194. free(contents);
  195. return 2; /* illegal content-type syntax! */
  196. }
  197. /* now point beyond the content-type specifier */
  198. sep = (char *)type + strlen(major)+strlen(minor)+1;
  199. /* find the following comma */
  200. sep=strchr(sep, FORM_FILE_SEPARATOR);
  201. }
  202. }
  203. else {
  204. type=NULL;
  205. sep=strchr(contp, FORM_FILE_SEPARATOR);
  206. }
  207. if(sep) {
  208. /* the next file name starts here */
  209. *sep =0;
  210. sep++;
  211. }
  212. if(!type) {
  213. /*
  214. * No type was specified, we scan through a few well-known
  215. * extensions and pick the first we match!
  216. */
  217. struct ContentType {
  218. const char *extension;
  219. const char *type;
  220. };
  221. static struct ContentType ctts[]={
  222. {".gif", "image/gif"},
  223. {".jpg", "image/jpeg"},
  224. {".jpeg", "image/jpeg"},
  225. {".txt", "text/plain"},
  226. {".html", "text/html"}
  227. };
  228. if(prevtype)
  229. /* default to the previously set/used! */
  230. type = prevtype;
  231. else
  232. /* It seems RFC1867 defines no Content-Type to default to
  233. text/plain so we don't actually need to set this: */
  234. type = HTTPPOST_CONTENTTYPE_DEFAULT;
  235. for(i=0; i<sizeof(ctts)/sizeof(ctts[0]); i++) {
  236. if(strlen(contp) >= strlen(ctts[i].extension)) {
  237. if(strequal(contp +
  238. strlen(contp) - strlen(ctts[i].extension),
  239. ctts[i].extension)) {
  240. type = ctts[i].type;
  241. break;
  242. }
  243. }
  244. }
  245. /* we have a type by now */
  246. }
  247. if(NULL == post) {
  248. /* For the first file name, we allocate and initiate the main list
  249. node */
  250. post = (struct curl_httppost *)malloc(sizeof(struct curl_httppost));
  251. if(post) {
  252. memset(post, 0, sizeof(struct curl_httppost));
  253. GetStr(&post->name, name); /* get the name */
  254. GetStr(&post->contents, contp); /* get the contents */
  255. post->contentslength = 0;
  256. post->flags = flags;
  257. if(type) {
  258. GetStr(&post->contenttype, (char *)type); /* get type */
  259. prevtype=post->contenttype; /* point to the allocated string! */
  260. }
  261. /* make the previous point to this */
  262. if(*last_post)
  263. (*last_post)->next = post;
  264. else
  265. (*httppost) = post;
  266. (*last_post) = post;
  267. }
  268. }
  269. else {
  270. /* we add a file name to the previously allocated node, known as
  271. 'post' now */
  272. subpost =(struct curl_httppost *)
  273. malloc(sizeof(struct curl_httppost));
  274. if(subpost) {
  275. memset(subpost, 0, sizeof(struct curl_httppost));
  276. GetStr(&subpost->name, name); /* get the name */
  277. GetStr(&subpost->contents, contp); /* get the contents */
  278. subpost->contentslength = 0;
  279. subpost->flags = flags;
  280. if(type) {
  281. GetStr(&subpost->contenttype, (char *)type); /* get type */
  282. prevtype=subpost->contenttype; /* point to allocated string! */
  283. }
  284. /* now, point our 'more' to the original 'more' */
  285. subpost->more = post->more;
  286. /* then move the original 'more' to point to ourselves */
  287. post->more = subpost;
  288. }
  289. }
  290. contp = sep; /* move the contents pointer to after the separator */
  291. } while(sep && *sep); /* loop if there's another file name */
  292. }
  293. else {
  294. post = (struct curl_httppost *)malloc(sizeof(struct curl_httppost));
  295. if(post) {
  296. memset(post, 0, sizeof(struct curl_httppost));
  297. GetStr(&post->name, name); /* get the name */
  298. if( contp[0]=='<' ) {
  299. GetStr(&post->contents, contp+1); /* get the contents */
  300. post->contentslength = 0;
  301. post->flags = HTTPPOST_READFILE;
  302. }
  303. else {
  304. GetStr(&post->contents, contp); /* get the contents */
  305. post->contentslength = 0;
  306. post->flags = 0;
  307. }
  308. /* make the previous point to this */
  309. if(*last_post)
  310. (*last_post)->next = post;
  311. else
  312. (*httppost) = post;
  313. (*last_post) = post;
  314. }
  315. }
  316. }
  317. else {
  318. free(contents);
  319. return 1;
  320. }
  321. free(contents);
  322. return 0;
  323. }
  324. int curl_formparse(char *input,
  325. struct curl_httppost **httppost,
  326. struct curl_httppost **last_post)
  327. {
  328. return FormParse(input, httppost, last_post);
  329. }
  330. /***************************************************************************
  331. *
  332. * AddHttpPost()
  333. *
  334. * Adds a HttpPost structure to the list, if parent_post is given becomes
  335. * a subpost of parent_post instead of a direct list element.
  336. *
  337. * Returns newly allocated HttpPost on success and NULL if malloc failed.
  338. *
  339. ***************************************************************************/
  340. static struct curl_httppost *
  341. AddHttpPost(char * name, size_t namelength,
  342. char * value, size_t contentslength,
  343. char * buffer, size_t bufferlength,
  344. char *contenttype,
  345. long flags,
  346. struct curl_slist* contentHeader,
  347. char *showfilename,
  348. struct curl_httppost *parent_post,
  349. struct curl_httppost **httppost,
  350. struct curl_httppost **last_post)
  351. {
  352. struct curl_httppost *post;
  353. post = (struct curl_httppost *)malloc(sizeof(struct curl_httppost));
  354. if(post) {
  355. memset(post, 0, sizeof(struct curl_httppost));
  356. post->name = name;
  357. post->namelength = (long)(name?(namelength?namelength:strlen(name)):0);
  358. post->contents = value;
  359. post->contentslength = (long)contentslength;
  360. post->buffer = buffer;
  361. post->bufferlength = (long)bufferlength;
  362. post->contenttype = contenttype;
  363. post->contentheader = contentHeader;
  364. post->showfilename = showfilename;
  365. post->flags = flags;
  366. }
  367. else
  368. return NULL;
  369. if (parent_post) {
  370. /* now, point our 'more' to the original 'more' */
  371. post->more = parent_post->more;
  372. /* then move the original 'more' to point to ourselves */
  373. parent_post->more = post;
  374. }
  375. else {
  376. /* make the previous point to this */
  377. if(*last_post)
  378. (*last_post)->next = post;
  379. else
  380. (*httppost) = post;
  381. (*last_post) = post;
  382. }
  383. return post;
  384. }
  385. /***************************************************************************
  386. *
  387. * AddFormInfo()
  388. *
  389. * Adds a FormInfo structure to the list presented by parent_form_info.
  390. *
  391. * Returns newly allocated FormInfo on success and NULL if malloc failed/
  392. * parent_form_info is NULL.
  393. *
  394. ***************************************************************************/
  395. static FormInfo * AddFormInfo(char *value,
  396. char *contenttype,
  397. FormInfo *parent_form_info)
  398. {
  399. FormInfo *form_info;
  400. form_info = (FormInfo *)malloc(sizeof(FormInfo));
  401. if(form_info) {
  402. memset(form_info, 0, sizeof(FormInfo));
  403. if (value)
  404. form_info->value = value;
  405. if (contenttype)
  406. form_info->contenttype = contenttype;
  407. form_info->flags = HTTPPOST_FILENAME;
  408. }
  409. else
  410. return NULL;
  411. if (parent_form_info) {
  412. /* now, point our 'more' to the original 'more' */
  413. form_info->more = parent_form_info->more;
  414. /* then move the original 'more' to point to ourselves */
  415. parent_form_info->more = form_info;
  416. }
  417. else
  418. return NULL;
  419. return form_info;
  420. }
  421. /***************************************************************************
  422. *
  423. * ContentTypeForFilename()
  424. *
  425. * Provides content type for filename if one of the known types (else
  426. * (either the prevtype or the default is returned).
  427. *
  428. * Returns some valid contenttype for filename.
  429. *
  430. ***************************************************************************/
  431. static const char * ContentTypeForFilename (const char *filename,
  432. const char *prevtype)
  433. {
  434. const char *contenttype = NULL;
  435. unsigned int i;
  436. /*
  437. * No type was specified, we scan through a few well-known
  438. * extensions and pick the first we match!
  439. */
  440. struct ContentType {
  441. const char *extension;
  442. const char *type;
  443. };
  444. static struct ContentType ctts[]={
  445. {".gif", "image/gif"},
  446. {".jpg", "image/jpeg"},
  447. {".jpeg", "image/jpeg"},
  448. {".txt", "text/plain"},
  449. {".html", "text/html"}
  450. };
  451. if(prevtype)
  452. /* default to the previously set/used! */
  453. contenttype = prevtype;
  454. else
  455. /* It seems RFC1867 defines no Content-Type to default to
  456. text/plain so we don't actually need to set this: */
  457. contenttype = HTTPPOST_CONTENTTYPE_DEFAULT;
  458. for(i=0; i<sizeof(ctts)/sizeof(ctts[0]); i++) {
  459. if(strlen(filename) >= strlen(ctts[i].extension)) {
  460. if(strequal(filename +
  461. strlen(filename) - strlen(ctts[i].extension),
  462. ctts[i].extension)) {
  463. contenttype = ctts[i].type;
  464. break;
  465. }
  466. }
  467. }
  468. /* we have a contenttype by now */
  469. return contenttype;
  470. }
  471. /***************************************************************************
  472. *
  473. * AllocAndCopy()
  474. *
  475. * Copies the data currently available under *buffer using newly allocated
  476. * buffer (that becomes *buffer). Uses buffer_length if not null, else
  477. * uses strlen to determine the length of the buffer to be copied
  478. *
  479. * Returns 0 on success and 1 if the malloc failed.
  480. *
  481. ***************************************************************************/
  482. static int AllocAndCopy(char **buffer, size_t buffer_length)
  483. {
  484. const char *src = *buffer;
  485. size_t length;
  486. bool add = FALSE;
  487. if (buffer_length)
  488. length = buffer_length;
  489. else {
  490. length = strlen(*buffer);
  491. add = TRUE;
  492. }
  493. *buffer = (char*)malloc(length+add);
  494. if (!*buffer)
  495. return 1;
  496. memcpy(*buffer, src, length);
  497. /* if length unknown do null termination */
  498. if (add)
  499. (*buffer)[length] = '\0';
  500. return 0;
  501. }
  502. /***************************************************************************
  503. *
  504. * FormAdd()
  505. *
  506. * Stores a 'name=value' formpost parameter and builds the appropriate
  507. * linked list.
  508. *
  509. * Has two principal functionalities: using files and byte arrays as
  510. * post parts. Byte arrays are either copied or just the pointer is stored
  511. * (as the user requests) while for files only the filename and not the
  512. * content is stored.
  513. *
  514. * While you may have only one byte array for each name, multiple filenames
  515. * are allowed (and because of this feature CURLFORM_END is needed after
  516. * using CURLFORM_FILE).
  517. *
  518. * Examples:
  519. *
  520. * Simple name/value pair with copied contents:
  521. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  522. * CURLFORM_COPYCONTENTS, "value", CURLFORM_END);
  523. *
  524. * name/value pair where only the content pointer is remembered:
  525. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  526. * CURLFORM_PTRCONTENTS, ptr, CURLFORM_CONTENTSLENGTH, 10, CURLFORM_END);
  527. * (if CURLFORM_CONTENTSLENGTH is missing strlen () is used)
  528. *
  529. * storing a filename (CONTENTTYPE is optional!):
  530. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  531. * CURLFORM_FILE, "filename1", CURLFORM_CONTENTTYPE, "plain/text",
  532. * CURLFORM_END);
  533. *
  534. * storing multiple filenames:
  535. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  536. * CURLFORM_FILE, "filename1", CURLFORM_FILE, "filename2", CURLFORM_END);
  537. *
  538. * Returns:
  539. * CURL_FORMADD_OK on success
  540. * CURL_FORMADD_MEMORY if the FormInfo allocation fails
  541. * CURL_FORMADD_OPTION_TWICE if one option is given twice for one Form
  542. * CURL_FORMADD_NULL if a null pointer was given for a char
  543. * CURL_FORMADD_MEMORY if the allocation of a FormInfo struct failed
  544. * CURL_FORMADD_UNKNOWN_OPTION if an unknown option was used
  545. * CURL_FORMADD_INCOMPLETE if the some FormInfo is not complete (or an error)
  546. * CURL_FORMADD_MEMORY if a HttpPost struct cannot be allocated
  547. * CURL_FORMADD_MEMORY if some allocation for string copying failed.
  548. * CURL_FORMADD_ILLEGAL_ARRAY if an illegal option is used in an array
  549. *
  550. ***************************************************************************/
  551. static
  552. CURLFORMcode FormAdd(struct curl_httppost **httppost,
  553. struct curl_httppost **last_post,
  554. va_list params)
  555. {
  556. FormInfo *first_form, *current_form, *form;
  557. CURLFORMcode return_value = CURL_FORMADD_OK;
  558. const char *prevtype = NULL;
  559. struct curl_httppost *post = NULL;
  560. CURLformoption option;
  561. struct curl_forms *forms = NULL;
  562. char *array_value=NULL; /* value read from an array */
  563. /* This is a state variable, that if TRUE means that we're parsing an
  564. array that we got passed to us. If FALSE we're parsing the input
  565. va_list arguments. */
  566. bool array_state = FALSE;
  567. /*
  568. * We need to allocate the first struct to fill in.
  569. */
  570. first_form = (FormInfo *)malloc(sizeof(struct FormInfo));
  571. if(first_form) {
  572. memset(first_form, 0, sizeof(FormInfo));
  573. current_form = first_form;
  574. }
  575. else
  576. return CURL_FORMADD_MEMORY;
  577. /*
  578. * Loop through all the options set.
  579. */
  580. while (1) {
  581. /* break if we have an error to report */
  582. if (return_value != CURL_FORMADD_OK)
  583. break;
  584. /* first see if we have more parts of the array param */
  585. if ( array_state ) {
  586. /* get the upcoming option from the given array */
  587. option = forms->option;
  588. array_value = (char *)forms->value;
  589. forms++; /* advance this to next entry */
  590. if (CURLFORM_END == option) {
  591. /* end of array state */
  592. array_state = FALSE;
  593. continue;
  594. }
  595. }
  596. else {
  597. /* This is not array-state, get next option */
  598. option = va_arg(params, CURLformoption);
  599. if (CURLFORM_END == option)
  600. break;
  601. }
  602. switch (option) {
  603. case CURLFORM_ARRAY:
  604. if(array_state)
  605. /* we don't support an array from within an array */
  606. return_value = CURL_FORMADD_ILLEGAL_ARRAY;
  607. else {
  608. forms = va_arg(params, struct curl_forms *);
  609. if (forms)
  610. array_state = TRUE;
  611. else
  612. return_value = CURL_FORMADD_NULL;
  613. }
  614. break;
  615. /*
  616. * Set the Name property.
  617. */
  618. case CURLFORM_PTRNAME:
  619. current_form->flags |= HTTPPOST_PTRNAME; /* fall through */
  620. case CURLFORM_COPYNAME:
  621. if (current_form->name)
  622. return_value = CURL_FORMADD_OPTION_TWICE;
  623. else {
  624. char *name = array_state?
  625. array_value:va_arg(params, char *);
  626. if (name)
  627. current_form->name = name; /* store for the moment */
  628. else
  629. return_value = CURL_FORMADD_NULL;
  630. }
  631. break;
  632. case CURLFORM_NAMELENGTH:
  633. if (current_form->namelength)
  634. return_value = CURL_FORMADD_OPTION_TWICE;
  635. else
  636. current_form->namelength =
  637. array_state?(long)array_value:va_arg(params, long);
  638. break;
  639. /*
  640. * Set the contents property.
  641. */
  642. case CURLFORM_PTRCONTENTS:
  643. current_form->flags |= HTTPPOST_PTRCONTENTS; /* fall through */
  644. case CURLFORM_COPYCONTENTS:
  645. if (current_form->value)
  646. return_value = CURL_FORMADD_OPTION_TWICE;
  647. else {
  648. char *value =
  649. array_state?array_value:va_arg(params, char *);
  650. if (value)
  651. current_form->value = value; /* store for the moment */
  652. else
  653. return_value = CURL_FORMADD_NULL;
  654. }
  655. break;
  656. case CURLFORM_CONTENTSLENGTH:
  657. if (current_form->contentslength)
  658. return_value = CURL_FORMADD_OPTION_TWICE;
  659. else
  660. current_form->contentslength =
  661. array_state?(long)array_value:va_arg(params, long);
  662. break;
  663. /* Get contents from a given file name */
  664. case CURLFORM_FILECONTENT:
  665. if (current_form->flags != 0)
  666. return_value = CURL_FORMADD_OPTION_TWICE;
  667. else {
  668. char *filename = array_state?
  669. array_value:va_arg(params, char *);
  670. if (filename) {
  671. current_form->value = strdup(filename);
  672. current_form->flags |= HTTPPOST_READFILE;
  673. }
  674. else
  675. return_value = CURL_FORMADD_NULL;
  676. }
  677. break;
  678. /* We upload a file */
  679. case CURLFORM_FILE:
  680. {
  681. char *filename = array_state?array_value:
  682. va_arg(params, char *);
  683. if (current_form->value) {
  684. if (current_form->flags & HTTPPOST_FILENAME) {
  685. if (filename) {
  686. if (!(current_form = AddFormInfo(strdup(filename),
  687. NULL, current_form)))
  688. return_value = CURL_FORMADD_MEMORY;
  689. }
  690. else
  691. return_value = CURL_FORMADD_NULL;
  692. }
  693. else
  694. return_value = CURL_FORMADD_OPTION_TWICE;
  695. }
  696. else {
  697. if (filename)
  698. current_form->value = strdup(filename);
  699. else
  700. return_value = CURL_FORMADD_NULL;
  701. current_form->flags |= HTTPPOST_FILENAME;
  702. }
  703. break;
  704. }
  705. case CURLFORM_BUFFER:
  706. {
  707. char *filename = array_state?array_value:
  708. va_arg(params, char *);
  709. if (current_form->value) {
  710. if (current_form->flags & HTTPPOST_BUFFER) {
  711. if (filename) {
  712. if (!(current_form = AddFormInfo(strdup(filename),
  713. NULL, current_form)))
  714. return_value = CURL_FORMADD_MEMORY;
  715. }
  716. else
  717. return_value = CURL_FORMADD_NULL;
  718. }
  719. else
  720. return_value = CURL_FORMADD_OPTION_TWICE;
  721. }
  722. else {
  723. if (filename)
  724. current_form->value = strdup(filename);
  725. else
  726. return_value = CURL_FORMADD_NULL;
  727. current_form->flags |= HTTPPOST_BUFFER;
  728. }
  729. break;
  730. }
  731. case CURLFORM_BUFFERPTR:
  732. current_form->flags |= HTTPPOST_PTRBUFFER;
  733. if (current_form->buffer)
  734. return_value = CURL_FORMADD_OPTION_TWICE;
  735. else {
  736. char *buffer =
  737. array_state?array_value:va_arg(params, char *);
  738. if (buffer)
  739. current_form->buffer = buffer; /* store for the moment */
  740. else
  741. return_value = CURL_FORMADD_NULL;
  742. }
  743. break;
  744. case CURLFORM_BUFFERLENGTH:
  745. if (current_form->bufferlength)
  746. return_value = CURL_FORMADD_OPTION_TWICE;
  747. else
  748. current_form->bufferlength =
  749. array_state?(long)array_value:va_arg(params, long);
  750. break;
  751. case CURLFORM_CONTENTTYPE:
  752. {
  753. char *contenttype =
  754. array_state?array_value:va_arg(params, char *);
  755. if (current_form->contenttype) {
  756. if (current_form->flags & HTTPPOST_FILENAME) {
  757. if (contenttype) {
  758. if (!(current_form = AddFormInfo(NULL,
  759. strdup(contenttype),
  760. current_form)))
  761. return_value = CURL_FORMADD_MEMORY;
  762. }
  763. else
  764. return_value = CURL_FORMADD_NULL;
  765. }
  766. else
  767. return_value = CURL_FORMADD_OPTION_TWICE;
  768. }
  769. else {
  770. if (contenttype)
  771. current_form->contenttype = strdup(contenttype);
  772. else
  773. return_value = CURL_FORMADD_NULL;
  774. }
  775. break;
  776. }
  777. case CURLFORM_CONTENTHEADER:
  778. {
  779. /* this "cast increases required alignment of target type" but
  780. we consider it OK anyway */
  781. struct curl_slist* list = array_state?
  782. (struct curl_slist*)array_value:
  783. va_arg(params, struct curl_slist*);
  784. if( current_form->contentheader )
  785. return_value = CURL_FORMADD_OPTION_TWICE;
  786. else
  787. current_form->contentheader = list;
  788. break;
  789. }
  790. case CURLFORM_FILENAME:
  791. {
  792. char *filename = array_state?array_value:
  793. va_arg(params, char *);
  794. if( current_form->showfilename )
  795. return_value = CURL_FORMADD_OPTION_TWICE;
  796. else
  797. current_form->showfilename = strdup(filename);
  798. break;
  799. }
  800. default:
  801. return_value = CURL_FORMADD_UNKNOWN_OPTION;
  802. }
  803. }
  804. if(CURL_FORMADD_OK == return_value) {
  805. /* go through the list, check for copleteness and if everything is
  806. * alright add the HttpPost item otherwise set return_value accordingly */
  807. post = NULL;
  808. for(form = first_form;
  809. form != NULL;
  810. form = form->more) {
  811. if ( ((!form->name || !form->value) && !post) ||
  812. ( (form->contentslength) &&
  813. (form->flags & HTTPPOST_FILENAME) ) ||
  814. ( (form->flags & HTTPPOST_FILENAME) &&
  815. (form->flags & HTTPPOST_PTRCONTENTS) ) ||
  816. ( (!form->buffer) &&
  817. (form->flags & HTTPPOST_BUFFER) &&
  818. (form->flags & HTTPPOST_PTRBUFFER) ) ||
  819. ( (form->flags & HTTPPOST_READFILE) &&
  820. (form->flags & HTTPPOST_PTRCONTENTS) )
  821. ) {
  822. return_value = CURL_FORMADD_INCOMPLETE;
  823. break;
  824. }
  825. else {
  826. if ( ((form->flags & HTTPPOST_FILENAME) ||
  827. (form->flags & HTTPPOST_BUFFER)) &&
  828. !form->contenttype ) {
  829. /* our contenttype is missing */
  830. form->contenttype
  831. = strdup(ContentTypeForFilename(form->value, prevtype));
  832. }
  833. if ( !(form->flags & HTTPPOST_PTRNAME) &&
  834. (form == first_form) ) {
  835. /* copy name (without strdup; possibly contains null characters) */
  836. if (AllocAndCopy(&form->name, form->namelength)) {
  837. return_value = CURL_FORMADD_MEMORY;
  838. break;
  839. }
  840. }
  841. if ( !(form->flags & HTTPPOST_FILENAME) &&
  842. !(form->flags & HTTPPOST_READFILE) &&
  843. !(form->flags & HTTPPOST_PTRCONTENTS) &&
  844. !(form->flags & HTTPPOST_PTRBUFFER) ) {
  845. /* copy value (without strdup; possibly contains null characters) */
  846. if (AllocAndCopy(&form->value, form->contentslength)) {
  847. return_value = CURL_FORMADD_MEMORY;
  848. break;
  849. }
  850. }
  851. post = AddHttpPost(form->name, form->namelength,
  852. form->value, form->contentslength,
  853. form->buffer, form->bufferlength,
  854. form->contenttype, form->flags,
  855. form->contentheader, form->showfilename,
  856. post, httppost,
  857. last_post);
  858. if(!post)
  859. return_value = CURL_FORMADD_MEMORY;
  860. if (form->contenttype)
  861. prevtype = form->contenttype;
  862. }
  863. }
  864. }
  865. /* always delete the allocated memory before returning */
  866. form = first_form;
  867. while (form != NULL) {
  868. FormInfo *delete_form;
  869. delete_form = form;
  870. form = form->more;
  871. free (delete_form);
  872. }
  873. return return_value;
  874. }
  875. CURLFORMcode curl_formadd(struct curl_httppost **httppost,
  876. struct curl_httppost **last_post,
  877. ...)
  878. {
  879. va_list arg;
  880. CURLFORMcode result;
  881. va_start(arg, last_post);
  882. result = FormAdd(httppost, last_post, arg);
  883. va_end(arg);
  884. return result;
  885. }
  886. static size_t AddFormData(struct FormData **formp,
  887. const void *line,
  888. size_t length)
  889. {
  890. struct FormData *newform = (struct FormData *)
  891. malloc(sizeof(struct FormData));
  892. newform->next = NULL;
  893. /* we make it easier for plain strings: */
  894. if(!length)
  895. length = strlen((char *)line);
  896. newform->line = (char *)malloc(length+1);
  897. memcpy(newform->line, line, length);
  898. newform->length = length;
  899. newform->line[length]=0; /* zero terminate for easier debugging */
  900. if(*formp) {
  901. (*formp)->next = newform;
  902. *formp = newform;
  903. }
  904. else
  905. *formp = newform;
  906. return length;
  907. }
  908. static size_t AddFormDataf(struct FormData **formp,
  909. const char *fmt, ...)
  910. {
  911. char s[4096];
  912. va_list ap;
  913. va_start(ap, fmt);
  914. vsprintf(s, fmt, ap);
  915. va_end(ap);
  916. return AddFormData(formp, s, 0);
  917. }
  918. char *Curl_FormBoundary(void)
  919. {
  920. char *retstring;
  921. static int randomizer=0; /* this is just so that two boundaries within
  922. the same form won't be identical */
  923. size_t i;
  924. static char table16[]="abcdef0123456789";
  925. retstring = (char *)malloc(BOUNDARY_LENGTH+1);
  926. if(!retstring)
  927. return NULL; /* failed */
  928. srand(time(NULL)+randomizer++); /* seed */
  929. strcpy(retstring, "----------------------------");
  930. for(i=strlen(retstring); i<BOUNDARY_LENGTH; i++)
  931. retstring[i] = table16[rand()%16];
  932. /* 28 dashes and 12 hexadecimal digits makes 12^16 (184884258895036416)
  933. combinations */
  934. retstring[BOUNDARY_LENGTH]=0; /* zero terminate */
  935. return retstring;
  936. }
  937. /* Used from http.c, this cleans a built FormData linked list */
  938. void Curl_formclean(struct FormData *form)
  939. {
  940. struct FormData *next;
  941. do {
  942. next=form->next; /* the following form line */
  943. free(form->line); /* free the line */
  944. free(form); /* free the struct */
  945. } while((form=next)); /* continue */
  946. }
  947. /* external function to free up a whole form post chain */
  948. void curl_formfree(struct curl_httppost *form)
  949. {
  950. struct curl_httppost *next;
  951. if(!form)
  952. /* no form to free, just get out of this */
  953. return;
  954. do {
  955. next=form->next; /* the following form line */
  956. /* recurse to sub-contents */
  957. if(form->more)
  958. curl_formfree(form->more);
  959. if( !(form->flags & HTTPPOST_PTRNAME) && form->name)
  960. free(form->name); /* free the name */
  961. if( !(form->flags & HTTPPOST_PTRCONTENTS) && form->contents)
  962. free(form->contents); /* free the contents */
  963. if(form->contenttype)
  964. free(form->contenttype); /* free the content type */
  965. if(form->showfilename)
  966. free(form->showfilename); /* free the faked file name */
  967. free(form); /* free the struct */
  968. } while((form=next)); /* continue */
  969. }
  970. CURLcode Curl_getFormData(struct FormData **finalform,
  971. struct curl_httppost *post,
  972. curl_off_t *sizep)
  973. {
  974. struct FormData *form = NULL;
  975. struct FormData *firstform;
  976. struct curl_httppost *file;
  977. CURLcode result = CURLE_OK;
  978. size_t size =0;
  979. char *boundary;
  980. char *fileboundary=NULL;
  981. struct curl_slist* curList;
  982. *finalform=NULL; /* default form is empty */
  983. if(!post)
  984. return result; /* no input => no output! */
  985. boundary = Curl_FormBoundary();
  986. /* Make the first line of the output */
  987. AddFormDataf(&form,
  988. "Content-Type: multipart/form-data;"
  989. " boundary=%s\r\n",
  990. boundary);
  991. /* we DO NOT count that line since that'll be part of the header! */
  992. firstform = form;
  993. do {
  994. if(size)
  995. size += AddFormDataf(&form, "\r\n");
  996. /* boundary */
  997. size += AddFormDataf(&form, "--%s\r\n", boundary);
  998. size += AddFormData(&form,
  999. "Content-Disposition: form-data; name=\"", 0);
  1000. size += AddFormData(&form, post->name, post->namelength);
  1001. size += AddFormData(&form, "\"", 0);
  1002. if(post->more) {
  1003. /* If used, this is a link to more file names, we must then do
  1004. the magic to include several files with the same field name */
  1005. fileboundary = Curl_FormBoundary();
  1006. size += AddFormDataf(&form,
  1007. "\r\nContent-Type: multipart/mixed,"
  1008. " boundary=%s\r\n",
  1009. fileboundary);
  1010. }
  1011. file = post;
  1012. do {
  1013. /* If 'showfilename' is set, that is a faked name passed on to us
  1014. to use to in the formpost. If that is not set, the actually used
  1015. local file name should be added. */
  1016. if(post->more) {
  1017. /* if multiple-file */
  1018. size += AddFormDataf(&form,
  1019. "\r\n--%s\r\nContent-Disposition: "
  1020. "attachment; filename=\"%s\"",
  1021. fileboundary,
  1022. (file->showfilename?file->showfilename:
  1023. file->contents));
  1024. }
  1025. else if((post->flags & HTTPPOST_FILENAME) ||
  1026. (post->flags & HTTPPOST_BUFFER)) {
  1027. size += AddFormDataf(&form,
  1028. "; filename=\"%s\"",
  1029. (post->showfilename?post->showfilename:
  1030. post->contents));
  1031. }
  1032. if(file->contenttype) {
  1033. /* we have a specified type */
  1034. size += AddFormDataf(&form,
  1035. "\r\nContent-Type: %s",
  1036. file->contenttype);
  1037. }
  1038. curList = file->contentheader;
  1039. while( curList ) {
  1040. /* Process the additional headers specified for this form */
  1041. size += AddFormDataf( &form, "\r\n%s", curList->data );
  1042. curList = curList->next;
  1043. }
  1044. #if 0
  1045. /* The header Content-Transfer-Encoding: seems to confuse some receivers
  1046. * (like the built-in PHP engine). While I can't see any reason why it
  1047. * should, I can just as well skip this to the benefit of the users who
  1048. * are using such confused receivers.
  1049. */
  1050. if(file->contenttype &&
  1051. !checkprefix("text/", file->contenttype)) {
  1052. /* this is not a text content, mention our binary encoding */
  1053. size += AddFormData(&form, "\r\nContent-Transfer-Encoding: binary", 0);
  1054. }
  1055. #endif
  1056. size += AddFormData(&form, "\r\n\r\n", 0);
  1057. if((post->flags & HTTPPOST_FILENAME) ||
  1058. (post->flags & HTTPPOST_READFILE)) {
  1059. /* we should include the contents from the specified file */
  1060. FILE *fileread;
  1061. char buffer[1024];
  1062. size_t nread;
  1063. fileread = strequal("-", file->contents)?stdin:
  1064. /* binary read for win32 crap */
  1065. /*VMS??*/ fopen(file->contents, "rb"); /* ONLY ALLOWS FOR STREAM FILES ON VMS */
  1066. /*VMS?? Stream files are OK, as are FIXED & VAR files WITHOUT implied CC */
  1067. /*VMS?? For implied CC, every record needs to have a \n appended & 1 added to SIZE */
  1068. if(fileread) {
  1069. while((nread = fread(buffer, 1, 1024, fileread)))
  1070. size += AddFormData(&form, buffer, nread);
  1071. if(fileread != stdin)
  1072. fclose(fileread);
  1073. }
  1074. else {
  1075. #if 0
  1076. /* File wasn't found, add a nothing field! */
  1077. size += AddFormData(&form, "", 0);
  1078. #endif
  1079. Curl_formclean(firstform);
  1080. free(boundary);
  1081. *finalform = NULL;
  1082. return CURLE_READ_ERROR;
  1083. }
  1084. }
  1085. else if (post->flags & HTTPPOST_BUFFER) {
  1086. /* include contents of buffer */
  1087. size += AddFormData(&form, post->buffer, post->bufferlength);
  1088. }
  1089. else {
  1090. /* include the contents we got */
  1091. size += AddFormData(&form, post->contents, post->contentslength);
  1092. }
  1093. } while((file = file->more)); /* for each specified file for this field */
  1094. if(post->more) {
  1095. /* this was a multiple-file inclusion, make a termination file
  1096. boundary: */
  1097. size += AddFormDataf(&form,
  1098. "\r\n--%s--",
  1099. fileboundary);
  1100. free(fileboundary);
  1101. }
  1102. } while((post=post->next)); /* for each field */
  1103. /* end-boundary for everything */
  1104. size += AddFormDataf(&form,
  1105. "\r\n--%s--\r\n",
  1106. boundary);
  1107. *sizep = size;
  1108. free(boundary);
  1109. *finalform=firstform;
  1110. return result;
  1111. }
  1112. int Curl_FormInit(struct Form *form, struct FormData *formdata )
  1113. {
  1114. if(!formdata)
  1115. return 1; /* error */
  1116. form->data = formdata;
  1117. form->sent = 0;
  1118. return 0;
  1119. }
  1120. /* fread() emulation */
  1121. size_t Curl_FormReader(char *buffer,
  1122. size_t size,
  1123. size_t nitems,
  1124. FILE *mydata)
  1125. {
  1126. struct Form *form;
  1127. size_t wantedsize;
  1128. size_t gotsize = 0;
  1129. form=(struct Form *)mydata;
  1130. wantedsize = size * nitems;
  1131. if(!form->data)
  1132. return 0; /* nothing, error, empty */
  1133. do {
  1134. if( (form->data->length - form->sent ) > wantedsize - gotsize) {
  1135. memcpy(buffer + gotsize , form->data->line + form->sent,
  1136. wantedsize - gotsize);
  1137. form->sent += wantedsize-gotsize;
  1138. return wantedsize;
  1139. }
  1140. memcpy(buffer+gotsize,
  1141. form->data->line + form->sent,
  1142. (form->data->length - form->sent) );
  1143. gotsize += form->data->length - form->sent;
  1144. form->sent = 0;
  1145. form->data = form->data->next; /* advance */
  1146. } while(form->data);
  1147. /* If we got an empty line and we have more data, we proceed to the next
  1148. line immediately to avoid returning zero before we've reached the end.
  1149. This is the bug reported November 22 1999 on curl 6.3. (Daniel) */
  1150. return gotsize;
  1151. }
  1152. /* possible (old) fread() emulation that copies at most one line */
  1153. size_t Curl_FormReadOneLine(char *buffer,
  1154. size_t size,
  1155. size_t nitems,
  1156. FILE *mydata)
  1157. {
  1158. struct Form *form;
  1159. size_t wantedsize;
  1160. size_t gotsize;
  1161. form=(struct Form *)mydata;
  1162. wantedsize = size * nitems;
  1163. if(!form->data)
  1164. return 0; /* nothing, error, empty */
  1165. do {
  1166. if( (form->data->length - form->sent ) > wantedsize ) {
  1167. memcpy(buffer, form->data->line + form->sent, wantedsize);
  1168. form->sent += wantedsize;
  1169. return wantedsize;
  1170. }
  1171. memcpy(buffer,
  1172. form->data->line + form->sent,
  1173. gotsize = (form->data->length - form->sent) );
  1174. form->sent = 0;
  1175. form->data = form->data->next; /* advance */
  1176. } while(!gotsize && form->data);
  1177. /* If we got an empty line and we have more data, we proceed to the next
  1178. line immediately to avoid returning zero before we've reached the end.
  1179. This is the bug reported November 22 1999 on curl 6.3. (Daniel) */
  1180. return gotsize;
  1181. }
  1182. #ifdef _FORM_DEBUG
  1183. int FormAddTest(const char * errormsg,
  1184. struct curl_httppost **httppost,
  1185. struct curl_httppost **last_post,
  1186. ...)
  1187. {
  1188. int result;
  1189. va_list arg;
  1190. va_start(arg, last_post);
  1191. if ((result = FormAdd(httppost, last_post, arg)))
  1192. fprintf (stderr, "ERROR doing FormAdd ret: %d action: %s\n", result,
  1193. errormsg);
  1194. va_end(arg);
  1195. return result;
  1196. }
  1197. int main()
  1198. {
  1199. char name1[] = "simple_COPYCONTENTS";
  1200. char name2[] = "COPYCONTENTS_+_CONTENTTYPE";
  1201. char name3[] = "PTRNAME_+_NAMELENGTH_+_COPYNAME_+_CONTENTSLENGTH";
  1202. char name4[] = "simple_PTRCONTENTS";
  1203. char name5[] = "PTRCONTENTS_+_CONTENTSLENGTH";
  1204. char name6[] = "PTRCONTENTS_+_CONTENTSLENGTH_+_CONTENTTYPE";
  1205. char name7[] = "FILE1_+_CONTENTTYPE";
  1206. char name8[] = "FILE1_+_FILE2";
  1207. char name9[] = "FILE1_+_FILE2_+_FILE3";
  1208. char name10[] = "ARRAY: FILE1_+_FILE2_+_FILE3";
  1209. char name11[] = "FILECONTENT";
  1210. char value1[] = "value for simple COPYCONTENTS";
  1211. char value2[] = "value for COPYCONTENTS + CONTENTTYPE";
  1212. char value3[] = "value for PTRNAME + NAMELENGTH + COPYNAME + CONTENTSLENGTH";
  1213. char value4[] = "value for simple PTRCONTENTS";
  1214. char value5[] = "value for PTRCONTENTS + CONTENTSLENGTH";
  1215. char value6[] = "value for PTRCOTNENTS + CONTENTSLENGTH + CONTENTTYPE";
  1216. char value7[] = "inet_ntoa_r.h";
  1217. char value8[] = "Makefile.b32.resp";
  1218. char type2[] = "image/gif";
  1219. char type6[] = "text/plain";
  1220. char type7[] = "text/html";
  1221. int name3length = strlen(name3);
  1222. int value3length = strlen(value3);
  1223. int value5length = strlen(value4);
  1224. int value6length = strlen(value5);
  1225. int errors = 0;
  1226. int size;
  1227. size_t nread;
  1228. char buffer[4096];
  1229. struct curl_httppost *httppost=NULL;
  1230. struct curl_httppost *last_post=NULL;
  1231. struct curl_forms forms[4];
  1232. struct FormData *form;
  1233. struct Form formread;
  1234. if (FormAddTest("simple COPYCONTENTS test", &httppost, &last_post,
  1235. CURLFORM_COPYNAME, name1, CURLFORM_COPYCONTENTS, value1,
  1236. CURLFORM_END))
  1237. ++errors;
  1238. if (FormAddTest("COPYCONTENTS + CONTENTTYPE test", &httppost, &last_post,
  1239. CURLFORM_COPYNAME, name2, CURLFORM_COPYCONTENTS, value2,
  1240. CURLFORM_CONTENTTYPE, type2, CURLFORM_END))
  1241. ++errors;
  1242. /* make null character at start to check that contentslength works
  1243. correctly */
  1244. name3[1] = '\0';
  1245. value3[1] = '\0';
  1246. if (FormAddTest("PTRNAME + NAMELENGTH + COPYNAME + CONTENTSLENGTH test",
  1247. &httppost, &last_post,
  1248. CURLFORM_PTRNAME, name3, CURLFORM_COPYCONTENTS, value3,
  1249. CURLFORM_CONTENTSLENGTH, value3length,
  1250. CURLFORM_NAMELENGTH, name3length, CURLFORM_END))
  1251. ++errors;
  1252. if (FormAddTest("simple PTRCONTENTS test", &httppost, &last_post,
  1253. CURLFORM_COPYNAME, name4, CURLFORM_PTRCONTENTS, value4,
  1254. CURLFORM_END))
  1255. ++errors;
  1256. /* make null character at start to check that contentslength works
  1257. correctly */
  1258. value5[1] = '\0';
  1259. if (FormAddTest("PTRCONTENTS + CONTENTSLENGTH test", &httppost, &last_post,
  1260. CURLFORM_COPYNAME, name5, CURLFORM_PTRCONTENTS, value5,
  1261. CURLFORM_CONTENTSLENGTH, value5length, CURLFORM_END))
  1262. ++errors;
  1263. /* make null character at start to check that contentslength works
  1264. correctly */
  1265. value6[1] = '\0';
  1266. if (FormAddTest("PTRCONTENTS + CONTENTSLENGTH + CONTENTTYPE test",
  1267. &httppost, &last_post,
  1268. CURLFORM_COPYNAME, name6, CURLFORM_PTRCONTENTS, value6,
  1269. CURLFORM_CONTENTSLENGTH, value6length,
  1270. CURLFORM_CONTENTTYPE, type6, CURLFORM_END))
  1271. ++errors;
  1272. if (FormAddTest("FILE + CONTENTTYPE test", &httppost, &last_post,
  1273. CURLFORM_COPYNAME, name7, CURLFORM_FILE, value7,
  1274. CURLFORM_CONTENTTYPE, type7, CURLFORM_END))
  1275. ++errors;
  1276. if (FormAddTest("FILE1 + FILE2 test", &httppost, &last_post,
  1277. CURLFORM_COPYNAME, name8, CURLFORM_FILE, value7,
  1278. CURLFORM_FILE, value8, CURLFORM_END))
  1279. ++errors;
  1280. if (FormAddTest("FILE1 + FILE2 + FILE3 test", &httppost, &last_post,
  1281. CURLFORM_COPYNAME, name9, CURLFORM_FILE, value7,
  1282. CURLFORM_FILE, value8, CURLFORM_FILE, value7, CURLFORM_END))
  1283. ++errors;
  1284. forms[0].option = CURLFORM_FILE;
  1285. forms[0].value = value7;
  1286. forms[1].option = CURLFORM_FILE;
  1287. forms[1].value = value8;
  1288. forms[2].option = CURLFORM_FILE;
  1289. forms[2].value = value7;
  1290. forms[3].option = CURLFORM_END;
  1291. if (FormAddTest("FILE1 + FILE2 + FILE3 ARRAY test", &httppost, &last_post,
  1292. CURLFORM_COPYNAME, name10, CURLFORM_ARRAY, forms,
  1293. CURLFORM_END))
  1294. ++errors;
  1295. if (FormAddTest("FILECONTENT test", &httppost, &last_post,
  1296. CURLFORM_COPYNAME, name11, CURLFORM_FILECONTENT, value7,
  1297. CURLFORM_END))
  1298. ++errors;
  1299. form=Curl_getFormData(httppost, &size);
  1300. Curl_FormInit(&formread, form);
  1301. do {
  1302. nread = Curl_FormReader(buffer, 1, sizeof(buffer),
  1303. (FILE *)&formread);
  1304. if(-1 == nread)
  1305. break;
  1306. fwrite(buffer, nread, 1, stdout);
  1307. } while(1);
  1308. fprintf(stdout, "size: %d\n", size);
  1309. if (errors)
  1310. fprintf(stdout, "\n==> %d Test(s) failed!\n", errors);
  1311. else
  1312. fprintf(stdout, "\nAll Tests seem to have worked (please check output)\n");
  1313. return 0;
  1314. }
  1315. #endif
  1316. #ifdef _OLD_FORM_DEBUG
  1317. int main(int argc, char **argv)
  1318. {
  1319. #if 0
  1320. char *testargs[]={
  1321. "name1 = data in number one",
  1322. "name2 = number two data",
  1323. "test = @upload"
  1324. };
  1325. #endif
  1326. int i;
  1327. char *nextarg;
  1328. struct curl_httppost *httppost=NULL;
  1329. struct curl_httppost *last_post=NULL;
  1330. struct curl_httppost *post;
  1331. int size;
  1332. int nread;
  1333. char buffer[4096];
  1334. struct FormData *form;
  1335. struct Form formread;
  1336. for(i=1; i<argc; i++) {
  1337. if( FormParse( argv[i],
  1338. &httppost,
  1339. &last_post)) {
  1340. fprintf(stderr, "Illegally formatted input field: '%s'!\n",
  1341. argv[i]);
  1342. return 1;
  1343. }
  1344. }
  1345. form=Curl_getFormData(httppost, &size);
  1346. Curl_FormInit(&formread, form);
  1347. do {
  1348. nread = Curl_FormReader(buffer, 1, sizeof(buffer),
  1349. (FILE *)&formread);
  1350. if(-1 == nread)
  1351. break;
  1352. fwrite(buffer, nread, 1, stderr);
  1353. } while(1);
  1354. fprintf(stderr, "size: %d\n", size);
  1355. return 0;
  1356. }
  1357. #endif
  1358. #endif /* CURL_DISABLE_HTTP */