formdata.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2015, 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 https://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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #ifndef CURL_DISABLE_HTTP
  25. #if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
  26. #include <libgen.h>
  27. #endif
  28. #include "urldata.h" /* for struct SessionHandle */
  29. #include "formdata.h"
  30. #include "vtls/vtls.h"
  31. #include "strequal.h"
  32. #include "sendf.h"
  33. #include "strdup.h"
  34. #include "curl_printf.h"
  35. /* The last #include files should be: */
  36. #include "curl_memory.h"
  37. #include "memdebug.h"
  38. #ifndef HAVE_BASENAME
  39. static char *Curl_basename(char *path);
  40. #define basename(x) Curl_basename((x))
  41. #endif
  42. static size_t readfromfile(struct Form *form, char *buffer, size_t size);
  43. static char *formboundary(struct SessionHandle *data);
  44. /* What kind of Content-Type to use on un-specified files with unrecognized
  45. extensions. */
  46. #define HTTPPOST_CONTENTTYPE_DEFAULT "application/octet-stream"
  47. #define FORM_FILE_SEPARATOR ','
  48. #define FORM_TYPE_SEPARATOR ';'
  49. #define HTTPPOST_PTRNAME CURL_HTTPPOST_PTRNAME
  50. #define HTTPPOST_FILENAME CURL_HTTPPOST_FILENAME
  51. #define HTTPPOST_PTRCONTENTS CURL_HTTPPOST_PTRCONTENTS
  52. #define HTTPPOST_READFILE CURL_HTTPPOST_READFILE
  53. #define HTTPPOST_PTRBUFFER CURL_HTTPPOST_PTRBUFFER
  54. #define HTTPPOST_CALLBACK CURL_HTTPPOST_CALLBACK
  55. #define HTTPPOST_BUFFER CURL_HTTPPOST_BUFFER
  56. /***************************************************************************
  57. *
  58. * AddHttpPost()
  59. *
  60. * Adds a HttpPost structure to the list, if parent_post is given becomes
  61. * a subpost of parent_post instead of a direct list element.
  62. *
  63. * Returns newly allocated HttpPost on success and NULL if malloc failed.
  64. *
  65. ***************************************************************************/
  66. static struct curl_httppost *
  67. AddHttpPost(char *name, size_t namelength,
  68. char *value, curl_off_t contentslength,
  69. char *buffer, size_t bufferlength,
  70. char *contenttype,
  71. long flags,
  72. struct curl_slist* contentHeader,
  73. char *showfilename, char *userp,
  74. struct curl_httppost *parent_post,
  75. struct curl_httppost **httppost,
  76. struct curl_httppost **last_post)
  77. {
  78. struct curl_httppost *post;
  79. post = calloc(1, sizeof(struct curl_httppost));
  80. if(post) {
  81. post->name = name;
  82. post->namelength = (long)(name?(namelength?namelength:strlen(name)):0);
  83. post->contents = value;
  84. post->contentlen = contentslength;
  85. post->buffer = buffer;
  86. post->bufferlength = (long)bufferlength;
  87. post->contenttype = contenttype;
  88. post->contentheader = contentHeader;
  89. post->showfilename = showfilename;
  90. post->userp = userp,
  91. post->flags = flags | CURL_HTTPPOST_LARGE;
  92. }
  93. else
  94. return NULL;
  95. if(parent_post) {
  96. /* now, point our 'more' to the original 'more' */
  97. post->more = parent_post->more;
  98. /* then move the original 'more' to point to ourselves */
  99. parent_post->more = post;
  100. }
  101. else {
  102. /* make the previous point to this */
  103. if(*last_post)
  104. (*last_post)->next = post;
  105. else
  106. (*httppost) = post;
  107. (*last_post) = post;
  108. }
  109. return post;
  110. }
  111. /***************************************************************************
  112. *
  113. * AddFormInfo()
  114. *
  115. * Adds a FormInfo structure to the list presented by parent_form_info.
  116. *
  117. * Returns newly allocated FormInfo on success and NULL if malloc failed/
  118. * parent_form_info is NULL.
  119. *
  120. ***************************************************************************/
  121. static FormInfo * AddFormInfo(char *value,
  122. char *contenttype,
  123. FormInfo *parent_form_info)
  124. {
  125. FormInfo *form_info;
  126. form_info = calloc(1, sizeof(struct FormInfo));
  127. if(form_info) {
  128. if(value)
  129. form_info->value = value;
  130. if(contenttype)
  131. form_info->contenttype = contenttype;
  132. form_info->flags = HTTPPOST_FILENAME;
  133. }
  134. else
  135. return NULL;
  136. if(parent_form_info) {
  137. /* now, point our 'more' to the original 'more' */
  138. form_info->more = parent_form_info->more;
  139. /* then move the original 'more' to point to ourselves */
  140. parent_form_info->more = form_info;
  141. }
  142. return form_info;
  143. }
  144. /***************************************************************************
  145. *
  146. * ContentTypeForFilename()
  147. *
  148. * Provides content type for filename if one of the known types (else
  149. * (either the prevtype or the default is returned).
  150. *
  151. * Returns some valid contenttype for filename.
  152. *
  153. ***************************************************************************/
  154. static const char *ContentTypeForFilename(const char *filename,
  155. const char *prevtype)
  156. {
  157. const char *contenttype = NULL;
  158. unsigned int i;
  159. /*
  160. * No type was specified, we scan through a few well-known
  161. * extensions and pick the first we match!
  162. */
  163. struct ContentType {
  164. const char *extension;
  165. const char *type;
  166. };
  167. static const struct ContentType ctts[]={
  168. {".gif", "image/gif"},
  169. {".jpg", "image/jpeg"},
  170. {".jpeg", "image/jpeg"},
  171. {".txt", "text/plain"},
  172. {".html", "text/html"},
  173. {".xml", "application/xml"}
  174. };
  175. if(prevtype)
  176. /* default to the previously set/used! */
  177. contenttype = prevtype;
  178. else
  179. contenttype = HTTPPOST_CONTENTTYPE_DEFAULT;
  180. if(filename) { /* in case a NULL was passed in */
  181. for(i=0; i<sizeof(ctts)/sizeof(ctts[0]); i++) {
  182. if(strlen(filename) >= strlen(ctts[i].extension)) {
  183. if(strequal(filename +
  184. strlen(filename) - strlen(ctts[i].extension),
  185. ctts[i].extension)) {
  186. contenttype = ctts[i].type;
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. /* we have a contenttype by now */
  193. return contenttype;
  194. }
  195. /***************************************************************************
  196. *
  197. * FormAdd()
  198. *
  199. * Stores a formpost parameter and builds the appropriate linked list.
  200. *
  201. * Has two principal functionalities: using files and byte arrays as
  202. * post parts. Byte arrays are either copied or just the pointer is stored
  203. * (as the user requests) while for files only the filename and not the
  204. * content is stored.
  205. *
  206. * While you may have only one byte array for each name, multiple filenames
  207. * are allowed (and because of this feature CURLFORM_END is needed after
  208. * using CURLFORM_FILE).
  209. *
  210. * Examples:
  211. *
  212. * Simple name/value pair with copied contents:
  213. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  214. * CURLFORM_COPYCONTENTS, "value", CURLFORM_END);
  215. *
  216. * name/value pair where only the content pointer is remembered:
  217. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  218. * CURLFORM_PTRCONTENTS, ptr, CURLFORM_CONTENTSLENGTH, 10, CURLFORM_END);
  219. * (if CURLFORM_CONTENTSLENGTH is missing strlen () is used)
  220. *
  221. * storing a filename (CONTENTTYPE is optional!):
  222. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  223. * CURLFORM_FILE, "filename1", CURLFORM_CONTENTTYPE, "plain/text",
  224. * CURLFORM_END);
  225. *
  226. * storing multiple filenames:
  227. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  228. * CURLFORM_FILE, "filename1", CURLFORM_FILE, "filename2", CURLFORM_END);
  229. *
  230. * Returns:
  231. * CURL_FORMADD_OK on success
  232. * CURL_FORMADD_MEMORY if the FormInfo allocation fails
  233. * CURL_FORMADD_OPTION_TWICE if one option is given twice for one Form
  234. * CURL_FORMADD_NULL if a null pointer was given for a char
  235. * CURL_FORMADD_MEMORY if the allocation of a FormInfo struct failed
  236. * CURL_FORMADD_UNKNOWN_OPTION if an unknown option was used
  237. * CURL_FORMADD_INCOMPLETE if the some FormInfo is not complete (or error)
  238. * CURL_FORMADD_MEMORY if a HttpPost struct cannot be allocated
  239. * CURL_FORMADD_MEMORY if some allocation for string copying failed.
  240. * CURL_FORMADD_ILLEGAL_ARRAY if an illegal option is used in an array
  241. *
  242. ***************************************************************************/
  243. static
  244. CURLFORMcode FormAdd(struct curl_httppost **httppost,
  245. struct curl_httppost **last_post,
  246. va_list params)
  247. {
  248. FormInfo *first_form, *current_form, *form = NULL;
  249. CURLFORMcode return_value = CURL_FORMADD_OK;
  250. const char *prevtype = NULL;
  251. struct curl_httppost *post = NULL;
  252. CURLformoption option;
  253. struct curl_forms *forms = NULL;
  254. char *array_value=NULL; /* value read from an array */
  255. /* This is a state variable, that if TRUE means that we're parsing an
  256. array that we got passed to us. If FALSE we're parsing the input
  257. va_list arguments. */
  258. bool array_state = FALSE;
  259. /*
  260. * We need to allocate the first struct to fill in.
  261. */
  262. first_form = calloc(1, sizeof(struct FormInfo));
  263. if(!first_form)
  264. return CURL_FORMADD_MEMORY;
  265. current_form = first_form;
  266. /*
  267. * Loop through all the options set. Break if we have an error to report.
  268. */
  269. while(return_value == CURL_FORMADD_OK) {
  270. /* first see if we have more parts of the array param */
  271. if(array_state && forms) {
  272. /* get the upcoming option from the given array */
  273. option = forms->option;
  274. array_value = (char *)forms->value;
  275. forms++; /* advance this to next entry */
  276. if(CURLFORM_END == option) {
  277. /* end of array state */
  278. array_state = FALSE;
  279. continue;
  280. }
  281. }
  282. else {
  283. /* This is not array-state, get next option */
  284. option = va_arg(params, CURLformoption);
  285. if(CURLFORM_END == option)
  286. break;
  287. }
  288. switch (option) {
  289. case CURLFORM_ARRAY:
  290. if(array_state)
  291. /* we don't support an array from within an array */
  292. return_value = CURL_FORMADD_ILLEGAL_ARRAY;
  293. else {
  294. forms = va_arg(params, struct curl_forms *);
  295. if(forms)
  296. array_state = TRUE;
  297. else
  298. return_value = CURL_FORMADD_NULL;
  299. }
  300. break;
  301. /*
  302. * Set the Name property.
  303. */
  304. case CURLFORM_PTRNAME:
  305. #ifdef CURL_DOES_CONVERSIONS
  306. /* Treat CURLFORM_PTR like CURLFORM_COPYNAME so that libcurl will copy
  307. * the data in all cases so that we'll have safe memory for the eventual
  308. * conversion.
  309. */
  310. #else
  311. current_form->flags |= HTTPPOST_PTRNAME; /* fall through */
  312. #endif
  313. case CURLFORM_COPYNAME:
  314. if(current_form->name)
  315. return_value = CURL_FORMADD_OPTION_TWICE;
  316. else {
  317. char *name = array_state?
  318. array_value:va_arg(params, char *);
  319. if(name)
  320. current_form->name = name; /* store for the moment */
  321. else
  322. return_value = CURL_FORMADD_NULL;
  323. }
  324. break;
  325. case CURLFORM_NAMELENGTH:
  326. if(current_form->namelength)
  327. return_value = CURL_FORMADD_OPTION_TWICE;
  328. else
  329. current_form->namelength =
  330. array_state?(size_t)array_value:(size_t)va_arg(params, long);
  331. break;
  332. /*
  333. * Set the contents property.
  334. */
  335. case CURLFORM_PTRCONTENTS:
  336. current_form->flags |= HTTPPOST_PTRCONTENTS; /* fall through */
  337. case CURLFORM_COPYCONTENTS:
  338. if(current_form->value)
  339. return_value = CURL_FORMADD_OPTION_TWICE;
  340. else {
  341. char *value =
  342. array_state?array_value:va_arg(params, char *);
  343. if(value)
  344. current_form->value = value; /* store for the moment */
  345. else
  346. return_value = CURL_FORMADD_NULL;
  347. }
  348. break;
  349. case CURLFORM_CONTENTSLENGTH:
  350. current_form->contentslength =
  351. array_state?(size_t)array_value:(size_t)va_arg(params, long);
  352. break;
  353. case CURLFORM_CONTENTLEN:
  354. current_form->flags |= CURL_HTTPPOST_LARGE;
  355. current_form->contentslength =
  356. array_state?(curl_off_t)array_value:va_arg(params, curl_off_t);
  357. break;
  358. /* Get contents from a given file name */
  359. case CURLFORM_FILECONTENT:
  360. if(current_form->flags & (HTTPPOST_PTRCONTENTS|HTTPPOST_READFILE))
  361. return_value = CURL_FORMADD_OPTION_TWICE;
  362. else {
  363. const char *filename = array_state?
  364. array_value:va_arg(params, char *);
  365. if(filename) {
  366. current_form->value = strdup(filename);
  367. if(!current_form->value)
  368. return_value = CURL_FORMADD_MEMORY;
  369. else {
  370. current_form->flags |= HTTPPOST_READFILE;
  371. current_form->value_alloc = TRUE;
  372. }
  373. }
  374. else
  375. return_value = CURL_FORMADD_NULL;
  376. }
  377. break;
  378. /* We upload a file */
  379. case CURLFORM_FILE:
  380. {
  381. const char *filename = array_state?array_value:
  382. va_arg(params, char *);
  383. if(current_form->value) {
  384. if(current_form->flags & HTTPPOST_FILENAME) {
  385. if(filename) {
  386. char *fname = strdup(filename);
  387. if(!fname)
  388. return_value = CURL_FORMADD_MEMORY;
  389. else {
  390. form = AddFormInfo(fname, NULL, current_form);
  391. if(!form) {
  392. free(fname);
  393. return_value = CURL_FORMADD_MEMORY;
  394. }
  395. else {
  396. form->value_alloc = TRUE;
  397. current_form = form;
  398. form = NULL;
  399. }
  400. }
  401. }
  402. else
  403. return_value = CURL_FORMADD_NULL;
  404. }
  405. else
  406. return_value = CURL_FORMADD_OPTION_TWICE;
  407. }
  408. else {
  409. if(filename) {
  410. current_form->value = strdup(filename);
  411. if(!current_form->value)
  412. return_value = CURL_FORMADD_MEMORY;
  413. else {
  414. current_form->flags |= HTTPPOST_FILENAME;
  415. current_form->value_alloc = TRUE;
  416. }
  417. }
  418. else
  419. return_value = CURL_FORMADD_NULL;
  420. }
  421. break;
  422. }
  423. case CURLFORM_BUFFERPTR:
  424. current_form->flags |= HTTPPOST_PTRBUFFER|HTTPPOST_BUFFER;
  425. if(current_form->buffer)
  426. return_value = CURL_FORMADD_OPTION_TWICE;
  427. else {
  428. char *buffer =
  429. array_state?array_value:va_arg(params, char *);
  430. if(buffer) {
  431. current_form->buffer = buffer; /* store for the moment */
  432. current_form->value = buffer; /* make it non-NULL to be accepted
  433. as fine */
  434. }
  435. else
  436. return_value = CURL_FORMADD_NULL;
  437. }
  438. break;
  439. case CURLFORM_BUFFERLENGTH:
  440. if(current_form->bufferlength)
  441. return_value = CURL_FORMADD_OPTION_TWICE;
  442. else
  443. current_form->bufferlength =
  444. array_state?(size_t)array_value:(size_t)va_arg(params, long);
  445. break;
  446. case CURLFORM_STREAM:
  447. current_form->flags |= HTTPPOST_CALLBACK;
  448. if(current_form->userp)
  449. return_value = CURL_FORMADD_OPTION_TWICE;
  450. else {
  451. char *userp =
  452. array_state?array_value:va_arg(params, char *);
  453. if(userp) {
  454. current_form->userp = userp;
  455. current_form->value = userp; /* this isn't strictly true but we
  456. derive a value from this later on
  457. and we need this non-NULL to be
  458. accepted as a fine form part */
  459. }
  460. else
  461. return_value = CURL_FORMADD_NULL;
  462. }
  463. break;
  464. case CURLFORM_CONTENTTYPE:
  465. {
  466. const char *contenttype =
  467. array_state?array_value:va_arg(params, char *);
  468. if(current_form->contenttype) {
  469. if(current_form->flags & HTTPPOST_FILENAME) {
  470. if(contenttype) {
  471. char *type = strdup(contenttype);
  472. if(!type)
  473. return_value = CURL_FORMADD_MEMORY;
  474. else {
  475. form = AddFormInfo(NULL, type, current_form);
  476. if(!form) {
  477. free(type);
  478. return_value = CURL_FORMADD_MEMORY;
  479. }
  480. else {
  481. form->contenttype_alloc = TRUE;
  482. current_form = form;
  483. form = NULL;
  484. }
  485. }
  486. }
  487. else
  488. return_value = CURL_FORMADD_NULL;
  489. }
  490. else
  491. return_value = CURL_FORMADD_OPTION_TWICE;
  492. }
  493. else {
  494. if(contenttype) {
  495. current_form->contenttype = strdup(contenttype);
  496. if(!current_form->contenttype)
  497. return_value = CURL_FORMADD_MEMORY;
  498. else
  499. current_form->contenttype_alloc = TRUE;
  500. }
  501. else
  502. return_value = CURL_FORMADD_NULL;
  503. }
  504. break;
  505. }
  506. case CURLFORM_CONTENTHEADER:
  507. {
  508. /* this "cast increases required alignment of target type" but
  509. we consider it OK anyway */
  510. struct curl_slist* list = array_state?
  511. (struct curl_slist*)(void*)array_value:
  512. va_arg(params, struct curl_slist*);
  513. if(current_form->contentheader)
  514. return_value = CURL_FORMADD_OPTION_TWICE;
  515. else
  516. current_form->contentheader = list;
  517. break;
  518. }
  519. case CURLFORM_FILENAME:
  520. case CURLFORM_BUFFER:
  521. {
  522. const char *filename = array_state?array_value:
  523. va_arg(params, char *);
  524. if(current_form->showfilename)
  525. return_value = CURL_FORMADD_OPTION_TWICE;
  526. else {
  527. current_form->showfilename = strdup(filename);
  528. if(!current_form->showfilename)
  529. return_value = CURL_FORMADD_MEMORY;
  530. else
  531. current_form->showfilename_alloc = TRUE;
  532. }
  533. break;
  534. }
  535. default:
  536. return_value = CURL_FORMADD_UNKNOWN_OPTION;
  537. break;
  538. }
  539. }
  540. if(CURL_FORMADD_OK != return_value) {
  541. /* On error, free allocated fields for all nodes of the FormInfo linked
  542. list without deallocating nodes. List nodes are deallocated later on */
  543. FormInfo *ptr;
  544. for(ptr = first_form; ptr != NULL; ptr = ptr->more) {
  545. if(ptr->name_alloc) {
  546. Curl_safefree(ptr->name);
  547. ptr->name_alloc = FALSE;
  548. }
  549. if(ptr->value_alloc) {
  550. Curl_safefree(ptr->value);
  551. ptr->value_alloc = FALSE;
  552. }
  553. if(ptr->contenttype_alloc) {
  554. Curl_safefree(ptr->contenttype);
  555. ptr->contenttype_alloc = FALSE;
  556. }
  557. if(ptr->showfilename_alloc) {
  558. Curl_safefree(ptr->showfilename);
  559. ptr->showfilename_alloc = FALSE;
  560. }
  561. }
  562. }
  563. if(CURL_FORMADD_OK == return_value) {
  564. /* go through the list, check for completeness and if everything is
  565. * alright add the HttpPost item otherwise set return_value accordingly */
  566. post = NULL;
  567. for(form = first_form;
  568. form != NULL;
  569. form = form->more) {
  570. if(((!form->name || !form->value) && !post) ||
  571. ( (form->contentslength) &&
  572. (form->flags & HTTPPOST_FILENAME) ) ||
  573. ( (form->flags & HTTPPOST_FILENAME) &&
  574. (form->flags & HTTPPOST_PTRCONTENTS) ) ||
  575. ( (!form->buffer) &&
  576. (form->flags & HTTPPOST_BUFFER) &&
  577. (form->flags & HTTPPOST_PTRBUFFER) ) ||
  578. ( (form->flags & HTTPPOST_READFILE) &&
  579. (form->flags & HTTPPOST_PTRCONTENTS) )
  580. ) {
  581. return_value = CURL_FORMADD_INCOMPLETE;
  582. break;
  583. }
  584. else {
  585. if(((form->flags & HTTPPOST_FILENAME) ||
  586. (form->flags & HTTPPOST_BUFFER)) &&
  587. !form->contenttype ) {
  588. char *f = form->flags & HTTPPOST_BUFFER?
  589. form->showfilename : form->value;
  590. /* our contenttype is missing */
  591. form->contenttype = strdup(ContentTypeForFilename(f, prevtype));
  592. if(!form->contenttype) {
  593. return_value = CURL_FORMADD_MEMORY;
  594. break;
  595. }
  596. form->contenttype_alloc = TRUE;
  597. }
  598. if(!(form->flags & HTTPPOST_PTRNAME) &&
  599. (form == first_form) ) {
  600. /* Note that there's small risk that form->name is NULL here if the
  601. app passed in a bad combo, so we better check for that first. */
  602. if(form->name) {
  603. /* copy name (without strdup; possibly contains null characters) */
  604. form->name = Curl_memdup(form->name, form->namelength?
  605. form->namelength:
  606. strlen(form->name)+1);
  607. }
  608. if(!form->name) {
  609. return_value = CURL_FORMADD_MEMORY;
  610. break;
  611. }
  612. form->name_alloc = TRUE;
  613. }
  614. if(!(form->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE |
  615. HTTPPOST_PTRCONTENTS | HTTPPOST_PTRBUFFER |
  616. HTTPPOST_CALLBACK)) && form->value) {
  617. /* copy value (without strdup; possibly contains null characters) */
  618. size_t clen = (size_t) form->contentslength;
  619. if(!clen)
  620. clen = strlen(form->value)+1;
  621. form->value = Curl_memdup(form->value, clen);
  622. if(!form->value) {
  623. return_value = CURL_FORMADD_MEMORY;
  624. break;
  625. }
  626. form->value_alloc = TRUE;
  627. }
  628. post = AddHttpPost(form->name, form->namelength,
  629. form->value, form->contentslength,
  630. form->buffer, form->bufferlength,
  631. form->contenttype, form->flags,
  632. form->contentheader, form->showfilename,
  633. form->userp,
  634. post, httppost,
  635. last_post);
  636. if(!post) {
  637. return_value = CURL_FORMADD_MEMORY;
  638. break;
  639. }
  640. if(form->contenttype)
  641. prevtype = form->contenttype;
  642. }
  643. }
  644. if(CURL_FORMADD_OK != return_value) {
  645. /* On error, free allocated fields for nodes of the FormInfo linked
  646. list which are not already owned by the httppost linked list
  647. without deallocating nodes. List nodes are deallocated later on */
  648. FormInfo *ptr;
  649. for(ptr = form; ptr != NULL; ptr = ptr->more) {
  650. if(ptr->name_alloc) {
  651. Curl_safefree(ptr->name);
  652. ptr->name_alloc = FALSE;
  653. }
  654. if(ptr->value_alloc) {
  655. Curl_safefree(ptr->value);
  656. ptr->value_alloc = FALSE;
  657. }
  658. if(ptr->contenttype_alloc) {
  659. Curl_safefree(ptr->contenttype);
  660. ptr->contenttype_alloc = FALSE;
  661. }
  662. if(ptr->showfilename_alloc) {
  663. Curl_safefree(ptr->showfilename);
  664. ptr->showfilename_alloc = FALSE;
  665. }
  666. }
  667. }
  668. }
  669. /* Always deallocate FormInfo linked list nodes without touching node
  670. fields given that these have either been deallocated or are owned
  671. now by the httppost linked list */
  672. while(first_form) {
  673. FormInfo *ptr = first_form->more;
  674. free(first_form);
  675. first_form = ptr;
  676. }
  677. return return_value;
  678. }
  679. /*
  680. * curl_formadd() is a public API to add a section to the multipart formpost.
  681. *
  682. * @unittest: 1308
  683. */
  684. CURLFORMcode curl_formadd(struct curl_httppost **httppost,
  685. struct curl_httppost **last_post,
  686. ...)
  687. {
  688. va_list arg;
  689. CURLFORMcode result;
  690. va_start(arg, last_post);
  691. result = FormAdd(httppost, last_post, arg);
  692. va_end(arg);
  693. return result;
  694. }
  695. #ifdef __VMS
  696. #include <fabdef.h>
  697. /*
  698. * get_vms_file_size does what it takes to get the real size of the file
  699. *
  700. * For fixed files, find out the size of the EOF block and adjust.
  701. *
  702. * For all others, have to read the entire file in, discarding the contents.
  703. * Most posted text files will be small, and binary files like zlib archives
  704. * and CD/DVD images should be either a STREAM_LF format or a fixed format.
  705. *
  706. */
  707. curl_off_t VmsRealFileSize(const char * name,
  708. const struct_stat * stat_buf)
  709. {
  710. char buffer[8192];
  711. curl_off_t count;
  712. int ret_stat;
  713. FILE * file;
  714. file = fopen(name, "r"); /* VMS */
  715. if(file == NULL)
  716. return 0;
  717. count = 0;
  718. ret_stat = 1;
  719. while(ret_stat > 0) {
  720. ret_stat = fread(buffer, 1, sizeof(buffer), file);
  721. if(ret_stat != 0)
  722. count += ret_stat;
  723. }
  724. fclose(file);
  725. return count;
  726. }
  727. /*
  728. *
  729. * VmsSpecialSize checks to see if the stat st_size can be trusted and
  730. * if not to call a routine to get the correct size.
  731. *
  732. */
  733. static curl_off_t VmsSpecialSize(const char * name,
  734. const struct_stat * stat_buf)
  735. {
  736. switch(stat_buf->st_fab_rfm) {
  737. case FAB$C_VAR:
  738. case FAB$C_VFC:
  739. return VmsRealFileSize(name, stat_buf);
  740. break;
  741. default:
  742. return stat_buf->st_size;
  743. }
  744. }
  745. #endif
  746. #ifndef __VMS
  747. #define filesize(name, stat_data) (stat_data.st_size)
  748. #else
  749. /* Getting the expected file size needs help on VMS */
  750. #define filesize(name, stat_data) VmsSpecialSize(name, &stat_data)
  751. #endif
  752. /*
  753. * AddFormData() adds a chunk of data to the FormData linked list.
  754. *
  755. * size is incremented by the chunk length, unless it is NULL
  756. */
  757. static CURLcode AddFormData(struct FormData **formp,
  758. enum formtype type,
  759. const void *line,
  760. curl_off_t length,
  761. curl_off_t *size)
  762. {
  763. struct FormData *newform = malloc(sizeof(struct FormData));
  764. if(!newform)
  765. return CURLE_OUT_OF_MEMORY;
  766. newform->next = NULL;
  767. if(length < 0 || (size && *size < 0))
  768. return CURLE_BAD_FUNCTION_ARGUMENT;
  769. if(type <= FORM_CONTENT) {
  770. /* we make it easier for plain strings: */
  771. if(!length)
  772. length = strlen((char *)line);
  773. #if (SIZEOF_SIZE_T < CURL_SIZEOF_CURL_OFF_T)
  774. else if(length >= (curl_off_t)(size_t)-1)
  775. return CURLE_BAD_FUNCTION_ARGUMENT;
  776. #endif
  777. newform->line = malloc((size_t)length+1);
  778. if(!newform->line) {
  779. free(newform);
  780. return CURLE_OUT_OF_MEMORY;
  781. }
  782. memcpy(newform->line, line, (size_t)length);
  783. newform->length = (size_t)length;
  784. newform->line[(size_t)length]=0; /* zero terminate for easier debugging */
  785. }
  786. else
  787. /* For callbacks and files we don't have any actual data so we just keep a
  788. pointer to whatever this points to */
  789. newform->line = (char *)line;
  790. newform->type = type;
  791. if(*formp) {
  792. (*formp)->next = newform;
  793. *formp = newform;
  794. }
  795. else
  796. *formp = newform;
  797. if(size) {
  798. if(type != FORM_FILE)
  799. /* for static content as well as callback data we add the size given
  800. as input argument */
  801. *size += length;
  802. else {
  803. /* Since this is a file to be uploaded here, add the size of the actual
  804. file */
  805. if(!strequal("-", newform->line)) {
  806. struct_stat file;
  807. if(!stat(newform->line, &file) && !S_ISDIR(file.st_mode))
  808. *size += filesize(newform->line, file);
  809. else
  810. return CURLE_BAD_FUNCTION_ARGUMENT;
  811. }
  812. }
  813. }
  814. return CURLE_OK;
  815. }
  816. /*
  817. * AddFormDataf() adds printf()-style formatted data to the formdata chain.
  818. */
  819. static CURLcode AddFormDataf(struct FormData **formp,
  820. curl_off_t *size,
  821. const char *fmt, ...)
  822. {
  823. char s[4096];
  824. va_list ap;
  825. va_start(ap, fmt);
  826. vsnprintf(s, sizeof(s), fmt, ap);
  827. va_end(ap);
  828. return AddFormData(formp, FORM_DATA, s, 0, size);
  829. }
  830. /*
  831. * Curl_formclean() is used from http.c, this cleans a built FormData linked
  832. * list
  833. */
  834. void Curl_formclean(struct FormData **form_ptr)
  835. {
  836. struct FormData *next, *form;
  837. form = *form_ptr;
  838. if(!form)
  839. return;
  840. do {
  841. next=form->next; /* the following form line */
  842. if(form->type <= FORM_CONTENT)
  843. free(form->line); /* free the line */
  844. free(form); /* free the struct */
  845. } while((form = next) != NULL); /* continue */
  846. *form_ptr = NULL;
  847. }
  848. /*
  849. * curl_formget()
  850. * Serialize a curl_httppost struct.
  851. * Returns 0 on success.
  852. *
  853. * @unittest: 1308
  854. */
  855. int curl_formget(struct curl_httppost *form, void *arg,
  856. curl_formget_callback append)
  857. {
  858. CURLcode result;
  859. curl_off_t size;
  860. struct FormData *data, *ptr;
  861. result = Curl_getformdata(NULL, &data, form, NULL, &size);
  862. if(result)
  863. return (int)result;
  864. for(ptr = data; ptr; ptr = ptr->next) {
  865. if((ptr->type == FORM_FILE) || (ptr->type == FORM_CALLBACK)) {
  866. char buffer[8192];
  867. size_t nread;
  868. struct Form temp;
  869. Curl_FormInit(&temp, ptr);
  870. do {
  871. nread = readfromfile(&temp, buffer, sizeof(buffer));
  872. if((nread == (size_t) -1) ||
  873. (nread > sizeof(buffer)) ||
  874. (nread != append(arg, buffer, nread))) {
  875. if(temp.fp)
  876. fclose(temp.fp);
  877. Curl_formclean(&data);
  878. return -1;
  879. }
  880. } while(nread);
  881. }
  882. else {
  883. if(ptr->length != append(arg, ptr->line, ptr->length)) {
  884. Curl_formclean(&data);
  885. return -1;
  886. }
  887. }
  888. }
  889. Curl_formclean(&data);
  890. return 0;
  891. }
  892. /*
  893. * curl_formfree() is an external function to free up a whole form post
  894. * chain
  895. */
  896. void curl_formfree(struct curl_httppost *form)
  897. {
  898. struct curl_httppost *next;
  899. if(!form)
  900. /* no form to free, just get out of this */
  901. return;
  902. do {
  903. next=form->next; /* the following form line */
  904. /* recurse to sub-contents */
  905. curl_formfree(form->more);
  906. if(!(form->flags & HTTPPOST_PTRNAME))
  907. free(form->name); /* free the name */
  908. if(!(form->flags &
  909. (HTTPPOST_PTRCONTENTS|HTTPPOST_BUFFER|HTTPPOST_CALLBACK))
  910. )
  911. free(form->contents); /* free the contents */
  912. free(form->contenttype); /* free the content type */
  913. free(form->showfilename); /* free the faked file name */
  914. free(form); /* free the struct */
  915. } while((form = next) != NULL); /* continue */
  916. }
  917. #ifndef HAVE_BASENAME
  918. /*
  919. (Quote from The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004
  920. Edition)
  921. The basename() function shall take the pathname pointed to by path and
  922. return a pointer to the final component of the pathname, deleting any
  923. trailing '/' characters.
  924. If the string pointed to by path consists entirely of the '/' character,
  925. basename() shall return a pointer to the string "/". If the string pointed
  926. to by path is exactly "//", it is implementation-defined whether '/' or "//"
  927. is returned.
  928. If path is a null pointer or points to an empty string, basename() shall
  929. return a pointer to the string ".".
  930. The basename() function may modify the string pointed to by path, and may
  931. return a pointer to static storage that may then be overwritten by a
  932. subsequent call to basename().
  933. The basename() function need not be reentrant. A function that is not
  934. required to be reentrant is not required to be thread-safe.
  935. */
  936. static char *Curl_basename(char *path)
  937. {
  938. /* Ignore all the details above for now and make a quick and simple
  939. implementaion here */
  940. char *s1;
  941. char *s2;
  942. s1=strrchr(path, '/');
  943. s2=strrchr(path, '\\');
  944. if(s1 && s2) {
  945. path = (s1 > s2? s1 : s2)+1;
  946. }
  947. else if(s1)
  948. path = s1 + 1;
  949. else if(s2)
  950. path = s2 + 1;
  951. return path;
  952. }
  953. #endif
  954. static char *strippath(const char *fullfile)
  955. {
  956. char *filename;
  957. char *base;
  958. filename = strdup(fullfile); /* duplicate since basename() may ruin the
  959. buffer it works on */
  960. if(!filename)
  961. return NULL;
  962. base = strdup(basename(filename));
  963. free(filename); /* free temporary buffer */
  964. return base; /* returns an allocated string or NULL ! */
  965. }
  966. static CURLcode formdata_add_filename(const struct curl_httppost *file,
  967. struct FormData **form,
  968. curl_off_t *size)
  969. {
  970. CURLcode result = CURLE_OK;
  971. char *filename = file->showfilename;
  972. char *filebasename = NULL;
  973. char *filename_escaped = NULL;
  974. if(!filename) {
  975. filebasename = strippath(file->contents);
  976. if(!filebasename)
  977. return CURLE_OUT_OF_MEMORY;
  978. filename = filebasename;
  979. }
  980. if(strchr(filename, '\\') || strchr(filename, '"')) {
  981. char *p0, *p1;
  982. /* filename need be escaped */
  983. filename_escaped = malloc(strlen(filename)*2+1);
  984. if(!filename_escaped) {
  985. free(filebasename);
  986. return CURLE_OUT_OF_MEMORY;
  987. }
  988. p0 = filename_escaped;
  989. p1 = filename;
  990. while(*p1) {
  991. if(*p1 == '\\' || *p1 == '"')
  992. *p0++ = '\\';
  993. *p0++ = *p1++;
  994. }
  995. *p0 = '\0';
  996. filename = filename_escaped;
  997. }
  998. result = AddFormDataf(form, size,
  999. "; filename=\"%s\"",
  1000. filename);
  1001. free(filename_escaped);
  1002. free(filebasename);
  1003. return result;
  1004. }
  1005. /*
  1006. * Curl_getformdata() converts a linked list of "meta data" into a complete
  1007. * (possibly huge) multipart formdata. The input list is in 'post', while the
  1008. * output resulting linked lists gets stored in '*finalform'. *sizep will get
  1009. * the total size of the whole POST.
  1010. * A multipart/form_data content-type is built, unless a custom content-type
  1011. * is passed in 'custom_content_type'.
  1012. *
  1013. * This function will not do a failf() for the potential memory failures but
  1014. * should for all other errors it spots. Just note that this function MAY get
  1015. * a NULL pointer in the 'data' argument.
  1016. */
  1017. CURLcode Curl_getformdata(struct SessionHandle *data,
  1018. struct FormData **finalform,
  1019. struct curl_httppost *post,
  1020. const char *custom_content_type,
  1021. curl_off_t *sizep)
  1022. {
  1023. struct FormData *form = NULL;
  1024. struct FormData *firstform;
  1025. struct curl_httppost *file;
  1026. CURLcode result = CURLE_OK;
  1027. curl_off_t size = 0; /* support potentially ENORMOUS formposts */
  1028. char *boundary;
  1029. char *fileboundary = NULL;
  1030. struct curl_slist* curList;
  1031. *finalform = NULL; /* default form is empty */
  1032. if(!post)
  1033. return result; /* no input => no output! */
  1034. boundary = formboundary(data);
  1035. if(!boundary)
  1036. return CURLE_OUT_OF_MEMORY;
  1037. /* Make the first line of the output */
  1038. result = AddFormDataf(&form, NULL,
  1039. "%s; boundary=%s\r\n",
  1040. custom_content_type?custom_content_type:
  1041. "Content-Type: multipart/form-data",
  1042. boundary);
  1043. if(result) {
  1044. free(boundary);
  1045. return result;
  1046. }
  1047. /* we DO NOT include that line in the total size of the POST, since it'll be
  1048. part of the header! */
  1049. firstform = form;
  1050. do {
  1051. if(size) {
  1052. result = AddFormDataf(&form, &size, "\r\n");
  1053. if(result)
  1054. break;
  1055. }
  1056. /* boundary */
  1057. result = AddFormDataf(&form, &size, "--%s\r\n", boundary);
  1058. if(result)
  1059. break;
  1060. /* Maybe later this should be disabled when a custom_content_type is
  1061. passed, since Content-Disposition is not meaningful for all multipart
  1062. types.
  1063. */
  1064. result = AddFormDataf(&form, &size,
  1065. "Content-Disposition: form-data; name=\"");
  1066. if(result)
  1067. break;
  1068. result = AddFormData(&form, FORM_DATA, post->name, post->namelength,
  1069. &size);
  1070. if(result)
  1071. break;
  1072. result = AddFormDataf(&form, &size, "\"");
  1073. if(result)
  1074. break;
  1075. if(post->more) {
  1076. /* If used, this is a link to more file names, we must then do
  1077. the magic to include several files with the same field name */
  1078. free(fileboundary);
  1079. fileboundary = formboundary(data);
  1080. if(!fileboundary) {
  1081. result = CURLE_OUT_OF_MEMORY;
  1082. break;
  1083. }
  1084. result = AddFormDataf(&form, &size,
  1085. "\r\nContent-Type: multipart/mixed;"
  1086. " boundary=%s\r\n",
  1087. fileboundary);
  1088. if(result)
  1089. break;
  1090. }
  1091. file = post;
  1092. do {
  1093. /* If 'showfilename' is set, that is a faked name passed on to us
  1094. to use to in the formpost. If that is not set, the actually used
  1095. local file name should be added. */
  1096. if(post->more) {
  1097. /* if multiple-file */
  1098. result = AddFormDataf(&form, &size,
  1099. "\r\n--%s\r\nContent-Disposition: "
  1100. "attachment",
  1101. fileboundary);
  1102. if(result)
  1103. break;
  1104. result = formdata_add_filename(file, &form, &size);
  1105. if(result)
  1106. break;
  1107. }
  1108. else if(post->flags & (HTTPPOST_FILENAME|HTTPPOST_BUFFER|
  1109. HTTPPOST_CALLBACK)) {
  1110. /* it should be noted that for the HTTPPOST_FILENAME and
  1111. HTTPPOST_CALLBACK cases the ->showfilename struct member is always
  1112. assigned at this point */
  1113. if(post->showfilename || (post->flags & HTTPPOST_FILENAME)) {
  1114. result = formdata_add_filename(post, &form, &size);
  1115. }
  1116. if(result)
  1117. break;
  1118. }
  1119. if(file->contenttype) {
  1120. /* we have a specified type */
  1121. result = AddFormDataf(&form, &size,
  1122. "\r\nContent-Type: %s",
  1123. file->contenttype);
  1124. if(result)
  1125. break;
  1126. }
  1127. curList = file->contentheader;
  1128. while(curList) {
  1129. /* Process the additional headers specified for this form */
  1130. result = AddFormDataf( &form, &size, "\r\n%s", curList->data );
  1131. if(result)
  1132. break;
  1133. curList = curList->next;
  1134. }
  1135. if(result)
  1136. break;
  1137. result = AddFormDataf(&form, &size, "\r\n\r\n");
  1138. if(result)
  1139. break;
  1140. if((post->flags & HTTPPOST_FILENAME) ||
  1141. (post->flags & HTTPPOST_READFILE)) {
  1142. /* we should include the contents from the specified file */
  1143. FILE *fileread;
  1144. fileread = strequal("-", file->contents)?
  1145. stdin:fopen(file->contents, "rb"); /* binary read for win32 */
  1146. /*
  1147. * VMS: This only allows for stream files on VMS. Stream files are
  1148. * OK, as are FIXED & VAR files WITHOUT implied CC For implied CC,
  1149. * every record needs to have a \n appended & 1 added to SIZE
  1150. */
  1151. if(fileread) {
  1152. if(fileread != stdin) {
  1153. /* close the file */
  1154. fclose(fileread);
  1155. /* add the file name only - for later reading from this */
  1156. result = AddFormData(&form, FORM_FILE, file->contents, 0, &size);
  1157. }
  1158. else {
  1159. /* When uploading from stdin, we can't know the size of the file,
  1160. * thus must read the full file as before. We *could* use chunked
  1161. * transfer-encoding, but that only works for HTTP 1.1 and we
  1162. * can't be sure we work with such a server.
  1163. */
  1164. size_t nread;
  1165. char buffer[512];
  1166. while((nread = fread(buffer, 1, sizeof(buffer), fileread)) != 0) {
  1167. result = AddFormData(&form, FORM_CONTENT, buffer, nread, &size);
  1168. if(result)
  1169. break;
  1170. }
  1171. }
  1172. }
  1173. else {
  1174. if(data)
  1175. failf(data, "couldn't open file \"%s\"", file->contents);
  1176. *finalform = NULL;
  1177. result = CURLE_READ_ERROR;
  1178. }
  1179. }
  1180. else if(post->flags & HTTPPOST_BUFFER)
  1181. /* include contents of buffer */
  1182. result = AddFormData(&form, FORM_CONTENT, post->buffer,
  1183. post->bufferlength, &size);
  1184. else if(post->flags & HTTPPOST_CALLBACK)
  1185. /* the contents should be read with the callback and the size is set
  1186. with the contentslength */
  1187. result = AddFormData(&form, FORM_CALLBACK, post->userp,
  1188. post->flags&CURL_HTTPPOST_LARGE?
  1189. post->contentlen:post->contentslength, &size);
  1190. else
  1191. /* include the contents we got */
  1192. result = AddFormData(&form, FORM_CONTENT, post->contents,
  1193. post->flags&CURL_HTTPPOST_LARGE?
  1194. post->contentlen:post->contentslength, &size);
  1195. file = file->more;
  1196. } while(file && !result); /* for each specified file for this field */
  1197. if(result)
  1198. break;
  1199. if(post->more) {
  1200. /* this was a multiple-file inclusion, make a termination file
  1201. boundary: */
  1202. result = AddFormDataf(&form, &size,
  1203. "\r\n--%s--",
  1204. fileboundary);
  1205. if(result)
  1206. break;
  1207. }
  1208. } while((post = post->next) != NULL); /* for each field */
  1209. /* end-boundary for everything */
  1210. if(!result)
  1211. result = AddFormDataf(&form, &size, "\r\n--%s--\r\n", boundary);
  1212. if(result) {
  1213. Curl_formclean(&firstform);
  1214. free(fileboundary);
  1215. free(boundary);
  1216. return result;
  1217. }
  1218. *sizep = size;
  1219. free(fileboundary);
  1220. free(boundary);
  1221. *finalform = firstform;
  1222. return result;
  1223. }
  1224. /*
  1225. * Curl_FormInit() inits the struct 'form' points to with the 'formdata'
  1226. * and resets the 'sent' counter.
  1227. */
  1228. int Curl_FormInit(struct Form *form, struct FormData *formdata )
  1229. {
  1230. if(!formdata)
  1231. return 1; /* error */
  1232. form->data = formdata;
  1233. form->sent = 0;
  1234. form->fp = NULL;
  1235. form->fread_func = ZERO_NULL;
  1236. return 0;
  1237. }
  1238. #ifndef __VMS
  1239. # define fopen_read fopen
  1240. #else
  1241. /*
  1242. * vmsfopenread
  1243. *
  1244. * For upload to work as expected on VMS, different optional
  1245. * parameters must be added to the fopen command based on
  1246. * record format of the file.
  1247. *
  1248. */
  1249. # define fopen_read vmsfopenread
  1250. static FILE * vmsfopenread(const char *file, const char *mode) {
  1251. struct_stat statbuf;
  1252. int result;
  1253. result = stat(file, &statbuf);
  1254. switch (statbuf.st_fab_rfm) {
  1255. case FAB$C_VAR:
  1256. case FAB$C_VFC:
  1257. case FAB$C_STMCR:
  1258. return fopen(file, "r"); /* VMS */
  1259. break;
  1260. default:
  1261. return fopen(file, "r", "rfm=stmlf", "ctx=stm");
  1262. }
  1263. }
  1264. #endif
  1265. /*
  1266. * readfromfile()
  1267. *
  1268. * The read callback that this function may use can return a value larger than
  1269. * 'size' (which then this function returns) that indicates a problem and it
  1270. * must be properly dealt with
  1271. */
  1272. static size_t readfromfile(struct Form *form, char *buffer,
  1273. size_t size)
  1274. {
  1275. size_t nread;
  1276. bool callback = (form->data->type == FORM_CALLBACK)?TRUE:FALSE;
  1277. if(callback) {
  1278. if(form->fread_func == ZERO_NULL)
  1279. return 0;
  1280. else
  1281. nread = form->fread_func(buffer, 1, size, form->data->line);
  1282. }
  1283. else {
  1284. if(!form->fp) {
  1285. /* this file hasn't yet been opened */
  1286. form->fp = fopen_read(form->data->line, "rb"); /* b is for binary */
  1287. if(!form->fp)
  1288. return (size_t)-1; /* failure */
  1289. }
  1290. nread = fread(buffer, 1, size, form->fp);
  1291. }
  1292. if(!nread) {
  1293. /* this is the last chunk from the file, move on */
  1294. if(form->fp) {
  1295. fclose(form->fp);
  1296. form->fp = NULL;
  1297. }
  1298. form->data = form->data->next;
  1299. }
  1300. return nread;
  1301. }
  1302. /*
  1303. * Curl_FormReader() is the fread() emulation function that will be used to
  1304. * deliver the formdata to the transfer loop and then sent away to the peer.
  1305. */
  1306. size_t Curl_FormReader(char *buffer,
  1307. size_t size,
  1308. size_t nitems,
  1309. FILE *mydata)
  1310. {
  1311. struct Form *form;
  1312. size_t wantedsize;
  1313. size_t gotsize = 0;
  1314. form=(struct Form *)mydata;
  1315. wantedsize = size * nitems;
  1316. if(!form->data)
  1317. return 0; /* nothing, error, empty */
  1318. if((form->data->type == FORM_FILE) ||
  1319. (form->data->type == FORM_CALLBACK)) {
  1320. gotsize = readfromfile(form, buffer, wantedsize);
  1321. if(gotsize)
  1322. /* If positive or -1, return. If zero, continue! */
  1323. return gotsize;
  1324. }
  1325. do {
  1326. if((form->data->length - form->sent ) > wantedsize - gotsize) {
  1327. memcpy(buffer + gotsize , form->data->line + form->sent,
  1328. wantedsize - gotsize);
  1329. form->sent += wantedsize-gotsize;
  1330. return wantedsize;
  1331. }
  1332. memcpy(buffer+gotsize,
  1333. form->data->line + form->sent,
  1334. (form->data->length - form->sent) );
  1335. gotsize += form->data->length - form->sent;
  1336. form->sent = 0;
  1337. form->data = form->data->next; /* advance */
  1338. } while(form->data && (form->data->type < FORM_CALLBACK));
  1339. /* If we got an empty line and we have more data, we proceed to the next
  1340. line immediately to avoid returning zero before we've reached the end. */
  1341. return gotsize;
  1342. }
  1343. /*
  1344. * Curl_formpostheader() returns the first line of the formpost, the
  1345. * request-header part (which is not part of the request-body like the rest of
  1346. * the post).
  1347. */
  1348. char *Curl_formpostheader(void *formp, size_t *len)
  1349. {
  1350. char *header;
  1351. struct Form *form=(struct Form *)formp;
  1352. if(!form->data)
  1353. return 0; /* nothing, ERROR! */
  1354. header = form->data->line;
  1355. *len = form->data->length;
  1356. form->data = form->data->next; /* advance */
  1357. return header;
  1358. }
  1359. /*
  1360. * formboundary() creates a suitable boundary string and returns an allocated
  1361. * one.
  1362. */
  1363. static char *formboundary(struct SessionHandle *data)
  1364. {
  1365. /* 24 dashes and 16 hexadecimal digits makes 64 bit (18446744073709551615)
  1366. combinations */
  1367. return aprintf("------------------------%08x%08x",
  1368. Curl_rand(data), Curl_rand(data));
  1369. }
  1370. #else /* CURL_DISABLE_HTTP */
  1371. CURLFORMcode curl_formadd(struct curl_httppost **httppost,
  1372. struct curl_httppost **last_post,
  1373. ...)
  1374. {
  1375. (void)httppost;
  1376. (void)last_post;
  1377. return CURL_FORMADD_DISABLED;
  1378. }
  1379. int curl_formget(struct curl_httppost *form, void *arg,
  1380. curl_formget_callback append)
  1381. {
  1382. (void) form;
  1383. (void) arg;
  1384. (void) append;
  1385. return CURL_FORMADD_DISABLED;
  1386. }
  1387. void curl_formfree(struct curl_httppost *form)
  1388. {
  1389. (void)form;
  1390. /* does nothing HTTP is disabled */
  1391. }
  1392. #endif /* !defined(CURL_DISABLE_HTTP) */