formdata.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. struct Curl_easy;
  27. #include "formdata.h"
  28. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_FORM_API)
  29. #if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
  30. #include <libgen.h>
  31. #endif
  32. #include "urldata.h" /* for struct Curl_easy */
  33. #include "mime.h"
  34. #include "vtls/vtls.h"
  35. #include "strcase.h"
  36. #include "sendf.h"
  37. #include "strdup.h"
  38. #include "rand.h"
  39. #include "warnless.h"
  40. /* The last 3 #include files should be in this order */
  41. #include "curl_printf.h"
  42. #include "curl_memory.h"
  43. #include "memdebug.h"
  44. #define HTTPPOST_PTRNAME CURL_HTTPPOST_PTRNAME
  45. #define HTTPPOST_FILENAME CURL_HTTPPOST_FILENAME
  46. #define HTTPPOST_PTRCONTENTS CURL_HTTPPOST_PTRCONTENTS
  47. #define HTTPPOST_READFILE CURL_HTTPPOST_READFILE
  48. #define HTTPPOST_PTRBUFFER CURL_HTTPPOST_PTRBUFFER
  49. #define HTTPPOST_CALLBACK CURL_HTTPPOST_CALLBACK
  50. #define HTTPPOST_BUFFER CURL_HTTPPOST_BUFFER
  51. /***************************************************************************
  52. *
  53. * AddHttpPost()
  54. *
  55. * Adds an HttpPost structure to the list, if parent_post is given becomes
  56. * a subpost of parent_post instead of a direct list element.
  57. *
  58. * Returns newly allocated HttpPost on success and NULL if malloc failed.
  59. *
  60. ***************************************************************************/
  61. static struct curl_httppost *
  62. AddHttpPost(char *name, size_t namelength,
  63. char *value, curl_off_t contentslength,
  64. char *buffer, size_t bufferlength,
  65. char *contenttype,
  66. long flags,
  67. struct curl_slist *contentHeader,
  68. char *showfilename, char *userp,
  69. struct curl_httppost *parent_post,
  70. struct curl_httppost **httppost,
  71. struct curl_httppost **last_post)
  72. {
  73. struct curl_httppost *post;
  74. if(!namelength && name)
  75. namelength = strlen(name);
  76. if((bufferlength > LONG_MAX) || (namelength > LONG_MAX))
  77. /* avoid overflow in typecasts below */
  78. return NULL;
  79. post = calloc(1, sizeof(struct curl_httppost));
  80. if(post) {
  81. post->name = name;
  82. post->namelength = (long)namelength;
  83. post->contents = value;
  84. post->contentlen = contentslength;
  85. post->buffer = buffer;
  86. post->bufferlength = (long)bufferlength;
  87. post->contenttype = contenttype;
  88. post->contentheader = contentHeader;
  89. post->showfilename = showfilename;
  90. post->userp = userp;
  91. post->flags = flags | CURL_HTTPPOST_LARGE;
  92. }
  93. else
  94. return NULL;
  95. if(parent_post) {
  96. /* now, point our 'more' to the original 'more' */
  97. post->more = parent_post->more;
  98. /* then move the original 'more' to point to ourselves */
  99. parent_post->more = post;
  100. }
  101. else {
  102. /* make the previous point to this */
  103. if(*last_post)
  104. (*last_post)->next = post;
  105. else
  106. (*httppost) = post;
  107. (*last_post) = post;
  108. }
  109. return post;
  110. }
  111. /***************************************************************************
  112. *
  113. * AddFormInfo()
  114. *
  115. * Adds a FormInfo structure to the list presented by parent_form_info.
  116. *
  117. * Returns newly allocated FormInfo on success and NULL if malloc failed/
  118. * parent_form_info is NULL.
  119. *
  120. ***************************************************************************/
  121. static struct FormInfo *AddFormInfo(char *value,
  122. char *contenttype,
  123. struct FormInfo *parent_form_info)
  124. {
  125. struct FormInfo *form_info;
  126. form_info = calloc(1, sizeof(struct FormInfo));
  127. if(!form_info)
  128. return NULL;
  129. if(value)
  130. form_info->value = value;
  131. if(contenttype)
  132. form_info->contenttype = contenttype;
  133. form_info->flags = HTTPPOST_FILENAME;
  134. if(parent_form_info) {
  135. /* now, point our 'more' to the original 'more' */
  136. form_info->more = parent_form_info->more;
  137. /* then move the original 'more' to point to ourselves */
  138. parent_form_info->more = form_info;
  139. }
  140. return form_info;
  141. }
  142. /***************************************************************************
  143. *
  144. * FormAdd()
  145. *
  146. * Stores a formpost parameter and builds the appropriate linked list.
  147. *
  148. * Has two principal functionalities: using files and byte arrays as
  149. * post parts. Byte arrays are either copied or just the pointer is stored
  150. * (as the user requests) while for files only the filename and not the
  151. * content is stored.
  152. *
  153. * While you may have only one byte array for each name, multiple filenames
  154. * are allowed (and because of this feature CURLFORM_END is needed after
  155. * using CURLFORM_FILE).
  156. *
  157. * Examples:
  158. *
  159. * Simple name/value pair with copied contents:
  160. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  161. * CURLFORM_COPYCONTENTS, "value", CURLFORM_END);
  162. *
  163. * name/value pair where only the content pointer is remembered:
  164. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  165. * CURLFORM_PTRCONTENTS, ptr, CURLFORM_CONTENTSLENGTH, 10, CURLFORM_END);
  166. * (if CURLFORM_CONTENTSLENGTH is missing strlen () is used)
  167. *
  168. * storing a filename (CONTENTTYPE is optional!):
  169. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  170. * CURLFORM_FILE, "filename1", CURLFORM_CONTENTTYPE, "plain/text",
  171. * CURLFORM_END);
  172. *
  173. * storing multiple filenames:
  174. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  175. * CURLFORM_FILE, "filename1", CURLFORM_FILE, "filename2", CURLFORM_END);
  176. *
  177. * Returns:
  178. * CURL_FORMADD_OK on success
  179. * CURL_FORMADD_MEMORY if the FormInfo allocation fails
  180. * CURL_FORMADD_OPTION_TWICE if one option is given twice for one Form
  181. * CURL_FORMADD_NULL if a null pointer was given for a char
  182. * CURL_FORMADD_MEMORY if the allocation of a FormInfo struct failed
  183. * CURL_FORMADD_UNKNOWN_OPTION if an unknown option was used
  184. * CURL_FORMADD_INCOMPLETE if the some FormInfo is not complete (or error)
  185. * CURL_FORMADD_MEMORY if an HttpPost struct cannot be allocated
  186. * CURL_FORMADD_MEMORY if some allocation for string copying failed.
  187. * CURL_FORMADD_ILLEGAL_ARRAY if an illegal option is used in an array
  188. *
  189. ***************************************************************************/
  190. static
  191. CURLFORMcode FormAdd(struct curl_httppost **httppost,
  192. struct curl_httppost **last_post,
  193. va_list params)
  194. {
  195. struct FormInfo *first_form, *current_form, *form = NULL;
  196. CURLFORMcode return_value = CURL_FORMADD_OK;
  197. const char *prevtype = NULL;
  198. struct curl_httppost *post = NULL;
  199. CURLformoption option;
  200. struct curl_forms *forms = NULL;
  201. char *array_value = NULL; /* value read from an array */
  202. /* This is a state variable, that if TRUE means that we are parsing an
  203. array that we got passed to us. If FALSE we are parsing the input
  204. va_list arguments. */
  205. bool array_state = FALSE;
  206. /*
  207. * We need to allocate the first struct to fill in.
  208. */
  209. first_form = calloc(1, sizeof(struct FormInfo));
  210. if(!first_form)
  211. return CURL_FORMADD_MEMORY;
  212. current_form = first_form;
  213. /*
  214. * Loop through all the options set. Break if we have an error to report.
  215. */
  216. while(return_value == CURL_FORMADD_OK) {
  217. /* first see if we have more parts of the array param */
  218. if(array_state && forms) {
  219. /* get the upcoming option from the given array */
  220. option = forms->option;
  221. array_value = (char *)forms->value;
  222. forms++; /* advance this to next entry */
  223. if(CURLFORM_END == option) {
  224. /* end of array state */
  225. array_state = FALSE;
  226. continue;
  227. }
  228. }
  229. else {
  230. /* This is not array-state, get next option. This gets an 'int' with
  231. va_arg() because CURLformoption might be a smaller type than int and
  232. might cause compiler warnings and wrong behavior. */
  233. option = (CURLformoption)va_arg(params, int);
  234. if(CURLFORM_END == option)
  235. break;
  236. }
  237. switch(option) {
  238. case CURLFORM_ARRAY:
  239. if(array_state)
  240. /* we do not support an array from within an array */
  241. return_value = CURL_FORMADD_ILLEGAL_ARRAY;
  242. else {
  243. forms = va_arg(params, struct curl_forms *);
  244. if(forms)
  245. array_state = TRUE;
  246. else
  247. return_value = CURL_FORMADD_NULL;
  248. }
  249. break;
  250. /*
  251. * Set the Name property.
  252. */
  253. case CURLFORM_PTRNAME:
  254. current_form->flags |= HTTPPOST_PTRNAME; /* fall through */
  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 :
  301. va_arg(params, curl_off_t);
  302. break;
  303. /* Get contents from a given filename */
  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 is not 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 is 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_memdup0(form->name, form->namelength ?
  566. form->namelength :
  567. strlen(form->name));
  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); /* default form is empty */
  668. result = Curl_getformdata(NULL, &toppart, form, NULL);
  669. if(!result)
  670. result = Curl_mime_prepare_headers(NULL, &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 filename */
  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 = Curl_memdup0(name, len);
  720. if(!zname)
  721. return CURLE_OUT_OF_MEMORY;
  722. res = curl_mime_name(part, zname);
  723. free(zname);
  724. return res;
  725. }
  726. /* wrap call to fseeko so it matches the calling convention of callback */
  727. static int fseeko_wrapper(void *stream, curl_off_t offset, int whence)
  728. {
  729. #if defined(HAVE__FSEEKI64)
  730. return _fseeki64(stream, (__int64)offset, whence);
  731. #elif defined(HAVE_FSEEKO) && defined(HAVE_DECL_FSEEKO)
  732. return fseeko(stream, (off_t)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(CURL *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 will not 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 filename 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 ?
  822. post->bufferlength : -1);
  823. else if(post->flags & HTTPPOST_CALLBACK) {
  824. /* the contents should be read with the callback and the size is set
  825. with the contentslength */
  826. if(!clen)
  827. clen = -1;
  828. result = curl_mime_data_cb(part, clen,
  829. fread_func, NULL, NULL, post->userp);
  830. }
  831. else {
  832. size_t uclen;
  833. if(!clen)
  834. uclen = CURL_ZERO_TERMINATED;
  835. else
  836. uclen = (size_t)clen;
  837. result = curl_mime_data(part, post->contents, uclen);
  838. }
  839. }
  840. /* Set fake filename. */
  841. if(!result && post->showfilename)
  842. if(post->more || (post->flags & (HTTPPOST_FILENAME | HTTPPOST_BUFFER |
  843. HTTPPOST_CALLBACK)))
  844. result = curl_mime_filename(part, post->showfilename);
  845. }
  846. }
  847. if(result)
  848. Curl_mime_cleanpart(finalform);
  849. return result;
  850. }
  851. #else
  852. /* if disabled */
  853. CURLFORMcode curl_formadd(struct curl_httppost **httppost,
  854. struct curl_httppost **last_post,
  855. ...)
  856. {
  857. (void)httppost;
  858. (void)last_post;
  859. return CURL_FORMADD_DISABLED;
  860. }
  861. int curl_formget(struct curl_httppost *form, void *arg,
  862. curl_formget_callback append)
  863. {
  864. (void) form;
  865. (void) arg;
  866. (void) append;
  867. return CURL_FORMADD_DISABLED;
  868. }
  869. void curl_formfree(struct curl_httppost *form)
  870. {
  871. (void)form;
  872. /* Nothing to do. */
  873. }
  874. #endif /* if disabled */