2
0

formdata.c 31 KB

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