formdata.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, 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 "mime.h"
  31. #include "non-ascii.h"
  32. #include "vtls/vtls.h"
  33. #include "strcase.h"
  34. #include "sendf.h"
  35. #include "strdup.h"
  36. #include "rand.h"
  37. /* The last 3 #include files should be in this order */
  38. #include "curl_printf.h"
  39. #include "curl_memory.h"
  40. #include "memdebug.h"
  41. /* What kind of Content-Type to use on un-specified files with unrecognized
  42. extensions. */
  43. #define HTTPPOST_CONTENTTYPE_DEFAULT "application/octet-stream"
  44. #define HTTPPOST_PTRNAME CURL_HTTPPOST_PTRNAME
  45. #define HTTPPOST_FILENAME CURL_HTTPPOST_FILENAME
  46. #define HTTPPOST_PTRCONTENTS CURL_HTTPPOST_PTRCONTENTS
  47. #define HTTPPOST_READFILE CURL_HTTPPOST_READFILE
  48. #define HTTPPOST_PTRBUFFER CURL_HTTPPOST_PTRBUFFER
  49. #define HTTPPOST_CALLBACK CURL_HTTPPOST_CALLBACK
  50. #define HTTPPOST_BUFFER CURL_HTTPPOST_BUFFER
  51. /***************************************************************************
  52. *
  53. * AddHttpPost()
  54. *
  55. * Adds a HttpPost structure to the list, if parent_post is given becomes
  56. * a subpost of parent_post instead of a direct list element.
  57. *
  58. * Returns newly allocated HttpPost on success and NULL if malloc failed.
  59. *
  60. ***************************************************************************/
  61. static struct curl_httppost *
  62. AddHttpPost(char *name, size_t namelength,
  63. char *value, curl_off_t contentslength,
  64. char *buffer, size_t bufferlength,
  65. char *contenttype,
  66. long flags,
  67. struct curl_slist *contentHeader,
  68. char *showfilename, char *userp,
  69. struct curl_httppost *parent_post,
  70. struct curl_httppost **httppost,
  71. struct curl_httppost **last_post)
  72. {
  73. struct curl_httppost *post;
  74. post = calloc(1, sizeof(struct curl_httppost));
  75. if(post) {
  76. post->name = name;
  77. post->namelength = (long)(name?(namelength?namelength:strlen(name)):0);
  78. post->contents = value;
  79. post->contentlen = contentslength;
  80. post->buffer = buffer;
  81. post->bufferlength = (long)bufferlength;
  82. post->contenttype = contenttype;
  83. post->contentheader = contentHeader;
  84. post->showfilename = showfilename;
  85. post->userp = userp;
  86. post->flags = flags | CURL_HTTPPOST_LARGE;
  87. }
  88. else
  89. return NULL;
  90. if(parent_post) {
  91. /* now, point our 'more' to the original 'more' */
  92. post->more = parent_post->more;
  93. /* then move the original 'more' to point to ourselves */
  94. parent_post->more = post;
  95. }
  96. else {
  97. /* make the previous point to this */
  98. if(*last_post)
  99. (*last_post)->next = post;
  100. else
  101. (*httppost) = post;
  102. (*last_post) = post;
  103. }
  104. return post;
  105. }
  106. /***************************************************************************
  107. *
  108. * AddFormInfo()
  109. *
  110. * Adds a FormInfo structure to the list presented by parent_form_info.
  111. *
  112. * Returns newly allocated FormInfo on success and NULL if malloc failed/
  113. * parent_form_info is NULL.
  114. *
  115. ***************************************************************************/
  116. static FormInfo * AddFormInfo(char *value,
  117. char *contenttype,
  118. FormInfo *parent_form_info)
  119. {
  120. FormInfo *form_info;
  121. form_info = calloc(1, sizeof(struct FormInfo));
  122. if(form_info) {
  123. if(value)
  124. form_info->value = value;
  125. if(contenttype)
  126. form_info->contenttype = contenttype;
  127. form_info->flags = HTTPPOST_FILENAME;
  128. }
  129. else
  130. return NULL;
  131. if(parent_form_info) {
  132. /* now, point our 'more' to the original 'more' */
  133. form_info->more = parent_form_info->more;
  134. /* then move the original 'more' to point to ourselves */
  135. parent_form_info->more = form_info;
  136. }
  137. return form_info;
  138. }
  139. /***************************************************************************
  140. *
  141. * FormAdd()
  142. *
  143. * Stores a formpost parameter and builds the appropriate linked list.
  144. *
  145. * Has two principal functionalities: using files and byte arrays as
  146. * post parts. Byte arrays are either copied or just the pointer is stored
  147. * (as the user requests) while for files only the filename and not the
  148. * content is stored.
  149. *
  150. * While you may have only one byte array for each name, multiple filenames
  151. * are allowed (and because of this feature CURLFORM_END is needed after
  152. * using CURLFORM_FILE).
  153. *
  154. * Examples:
  155. *
  156. * Simple name/value pair with copied contents:
  157. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  158. * CURLFORM_COPYCONTENTS, "value", CURLFORM_END);
  159. *
  160. * name/value pair where only the content pointer is remembered:
  161. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  162. * CURLFORM_PTRCONTENTS, ptr, CURLFORM_CONTENTSLENGTH, 10, CURLFORM_END);
  163. * (if CURLFORM_CONTENTSLENGTH is missing strlen () is used)
  164. *
  165. * storing a filename (CONTENTTYPE is optional!):
  166. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  167. * CURLFORM_FILE, "filename1", CURLFORM_CONTENTTYPE, "plain/text",
  168. * CURLFORM_END);
  169. *
  170. * storing multiple filenames:
  171. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  172. * CURLFORM_FILE, "filename1", CURLFORM_FILE, "filename2", CURLFORM_END);
  173. *
  174. * Returns:
  175. * CURL_FORMADD_OK on success
  176. * CURL_FORMADD_MEMORY if the FormInfo allocation fails
  177. * CURL_FORMADD_OPTION_TWICE if one option is given twice for one Form
  178. * CURL_FORMADD_NULL if a null pointer was given for a char
  179. * CURL_FORMADD_MEMORY if the allocation of a FormInfo struct failed
  180. * CURL_FORMADD_UNKNOWN_OPTION if an unknown option was used
  181. * CURL_FORMADD_INCOMPLETE if the some FormInfo is not complete (or error)
  182. * CURL_FORMADD_MEMORY if a HttpPost struct cannot be allocated
  183. * CURL_FORMADD_MEMORY if some allocation for string copying failed.
  184. * CURL_FORMADD_ILLEGAL_ARRAY if an illegal option is used in an array
  185. *
  186. ***************************************************************************/
  187. static
  188. CURLFORMcode FormAdd(struct curl_httppost **httppost,
  189. struct curl_httppost **last_post,
  190. va_list params)
  191. {
  192. FormInfo *first_form, *current_form, *form = NULL;
  193. CURLFORMcode return_value = CURL_FORMADD_OK;
  194. const char *prevtype = NULL;
  195. struct curl_httppost *post = NULL;
  196. CURLformoption option;
  197. struct curl_forms *forms = NULL;
  198. char *array_value = NULL; /* value read from an array */
  199. /* This is a state variable, that if TRUE means that we're parsing an
  200. array that we got passed to us. If FALSE we're parsing the input
  201. va_list arguments. */
  202. bool array_state = FALSE;
  203. /*
  204. * We need to allocate the first struct to fill in.
  205. */
  206. first_form = calloc(1, sizeof(struct FormInfo));
  207. if(!first_form)
  208. return CURL_FORMADD_MEMORY;
  209. current_form = first_form;
  210. /*
  211. * Loop through all the options set. Break if we have an error to report.
  212. */
  213. while(return_value == CURL_FORMADD_OK) {
  214. /* first see if we have more parts of the array param */
  215. if(array_state && forms) {
  216. /* get the upcoming option from the given array */
  217. option = forms->option;
  218. array_value = (char *)forms->value;
  219. forms++; /* advance this to next entry */
  220. if(CURLFORM_END == option) {
  221. /* end of array state */
  222. array_state = FALSE;
  223. continue;
  224. }
  225. }
  226. else {
  227. /* This is not array-state, get next option */
  228. option = va_arg(params, CURLformoption);
  229. if(CURLFORM_END == option)
  230. break;
  231. }
  232. switch(option) {
  233. case CURLFORM_ARRAY:
  234. if(array_state)
  235. /* we don't support an array from within an array */
  236. return_value = CURL_FORMADD_ILLEGAL_ARRAY;
  237. else {
  238. forms = va_arg(params, struct curl_forms *);
  239. if(forms)
  240. array_state = TRUE;
  241. else
  242. return_value = CURL_FORMADD_NULL;
  243. }
  244. break;
  245. /*
  246. * Set the Name property.
  247. */
  248. case CURLFORM_PTRNAME:
  249. #ifdef CURL_DOES_CONVERSIONS
  250. /* Treat CURLFORM_PTR like CURLFORM_COPYNAME so that libcurl will copy
  251. * the data in all cases so that we'll have safe memory for the eventual
  252. * conversion.
  253. */
  254. #else
  255. current_form->flags |= HTTPPOST_PTRNAME; /* fall through */
  256. #endif
  257. /* FALLTHROUGH */
  258. case CURLFORM_COPYNAME:
  259. if(current_form->name)
  260. return_value = CURL_FORMADD_OPTION_TWICE;
  261. else {
  262. char *name = array_state?
  263. array_value:va_arg(params, char *);
  264. if(name)
  265. current_form->name = name; /* store for the moment */
  266. else
  267. return_value = CURL_FORMADD_NULL;
  268. }
  269. break;
  270. case CURLFORM_NAMELENGTH:
  271. if(current_form->namelength)
  272. return_value = CURL_FORMADD_OPTION_TWICE;
  273. else
  274. current_form->namelength =
  275. array_state?(size_t)array_value:(size_t)va_arg(params, long);
  276. break;
  277. /*
  278. * Set the contents property.
  279. */
  280. case CURLFORM_PTRCONTENTS:
  281. current_form->flags |= HTTPPOST_PTRCONTENTS; /* fall through */
  282. case CURLFORM_COPYCONTENTS:
  283. if(current_form->value)
  284. return_value = CURL_FORMADD_OPTION_TWICE;
  285. else {
  286. char *value =
  287. array_state?array_value:va_arg(params, char *);
  288. if(value)
  289. current_form->value = value; /* store for the moment */
  290. else
  291. return_value = CURL_FORMADD_NULL;
  292. }
  293. break;
  294. case CURLFORM_CONTENTSLENGTH:
  295. current_form->contentslength =
  296. array_state?(size_t)array_value:(size_t)va_arg(params, long);
  297. break;
  298. case CURLFORM_CONTENTLEN:
  299. current_form->flags |= CURL_HTTPPOST_LARGE;
  300. current_form->contentslength =
  301. array_state?(curl_off_t)(size_t)array_value:va_arg(params, curl_off_t);
  302. break;
  303. /* Get contents from a given file name */
  304. case CURLFORM_FILECONTENT:
  305. if(current_form->flags & (HTTPPOST_PTRCONTENTS|HTTPPOST_READFILE))
  306. return_value = CURL_FORMADD_OPTION_TWICE;
  307. else {
  308. const char *filename = array_state?
  309. array_value:va_arg(params, char *);
  310. if(filename) {
  311. current_form->value = strdup(filename);
  312. if(!current_form->value)
  313. return_value = CURL_FORMADD_MEMORY;
  314. else {
  315. current_form->flags |= HTTPPOST_READFILE;
  316. current_form->value_alloc = TRUE;
  317. }
  318. }
  319. else
  320. return_value = CURL_FORMADD_NULL;
  321. }
  322. break;
  323. /* We upload a file */
  324. case CURLFORM_FILE:
  325. {
  326. const char *filename = array_state?array_value:
  327. va_arg(params, char *);
  328. if(current_form->value) {
  329. if(current_form->flags & HTTPPOST_FILENAME) {
  330. if(filename) {
  331. char *fname = strdup(filename);
  332. if(!fname)
  333. return_value = CURL_FORMADD_MEMORY;
  334. else {
  335. form = AddFormInfo(fname, NULL, current_form);
  336. if(!form) {
  337. free(fname);
  338. return_value = CURL_FORMADD_MEMORY;
  339. }
  340. else {
  341. form->value_alloc = TRUE;
  342. current_form = form;
  343. form = NULL;
  344. }
  345. }
  346. }
  347. else
  348. return_value = CURL_FORMADD_NULL;
  349. }
  350. else
  351. return_value = CURL_FORMADD_OPTION_TWICE;
  352. }
  353. else {
  354. if(filename) {
  355. current_form->value = strdup(filename);
  356. if(!current_form->value)
  357. return_value = CURL_FORMADD_MEMORY;
  358. else {
  359. current_form->flags |= HTTPPOST_FILENAME;
  360. current_form->value_alloc = TRUE;
  361. }
  362. }
  363. else
  364. return_value = CURL_FORMADD_NULL;
  365. }
  366. break;
  367. }
  368. case CURLFORM_BUFFERPTR:
  369. current_form->flags |= HTTPPOST_PTRBUFFER|HTTPPOST_BUFFER;
  370. if(current_form->buffer)
  371. return_value = CURL_FORMADD_OPTION_TWICE;
  372. else {
  373. char *buffer =
  374. array_state?array_value:va_arg(params, char *);
  375. if(buffer) {
  376. current_form->buffer = buffer; /* store for the moment */
  377. current_form->value = buffer; /* make it non-NULL to be accepted
  378. as fine */
  379. }
  380. else
  381. return_value = CURL_FORMADD_NULL;
  382. }
  383. break;
  384. case CURLFORM_BUFFERLENGTH:
  385. if(current_form->bufferlength)
  386. return_value = CURL_FORMADD_OPTION_TWICE;
  387. else
  388. current_form->bufferlength =
  389. array_state?(size_t)array_value:(size_t)va_arg(params, long);
  390. break;
  391. case CURLFORM_STREAM:
  392. current_form->flags |= HTTPPOST_CALLBACK;
  393. if(current_form->userp)
  394. return_value = CURL_FORMADD_OPTION_TWICE;
  395. else {
  396. char *userp =
  397. array_state?array_value:va_arg(params, char *);
  398. if(userp) {
  399. current_form->userp = userp;
  400. current_form->value = userp; /* this isn't strictly true but we
  401. derive a value from this later on
  402. and we need this non-NULL to be
  403. accepted as a fine form part */
  404. }
  405. else
  406. return_value = CURL_FORMADD_NULL;
  407. }
  408. break;
  409. case CURLFORM_CONTENTTYPE:
  410. {
  411. const char *contenttype =
  412. array_state?array_value:va_arg(params, char *);
  413. if(current_form->contenttype) {
  414. if(current_form->flags & HTTPPOST_FILENAME) {
  415. if(contenttype) {
  416. char *type = strdup(contenttype);
  417. if(!type)
  418. return_value = CURL_FORMADD_MEMORY;
  419. else {
  420. form = AddFormInfo(NULL, type, current_form);
  421. if(!form) {
  422. free(type);
  423. return_value = CURL_FORMADD_MEMORY;
  424. }
  425. else {
  426. form->contenttype_alloc = TRUE;
  427. current_form = form;
  428. form = NULL;
  429. }
  430. }
  431. }
  432. else
  433. return_value = CURL_FORMADD_NULL;
  434. }
  435. else
  436. return_value = CURL_FORMADD_OPTION_TWICE;
  437. }
  438. else {
  439. if(contenttype) {
  440. current_form->contenttype = strdup(contenttype);
  441. if(!current_form->contenttype)
  442. return_value = CURL_FORMADD_MEMORY;
  443. else
  444. current_form->contenttype_alloc = TRUE;
  445. }
  446. else
  447. return_value = CURL_FORMADD_NULL;
  448. }
  449. break;
  450. }
  451. case CURLFORM_CONTENTHEADER:
  452. {
  453. /* this "cast increases required alignment of target type" but
  454. we consider it OK anyway */
  455. struct curl_slist *list = array_state?
  456. (struct curl_slist *)(void *)array_value:
  457. va_arg(params, struct curl_slist *);
  458. if(current_form->contentheader)
  459. return_value = CURL_FORMADD_OPTION_TWICE;
  460. else
  461. current_form->contentheader = list;
  462. break;
  463. }
  464. case CURLFORM_FILENAME:
  465. case CURLFORM_BUFFER:
  466. {
  467. const char *filename = array_state?array_value:
  468. va_arg(params, char *);
  469. if(current_form->showfilename)
  470. return_value = CURL_FORMADD_OPTION_TWICE;
  471. else {
  472. current_form->showfilename = strdup(filename);
  473. if(!current_form->showfilename)
  474. return_value = CURL_FORMADD_MEMORY;
  475. else
  476. current_form->showfilename_alloc = TRUE;
  477. }
  478. break;
  479. }
  480. default:
  481. return_value = CURL_FORMADD_UNKNOWN_OPTION;
  482. break;
  483. }
  484. }
  485. if(CURL_FORMADD_OK != return_value) {
  486. /* On error, free allocated fields for all nodes of the FormInfo linked
  487. list without deallocating nodes. List nodes are deallocated later on */
  488. FormInfo *ptr;
  489. for(ptr = first_form; ptr != NULL; ptr = ptr->more) {
  490. if(ptr->name_alloc) {
  491. Curl_safefree(ptr->name);
  492. ptr->name_alloc = FALSE;
  493. }
  494. if(ptr->value_alloc) {
  495. Curl_safefree(ptr->value);
  496. ptr->value_alloc = FALSE;
  497. }
  498. if(ptr->contenttype_alloc) {
  499. Curl_safefree(ptr->contenttype);
  500. ptr->contenttype_alloc = FALSE;
  501. }
  502. if(ptr->showfilename_alloc) {
  503. Curl_safefree(ptr->showfilename);
  504. ptr->showfilename_alloc = FALSE;
  505. }
  506. }
  507. }
  508. if(CURL_FORMADD_OK == return_value) {
  509. /* go through the list, check for completeness and if everything is
  510. * alright add the HttpPost item otherwise set return_value accordingly */
  511. post = NULL;
  512. for(form = first_form;
  513. form != NULL;
  514. form = form->more) {
  515. if(((!form->name || !form->value) && !post) ||
  516. ( (form->contentslength) &&
  517. (form->flags & HTTPPOST_FILENAME) ) ||
  518. ( (form->flags & HTTPPOST_FILENAME) &&
  519. (form->flags & HTTPPOST_PTRCONTENTS) ) ||
  520. ( (!form->buffer) &&
  521. (form->flags & HTTPPOST_BUFFER) &&
  522. (form->flags & HTTPPOST_PTRBUFFER) ) ||
  523. ( (form->flags & HTTPPOST_READFILE) &&
  524. (form->flags & HTTPPOST_PTRCONTENTS) )
  525. ) {
  526. return_value = CURL_FORMADD_INCOMPLETE;
  527. break;
  528. }
  529. if(((form->flags & HTTPPOST_FILENAME) ||
  530. (form->flags & HTTPPOST_BUFFER)) &&
  531. !form->contenttype) {
  532. char *f = form->flags & HTTPPOST_BUFFER?
  533. form->showfilename : form->value;
  534. char const *type;
  535. type = Curl_mime_contenttype(f);
  536. if(!type)
  537. type = prevtype;
  538. if(!type)
  539. type = FILE_CONTENTTYPE_DEFAULT;
  540. /* our contenttype is missing */
  541. form->contenttype = strdup(type);
  542. if(!form->contenttype) {
  543. return_value = CURL_FORMADD_MEMORY;
  544. break;
  545. }
  546. form->contenttype_alloc = TRUE;
  547. }
  548. if(form->name && form->namelength) {
  549. /* Name should not contain nul bytes. */
  550. size_t i;
  551. for(i = 0; i < form->namelength; i++)
  552. if(!form->name[i]) {
  553. return_value = CURL_FORMADD_NULL;
  554. break;
  555. }
  556. if(return_value != CURL_FORMADD_OK)
  557. break;
  558. }
  559. if(!(form->flags & HTTPPOST_PTRNAME) &&
  560. (form == first_form) ) {
  561. /* Note that there's small risk that form->name is NULL here if the
  562. app passed in a bad combo, so we better check for that first. */
  563. if(form->name) {
  564. /* copy name (without strdup; possibly not nul-terminated) */
  565. form->name = Curl_memdup(form->name, form->namelength?
  566. form->namelength:
  567. strlen(form->name) + 1);
  568. }
  569. if(!form->name) {
  570. return_value = CURL_FORMADD_MEMORY;
  571. break;
  572. }
  573. form->name_alloc = TRUE;
  574. }
  575. if(!(form->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE |
  576. HTTPPOST_PTRCONTENTS | HTTPPOST_PTRBUFFER |
  577. HTTPPOST_CALLBACK)) && form->value) {
  578. /* copy value (without strdup; possibly contains null characters) */
  579. size_t clen = (size_t) form->contentslength;
  580. if(!clen)
  581. clen = strlen(form->value) + 1;
  582. form->value = Curl_memdup(form->value, clen);
  583. if(!form->value) {
  584. return_value = CURL_FORMADD_MEMORY;
  585. break;
  586. }
  587. form->value_alloc = TRUE;
  588. }
  589. post = AddHttpPost(form->name, form->namelength,
  590. form->value, form->contentslength,
  591. form->buffer, form->bufferlength,
  592. form->contenttype, form->flags,
  593. form->contentheader, form->showfilename,
  594. form->userp,
  595. post, httppost,
  596. last_post);
  597. if(!post) {
  598. return_value = CURL_FORMADD_MEMORY;
  599. break;
  600. }
  601. if(form->contenttype)
  602. prevtype = form->contenttype;
  603. }
  604. if(CURL_FORMADD_OK != return_value) {
  605. /* On error, free allocated fields for nodes of the FormInfo linked
  606. list which are not already owned by the httppost linked list
  607. without deallocating nodes. List nodes are deallocated later on */
  608. FormInfo *ptr;
  609. for(ptr = form; ptr != NULL; ptr = ptr->more) {
  610. if(ptr->name_alloc) {
  611. Curl_safefree(ptr->name);
  612. ptr->name_alloc = FALSE;
  613. }
  614. if(ptr->value_alloc) {
  615. Curl_safefree(ptr->value);
  616. ptr->value_alloc = FALSE;
  617. }
  618. if(ptr->contenttype_alloc) {
  619. Curl_safefree(ptr->contenttype);
  620. ptr->contenttype_alloc = FALSE;
  621. }
  622. if(ptr->showfilename_alloc) {
  623. Curl_safefree(ptr->showfilename);
  624. ptr->showfilename_alloc = FALSE;
  625. }
  626. }
  627. }
  628. }
  629. /* Always deallocate FormInfo linked list nodes without touching node
  630. fields given that these have either been deallocated or are owned
  631. now by the httppost linked list */
  632. while(first_form) {
  633. FormInfo *ptr = first_form->more;
  634. free(first_form);
  635. first_form = ptr;
  636. }
  637. return return_value;
  638. }
  639. /*
  640. * curl_formadd() is a public API to add a section to the multipart formpost.
  641. *
  642. * @unittest: 1308
  643. */
  644. CURLFORMcode curl_formadd(struct curl_httppost **httppost,
  645. struct curl_httppost **last_post,
  646. ...)
  647. {
  648. va_list arg;
  649. CURLFORMcode result;
  650. va_start(arg, last_post);
  651. result = FormAdd(httppost, last_post, arg);
  652. va_end(arg);
  653. return result;
  654. }
  655. /*
  656. * curl_formget()
  657. * Serialize a curl_httppost struct.
  658. * Returns 0 on success.
  659. *
  660. * @unittest: 1308
  661. */
  662. int curl_formget(struct curl_httppost *form, void *arg,
  663. curl_formget_callback append)
  664. {
  665. CURLcode result;
  666. curl_mimepart toppart;
  667. Curl_mime_initpart(&toppart, NULL); /* default form is empty */
  668. result = Curl_getformdata(NULL, &toppart, form, NULL);
  669. if(!result)
  670. result = Curl_mime_prepare_headers(&toppart, "multipart/form-data",
  671. NULL, MIMESTRATEGY_FORM);
  672. while(!result) {
  673. char buffer[8192];
  674. size_t nread = Curl_mime_read(buffer, 1, sizeof buffer, &toppart);
  675. if(!nread)
  676. break;
  677. switch(nread) {
  678. default:
  679. if(append(arg, buffer, nread) != nread)
  680. result = CURLE_READ_ERROR;
  681. break;
  682. case CURL_READFUNC_ABORT:
  683. case CURL_READFUNC_PAUSE:
  684. break;
  685. }
  686. }
  687. Curl_mime_cleanpart(&toppart);
  688. return (int) result;
  689. }
  690. /*
  691. * curl_formfree() is an external function to free up a whole form post
  692. * chain
  693. */
  694. void curl_formfree(struct curl_httppost *form)
  695. {
  696. struct curl_httppost *next;
  697. if(!form)
  698. /* no form to free, just get out of this */
  699. return;
  700. do {
  701. next = form->next; /* the following form line */
  702. /* recurse to sub-contents */
  703. curl_formfree(form->more);
  704. if(!(form->flags & HTTPPOST_PTRNAME))
  705. free(form->name); /* free the name */
  706. if(!(form->flags &
  707. (HTTPPOST_PTRCONTENTS|HTTPPOST_BUFFER|HTTPPOST_CALLBACK))
  708. )
  709. free(form->contents); /* free the contents */
  710. free(form->contenttype); /* free the content type */
  711. free(form->showfilename); /* free the faked file name */
  712. free(form); /* free the struct */
  713. form = next;
  714. } while(form); /* continue */
  715. }
  716. /* Set mime part name, taking care of non nul-terminated name string. */
  717. static CURLcode setname(curl_mimepart *part, const char *name, size_t len)
  718. {
  719. char *zname;
  720. CURLcode res;
  721. if(!name || !len)
  722. return curl_mime_name(part, name);
  723. zname = malloc(len + 1);
  724. if(!zname)
  725. return CURLE_OUT_OF_MEMORY;
  726. memcpy(zname, name, len);
  727. zname[len] = '\0';
  728. res = curl_mime_name(part, zname);
  729. free(zname);
  730. return res;
  731. }
  732. /*
  733. * Curl_getformdata() converts a linked list of "meta data" into a mime
  734. * structure. The input list is in 'post', while the output is stored in
  735. * mime part at '*finalform'.
  736. *
  737. * This function will not do a failf() for the potential memory failures but
  738. * should for all other errors it spots. Just note that this function MAY get
  739. * a NULL pointer in the 'data' argument.
  740. */
  741. CURLcode Curl_getformdata(struct Curl_easy *data,
  742. curl_mimepart *finalform,
  743. struct curl_httppost *post,
  744. curl_read_callback fread_func)
  745. {
  746. CURLcode result = CURLE_OK;
  747. curl_mime *form = NULL;
  748. curl_mime *multipart;
  749. curl_mimepart *part;
  750. struct curl_httppost *file;
  751. Curl_mime_cleanpart(finalform); /* default form is empty */
  752. if(!post)
  753. return result; /* no input => no output! */
  754. form = curl_mime_init(data);
  755. if(!form)
  756. result = CURLE_OUT_OF_MEMORY;
  757. if(!result)
  758. result = curl_mime_subparts(finalform, form);
  759. /* Process each top part. */
  760. for(; !result && post; post = post->next) {
  761. /* If we have more than a file here, create a mime subpart and fill it. */
  762. multipart = form;
  763. if(post->more) {
  764. part = curl_mime_addpart(form);
  765. if(!part)
  766. result = CURLE_OUT_OF_MEMORY;
  767. if(!result)
  768. result = setname(part, post->name, post->namelength);
  769. if(!result) {
  770. multipart = curl_mime_init(data);
  771. if(!multipart)
  772. result = CURLE_OUT_OF_MEMORY;
  773. }
  774. if(!result)
  775. result = curl_mime_subparts(part, multipart);
  776. }
  777. /* Generate all the part contents. */
  778. for(file = post; !result && file; file = file->more) {
  779. /* Create the part. */
  780. part = curl_mime_addpart(multipart);
  781. if(!part)
  782. result = CURLE_OUT_OF_MEMORY;
  783. /* Set the headers. */
  784. if(!result)
  785. result = curl_mime_headers(part, file->contentheader, 0);
  786. /* Set the content type. */
  787. if(!result && file->contenttype)
  788. result = curl_mime_type(part, file->contenttype);
  789. /* Set field name. */
  790. if(!result && !post->more)
  791. result = setname(part, post->name, post->namelength);
  792. /* Process contents. */
  793. if(!result) {
  794. curl_off_t clen = post->contentslength;
  795. if(post->flags & CURL_HTTPPOST_LARGE)
  796. clen = post->contentlen;
  797. if(!clen)
  798. clen = -1;
  799. if(post->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE)) {
  800. if(!strcmp(file->contents, "-")) {
  801. /* There are a few cases where the code below won't work; in
  802. particular, freopen(stdin) by the caller is not guaranteed
  803. to result as expected. This feature has been kept for backward
  804. compatibility: use of "-" pseudo file name should be avoided. */
  805. result = curl_mime_data_cb(part, (curl_off_t) -1,
  806. (curl_read_callback) fread,
  807. (curl_seek_callback) fseek,
  808. NULL, (void *) stdin);
  809. }
  810. else
  811. result = curl_mime_filedata(part, file->contents);
  812. if(!result && (post->flags & HTTPPOST_READFILE))
  813. result = curl_mime_filename(part, NULL);
  814. }
  815. else if(post->flags & HTTPPOST_BUFFER)
  816. result = curl_mime_data(part, post->buffer,
  817. post->bufferlength? post->bufferlength: -1);
  818. else if(post->flags & HTTPPOST_CALLBACK)
  819. /* the contents should be read with the callback and the size is set
  820. with the contentslength */
  821. result = curl_mime_data_cb(part, clen,
  822. fread_func, NULL, NULL, post->userp);
  823. else {
  824. result = curl_mime_data(part, post->contents, (ssize_t) clen);
  825. #ifdef CURL_DOES_CONVERSIONS
  826. /* Convert textual contents now. */
  827. if(!result && data && part->datasize)
  828. result = Curl_convert_to_network(data, part->data, part->datasize);
  829. #endif
  830. }
  831. }
  832. /* Set fake file name. */
  833. if(!result && post->showfilename)
  834. if(post->more || (post->flags & (HTTPPOST_FILENAME | HTTPPOST_BUFFER |
  835. HTTPPOST_CALLBACK)))
  836. result = curl_mime_filename(part, post->showfilename);
  837. }
  838. }
  839. if(result)
  840. Curl_mime_cleanpart(finalform);
  841. return result;
  842. }
  843. #else /* CURL_DISABLE_HTTP */
  844. CURLFORMcode curl_formadd(struct curl_httppost **httppost,
  845. struct curl_httppost **last_post,
  846. ...)
  847. {
  848. (void)httppost;
  849. (void)last_post;
  850. return CURL_FORMADD_DISABLED;
  851. }
  852. int curl_formget(struct curl_httppost *form, void *arg,
  853. curl_formget_callback append)
  854. {
  855. (void) form;
  856. (void) arg;
  857. (void) append;
  858. return CURL_FORMADD_DISABLED;
  859. }
  860. void curl_formfree(struct curl_httppost *form)
  861. {
  862. (void)form;
  863. /* does nothing HTTP is disabled */
  864. }
  865. #endif /* !defined(CURL_DISABLE_HTTP) */