formdata.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, Daniel Stenberg, <[email protected]>, 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$
  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. #include <curl/curl.h>
  97. #include "formdata.h"
  98. #include "strequal.h"
  99. /* The last #include file should be: */
  100. #ifdef MALLOCDEBUG
  101. #include "memdebug.h"
  102. #endif
  103. /* Length of the random boundary string. The risk of this being used
  104. in binary data is very close to zero, 64^32 makes
  105. 6277101735386680763835789423207666416102355444464034512896
  106. combinations... */
  107. #define BOUNDARY_LENGTH 32
  108. /* What kind of Content-Type to use on un-specified files with unrecognized
  109. extensions. */
  110. #define HTTPPOST_CONTENTTYPE_DEFAULT "text/plain"
  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/plain"}
  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, long namelength,
  342. char * value, long contentslength,
  343. /* CMC: Added support for buffer uploads */
  344. char * buffer, long bufferlength,
  345. char *contenttype,
  346. long flags,
  347. struct curl_slist* contentHeader,
  348. char *showfilename,
  349. struct curl_httppost *parent_post,
  350. struct curl_httppost **httppost,
  351. struct curl_httppost **last_post)
  352. {
  353. struct curl_httppost *post;
  354. post = (struct curl_httppost *)malloc(sizeof(struct curl_httppost));
  355. if(post) {
  356. memset(post, 0, sizeof(struct curl_httppost));
  357. post->name = name;
  358. post->namelength = name?(namelength?namelength:(long)strlen(name)):0;
  359. post->contents = value;
  360. post->contentslength = contentslength;
  361. /* CMC: Added support for buffer uploads */
  362. post->buffer = buffer;
  363. post->bufferlength = bufferlength;
  364. post->contenttype = contenttype;
  365. post->contentheader = contentHeader;
  366. post->showfilename = showfilename;
  367. post->flags = flags;
  368. }
  369. else
  370. return NULL;
  371. if (parent_post) {
  372. /* now, point our 'more' to the original 'more' */
  373. post->more = parent_post->more;
  374. /* then move the original 'more' to point to ourselves */
  375. parent_post->more = post;
  376. }
  377. else {
  378. /* make the previous point to this */
  379. if(*last_post)
  380. (*last_post)->next = post;
  381. else
  382. (*httppost) = post;
  383. (*last_post) = post;
  384. }
  385. return post;
  386. }
  387. /***************************************************************************
  388. *
  389. * AddFormInfo()
  390. *
  391. * Adds a FormInfo structure to the list presented by parent_form_info.
  392. *
  393. * Returns newly allocated FormInfo on success and NULL if malloc failed/
  394. * parent_form_info is NULL.
  395. *
  396. ***************************************************************************/
  397. static FormInfo * AddFormInfo(char *value,
  398. char *contenttype,
  399. FormInfo *parent_form_info)
  400. {
  401. FormInfo *form_info;
  402. form_info = (FormInfo *)malloc(sizeof(FormInfo));
  403. if(form_info) {
  404. memset(form_info, 0, sizeof(FormInfo));
  405. if (value)
  406. form_info->value = value;
  407. if (contenttype)
  408. form_info->contenttype = contenttype;
  409. form_info->flags = HTTPPOST_FILENAME;
  410. }
  411. else
  412. return NULL;
  413. if (parent_form_info) {
  414. /* now, point our 'more' to the original 'more' */
  415. form_info->more = parent_form_info->more;
  416. /* then move the original 'more' to point to ourselves */
  417. parent_form_info->more = form_info;
  418. }
  419. else
  420. return NULL;
  421. return form_info;
  422. }
  423. /***************************************************************************
  424. *
  425. * ContentTypeForFilename()
  426. *
  427. * Provides content type for filename if one of the known types (else
  428. * (either the prevtype or the default is returned).
  429. *
  430. * Returns some valid contenttype for filename.
  431. *
  432. ***************************************************************************/
  433. static const char * ContentTypeForFilename (const char *filename,
  434. const char *prevtype)
  435. {
  436. const char *contenttype = NULL;
  437. unsigned int i;
  438. /*
  439. * No type was specified, we scan through a few well-known
  440. * extensions and pick the first we match!
  441. */
  442. struct ContentType {
  443. const char *extension;
  444. const char *type;
  445. };
  446. static struct ContentType ctts[]={
  447. {".gif", "image/gif"},
  448. {".jpg", "image/jpeg"},
  449. {".jpeg", "image/jpeg"},
  450. {".txt", "text/plain"},
  451. {".html", "text/plain"}
  452. };
  453. if(prevtype)
  454. /* default to the previously set/used! */
  455. contenttype = prevtype;
  456. else
  457. /* It seems RFC1867 defines no Content-Type to default to
  458. text/plain so we don't actually need to set this: */
  459. contenttype = HTTPPOST_CONTENTTYPE_DEFAULT;
  460. for(i=0; i<sizeof(ctts)/sizeof(ctts[0]); i++) {
  461. if(strlen(filename) >= strlen(ctts[i].extension)) {
  462. if(strequal(filename +
  463. strlen(filename) - strlen(ctts[i].extension),
  464. ctts[i].extension)) {
  465. contenttype = ctts[i].type;
  466. break;
  467. }
  468. }
  469. }
  470. /* we have a contenttype by now */
  471. return contenttype;
  472. }
  473. /***************************************************************************
  474. *
  475. * AllocAndCopy()
  476. *
  477. * Copies the data currently available under *buffer using newly allocated
  478. * buffer (that becomes *buffer). Uses buffer_length if not null, else
  479. * uses strlen to determine the length of the buffer to be copied
  480. *
  481. * Returns 0 on success and 1 if the malloc failed.
  482. *
  483. ***************************************************************************/
  484. static int AllocAndCopy (char **buffer, int buffer_length)
  485. {
  486. const char *src = *buffer;
  487. int length, add = 0;
  488. if (buffer_length)
  489. length = buffer_length;
  490. else {
  491. length = strlen(*buffer);
  492. add = 1;
  493. }
  494. *buffer = (char*)malloc(length+add);
  495. if (!*buffer)
  496. return 1;
  497. memcpy(*buffer, src, length);
  498. /* if length unknown do null termination */
  499. if (add)
  500. (*buffer)[length] = '\0';
  501. return 0;
  502. }
  503. /***************************************************************************
  504. *
  505. * FormAdd()
  506. *
  507. * Stores a 'name=value' formpost parameter and builds the appropriate
  508. * linked list.
  509. *
  510. * Has two principal functionalities: using files and byte arrays as
  511. * post parts. Byte arrays are either copied or just the pointer is stored
  512. * (as the user requests) while for files only the filename and not the
  513. * content is stored.
  514. *
  515. * While you may have only one byte array for each name, multiple filenames
  516. * are allowed (and because of this feature CURLFORM_END is needed after
  517. * using CURLFORM_FILE).
  518. *
  519. * Examples:
  520. *
  521. * Simple name/value pair with copied contents:
  522. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  523. * CURLFORM_COPYCONTENTS, "value", CURLFORM_END);
  524. *
  525. * name/value pair where only the content pointer is remembered:
  526. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  527. * CURLFORM_PTRCONTENTS, ptr, CURLFORM_CONTENTSLENGTH, 10, CURLFORM_END);
  528. * (if CURLFORM_CONTENTSLENGTH is missing strlen () is used)
  529. *
  530. * storing a filename (CONTENTTYPE is optional!):
  531. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  532. * CURLFORM_FILE, "filename1", CURLFORM_CONTENTTYPE, "plain/text",
  533. * CURLFORM_END);
  534. *
  535. * storing multiple filenames:
  536. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  537. * CURLFORM_FILE, "filename1", CURLFORM_FILE, "filename2", CURLFORM_END);
  538. *
  539. * Returns:
  540. * CURL_FORMADD_OK on success
  541. * CURL_FORMADD_MEMORY if the FormInfo allocation fails
  542. * CURL_FORMADD_OPTION_TWICE if one option is given twice for one Form
  543. * CURL_FORMADD_NULL if a null pointer was given for a char
  544. * CURL_FORMADD_MEMORY if the allocation of a FormInfo struct failed
  545. * CURL_FORMADD_UNKNOWN_OPTION if an unknown option was used
  546. * CURL_FORMADD_INCOMPLETE if the some FormInfo is not complete (or an error)
  547. * CURL_FORMADD_MEMORY if a HttpPost struct cannot be allocated
  548. * CURL_FORMADD_MEMORY if some allocation for string copying failed.
  549. * CURL_FORMADD_ILLEGAL_ARRAY if an illegal option is used in an array
  550. *
  551. ***************************************************************************/
  552. static
  553. CURLFORMcode FormAdd(struct curl_httppost **httppost,
  554. struct curl_httppost **last_post,
  555. va_list params)
  556. {
  557. FormInfo *first_form, *current_form, *form;
  558. CURLFORMcode return_value = CURL_FORMADD_OK;
  559. const char *prevtype = NULL;
  560. struct curl_httppost *post = NULL;
  561. CURLformoption option;
  562. struct curl_forms *forms = NULL;
  563. char *array_value=NULL; /* value read from an array */
  564. /* This is a state variable, that if TRUE means that we're parsing an
  565. array that we got passed to us. If FALSE we're parsing the input
  566. va_list arguments. */
  567. bool array_state = FALSE;
  568. /*
  569. * We need to allocate the first struct to fill in.
  570. */
  571. first_form = (FormInfo *)malloc(sizeof(struct FormInfo));
  572. if(first_form) {
  573. memset(first_form, 0, sizeof(FormInfo));
  574. current_form = first_form;
  575. }
  576. else
  577. return CURL_FORMADD_MEMORY;
  578. /*
  579. * Loop through all the options set.
  580. */
  581. while (1) {
  582. /* break if we have an error to report */
  583. if (return_value != CURL_FORMADD_OK)
  584. break;
  585. /* first see if we have more parts of the array param */
  586. if ( array_state ) {
  587. /* get the upcoming option from the given array */
  588. option = forms->option;
  589. array_value = (char *)forms->value;
  590. forms++; /* advance this to next entry */
  591. if (CURLFORM_END == option) {
  592. /* end of array state */
  593. array_state = FALSE;
  594. continue;
  595. }
  596. }
  597. else {
  598. /* This is not array-state, get next option */
  599. option = va_arg(params, CURLformoption);
  600. if (CURLFORM_END == option)
  601. break;
  602. }
  603. switch (option) {
  604. case CURLFORM_ARRAY:
  605. if(array_state)
  606. /* we don't support an array from within an array */
  607. return_value = CURL_FORMADD_ILLEGAL_ARRAY;
  608. else {
  609. forms = va_arg(params, struct curl_forms *);
  610. if (forms)
  611. array_state = TRUE;
  612. else
  613. return_value = CURL_FORMADD_NULL;
  614. }
  615. break;
  616. /*
  617. * Set the Name property.
  618. */
  619. case CURLFORM_PTRNAME:
  620. current_form->flags |= HTTPPOST_PTRNAME; /* fall through */
  621. case CURLFORM_COPYNAME:
  622. if (current_form->name)
  623. return_value = CURL_FORMADD_OPTION_TWICE;
  624. else {
  625. char *name = array_state?
  626. array_value:va_arg(params, char *);
  627. if (name)
  628. current_form->name = name; /* store for the moment */
  629. else
  630. return_value = CURL_FORMADD_NULL;
  631. }
  632. break;
  633. case CURLFORM_NAMELENGTH:
  634. if (current_form->namelength)
  635. return_value = CURL_FORMADD_OPTION_TWICE;
  636. else
  637. current_form->namelength =
  638. array_state?(long)array_value:va_arg(params, long);
  639. break;
  640. /*
  641. * Set the contents property.
  642. */
  643. case CURLFORM_PTRCONTENTS:
  644. current_form->flags |= HTTPPOST_PTRCONTENTS; /* fall through */
  645. case CURLFORM_COPYCONTENTS:
  646. if (current_form->value)
  647. return_value = CURL_FORMADD_OPTION_TWICE;
  648. else {
  649. char *value =
  650. array_state?array_value:va_arg(params, char *);
  651. if (value)
  652. current_form->value = value; /* store for the moment */
  653. else
  654. return_value = CURL_FORMADD_NULL;
  655. }
  656. break;
  657. case CURLFORM_CONTENTSLENGTH:
  658. if (current_form->contentslength)
  659. return_value = CURL_FORMADD_OPTION_TWICE;
  660. else
  661. current_form->contentslength =
  662. array_state?(long)array_value:va_arg(params, long);
  663. break;
  664. /* Get contents from a given file name */
  665. case CURLFORM_FILECONTENT:
  666. if (current_form->flags != 0)
  667. return_value = CURL_FORMADD_OPTION_TWICE;
  668. else {
  669. char *filename = array_state?
  670. array_value:va_arg(params, char *);
  671. if (filename) {
  672. current_form->value = strdup(filename);
  673. current_form->flags |= HTTPPOST_READFILE;
  674. }
  675. else
  676. return_value = CURL_FORMADD_NULL;
  677. }
  678. break;
  679. /* We upload a file */
  680. case CURLFORM_FILE:
  681. {
  682. char *filename = array_state?array_value:
  683. va_arg(params, char *);
  684. if (current_form->value) {
  685. if (current_form->flags & HTTPPOST_FILENAME) {
  686. if (filename) {
  687. if (!(current_form = AddFormInfo(strdup(filename),
  688. NULL, current_form)))
  689. return_value = CURL_FORMADD_MEMORY;
  690. }
  691. else
  692. return_value = CURL_FORMADD_NULL;
  693. }
  694. else
  695. return_value = CURL_FORMADD_OPTION_TWICE;
  696. }
  697. else {
  698. if (filename)
  699. current_form->value = strdup(filename);
  700. else
  701. return_value = CURL_FORMADD_NULL;
  702. current_form->flags |= HTTPPOST_FILENAME;
  703. }
  704. break;
  705. }
  706. /* CMC: Added support for buffer uploads */
  707. case CURLFORM_BUFFER:
  708. {
  709. char *filename = array_state?array_value:
  710. va_arg(params, char *);
  711. if (current_form->value) {
  712. if (current_form->flags & HTTPPOST_BUFFER) {
  713. if (filename) {
  714. if (!(current_form = AddFormInfo(strdup(filename),
  715. NULL, current_form)))
  716. return_value = CURL_FORMADD_MEMORY;
  717. }
  718. else
  719. return_value = CURL_FORMADD_NULL;
  720. }
  721. else
  722. return_value = CURL_FORMADD_OPTION_TWICE;
  723. }
  724. else {
  725. if (filename)
  726. current_form->value = strdup(filename);
  727. else
  728. return_value = CURL_FORMADD_NULL;
  729. current_form->flags |= HTTPPOST_BUFFER;
  730. }
  731. break;
  732. }
  733. /* CMC: Added support for buffer uploads */
  734. case CURLFORM_BUFFERPTR:
  735. current_form->flags |= HTTPPOST_PTRBUFFER;
  736. if (current_form->buffer)
  737. return_value = CURL_FORMADD_OPTION_TWICE;
  738. else {
  739. char *buffer =
  740. array_state?array_value:va_arg(params, char *);
  741. if (buffer)
  742. current_form->buffer = buffer; /* store for the moment */
  743. else
  744. return_value = CURL_FORMADD_NULL;
  745. }
  746. break;
  747. /* CMC: Added support for buffer uploads */
  748. case CURLFORM_BUFFERLENGTH:
  749. if (current_form->bufferlength)
  750. return_value = CURL_FORMADD_OPTION_TWICE;
  751. else
  752. current_form->bufferlength =
  753. array_state?(long)array_value:va_arg(params, long);
  754. break;
  755. case CURLFORM_CONTENTTYPE:
  756. {
  757. char *contenttype =
  758. array_state?array_value:va_arg(params, char *);
  759. if (current_form->contenttype) {
  760. if (current_form->flags & HTTPPOST_FILENAME) {
  761. if (contenttype) {
  762. if (!(current_form = AddFormInfo(NULL,
  763. strdup(contenttype),
  764. current_form)))
  765. return_value = CURL_FORMADD_MEMORY;
  766. }
  767. else
  768. return_value = CURL_FORMADD_NULL;
  769. }
  770. else
  771. return_value = CURL_FORMADD_OPTION_TWICE;
  772. }
  773. else {
  774. if (contenttype)
  775. current_form->contenttype = strdup(contenttype);
  776. else
  777. return_value = CURL_FORMADD_NULL;
  778. }
  779. break;
  780. }
  781. case CURLFORM_CONTENTHEADER:
  782. {
  783. /* this "cast increases required alignment of target type" but
  784. we consider it OK anyway */
  785. struct curl_slist* list = array_state?
  786. (struct curl_slist*)array_value:
  787. va_arg(params, struct curl_slist*);
  788. if( current_form->contentheader )
  789. return_value = CURL_FORMADD_OPTION_TWICE;
  790. else
  791. current_form->contentheader = list;
  792. break;
  793. }
  794. case CURLFORM_FILENAME:
  795. {
  796. char *filename = array_state?array_value:
  797. va_arg(params, char *);
  798. if( current_form->showfilename )
  799. return_value = CURL_FORMADD_OPTION_TWICE;
  800. else
  801. current_form->showfilename = strdup(filename);
  802. break;
  803. }
  804. default:
  805. return_value = CURL_FORMADD_UNKNOWN_OPTION;
  806. }
  807. }
  808. if(CURL_FORMADD_OK == return_value) {
  809. /* go through the list, check for copleteness and if everything is
  810. * alright add the HttpPost item otherwise set return_value accordingly */
  811. post = NULL;
  812. for(form = first_form;
  813. form != NULL;
  814. form = form->more) {
  815. if ( ((!form->name || !form->value) && !post) ||
  816. ( (form->contentslength) &&
  817. (form->flags & HTTPPOST_FILENAME) ) ||
  818. ( (form->flags & HTTPPOST_FILENAME) &&
  819. (form->flags & HTTPPOST_PTRCONTENTS) ) ||
  820. /* CMC: Added support for buffer uploads */
  821. ( (!form->buffer) &&
  822. (form->flags & HTTPPOST_BUFFER) &&
  823. (form->flags & HTTPPOST_PTRBUFFER) ) ||
  824. ( (form->flags & HTTPPOST_READFILE) &&
  825. (form->flags & HTTPPOST_PTRCONTENTS) )
  826. ) {
  827. return_value = CURL_FORMADD_INCOMPLETE;
  828. break;
  829. }
  830. else {
  831. if ( ((form->flags & HTTPPOST_FILENAME) ||
  832. (form->flags & HTTPPOST_BUFFER)) &&
  833. !form->contenttype ) {
  834. /* our contenttype is missing */
  835. form->contenttype
  836. = strdup(ContentTypeForFilename(form->value, prevtype));
  837. }
  838. if ( !(form->flags & HTTPPOST_PTRNAME) &&
  839. (form == first_form) ) {
  840. /* copy name (without strdup; possibly contains null characters) */
  841. if (AllocAndCopy(&form->name, form->namelength)) {
  842. return_value = CURL_FORMADD_MEMORY;
  843. break;
  844. }
  845. }
  846. if ( !(form->flags & HTTPPOST_FILENAME) &&
  847. !(form->flags & HTTPPOST_READFILE) &&
  848. !(form->flags & HTTPPOST_PTRCONTENTS) &&
  849. /* CMC: Added support for buffer uploads */
  850. !(form->flags & HTTPPOST_PTRBUFFER) ) {
  851. /* copy value (without strdup; possibly contains null characters) */
  852. if (AllocAndCopy(&form->value, form->contentslength)) {
  853. return_value = CURL_FORMADD_MEMORY;
  854. break;
  855. }
  856. }
  857. post = AddHttpPost(form->name, form->namelength,
  858. form->value, form->contentslength,
  859. /* CMC: Added support for buffer uploads */
  860. form->buffer, form->bufferlength,
  861. form->contenttype, form->flags,
  862. form->contentheader, form->showfilename,
  863. post, httppost,
  864. last_post);
  865. if(!post)
  866. return_value = CURL_FORMADD_MEMORY;
  867. if (form->contenttype)
  868. prevtype = form->contenttype;
  869. }
  870. }
  871. }
  872. /* always delete the allocated memory before returning */
  873. form = first_form;
  874. while (form != NULL) {
  875. FormInfo *delete_form;
  876. delete_form = form;
  877. form = form->more;
  878. free (delete_form);
  879. }
  880. return return_value;
  881. }
  882. CURLFORMcode curl_formadd(struct curl_httppost **httppost,
  883. struct curl_httppost **last_post,
  884. ...)
  885. {
  886. va_list arg;
  887. CURLFORMcode result;
  888. va_start(arg, last_post);
  889. result = FormAdd(httppost, last_post, arg);
  890. va_end(arg);
  891. return result;
  892. }
  893. static int AddFormData(struct FormData **formp,
  894. const void *line,
  895. long length)
  896. {
  897. struct FormData *newform = (struct FormData *)
  898. malloc(sizeof(struct FormData));
  899. newform->next = NULL;
  900. /* we make it easier for plain strings: */
  901. if(!length)
  902. length = strlen((char *)line);
  903. newform->line = (char *)malloc(length+1);
  904. memcpy(newform->line, line, length);
  905. newform->length = length;
  906. newform->line[length]=0; /* zero terminate for easier debugging */
  907. if(*formp) {
  908. (*formp)->next = newform;
  909. *formp = newform;
  910. }
  911. else
  912. *formp = newform;
  913. return length;
  914. }
  915. static int AddFormDataf(struct FormData **formp,
  916. const char *fmt, ...)
  917. {
  918. char s[4096];
  919. va_list ap;
  920. va_start(ap, fmt);
  921. vsprintf(s, fmt, ap);
  922. va_end(ap);
  923. return AddFormData(formp, s, 0);
  924. }
  925. char *Curl_FormBoundary(void)
  926. {
  927. char *retstring;
  928. static int randomizer=0; /* this is just so that two boundaries within
  929. the same form won't be identical */
  930. int i;
  931. static char table62[]=
  932. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  933. retstring = (char *)malloc(BOUNDARY_LENGTH);
  934. if(!retstring)
  935. return NULL; /* failed */
  936. srand(time(NULL)+randomizer++); /* seed */
  937. strcpy(retstring, "curl"); /* bonus commercials 8*) */
  938. for(i=4; i<(BOUNDARY_LENGTH-1); i++) {
  939. retstring[i] = table62[rand()%62];
  940. }
  941. retstring[BOUNDARY_LENGTH-1]=0; /* zero terminate */
  942. return retstring;
  943. }
  944. /* Used from http.c, this cleans a built FormData linked list */
  945. void Curl_formclean(struct FormData *form)
  946. {
  947. struct FormData *next;
  948. do {
  949. next=form->next; /* the following form line */
  950. free(form->line); /* free the line */
  951. free(form); /* free the struct */
  952. } while((form=next)); /* continue */
  953. }
  954. /* external function to free up a whole form post chain */
  955. void curl_formfree(struct curl_httppost *form)
  956. {
  957. struct curl_httppost *next;
  958. if(!form)
  959. /* no form to free, just get out of this */
  960. return;
  961. do {
  962. next=form->next; /* the following form line */
  963. /* recurse to sub-contents */
  964. if(form->more)
  965. curl_formfree(form->more);
  966. if( !(form->flags & HTTPPOST_PTRNAME) && form->name)
  967. free(form->name); /* free the name */
  968. if( !(form->flags & HTTPPOST_PTRCONTENTS) && form->contents)
  969. free(form->contents); /* free the contents */
  970. if(form->contenttype)
  971. free(form->contenttype); /* free the content type */
  972. if(form->showfilename)
  973. free(form->showfilename); /* free the faked file name */
  974. free(form); /* free the struct */
  975. } while((form=next)); /* continue */
  976. }
  977. CURLcode Curl_getFormData(struct FormData **finalform,
  978. struct curl_httppost *post,
  979. int *sizep)
  980. {
  981. struct FormData *form = NULL;
  982. struct FormData *firstform;
  983. struct curl_httppost *file;
  984. CURLcode result = CURLE_OK;
  985. int size =0;
  986. char *boundary;
  987. char *fileboundary=NULL;
  988. struct curl_slist* curList;
  989. *finalform=NULL; /* default form is empty */
  990. if(!post)
  991. return result; /* no input => no output! */
  992. boundary = Curl_FormBoundary();
  993. /* Make the first line of the output */
  994. AddFormDataf(&form,
  995. "Content-Type: multipart/form-data;"
  996. " boundary=%s\r\n",
  997. boundary);
  998. /* we DO NOT count that line since that'll be part of the header! */
  999. firstform = form;
  1000. do {
  1001. if(size)
  1002. size += AddFormDataf(&form, "\r\n");
  1003. /* boundary */
  1004. size += AddFormDataf(&form, "--%s\r\n", boundary);
  1005. size += AddFormData(&form,
  1006. "Content-Disposition: form-data; name=\"", 0);
  1007. size += AddFormData(&form, post->name, post->namelength);
  1008. size += AddFormData(&form, "\"", 0);
  1009. if(post->more) {
  1010. /* If used, this is a link to more file names, we must then do
  1011. the magic to include several files with the same field name */
  1012. fileboundary = Curl_FormBoundary();
  1013. size += AddFormDataf(&form,
  1014. "\r\nContent-Type: multipart/mixed,"
  1015. " boundary=%s\r\n",
  1016. fileboundary);
  1017. }
  1018. file = post;
  1019. do {
  1020. /* If 'showfilename' is set, that is a faked name passed on to us
  1021. to use to in the formpost. If that is not set, the actually used
  1022. local file name should be added. */
  1023. if(post->more) {
  1024. /* if multiple-file */
  1025. size += AddFormDataf(&form,
  1026. "\r\n--%s\r\nContent-Disposition: "
  1027. "attachment; filename=\"%s\"",
  1028. fileboundary,
  1029. (file->showfilename?file->showfilename:
  1030. file->contents));
  1031. }
  1032. else if((post->flags & HTTPPOST_FILENAME) ||
  1033. /* CMC: Added support for buffer uploads */
  1034. (post->flags & HTTPPOST_BUFFER)) {
  1035. size += AddFormDataf(&form,
  1036. "; filename=\"%s\"",
  1037. (post->showfilename?post->showfilename:
  1038. post->contents));
  1039. }
  1040. if(file->contenttype) {
  1041. /* we have a specified type */
  1042. size += AddFormDataf(&form,
  1043. "\r\nContent-Type: %s",
  1044. file->contenttype);
  1045. }
  1046. curList = file->contentheader;
  1047. while( curList ) {
  1048. /* Process the additional headers specified for this form */
  1049. size += AddFormDataf( &form, "\r\n%s", curList->data );
  1050. curList = curList->next;
  1051. }
  1052. #if 0
  1053. /* The header Content-Transfer-Encoding: seems to confuse some receivers
  1054. * (like the built-in PHP engine). While I can't see any reason why it
  1055. * should, I can just as well skip this to the benefit of the users who
  1056. * are using such confused receivers.
  1057. */
  1058. if(file->contenttype &&
  1059. !checkprefix("text/", file->contenttype)) {
  1060. /* this is not a text content, mention our binary encoding */
  1061. size += AddFormData(&form, "\r\nContent-Transfer-Encoding: binary", 0);
  1062. }
  1063. #endif
  1064. size += AddFormData(&form, "\r\n\r\n", 0);
  1065. if((post->flags & HTTPPOST_FILENAME) ||
  1066. (post->flags & HTTPPOST_READFILE)) {
  1067. /* we should include the contents from the specified file */
  1068. FILE *fileread;
  1069. char buffer[1024];
  1070. int nread;
  1071. fileread = strequal("-", file->contents)?stdin:
  1072. /* binary read for win32 crap */
  1073. /*VMS??*/ fopen(file->contents, "rb"); /* ONLY ALLOWS FOR STREAM FILES ON VMS */
  1074. /*VMS?? Stream files are OK, as are FIXED & VAR files WITHOUT implied CC */
  1075. /*VMS?? For implied CC, every record needs to have a \n appended & 1 added to SIZE */
  1076. if(fileread) {
  1077. while((nread = fread(buffer, 1, 1024, fileread)))
  1078. size += AddFormData(&form, buffer, nread);
  1079. if(fileread != stdin)
  1080. fclose(fileread);
  1081. }
  1082. else {
  1083. #if 0
  1084. /* File wasn't found, add a nothing field! */
  1085. size += AddFormData(&form, "", 0);
  1086. #endif
  1087. Curl_formclean(firstform);
  1088. free(boundary);
  1089. *finalform = NULL;
  1090. return CURLE_READ_ERROR;
  1091. }
  1092. /* CMC: Added support for buffer uploads */
  1093. } else if (post->flags & HTTPPOST_BUFFER) {
  1094. /* include contents of buffer */
  1095. size += AddFormData(&form, post->buffer, post->bufferlength);
  1096. }
  1097. else {
  1098. /* include the contents we got */
  1099. size += AddFormData(&form, post->contents, post->contentslength);
  1100. }
  1101. } while((file = file->more)); /* for each specified file for this field */
  1102. if(post->more) {
  1103. /* this was a multiple-file inclusion, make a termination file
  1104. boundary: */
  1105. size += AddFormDataf(&form,
  1106. "\r\n--%s--",
  1107. fileboundary);
  1108. free(fileboundary);
  1109. }
  1110. } while((post=post->next)); /* for each field */
  1111. /* end-boundary for everything */
  1112. size += AddFormDataf(&form,
  1113. "\r\n--%s--\r\n",
  1114. boundary);
  1115. *sizep = size;
  1116. free(boundary);
  1117. *finalform=firstform;
  1118. return result;
  1119. }
  1120. int Curl_FormInit(struct Form *form, struct FormData *formdata )
  1121. {
  1122. if(!formdata)
  1123. return 1; /* error */
  1124. form->data = formdata;
  1125. form->sent = 0;
  1126. return 0;
  1127. }
  1128. /* fread() emulation */
  1129. int Curl_FormReader(char *buffer,
  1130. size_t size,
  1131. size_t nitems,
  1132. FILE *mydata)
  1133. {
  1134. struct Form *form;
  1135. int wantedsize;
  1136. int gotsize = 0;
  1137. form=(struct Form *)mydata;
  1138. wantedsize = size * nitems;
  1139. if(!form->data)
  1140. return 0; /* nothing, error, empty */
  1141. do {
  1142. if( (form->data->length - form->sent ) > wantedsize - gotsize) {
  1143. memcpy(buffer + gotsize , form->data->line + form->sent,
  1144. wantedsize - gotsize);
  1145. form->sent += wantedsize-gotsize;
  1146. return wantedsize;
  1147. }
  1148. memcpy(buffer+gotsize,
  1149. form->data->line + form->sent,
  1150. (form->data->length - form->sent) );
  1151. gotsize += form->data->length - form->sent;
  1152. form->sent = 0;
  1153. form->data = form->data->next; /* advance */
  1154. } while(form->data);
  1155. /* If we got an empty line and we have more data, we proceed to the next
  1156. line immediately to avoid returning zero before we've reached the end.
  1157. This is the bug reported November 22 1999 on curl 6.3. (Daniel) */
  1158. return gotsize;
  1159. }
  1160. /* possible (old) fread() emulation that copies at most one line */
  1161. int Curl_FormReadOneLine(char *buffer,
  1162. size_t size,
  1163. size_t nitems,
  1164. FILE *mydata)
  1165. {
  1166. struct Form *form;
  1167. int wantedsize;
  1168. int gotsize;
  1169. form=(struct Form *)mydata;
  1170. wantedsize = size * nitems;
  1171. if(!form->data)
  1172. return -1; /* nothing, error, empty */
  1173. do {
  1174. if( (form->data->length - form->sent ) > wantedsize ) {
  1175. memcpy(buffer, form->data->line + form->sent, wantedsize);
  1176. form->sent += wantedsize;
  1177. return wantedsize;
  1178. }
  1179. memcpy(buffer,
  1180. form->data->line + form->sent,
  1181. gotsize = (form->data->length - form->sent) );
  1182. form->sent = 0;
  1183. form->data = form->data->next; /* advance */
  1184. } while(!gotsize && form->data);
  1185. /* If we got an empty line and we have more data, we proceed to the next
  1186. line immediately to avoid returning zero before we've reached the end.
  1187. This is the bug reported November 22 1999 on curl 6.3. (Daniel) */
  1188. return gotsize;
  1189. }
  1190. #ifdef _FORM_DEBUG
  1191. int FormAddTest(const char * errormsg,
  1192. struct curl_httppost **httppost,
  1193. struct curl_httppost **last_post,
  1194. ...)
  1195. {
  1196. int result;
  1197. va_list arg;
  1198. va_start(arg, last_post);
  1199. if ((result = FormAdd(httppost, last_post, arg)))
  1200. fprintf (stderr, "ERROR doing FormAdd ret: %d action: %s\n", result,
  1201. errormsg);
  1202. va_end(arg);
  1203. return result;
  1204. }
  1205. int main()
  1206. {
  1207. char name1[] = "simple_COPYCONTENTS";
  1208. char name2[] = "COPYCONTENTS_+_CONTENTTYPE";
  1209. char name3[] = "PTRNAME_+_NAMELENGTH_+_COPYNAME_+_CONTENTSLENGTH";
  1210. char name4[] = "simple_PTRCONTENTS";
  1211. char name5[] = "PTRCONTENTS_+_CONTENTSLENGTH";
  1212. char name6[] = "PTRCONTENTS_+_CONTENTSLENGTH_+_CONTENTTYPE";
  1213. char name7[] = "FILE1_+_CONTENTTYPE";
  1214. char name8[] = "FILE1_+_FILE2";
  1215. char name9[] = "FILE1_+_FILE2_+_FILE3";
  1216. char name10[] = "ARRAY: FILE1_+_FILE2_+_FILE3";
  1217. char name11[] = "FILECONTENT";
  1218. char value1[] = "value for simple COPYCONTENTS";
  1219. char value2[] = "value for COPYCONTENTS + CONTENTTYPE";
  1220. char value3[] = "value for PTRNAME + NAMELENGTH + COPYNAME + CONTENTSLENGTH";
  1221. char value4[] = "value for simple PTRCONTENTS";
  1222. char value5[] = "value for PTRCONTENTS + CONTENTSLENGTH";
  1223. char value6[] = "value for PTRCOTNENTS + CONTENTSLENGTH + CONTENTTYPE";
  1224. char value7[] = "inet_ntoa_r.h";
  1225. char value8[] = "Makefile.b32.resp";
  1226. char type2[] = "image/gif";
  1227. char type6[] = "text/plain";
  1228. char type7[] = "text/html";
  1229. int name3length = strlen(name3);
  1230. int value3length = strlen(value3);
  1231. int value5length = strlen(value4);
  1232. int value6length = strlen(value5);
  1233. int errors = 0;
  1234. int size;
  1235. int nread;
  1236. char buffer[4096];
  1237. struct curl_httppost *httppost=NULL;
  1238. struct curl_httppost *last_post=NULL;
  1239. struct curl_forms forms[4];
  1240. struct FormData *form;
  1241. struct Form formread;
  1242. if (FormAddTest("simple COPYCONTENTS test", &httppost, &last_post,
  1243. CURLFORM_COPYNAME, name1, CURLFORM_COPYCONTENTS, value1,
  1244. CURLFORM_END))
  1245. ++errors;
  1246. if (FormAddTest("COPYCONTENTS + CONTENTTYPE test", &httppost, &last_post,
  1247. CURLFORM_COPYNAME, name2, CURLFORM_COPYCONTENTS, value2,
  1248. CURLFORM_CONTENTTYPE, type2, CURLFORM_END))
  1249. ++errors;
  1250. /* make null character at start to check that contentslength works
  1251. correctly */
  1252. name3[1] = '\0';
  1253. value3[1] = '\0';
  1254. if (FormAddTest("PTRNAME + NAMELENGTH + COPYNAME + CONTENTSLENGTH test",
  1255. &httppost, &last_post,
  1256. CURLFORM_PTRNAME, name3, CURLFORM_COPYCONTENTS, value3,
  1257. CURLFORM_CONTENTSLENGTH, value3length,
  1258. CURLFORM_NAMELENGTH, name3length, CURLFORM_END))
  1259. ++errors;
  1260. if (FormAddTest("simple PTRCONTENTS test", &httppost, &last_post,
  1261. CURLFORM_COPYNAME, name4, CURLFORM_PTRCONTENTS, value4,
  1262. CURLFORM_END))
  1263. ++errors;
  1264. /* make null character at start to check that contentslength works
  1265. correctly */
  1266. value5[1] = '\0';
  1267. if (FormAddTest("PTRCONTENTS + CONTENTSLENGTH test", &httppost, &last_post,
  1268. CURLFORM_COPYNAME, name5, CURLFORM_PTRCONTENTS, value5,
  1269. CURLFORM_CONTENTSLENGTH, value5length, CURLFORM_END))
  1270. ++errors;
  1271. /* make null character at start to check that contentslength works
  1272. correctly */
  1273. value6[1] = '\0';
  1274. if (FormAddTest("PTRCONTENTS + CONTENTSLENGTH + CONTENTTYPE test",
  1275. &httppost, &last_post,
  1276. CURLFORM_COPYNAME, name6, CURLFORM_PTRCONTENTS, value6,
  1277. CURLFORM_CONTENTSLENGTH, value6length,
  1278. CURLFORM_CONTENTTYPE, type6, CURLFORM_END))
  1279. ++errors;
  1280. if (FormAddTest("FILE + CONTENTTYPE test", &httppost, &last_post,
  1281. CURLFORM_COPYNAME, name7, CURLFORM_FILE, value7,
  1282. CURLFORM_CONTENTTYPE, type7, CURLFORM_END))
  1283. ++errors;
  1284. if (FormAddTest("FILE1 + FILE2 test", &httppost, &last_post,
  1285. CURLFORM_COPYNAME, name8, CURLFORM_FILE, value7,
  1286. CURLFORM_FILE, value8, CURLFORM_END))
  1287. ++errors;
  1288. if (FormAddTest("FILE1 + FILE2 + FILE3 test", &httppost, &last_post,
  1289. CURLFORM_COPYNAME, name9, CURLFORM_FILE, value7,
  1290. CURLFORM_FILE, value8, CURLFORM_FILE, value7, CURLFORM_END))
  1291. ++errors;
  1292. forms[0].option = CURLFORM_FILE;
  1293. forms[0].value = value7;
  1294. forms[1].option = CURLFORM_FILE;
  1295. forms[1].value = value8;
  1296. forms[2].option = CURLFORM_FILE;
  1297. forms[2].value = value7;
  1298. forms[3].option = CURLFORM_END;
  1299. if (FormAddTest("FILE1 + FILE2 + FILE3 ARRAY test", &httppost, &last_post,
  1300. CURLFORM_COPYNAME, name10, CURLFORM_ARRAY, forms,
  1301. CURLFORM_END))
  1302. ++errors;
  1303. if (FormAddTest("FILECONTENT test", &httppost, &last_post,
  1304. CURLFORM_COPYNAME, name11, CURLFORM_FILECONTENT, value7,
  1305. CURLFORM_END))
  1306. ++errors;
  1307. form=Curl_getFormData(httppost, &size);
  1308. Curl_FormInit(&formread, form);
  1309. do {
  1310. nread = Curl_FormReader(buffer, 1, sizeof(buffer),
  1311. (FILE *)&formread);
  1312. if(-1 == nread)
  1313. break;
  1314. fwrite(buffer, nread, 1, stdout);
  1315. } while(1);
  1316. fprintf(stdout, "size: %d\n", size);
  1317. if (errors)
  1318. fprintf(stdout, "\n==> %d Test(s) failed!\n", errors);
  1319. else
  1320. fprintf(stdout, "\nAll Tests seem to have worked (please check output)\n");
  1321. return 0;
  1322. }
  1323. #endif
  1324. #ifdef _OLD_FORM_DEBUG
  1325. int main(int argc, char **argv)
  1326. {
  1327. #if 0
  1328. char *testargs[]={
  1329. "name1 = data in number one",
  1330. "name2 = number two data",
  1331. "test = @upload"
  1332. };
  1333. #endif
  1334. int i;
  1335. char *nextarg;
  1336. struct curl_httppost *httppost=NULL;
  1337. struct curl_httppost *last_post=NULL;
  1338. struct curl_httppost *post;
  1339. int size;
  1340. int nread;
  1341. char buffer[4096];
  1342. struct FormData *form;
  1343. struct Form formread;
  1344. for(i=1; i<argc; i++) {
  1345. if( FormParse( argv[i],
  1346. &httppost,
  1347. &last_post)) {
  1348. fprintf(stderr, "Illegally formatted input field: '%s'!\n",
  1349. argv[i]);
  1350. return 1;
  1351. }
  1352. }
  1353. form=Curl_getFormData(httppost, &size);
  1354. Curl_FormInit(&formread, form);
  1355. do {
  1356. nread = Curl_FormReader(buffer, 1, sizeof(buffer),
  1357. (FILE *)&formread);
  1358. if(-1 == nread)
  1359. break;
  1360. fwrite(buffer, nread, 1, stderr);
  1361. } while(1);
  1362. fprintf(stderr, "size: %d\n", size);
  1363. return 0;
  1364. }
  1365. #endif
  1366. #endif /* CURL_DISABLE_HTTP */
  1367. /*
  1368. * local variables:
  1369. * eval: (load-file "../curl-mode.el")
  1370. * end:
  1371. * vim600: fdm=marker
  1372. * vim: et sw=2 ts=2 sts=2 tw=78
  1373. */