formdata.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "formdata.h"
  27. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_MIME)
  28. #if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
  29. #include <libgen.h>
  30. #endif
  31. #include "urldata.h" /* for struct Curl_easy */
  32. #include "mime.h"
  33. #include "vtls/vtls.h"
  34. #include "strcase.h"
  35. #include "sendf.h"
  36. #include "strdup.h"
  37. #include "rand.h"
  38. #include "warnless.h"
  39. /* The last 3 #include files should be in this order */
  40. #include "curl_printf.h"
  41. #include "curl_memory.h"
  42. #include "memdebug.h"
  43. #define HTTPPOST_PTRNAME CURL_HTTPPOST_PTRNAME
  44. #define HTTPPOST_FILENAME CURL_HTTPPOST_FILENAME
  45. #define HTTPPOST_PTRCONTENTS CURL_HTTPPOST_PTRCONTENTS
  46. #define HTTPPOST_READFILE CURL_HTTPPOST_READFILE
  47. #define HTTPPOST_PTRBUFFER CURL_HTTPPOST_PTRBUFFER
  48. #define HTTPPOST_CALLBACK CURL_HTTPPOST_CALLBACK
  49. #define HTTPPOST_BUFFER CURL_HTTPPOST_BUFFER
  50. /***************************************************************************
  51. *
  52. * AddHttpPost()
  53. *
  54. * Adds a HttpPost structure to the list, if parent_post is given becomes
  55. * a subpost of parent_post instead of a direct list element.
  56. *
  57. * Returns newly allocated HttpPost on success and NULL if malloc failed.
  58. *
  59. ***************************************************************************/
  60. static struct curl_httppost *
  61. AddHttpPost(char *name, size_t namelength,
  62. char *value, curl_off_t contentslength,
  63. char *buffer, size_t bufferlength,
  64. char *contenttype,
  65. long flags,
  66. struct curl_slist *contentHeader,
  67. char *showfilename, char *userp,
  68. struct curl_httppost *parent_post,
  69. struct curl_httppost **httppost,
  70. struct curl_httppost **last_post)
  71. {
  72. struct curl_httppost *post;
  73. if(!namelength && name)
  74. namelength = strlen(name);
  75. if((bufferlength > LONG_MAX) || (namelength > LONG_MAX))
  76. /* avoid overflow in typecasts below */
  77. return NULL;
  78. post = calloc(1, sizeof(struct curl_httppost));
  79. if(post) {
  80. post->name = name;
  81. post->namelength = (long)namelength;
  82. post->contents = value;
  83. post->contentlen = contentslength;
  84. post->buffer = buffer;
  85. post->bufferlength = (long)bufferlength;
  86. post->contenttype = contenttype;
  87. post->contentheader = contentHeader;
  88. post->showfilename = showfilename;
  89. post->userp = userp;
  90. post->flags = flags | CURL_HTTPPOST_LARGE;
  91. }
  92. else
  93. return NULL;
  94. if(parent_post) {
  95. /* now, point our 'more' to the original 'more' */
  96. post->more = parent_post->more;
  97. /* then move the original 'more' to point to ourselves */
  98. parent_post->more = post;
  99. }
  100. else {
  101. /* make the previous point to this */
  102. if(*last_post)
  103. (*last_post)->next = post;
  104. else
  105. (*httppost) = post;
  106. (*last_post) = post;
  107. }
  108. return post;
  109. }
  110. /***************************************************************************
  111. *
  112. * AddFormInfo()
  113. *
  114. * Adds a FormInfo structure to the list presented by parent_form_info.
  115. *
  116. * Returns newly allocated FormInfo on success and NULL if malloc failed/
  117. * parent_form_info is NULL.
  118. *
  119. ***************************************************************************/
  120. static struct FormInfo *AddFormInfo(char *value,
  121. char *contenttype,
  122. struct FormInfo *parent_form_info)
  123. {
  124. struct FormInfo *form_info;
  125. form_info = calloc(1, sizeof(struct FormInfo));
  126. if(form_info) {
  127. if(value)
  128. form_info->value = value;
  129. if(contenttype)
  130. form_info->contenttype = contenttype;
  131. form_info->flags = HTTPPOST_FILENAME;
  132. }
  133. else
  134. return NULL;
  135. if(parent_form_info) {
  136. /* now, point our 'more' to the original 'more' */
  137. form_info->more = parent_form_info->more;
  138. /* then move the original 'more' to point to ourselves */
  139. parent_form_info->more = form_info;
  140. }
  141. return form_info;
  142. }
  143. /***************************************************************************
  144. *
  145. * FormAdd()
  146. *
  147. * Stores a formpost parameter and builds the appropriate linked list.
  148. *
  149. * Has two principal functionalities: using files and byte arrays as
  150. * post parts. Byte arrays are either copied or just the pointer is stored
  151. * (as the user requests) while for files only the filename and not the
  152. * content is stored.
  153. *
  154. * While you may have only one byte array for each name, multiple filenames
  155. * are allowed (and because of this feature CURLFORM_END is needed after
  156. * using CURLFORM_FILE).
  157. *
  158. * Examples:
  159. *
  160. * Simple name/value pair with copied contents:
  161. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  162. * CURLFORM_COPYCONTENTS, "value", CURLFORM_END);
  163. *
  164. * name/value pair where only the content pointer is remembered:
  165. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  166. * CURLFORM_PTRCONTENTS, ptr, CURLFORM_CONTENTSLENGTH, 10, CURLFORM_END);
  167. * (if CURLFORM_CONTENTSLENGTH is missing strlen () is used)
  168. *
  169. * storing a filename (CONTENTTYPE is optional!):
  170. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  171. * CURLFORM_FILE, "filename1", CURLFORM_CONTENTTYPE, "plain/text",
  172. * CURLFORM_END);
  173. *
  174. * storing multiple filenames:
  175. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  176. * CURLFORM_FILE, "filename1", CURLFORM_FILE, "filename2", CURLFORM_END);
  177. *
  178. * Returns:
  179. * CURL_FORMADD_OK on success
  180. * CURL_FORMADD_MEMORY if the FormInfo allocation fails
  181. * CURL_FORMADD_OPTION_TWICE if one option is given twice for one Form
  182. * CURL_FORMADD_NULL if a null pointer was given for a char
  183. * CURL_FORMADD_MEMORY if the allocation of a FormInfo struct failed
  184. * CURL_FORMADD_UNKNOWN_OPTION if an unknown option was used
  185. * CURL_FORMADD_INCOMPLETE if the some FormInfo is not complete (or error)
  186. * CURL_FORMADD_MEMORY if a HttpPost struct cannot be allocated
  187. * CURL_FORMADD_MEMORY if some allocation for string copying failed.
  188. * CURL_FORMADD_ILLEGAL_ARRAY if an illegal option is used in an array
  189. *
  190. ***************************************************************************/
  191. static
  192. CURLFORMcode FormAdd(struct curl_httppost **httppost,
  193. struct curl_httppost **last_post,
  194. va_list params)
  195. {
  196. struct FormInfo *first_form, *current_form, *form = NULL;
  197. CURLFORMcode return_value = CURL_FORMADD_OK;
  198. const char *prevtype = NULL;
  199. struct curl_httppost *post = NULL;
  200. CURLformoption option;
  201. struct curl_forms *forms = NULL;
  202. char *array_value = NULL; /* value read from an array */
  203. /* This is a state variable, that if TRUE means that we're parsing an
  204. array that we got passed to us. If FALSE we're parsing the input
  205. va_list arguments. */
  206. bool array_state = FALSE;
  207. /*
  208. * We need to allocate the first struct to fill in.
  209. */
  210. first_form = calloc(1, sizeof(struct FormInfo));
  211. if(!first_form)
  212. return CURL_FORMADD_MEMORY;
  213. current_form = first_form;
  214. /*
  215. * Loop through all the options set. Break if we have an error to report.
  216. */
  217. while(return_value == CURL_FORMADD_OK) {
  218. /* first see if we have more parts of the array param */
  219. if(array_state && forms) {
  220. /* get the upcoming option from the given array */
  221. option = forms->option;
  222. array_value = (char *)forms->value;
  223. forms++; /* advance this to next entry */
  224. if(CURLFORM_END == option) {
  225. /* end of array state */
  226. array_state = FALSE;
  227. continue;
  228. }
  229. }
  230. else {
  231. /* This is not array-state, get next option. This gets an 'int' with
  232. va_arg() because CURLformoption might be a smaller type than int and
  233. might cause compiler warnings and wrong behavior. */
  234. option = (CURLformoption)va_arg(params, int);
  235. if(CURLFORM_END == option)
  236. break;
  237. }
  238. switch(option) {
  239. case CURLFORM_ARRAY:
  240. if(array_state)
  241. /* we don't support an array from within an array */
  242. return_value = CURL_FORMADD_ILLEGAL_ARRAY;
  243. else {
  244. forms = va_arg(params, struct curl_forms *);
  245. if(forms)
  246. array_state = TRUE;
  247. else
  248. return_value = CURL_FORMADD_NULL;
  249. }
  250. break;
  251. /*
  252. * Set the Name property.
  253. */
  254. case CURLFORM_PTRNAME:
  255. current_form->flags |= HTTPPOST_PTRNAME; /* fall through */
  256. /* FALLTHROUGH */
  257. case CURLFORM_COPYNAME:
  258. if(current_form->name)
  259. return_value = CURL_FORMADD_OPTION_TWICE;
  260. else {
  261. char *name = array_state?
  262. array_value:va_arg(params, char *);
  263. if(name)
  264. current_form->name = name; /* store for the moment */
  265. else
  266. return_value = CURL_FORMADD_NULL;
  267. }
  268. break;
  269. case CURLFORM_NAMELENGTH:
  270. if(current_form->namelength)
  271. return_value = CURL_FORMADD_OPTION_TWICE;
  272. else
  273. current_form->namelength =
  274. array_state?(size_t)array_value:(size_t)va_arg(params, long);
  275. break;
  276. /*
  277. * Set the contents property.
  278. */
  279. case CURLFORM_PTRCONTENTS:
  280. current_form->flags |= HTTPPOST_PTRCONTENTS;
  281. /* FALLTHROUGH */
  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. struct 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 null-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. struct 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. struct 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. if(nread > sizeof(buffer) || append(arg, buffer, nread) != nread) {
  678. result = CURLE_READ_ERROR;
  679. if(nread == CURL_READFUNC_ABORT)
  680. result = CURLE_ABORTED_BY_CALLBACK;
  681. }
  682. }
  683. Curl_mime_cleanpart(&toppart);
  684. return (int) result;
  685. }
  686. /*
  687. * curl_formfree() is an external function to free up a whole form post
  688. * chain
  689. */
  690. void curl_formfree(struct curl_httppost *form)
  691. {
  692. struct curl_httppost *next;
  693. if(!form)
  694. /* no form to free, just get out of this */
  695. return;
  696. do {
  697. next = form->next; /* the following form line */
  698. /* recurse to sub-contents */
  699. curl_formfree(form->more);
  700. if(!(form->flags & HTTPPOST_PTRNAME))
  701. free(form->name); /* free the name */
  702. if(!(form->flags &
  703. (HTTPPOST_PTRCONTENTS|HTTPPOST_BUFFER|HTTPPOST_CALLBACK))
  704. )
  705. free(form->contents); /* free the contents */
  706. free(form->contenttype); /* free the content type */
  707. free(form->showfilename); /* free the faked file name */
  708. free(form); /* free the struct */
  709. form = next;
  710. } while(form); /* continue */
  711. }
  712. /* Set mime part name, taking care of non null-terminated name string. */
  713. static CURLcode setname(curl_mimepart *part, const char *name, size_t len)
  714. {
  715. char *zname;
  716. CURLcode res;
  717. if(!name || !len)
  718. return curl_mime_name(part, name);
  719. zname = malloc(len + 1);
  720. if(!zname)
  721. return CURLE_OUT_OF_MEMORY;
  722. memcpy(zname, name, len);
  723. zname[len] = '\0';
  724. res = curl_mime_name(part, zname);
  725. free(zname);
  726. return res;
  727. }
  728. /*
  729. * Curl_getformdata() converts a linked list of "meta data" into a mime
  730. * structure. The input list is in 'post', while the output is stored in
  731. * mime part at '*finalform'.
  732. *
  733. * This function will not do a failf() for the potential memory failures but
  734. * should for all other errors it spots. Just note that this function MAY get
  735. * a NULL pointer in the 'data' argument.
  736. */
  737. CURLcode Curl_getformdata(struct Curl_easy *data,
  738. curl_mimepart *finalform,
  739. struct curl_httppost *post,
  740. curl_read_callback fread_func)
  741. {
  742. CURLcode result = CURLE_OK;
  743. curl_mime *form = NULL;
  744. curl_mimepart *part;
  745. struct curl_httppost *file;
  746. Curl_mime_cleanpart(finalform); /* default form is empty */
  747. if(!post)
  748. return result; /* no input => no output! */
  749. form = curl_mime_init(data);
  750. if(!form)
  751. result = CURLE_OUT_OF_MEMORY;
  752. if(!result)
  753. result = curl_mime_subparts(finalform, form);
  754. /* Process each top part. */
  755. for(; !result && post; post = post->next) {
  756. /* If we have more than a file here, create a mime subpart and fill it. */
  757. curl_mime *multipart = form;
  758. if(post->more) {
  759. part = curl_mime_addpart(form);
  760. if(!part)
  761. result = CURLE_OUT_OF_MEMORY;
  762. if(!result)
  763. result = setname(part, post->name, post->namelength);
  764. if(!result) {
  765. multipart = curl_mime_init(data);
  766. if(!multipart)
  767. result = CURLE_OUT_OF_MEMORY;
  768. }
  769. if(!result)
  770. result = curl_mime_subparts(part, multipart);
  771. }
  772. /* Generate all the part contents. */
  773. for(file = post; !result && file; file = file->more) {
  774. /* Create the part. */
  775. part = curl_mime_addpart(multipart);
  776. if(!part)
  777. result = CURLE_OUT_OF_MEMORY;
  778. /* Set the headers. */
  779. if(!result)
  780. result = curl_mime_headers(part, file->contentheader, 0);
  781. /* Set the content type. */
  782. if(!result && file->contenttype)
  783. result = curl_mime_type(part, file->contenttype);
  784. /* Set field name. */
  785. if(!result && !post->more)
  786. result = setname(part, post->name, post->namelength);
  787. /* Process contents. */
  788. if(!result) {
  789. curl_off_t clen = post->contentslength;
  790. if(post->flags & CURL_HTTPPOST_LARGE)
  791. clen = post->contentlen;
  792. if(post->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE)) {
  793. if(!strcmp(file->contents, "-")) {
  794. /* There are a few cases where the code below won't work; in
  795. particular, freopen(stdin) by the caller is not guaranteed
  796. to result as expected. This feature has been kept for backward
  797. compatibility: use of "-" pseudo file name should be avoided. */
  798. result = curl_mime_data_cb(part, (curl_off_t) -1,
  799. (curl_read_callback) fread,
  800. CURLX_FUNCTION_CAST(curl_seek_callback,
  801. fseek),
  802. NULL, (void *) stdin);
  803. }
  804. else
  805. result = curl_mime_filedata(part, file->contents);
  806. if(!result && (post->flags & HTTPPOST_READFILE))
  807. result = curl_mime_filename(part, NULL);
  808. }
  809. else if(post->flags & HTTPPOST_BUFFER)
  810. result = curl_mime_data(part, post->buffer,
  811. post->bufferlength? post->bufferlength: -1);
  812. else if(post->flags & HTTPPOST_CALLBACK) {
  813. /* the contents should be read with the callback and the size is set
  814. with the contentslength */
  815. if(!clen)
  816. clen = -1;
  817. result = curl_mime_data_cb(part, clen,
  818. fread_func, NULL, NULL, post->userp);
  819. }
  820. else {
  821. size_t uclen;
  822. if(!clen)
  823. uclen = CURL_ZERO_TERMINATED;
  824. else
  825. uclen = (size_t)clen;
  826. result = curl_mime_data(part, post->contents, uclen);
  827. }
  828. }
  829. /* Set fake file name. */
  830. if(!result && post->showfilename)
  831. if(post->more || (post->flags & (HTTPPOST_FILENAME | HTTPPOST_BUFFER |
  832. HTTPPOST_CALLBACK)))
  833. result = curl_mime_filename(part, post->showfilename);
  834. }
  835. }
  836. if(result)
  837. Curl_mime_cleanpart(finalform);
  838. return result;
  839. }
  840. #else
  841. /* if disabled */
  842. CURLFORMcode curl_formadd(struct curl_httppost **httppost,
  843. struct curl_httppost **last_post,
  844. ...)
  845. {
  846. (void)httppost;
  847. (void)last_post;
  848. return CURL_FORMADD_DISABLED;
  849. }
  850. int curl_formget(struct curl_httppost *form, void *arg,
  851. curl_formget_callback append)
  852. {
  853. (void) form;
  854. (void) arg;
  855. (void) append;
  856. return CURL_FORMADD_DISABLED;
  857. }
  858. void curl_formfree(struct curl_httppost *form)
  859. {
  860. (void)form;
  861. /* does nothing HTTP is disabled */
  862. }
  863. #endif /* if disabled */