formdata.c 46 KB

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