formdata.c 29 KB

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