formdata.c 30 KB

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