formdata.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, 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 http://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. #if !defined(CURL_DISABLE_HTTP) || defined(USE_SSLEAY)
  25. #if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
  26. #include <libgen.h>
  27. #endif
  28. #include "urldata.h" /* for struct SessionHandle */
  29. #include "formdata.h"
  30. #include "sslgen.h"
  31. #include "strequal.h"
  32. #include "curl_memory.h"
  33. #include "sendf.h"
  34. #define _MPRINTF_REPLACE /* use our functions only */
  35. #include <curl/mprintf.h>
  36. /* The last #include file should be: */
  37. #include "memdebug.h"
  38. #endif /* !defined(CURL_DISABLE_HTTP) || defined(USE_SSLEAY) */
  39. #ifndef CURL_DISABLE_HTTP
  40. #ifndef HAVE_BASENAME
  41. static char *Curl_basename(char *path);
  42. #define basename(x) Curl_basename((x))
  43. #endif
  44. static size_t readfromfile(struct Form *form, char *buffer, size_t size);
  45. static char *formboundary(struct SessionHandle *data);
  46. /* What kind of Content-Type to use on un-specified files with unrecognized
  47. extensions. */
  48. #define HTTPPOST_CONTENTTYPE_DEFAULT "application/octet-stream"
  49. #define FORM_FILE_SEPARATOR ','
  50. #define FORM_TYPE_SEPARATOR ';'
  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, size_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->contentslength = (long)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;
  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(strequal(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. * memdup()
  193. *
  194. * Copies the 'source' data to a newly allocated buffer buffer (that is
  195. * returned). Uses buffer_length if not null, else uses strlen to determine
  196. * the length of the buffer to be copied
  197. *
  198. * Returns the new pointer or NULL on failure.
  199. *
  200. ***************************************************************************/
  201. static char *memdup(const char *src, size_t buffer_length)
  202. {
  203. size_t length;
  204. bool add = FALSE;
  205. char *buffer;
  206. if(buffer_length)
  207. length = buffer_length;
  208. else if(src) {
  209. length = strlen(src);
  210. add = TRUE;
  211. }
  212. else
  213. /* no length and a NULL src pointer! */
  214. return strdup("");
  215. buffer = malloc(length+add);
  216. if(!buffer)
  217. return NULL; /* fail */
  218. memcpy(buffer, src, length);
  219. /* if length unknown do null termination */
  220. if(add)
  221. buffer[length] = '\0';
  222. return buffer;
  223. }
  224. /***************************************************************************
  225. *
  226. * FormAdd()
  227. *
  228. * Stores a formpost parameter and builds the appropriate linked list.
  229. *
  230. * Has two principal functionalities: using files and byte arrays as
  231. * post parts. Byte arrays are either copied or just the pointer is stored
  232. * (as the user requests) while for files only the filename and not the
  233. * content is stored.
  234. *
  235. * While you may have only one byte array for each name, multiple filenames
  236. * are allowed (and because of this feature CURLFORM_END is needed after
  237. * using CURLFORM_FILE).
  238. *
  239. * Examples:
  240. *
  241. * Simple name/value pair with copied contents:
  242. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  243. * CURLFORM_COPYCONTENTS, "value", CURLFORM_END);
  244. *
  245. * name/value pair where only the content pointer is remembered:
  246. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  247. * CURLFORM_PTRCONTENTS, ptr, CURLFORM_CONTENTSLENGTH, 10, CURLFORM_END);
  248. * (if CURLFORM_CONTENTSLENGTH is missing strlen () is used)
  249. *
  250. * storing a filename (CONTENTTYPE is optional!):
  251. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  252. * CURLFORM_FILE, "filename1", CURLFORM_CONTENTTYPE, "plain/text",
  253. * CURLFORM_END);
  254. *
  255. * storing multiple filenames:
  256. * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
  257. * CURLFORM_FILE, "filename1", CURLFORM_FILE, "filename2", CURLFORM_END);
  258. *
  259. * Returns:
  260. * CURL_FORMADD_OK on success
  261. * CURL_FORMADD_MEMORY if the FormInfo allocation fails
  262. * CURL_FORMADD_OPTION_TWICE if one option is given twice for one Form
  263. * CURL_FORMADD_NULL if a null pointer was given for a char
  264. * CURL_FORMADD_MEMORY if the allocation of a FormInfo struct failed
  265. * CURL_FORMADD_UNKNOWN_OPTION if an unknown option was used
  266. * CURL_FORMADD_INCOMPLETE if the some FormInfo is not complete (or error)
  267. * CURL_FORMADD_MEMORY if a HttpPost struct cannot be allocated
  268. * CURL_FORMADD_MEMORY if some allocation for string copying failed.
  269. * CURL_FORMADD_ILLEGAL_ARRAY if an illegal option is used in an array
  270. *
  271. ***************************************************************************/
  272. static
  273. CURLFORMcode FormAdd(struct curl_httppost **httppost,
  274. struct curl_httppost **last_post,
  275. va_list params)
  276. {
  277. FormInfo *first_form, *current_form, *form = NULL;
  278. CURLFORMcode return_value = CURL_FORMADD_OK;
  279. const char *prevtype = NULL;
  280. struct curl_httppost *post = NULL;
  281. CURLformoption option;
  282. struct curl_forms *forms = NULL;
  283. char *array_value=NULL; /* value read from an array */
  284. /* This is a state variable, that if TRUE means that we're parsing an
  285. array that we got passed to us. If FALSE we're parsing the input
  286. va_list arguments. */
  287. bool array_state = FALSE;
  288. /*
  289. * We need to allocate the first struct to fill in.
  290. */
  291. first_form = calloc(1, sizeof(struct FormInfo));
  292. if(!first_form)
  293. return CURL_FORMADD_MEMORY;
  294. current_form = first_form;
  295. /*
  296. * Loop through all the options set. Break if we have an error to report.
  297. */
  298. while(return_value == CURL_FORMADD_OK) {
  299. /* first see if we have more parts of the array param */
  300. if(array_state && forms) {
  301. /* get the upcoming option from the given array */
  302. option = forms->option;
  303. array_value = (char *)forms->value;
  304. forms++; /* advance this to next entry */
  305. if(CURLFORM_END == option) {
  306. /* end of array state */
  307. array_state = FALSE;
  308. continue;
  309. }
  310. }
  311. else {
  312. /* This is not array-state, get next option */
  313. option = va_arg(params, CURLformoption);
  314. if(CURLFORM_END == option)
  315. break;
  316. }
  317. switch (option) {
  318. case CURLFORM_ARRAY:
  319. if(array_state)
  320. /* we don't support an array from within an array */
  321. return_value = CURL_FORMADD_ILLEGAL_ARRAY;
  322. else {
  323. forms = va_arg(params, struct curl_forms *);
  324. if(forms)
  325. array_state = TRUE;
  326. else
  327. return_value = CURL_FORMADD_NULL;
  328. }
  329. break;
  330. /*
  331. * Set the Name property.
  332. */
  333. case CURLFORM_PTRNAME:
  334. #ifdef CURL_DOES_CONVERSIONS
  335. /* Treat CURLFORM_PTR like CURLFORM_COPYNAME so that libcurl will copy
  336. * the data in all cases so that we'll have safe memory for the eventual
  337. * conversion.
  338. */
  339. #else
  340. current_form->flags |= HTTPPOST_PTRNAME; /* fall through */
  341. #endif
  342. case CURLFORM_COPYNAME:
  343. if(current_form->name)
  344. return_value = CURL_FORMADD_OPTION_TWICE;
  345. else {
  346. char *name = array_state?
  347. array_value:va_arg(params, char *);
  348. if(name)
  349. current_form->name = name; /* store for the moment */
  350. else
  351. return_value = CURL_FORMADD_NULL;
  352. }
  353. break;
  354. case CURLFORM_NAMELENGTH:
  355. if(current_form->namelength)
  356. return_value = CURL_FORMADD_OPTION_TWICE;
  357. else
  358. current_form->namelength =
  359. array_state?(size_t)array_value:(size_t)va_arg(params, long);
  360. break;
  361. /*
  362. * Set the contents property.
  363. */
  364. case CURLFORM_PTRCONTENTS:
  365. current_form->flags |= HTTPPOST_PTRCONTENTS; /* fall through */
  366. case CURLFORM_COPYCONTENTS:
  367. if(current_form->value)
  368. return_value = CURL_FORMADD_OPTION_TWICE;
  369. else {
  370. char *value =
  371. array_state?array_value:va_arg(params, char *);
  372. if(value)
  373. current_form->value = value; /* store for the moment */
  374. else
  375. return_value = CURL_FORMADD_NULL;
  376. }
  377. break;
  378. case CURLFORM_CONTENTSLENGTH:
  379. if(current_form->contentslength)
  380. return_value = CURL_FORMADD_OPTION_TWICE;
  381. else
  382. current_form->contentslength =
  383. array_state?(size_t)array_value:(size_t)va_arg(params, long);
  384. break;
  385. /* Get contents from a given file name */
  386. case CURLFORM_FILECONTENT:
  387. if(current_form->flags & (HTTPPOST_PTRCONTENTS|HTTPPOST_READFILE))
  388. return_value = CURL_FORMADD_OPTION_TWICE;
  389. else {
  390. const char *filename = array_state?
  391. array_value:va_arg(params, char *);
  392. if(filename) {
  393. current_form->value = strdup(filename);
  394. if(!current_form->value)
  395. return_value = CURL_FORMADD_MEMORY;
  396. else {
  397. current_form->flags |= HTTPPOST_READFILE;
  398. current_form->value_alloc = TRUE;
  399. }
  400. }
  401. else
  402. return_value = CURL_FORMADD_NULL;
  403. }
  404. break;
  405. /* We upload a file */
  406. case CURLFORM_FILE:
  407. {
  408. const char *filename = array_state?array_value:
  409. va_arg(params, char *);
  410. if(current_form->value) {
  411. if(current_form->flags & HTTPPOST_FILENAME) {
  412. if(filename) {
  413. char *fname = strdup(filename);
  414. if(!fname)
  415. return_value = CURL_FORMADD_MEMORY;
  416. else {
  417. form = AddFormInfo(fname, NULL, current_form);
  418. if(!form) {
  419. Curl_safefree(fname);
  420. return_value = CURL_FORMADD_MEMORY;
  421. }
  422. else {
  423. form->value_alloc = TRUE;
  424. current_form = form;
  425. form = NULL;
  426. }
  427. }
  428. }
  429. else
  430. return_value = CURL_FORMADD_NULL;
  431. }
  432. else
  433. return_value = CURL_FORMADD_OPTION_TWICE;
  434. }
  435. else {
  436. if(filename) {
  437. current_form->value = strdup(filename);
  438. if(!current_form->value)
  439. return_value = CURL_FORMADD_MEMORY;
  440. else {
  441. current_form->flags |= HTTPPOST_FILENAME;
  442. current_form->value_alloc = TRUE;
  443. }
  444. }
  445. else
  446. return_value = CURL_FORMADD_NULL;
  447. }
  448. break;
  449. }
  450. case CURLFORM_BUFFERPTR:
  451. current_form->flags |= HTTPPOST_PTRBUFFER|HTTPPOST_BUFFER;
  452. if(current_form->buffer)
  453. return_value = CURL_FORMADD_OPTION_TWICE;
  454. else {
  455. char *buffer =
  456. array_state?array_value:va_arg(params, char *);
  457. if(buffer) {
  458. current_form->buffer = buffer; /* store for the moment */
  459. current_form->value = buffer; /* make it non-NULL to be accepted
  460. as fine */
  461. }
  462. else
  463. return_value = CURL_FORMADD_NULL;
  464. }
  465. break;
  466. case CURLFORM_BUFFERLENGTH:
  467. if(current_form->bufferlength)
  468. return_value = CURL_FORMADD_OPTION_TWICE;
  469. else
  470. current_form->bufferlength =
  471. array_state?(size_t)array_value:(size_t)va_arg(params, long);
  472. break;
  473. case CURLFORM_STREAM:
  474. current_form->flags |= HTTPPOST_CALLBACK;
  475. if(current_form->userp)
  476. return_value = CURL_FORMADD_OPTION_TWICE;
  477. else {
  478. char *userp =
  479. array_state?array_value:va_arg(params, char *);
  480. if(userp) {
  481. current_form->userp = userp;
  482. current_form->value = userp; /* this isn't strictly true but we
  483. derive a value from this later on
  484. and we need this non-NULL to be
  485. accepted as a fine form part */
  486. }
  487. else
  488. return_value = CURL_FORMADD_NULL;
  489. }
  490. break;
  491. case CURLFORM_CONTENTTYPE:
  492. {
  493. const char *contenttype =
  494. array_state?array_value:va_arg(params, char *);
  495. if(current_form->contenttype) {
  496. if(current_form->flags & HTTPPOST_FILENAME) {
  497. if(contenttype) {
  498. char *type = strdup(contenttype);
  499. if(!type)
  500. return_value = CURL_FORMADD_MEMORY;
  501. else {
  502. form = AddFormInfo(NULL, type, current_form);
  503. if(!form) {
  504. Curl_safefree(type);
  505. return_value = CURL_FORMADD_MEMORY;
  506. }
  507. else {
  508. form->contenttype_alloc = TRUE;
  509. current_form = form;
  510. form = NULL;
  511. }
  512. }
  513. }
  514. else
  515. return_value = CURL_FORMADD_NULL;
  516. }
  517. else
  518. return_value = CURL_FORMADD_OPTION_TWICE;
  519. }
  520. else {
  521. if(contenttype) {
  522. current_form->contenttype = strdup(contenttype);
  523. if(!current_form->contenttype)
  524. return_value = CURL_FORMADD_MEMORY;
  525. else
  526. current_form->contenttype_alloc = TRUE;
  527. }
  528. else
  529. return_value = CURL_FORMADD_NULL;
  530. }
  531. break;
  532. }
  533. case CURLFORM_CONTENTHEADER:
  534. {
  535. /* this "cast increases required alignment of target type" but
  536. we consider it OK anyway */
  537. struct curl_slist* list = array_state?
  538. (struct curl_slist*)array_value:
  539. va_arg(params, struct curl_slist*);
  540. if(current_form->contentheader)
  541. return_value = CURL_FORMADD_OPTION_TWICE;
  542. else
  543. current_form->contentheader = list;
  544. break;
  545. }
  546. case CURLFORM_FILENAME:
  547. case CURLFORM_BUFFER:
  548. {
  549. const char *filename = array_state?array_value:
  550. va_arg(params, char *);
  551. if(current_form->showfilename)
  552. return_value = CURL_FORMADD_OPTION_TWICE;
  553. else {
  554. current_form->showfilename = strdup(filename);
  555. if(!current_form->showfilename)
  556. return_value = CURL_FORMADD_MEMORY;
  557. else
  558. current_form->showfilename_alloc = TRUE;
  559. }
  560. break;
  561. }
  562. default:
  563. return_value = CURL_FORMADD_UNKNOWN_OPTION;
  564. break;
  565. }
  566. }
  567. if(CURL_FORMADD_OK != return_value) {
  568. /* On error, free allocated fields for all nodes of the FormInfo linked
  569. list without deallocating nodes. List nodes are deallocated later on */
  570. FormInfo *ptr;
  571. for(ptr = first_form; ptr != NULL; ptr = ptr->more) {
  572. if(ptr->name_alloc) {
  573. Curl_safefree(ptr->name);
  574. ptr->name_alloc = FALSE;
  575. }
  576. if(ptr->value_alloc) {
  577. Curl_safefree(ptr->value);
  578. ptr->value_alloc = FALSE;
  579. }
  580. if(ptr->contenttype_alloc) {
  581. Curl_safefree(ptr->contenttype);
  582. ptr->contenttype_alloc = FALSE;
  583. }
  584. if(ptr->showfilename_alloc) {
  585. Curl_safefree(ptr->showfilename);
  586. ptr->showfilename_alloc = FALSE;
  587. }
  588. }
  589. }
  590. if(CURL_FORMADD_OK == return_value) {
  591. /* go through the list, check for completeness and if everything is
  592. * alright add the HttpPost item otherwise set return_value accordingly */
  593. post = NULL;
  594. for(form = first_form;
  595. form != NULL;
  596. form = form->more) {
  597. if(((!form->name || !form->value) && !post) ||
  598. ( (form->contentslength) &&
  599. (form->flags & HTTPPOST_FILENAME) ) ||
  600. ( (form->flags & HTTPPOST_FILENAME) &&
  601. (form->flags & HTTPPOST_PTRCONTENTS) ) ||
  602. ( (!form->buffer) &&
  603. (form->flags & HTTPPOST_BUFFER) &&
  604. (form->flags & HTTPPOST_PTRBUFFER) ) ||
  605. ( (form->flags & HTTPPOST_READFILE) &&
  606. (form->flags & HTTPPOST_PTRCONTENTS) )
  607. ) {
  608. return_value = CURL_FORMADD_INCOMPLETE;
  609. break;
  610. }
  611. else {
  612. if(((form->flags & HTTPPOST_FILENAME) ||
  613. (form->flags & HTTPPOST_BUFFER)) &&
  614. !form->contenttype ) {
  615. char *f = form->flags & HTTPPOST_BUFFER?
  616. form->showfilename : form->value;
  617. /* our contenttype is missing */
  618. form->contenttype = strdup(ContentTypeForFilename(f, prevtype));
  619. if(!form->contenttype) {
  620. return_value = CURL_FORMADD_MEMORY;
  621. break;
  622. }
  623. form->contenttype_alloc = TRUE;
  624. }
  625. if(!(form->flags & HTTPPOST_PTRNAME) &&
  626. (form == first_form) ) {
  627. /* Note that there's small risk that form->name is NULL here if the
  628. app passed in a bad combo, so we better check for that first. */
  629. if(form->name)
  630. /* copy name (without strdup; possibly contains null characters) */
  631. form->name = memdup(form->name, form->namelength);
  632. if(!form->name) {
  633. return_value = CURL_FORMADD_MEMORY;
  634. break;
  635. }
  636. form->name_alloc = TRUE;
  637. }
  638. if(!(form->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE |
  639. HTTPPOST_PTRCONTENTS | HTTPPOST_PTRBUFFER |
  640. HTTPPOST_CALLBACK)) ) {
  641. /* copy value (without strdup; possibly contains null characters) */
  642. form->value = memdup(form->value, form->contentslength);
  643. if(!form->value) {
  644. return_value = CURL_FORMADD_MEMORY;
  645. break;
  646. }
  647. form->value_alloc = TRUE;
  648. }
  649. post = AddHttpPost(form->name, form->namelength,
  650. form->value, form->contentslength,
  651. form->buffer, form->bufferlength,
  652. form->contenttype, form->flags,
  653. form->contentheader, form->showfilename,
  654. form->userp,
  655. post, httppost,
  656. last_post);
  657. if(!post) {
  658. return_value = CURL_FORMADD_MEMORY;
  659. break;
  660. }
  661. if(form->contenttype)
  662. prevtype = form->contenttype;
  663. }
  664. }
  665. if(CURL_FORMADD_OK != return_value) {
  666. /* On error, free allocated fields for nodes of the FormInfo linked
  667. list which are not already owned by the httppost linked list
  668. without deallocating nodes. List nodes are deallocated later on */
  669. FormInfo *ptr;
  670. for(ptr = form; ptr != NULL; ptr = ptr->more) {
  671. if(ptr->name_alloc) {
  672. Curl_safefree(ptr->name);
  673. ptr->name_alloc = FALSE;
  674. }
  675. if(ptr->value_alloc) {
  676. Curl_safefree(ptr->value);
  677. ptr->value_alloc = FALSE;
  678. }
  679. if(ptr->contenttype_alloc) {
  680. Curl_safefree(ptr->contenttype);
  681. ptr->contenttype_alloc = FALSE;
  682. }
  683. if(ptr->showfilename_alloc) {
  684. Curl_safefree(ptr->showfilename);
  685. ptr->showfilename_alloc = FALSE;
  686. }
  687. }
  688. }
  689. }
  690. /* Always deallocate FormInfo linked list nodes without touching node
  691. fields given that these have either been deallocated or are owned
  692. now by the httppost linked list */
  693. while(first_form) {
  694. FormInfo *ptr = first_form->more;
  695. Curl_safefree(first_form);
  696. first_form = ptr;
  697. }
  698. return return_value;
  699. }
  700. /*
  701. * curl_formadd() is a public API to add a section to the multipart formpost.
  702. *
  703. * @unittest: 1308
  704. */
  705. CURLFORMcode curl_formadd(struct curl_httppost **httppost,
  706. struct curl_httppost **last_post,
  707. ...)
  708. {
  709. va_list arg;
  710. CURLFORMcode result;
  711. va_start(arg, last_post);
  712. result = FormAdd(httppost, last_post, arg);
  713. va_end(arg);
  714. return result;
  715. }
  716. #ifdef __VMS
  717. #include <fabdef.h>
  718. /*
  719. * get_vms_file_size does what it takes to get the real size of the file
  720. *
  721. * For fixed files, find out the size of the EOF block and adjust.
  722. *
  723. * For all others, have to read the entire file in, discarding the contents.
  724. * Most posted text files will be small, and binary files like zlib archives
  725. * and CD/DVD images should be either a STREAM_LF format or a fixed format.
  726. *
  727. */
  728. curl_off_t VmsRealFileSize(const char * name,
  729. const struct_stat * stat_buf)
  730. {
  731. char buffer[8192];
  732. curl_off_t count;
  733. int ret_stat;
  734. FILE * file;
  735. file = fopen(name, "r");
  736. if(file == NULL)
  737. return 0;
  738. count = 0;
  739. ret_stat = 1;
  740. while(ret_stat > 0) {
  741. ret_stat = fread(buffer, 1, sizeof(buffer), file);
  742. if(ret_stat != 0)
  743. count += ret_stat;
  744. }
  745. fclose(file);
  746. return count;
  747. }
  748. /*
  749. *
  750. * VmsSpecialSize checks to see if the stat st_size can be trusted and
  751. * if not to call a routine to get the correct size.
  752. *
  753. */
  754. static curl_off_t VmsSpecialSize(const char * name,
  755. const struct_stat * stat_buf)
  756. {
  757. switch(stat_buf->st_fab_rfm) {
  758. case FAB$C_VAR:
  759. case FAB$C_VFC:
  760. return VmsRealFileSize(name, stat_buf);
  761. break;
  762. default:
  763. return stat_buf->st_size;
  764. }
  765. }
  766. #endif
  767. #ifndef __VMS
  768. #define filesize(name, stat_data) (stat_data.st_size)
  769. #else
  770. /* Getting the expected file size needs help on VMS */
  771. #define filesize(name, stat_data) VmsSpecialSize(name, &stat_data)
  772. #endif
  773. /*
  774. * AddFormData() adds a chunk of data to the FormData linked list.
  775. *
  776. * size is incremented by the chunk length, unless it is NULL
  777. */
  778. static CURLcode AddFormData(struct FormData **formp,
  779. enum formtype type,
  780. const void *line,
  781. size_t length,
  782. curl_off_t *size)
  783. {
  784. struct FormData *newform = malloc(sizeof(struct FormData));
  785. if(!newform)
  786. return CURLE_OUT_OF_MEMORY;
  787. newform->next = NULL;
  788. if(type <= FORM_CONTENT) {
  789. /* we make it easier for plain strings: */
  790. if(!length)
  791. length = strlen((char *)line);
  792. newform->line = malloc(length+1);
  793. if(!newform->line) {
  794. free(newform);
  795. return CURLE_OUT_OF_MEMORY;
  796. }
  797. memcpy(newform->line, line, length);
  798. newform->length = length;
  799. newform->line[length]=0; /* zero terminate for easier debugging */
  800. }
  801. else
  802. /* For callbacks and files we don't have any actual data so we just keep a
  803. pointer to whatever this points to */
  804. newform->line = (char *)line;
  805. newform->type = type;
  806. if(*formp) {
  807. (*formp)->next = newform;
  808. *formp = newform;
  809. }
  810. else
  811. *formp = newform;
  812. if(size) {
  813. if(type != FORM_FILE)
  814. /* for static content as well as callback data we add the size given
  815. as input argument */
  816. *size += length;
  817. else {
  818. /* Since this is a file to be uploaded here, add the size of the actual
  819. file */
  820. if(!strequal("-", newform->line)) {
  821. struct_stat file;
  822. if(!stat(newform->line, &file) && !S_ISDIR(file.st_mode))
  823. *size += filesize(newform->line, file);
  824. else
  825. return CURLE_BAD_FUNCTION_ARGUMENT;
  826. }
  827. }
  828. }
  829. return CURLE_OK;
  830. }
  831. /*
  832. * AddFormDataf() adds printf()-style formatted data to the formdata chain.
  833. */
  834. static CURLcode AddFormDataf(struct FormData **formp,
  835. curl_off_t *size,
  836. const char *fmt, ...)
  837. {
  838. char s[4096];
  839. va_list ap;
  840. va_start(ap, fmt);
  841. vsnprintf(s, sizeof(s), fmt, ap);
  842. va_end(ap);
  843. return AddFormData(formp, FORM_DATA, s, 0, size);
  844. }
  845. /*
  846. * Curl_formclean() is used from http.c, this cleans a built FormData linked
  847. * list
  848. */
  849. void Curl_formclean(struct FormData **form_ptr)
  850. {
  851. struct FormData *next, *form;
  852. form = *form_ptr;
  853. if(!form)
  854. return;
  855. do {
  856. next=form->next; /* the following form line */
  857. if(form->type <= FORM_CONTENT)
  858. free(form->line); /* free the line */
  859. free(form); /* free the struct */
  860. } while((form = next) != NULL); /* continue */
  861. *form_ptr = NULL;
  862. }
  863. /*
  864. * curl_formget()
  865. * Serialize a curl_httppost struct.
  866. * Returns 0 on success.
  867. *
  868. * @unittest: 1308
  869. */
  870. int curl_formget(struct curl_httppost *form, void *arg,
  871. curl_formget_callback append)
  872. {
  873. CURLcode rc;
  874. curl_off_t size;
  875. struct FormData *data, *ptr;
  876. rc = Curl_getformdata(NULL, &data, form, NULL, &size);
  877. if(rc != CURLE_OK)
  878. return (int)rc;
  879. for(ptr = data; ptr; ptr = ptr->next) {
  880. if((ptr->type == FORM_FILE) || (ptr->type == FORM_CALLBACK)) {
  881. char buffer[8192];
  882. size_t nread;
  883. struct Form temp;
  884. Curl_FormInit(&temp, ptr);
  885. do {
  886. nread = readfromfile(&temp, buffer, sizeof(buffer));
  887. if((nread == (size_t) -1) ||
  888. (nread > sizeof(buffer)) ||
  889. (nread != append(arg, buffer, nread))) {
  890. if(temp.fp)
  891. fclose(temp.fp);
  892. Curl_formclean(&data);
  893. return -1;
  894. }
  895. } while(nread);
  896. }
  897. else {
  898. if(ptr->length != append(arg, ptr->line, ptr->length)) {
  899. Curl_formclean(&data);
  900. return -1;
  901. }
  902. }
  903. }
  904. Curl_formclean(&data);
  905. return 0;
  906. }
  907. /*
  908. * curl_formfree() is an external function to free up a whole form post
  909. * chain
  910. */
  911. void curl_formfree(struct curl_httppost *form)
  912. {
  913. struct curl_httppost *next;
  914. if(!form)
  915. /* no form to free, just get out of this */
  916. return;
  917. do {
  918. next=form->next; /* the following form line */
  919. /* recurse to sub-contents */
  920. if(form->more)
  921. curl_formfree(form->more);
  922. if(!(form->flags & HTTPPOST_PTRNAME) && form->name)
  923. free(form->name); /* free the name */
  924. if(!(form->flags &
  925. (HTTPPOST_PTRCONTENTS|HTTPPOST_BUFFER|HTTPPOST_CALLBACK)) &&
  926. form->contents)
  927. free(form->contents); /* free the contents */
  928. if(form->contenttype)
  929. free(form->contenttype); /* free the content type */
  930. if(form->showfilename)
  931. free(form->showfilename); /* free the faked file name */
  932. free(form); /* free the struct */
  933. } while((form = next) != NULL); /* continue */
  934. }
  935. #ifndef HAVE_BASENAME
  936. /*
  937. (Quote from The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004
  938. Edition)
  939. The basename() function shall take the pathname pointed to by path and
  940. return a pointer to the final component of the pathname, deleting any
  941. trailing '/' characters.
  942. If the string pointed to by path consists entirely of the '/' character,
  943. basename() shall return a pointer to the string "/". If the string pointed
  944. to by path is exactly "//", it is implementation-defined whether '/' or "//"
  945. is returned.
  946. If path is a null pointer or points to an empty string, basename() shall
  947. return a pointer to the string ".".
  948. The basename() function may modify the string pointed to by path, and may
  949. return a pointer to static storage that may then be overwritten by a
  950. subsequent call to basename().
  951. The basename() function need not be reentrant. A function that is not
  952. required to be reentrant is not required to be thread-safe.
  953. */
  954. static char *Curl_basename(char *path)
  955. {
  956. /* Ignore all the details above for now and make a quick and simple
  957. implementaion here */
  958. char *s1;
  959. char *s2;
  960. s1=strrchr(path, '/');
  961. s2=strrchr(path, '\\');
  962. if(s1 && s2) {
  963. path = (s1 > s2? s1 : s2)+1;
  964. }
  965. else if(s1)
  966. path = s1 + 1;
  967. else if(s2)
  968. path = s2 + 1;
  969. return path;
  970. }
  971. #endif
  972. static char *strippath(const char *fullfile)
  973. {
  974. char *filename;
  975. char *base;
  976. filename = strdup(fullfile); /* duplicate since basename() may ruin the
  977. buffer it works on */
  978. if(!filename)
  979. return NULL;
  980. base = strdup(basename(filename));
  981. free(filename); /* free temporary buffer */
  982. return base; /* returns an allocated string or NULL ! */
  983. }
  984. static CURLcode formdata_add_filename(const struct curl_httppost *file,
  985. struct FormData **form,
  986. curl_off_t *size)
  987. {
  988. CURLcode result = CURLE_OK;
  989. char *filename = file->showfilename;
  990. char *filebasename = NULL;
  991. char *filename_escaped = NULL;
  992. if(!filename) {
  993. filebasename = strippath(file->contents);
  994. if(!filebasename)
  995. return CURLE_OUT_OF_MEMORY;
  996. filename = filebasename;
  997. }
  998. if(strchr(filename, '\\') || strchr(filename, '"')) {
  999. char *p0, *p1;
  1000. /* filename need be escaped */
  1001. filename_escaped = malloc(strlen(filename)*2+1);
  1002. if(!filename_escaped)
  1003. return CURLE_OUT_OF_MEMORY;
  1004. p0 = filename_escaped;
  1005. p1 = filename;
  1006. while(*p1) {
  1007. if(*p1 == '\\' || *p1 == '"')
  1008. *p0++ = '\\';
  1009. *p0++ = *p1++;
  1010. }
  1011. *p0 = '\0';
  1012. filename = filename_escaped;
  1013. }
  1014. result = AddFormDataf(form, size,
  1015. "; filename=\"%s\"",
  1016. filename);
  1017. Curl_safefree(filename_escaped);
  1018. Curl_safefree(filebasename);
  1019. return result;
  1020. }
  1021. /*
  1022. * Curl_getformdata() converts a linked list of "meta data" into a complete
  1023. * (possibly huge) multipart formdata. The input list is in 'post', while the
  1024. * output resulting linked lists gets stored in '*finalform'. *sizep will get
  1025. * the total size of the whole POST.
  1026. * A multipart/form_data content-type is built, unless a custom content-type
  1027. * is passed in 'custom_content_type'.
  1028. *
  1029. * This function will not do a failf() for the potential memory failures but
  1030. * should for all other errors it spots. Just note that this function MAY get
  1031. * a NULL pointer in the 'data' argument.
  1032. */
  1033. CURLcode Curl_getformdata(struct SessionHandle *data,
  1034. struct FormData **finalform,
  1035. struct curl_httppost *post,
  1036. const char *custom_content_type,
  1037. curl_off_t *sizep)
  1038. {
  1039. struct FormData *form = NULL;
  1040. struct FormData *firstform;
  1041. struct curl_httppost *file;
  1042. CURLcode result = CURLE_OK;
  1043. curl_off_t size = 0; /* support potentially ENORMOUS formposts */
  1044. char *boundary;
  1045. char *fileboundary = NULL;
  1046. struct curl_slist* curList;
  1047. *finalform = NULL; /* default form is empty */
  1048. if(!post)
  1049. return result; /* no input => no output! */
  1050. boundary = formboundary(data);
  1051. if(!boundary)
  1052. return CURLE_OUT_OF_MEMORY;
  1053. /* Make the first line of the output */
  1054. result = AddFormDataf(&form, NULL,
  1055. "%s; boundary=%s\r\n",
  1056. custom_content_type?custom_content_type:
  1057. "Content-Type: multipart/form-data",
  1058. boundary);
  1059. if(result) {
  1060. Curl_safefree(boundary);
  1061. return result;
  1062. }
  1063. /* we DO NOT include that line in the total size of the POST, since it'll be
  1064. part of the header! */
  1065. firstform = form;
  1066. do {
  1067. if(size) {
  1068. result = AddFormDataf(&form, &size, "\r\n");
  1069. if(result)
  1070. break;
  1071. }
  1072. /* boundary */
  1073. result = AddFormDataf(&form, &size, "--%s\r\n", boundary);
  1074. if(result)
  1075. break;
  1076. /* Maybe later this should be disabled when a custom_content_type is
  1077. passed, since Content-Disposition is not meaningful for all multipart
  1078. types.
  1079. */
  1080. result = AddFormDataf(&form, &size,
  1081. "Content-Disposition: form-data; name=\"");
  1082. if(result)
  1083. break;
  1084. result = AddFormData(&form, FORM_DATA, post->name, post->namelength,
  1085. &size);
  1086. if(result)
  1087. break;
  1088. result = AddFormDataf(&form, &size, "\"");
  1089. if(result)
  1090. break;
  1091. if(post->more) {
  1092. /* If used, this is a link to more file names, we must then do
  1093. the magic to include several files with the same field name */
  1094. Curl_safefree(fileboundary);
  1095. fileboundary = formboundary(data);
  1096. if(!fileboundary) {
  1097. result = CURLE_OUT_OF_MEMORY;
  1098. break;
  1099. }
  1100. result = AddFormDataf(&form, &size,
  1101. "\r\nContent-Type: multipart/mixed,"
  1102. " boundary=%s\r\n",
  1103. fileboundary);
  1104. if(result)
  1105. break;
  1106. }
  1107. file = post;
  1108. do {
  1109. /* If 'showfilename' is set, that is a faked name passed on to us
  1110. to use to in the formpost. If that is not set, the actually used
  1111. local file name should be added. */
  1112. if(post->more) {
  1113. /* if multiple-file */
  1114. result = AddFormDataf(&form, &size,
  1115. "\r\n--%s\r\nContent-Disposition: "
  1116. "attachment",
  1117. fileboundary);
  1118. if(result)
  1119. break;
  1120. result = formdata_add_filename(file, &form, &size);
  1121. if(result)
  1122. break;
  1123. }
  1124. else if(post->flags & (HTTPPOST_FILENAME|HTTPPOST_BUFFER|
  1125. HTTPPOST_CALLBACK)) {
  1126. /* it should be noted that for the HTTPPOST_FILENAME and
  1127. HTTPPOST_CALLBACK cases the ->showfilename struct member is always
  1128. assigned at this point */
  1129. if(post->showfilename || (post->flags & HTTPPOST_FILENAME)) {
  1130. result = formdata_add_filename(post, &form, &size);
  1131. }
  1132. if(result)
  1133. break;
  1134. }
  1135. if(file->contenttype) {
  1136. /* we have a specified type */
  1137. result = AddFormDataf(&form, &size,
  1138. "\r\nContent-Type: %s",
  1139. file->contenttype);
  1140. if(result)
  1141. break;
  1142. }
  1143. curList = file->contentheader;
  1144. while(curList) {
  1145. /* Process the additional headers specified for this form */
  1146. result = AddFormDataf( &form, &size, "\r\n%s", curList->data );
  1147. if(result)
  1148. break;
  1149. curList = curList->next;
  1150. }
  1151. if(result)
  1152. break;
  1153. result = AddFormDataf(&form, &size, "\r\n\r\n");
  1154. if(result)
  1155. break;
  1156. if((post->flags & HTTPPOST_FILENAME) ||
  1157. (post->flags & HTTPPOST_READFILE)) {
  1158. /* we should include the contents from the specified file */
  1159. FILE *fileread;
  1160. fileread = strequal("-", file->contents)?
  1161. stdin:fopen(file->contents, "rb"); /* binary read for win32 */
  1162. /*
  1163. * VMS: This only allows for stream files on VMS. Stream files are
  1164. * OK, as are FIXED & VAR files WITHOUT implied CC For implied CC,
  1165. * every record needs to have a \n appended & 1 added to SIZE
  1166. */
  1167. if(fileread) {
  1168. if(fileread != stdin) {
  1169. /* close the file */
  1170. fclose(fileread);
  1171. /* add the file name only - for later reading from this */
  1172. result = AddFormData(&form, FORM_FILE, file->contents, 0, &size);
  1173. }
  1174. else {
  1175. /* When uploading from stdin, we can't know the size of the file,
  1176. * thus must read the full file as before. We *could* use chunked
  1177. * transfer-encoding, but that only works for HTTP 1.1 and we
  1178. * can't be sure we work with such a server.
  1179. */
  1180. size_t nread;
  1181. char buffer[512];
  1182. while((nread = fread(buffer, 1, sizeof(buffer), fileread)) != 0) {
  1183. result = AddFormData(&form, FORM_CONTENT, buffer, nread, &size);
  1184. if(result)
  1185. break;
  1186. }
  1187. }
  1188. }
  1189. else {
  1190. if(data)
  1191. failf(data, "couldn't open file \"%s\"", file->contents);
  1192. *finalform = NULL;
  1193. result = CURLE_READ_ERROR;
  1194. }
  1195. }
  1196. else if(post->flags & HTTPPOST_BUFFER)
  1197. /* include contents of buffer */
  1198. result = AddFormData(&form, FORM_CONTENT, post->buffer,
  1199. post->bufferlength, &size);
  1200. else if(post->flags & HTTPPOST_CALLBACK)
  1201. /* the contents should be read with the callback and the size
  1202. is set with the contentslength */
  1203. result = AddFormData(&form, FORM_CALLBACK, post->userp,
  1204. post->contentslength, &size);
  1205. else
  1206. /* include the contents we got */
  1207. result = AddFormData(&form, FORM_CONTENT, post->contents,
  1208. post->contentslength, &size);
  1209. file = file->more;
  1210. } while(file && !result); /* for each specified file for this field */
  1211. if(result)
  1212. break;
  1213. if(post->more) {
  1214. /* this was a multiple-file inclusion, make a termination file
  1215. boundary: */
  1216. result = AddFormDataf(&form, &size,
  1217. "\r\n--%s--",
  1218. fileboundary);
  1219. if(result)
  1220. break;
  1221. }
  1222. } while((post = post->next) != NULL); /* for each field */
  1223. /* end-boundary for everything */
  1224. if(CURLE_OK == result)
  1225. result = AddFormDataf(&form, &size,
  1226. "\r\n--%s--\r\n",
  1227. boundary);
  1228. if(result) {
  1229. Curl_formclean(&firstform);
  1230. Curl_safefree(fileboundary);
  1231. Curl_safefree(boundary);
  1232. return result;
  1233. }
  1234. *sizep = size;
  1235. Curl_safefree(fileboundary);
  1236. Curl_safefree(boundary);
  1237. *finalform = firstform;
  1238. return result;
  1239. }
  1240. /*
  1241. * Curl_FormInit() inits the struct 'form' points to with the 'formdata'
  1242. * and resets the 'sent' counter.
  1243. */
  1244. int Curl_FormInit(struct Form *form, struct FormData *formdata )
  1245. {
  1246. if(!formdata)
  1247. return 1; /* error */
  1248. form->data = formdata;
  1249. form->sent = 0;
  1250. form->fp = NULL;
  1251. form->fread_func = ZERO_NULL;
  1252. return 0;
  1253. }
  1254. #ifndef __VMS
  1255. # define fopen_read fopen
  1256. #else
  1257. /*
  1258. * vmsfopenread
  1259. *
  1260. * For upload to work as expected on VMS, different optional
  1261. * parameters must be added to the fopen command based on
  1262. * record format of the file.
  1263. *
  1264. */
  1265. # define fopen_read vmsfopenread
  1266. static FILE * vmsfopenread(const char *file, const char *mode) {
  1267. struct_stat statbuf;
  1268. int result;
  1269. result = stat(file, &statbuf);
  1270. switch (statbuf.st_fab_rfm) {
  1271. case FAB$C_VAR:
  1272. case FAB$C_VFC:
  1273. case FAB$C_STMCR:
  1274. return fopen(file, "r");
  1275. break;
  1276. default:
  1277. return fopen(file, "r", "rfm=stmlf", "ctx=stm");
  1278. }
  1279. }
  1280. #endif
  1281. /*
  1282. * readfromfile()
  1283. *
  1284. * The read callback that this function may use can return a value larger than
  1285. * 'size' (which then this function returns) that indicates a problem and it
  1286. * must be properly dealt with
  1287. */
  1288. static size_t readfromfile(struct Form *form, char *buffer,
  1289. size_t size)
  1290. {
  1291. size_t nread;
  1292. bool callback = (form->data->type == FORM_CALLBACK)?TRUE:FALSE;
  1293. if(callback) {
  1294. if(form->fread_func == ZERO_NULL)
  1295. return 0;
  1296. else
  1297. nread = form->fread_func(buffer, 1, size, form->data->line);
  1298. }
  1299. else {
  1300. if(!form->fp) {
  1301. /* this file hasn't yet been opened */
  1302. form->fp = fopen_read(form->data->line, "rb"); /* b is for binary */
  1303. if(!form->fp)
  1304. return (size_t)-1; /* failure */
  1305. }
  1306. nread = fread(buffer, 1, size, form->fp);
  1307. }
  1308. if(!nread) {
  1309. /* this is the last chunk from the file, move on */
  1310. if(form->fp) {
  1311. fclose(form->fp);
  1312. form->fp = NULL;
  1313. }
  1314. form->data = form->data->next;
  1315. }
  1316. return nread;
  1317. }
  1318. /*
  1319. * Curl_FormReader() is the fread() emulation function that will be used to
  1320. * deliver the formdata to the transfer loop and then sent away to the peer.
  1321. */
  1322. size_t Curl_FormReader(char *buffer,
  1323. size_t size,
  1324. size_t nitems,
  1325. FILE *mydata)
  1326. {
  1327. struct Form *form;
  1328. size_t wantedsize;
  1329. size_t gotsize = 0;
  1330. form=(struct Form *)mydata;
  1331. wantedsize = size * nitems;
  1332. if(!form->data)
  1333. return 0; /* nothing, error, empty */
  1334. if((form->data->type == FORM_FILE) ||
  1335. (form->data->type == FORM_CALLBACK)) {
  1336. gotsize = readfromfile(form, buffer, wantedsize);
  1337. if(gotsize)
  1338. /* If positive or -1, return. If zero, continue! */
  1339. return gotsize;
  1340. }
  1341. do {
  1342. if((form->data->length - form->sent ) > wantedsize - gotsize) {
  1343. memcpy(buffer + gotsize , form->data->line + form->sent,
  1344. wantedsize - gotsize);
  1345. form->sent += wantedsize-gotsize;
  1346. return wantedsize;
  1347. }
  1348. memcpy(buffer+gotsize,
  1349. form->data->line + form->sent,
  1350. (form->data->length - form->sent) );
  1351. gotsize += form->data->length - form->sent;
  1352. form->sent = 0;
  1353. form->data = form->data->next; /* advance */
  1354. } while(form->data && (form->data->type < FORM_CALLBACK));
  1355. /* If we got an empty line and we have more data, we proceed to the next
  1356. line immediately to avoid returning zero before we've reached the end. */
  1357. return gotsize;
  1358. }
  1359. /*
  1360. * Curl_formpostheader() returns the first line of the formpost, the
  1361. * request-header part (which is not part of the request-body like the rest of
  1362. * the post).
  1363. */
  1364. char *Curl_formpostheader(void *formp, size_t *len)
  1365. {
  1366. char *header;
  1367. struct Form *form=(struct Form *)formp;
  1368. if(!form->data)
  1369. return 0; /* nothing, ERROR! */
  1370. header = form->data->line;
  1371. *len = form->data->length;
  1372. form->data = form->data->next; /* advance */
  1373. return header;
  1374. }
  1375. /*
  1376. * formboundary() creates a suitable boundary string and returns an allocated
  1377. * one.
  1378. */
  1379. static char *formboundary(struct SessionHandle *data)
  1380. {
  1381. /* 24 dashes and 16 hexadecimal digits makes 64 bit (18446744073709551615)
  1382. combinations */
  1383. return aprintf("------------------------%08x%08x",
  1384. Curl_rand(data), Curl_rand(data));
  1385. }
  1386. #else /* CURL_DISABLE_HTTP */
  1387. CURLFORMcode curl_formadd(struct curl_httppost **httppost,
  1388. struct curl_httppost **last_post,
  1389. ...)
  1390. {
  1391. (void)httppost;
  1392. (void)last_post;
  1393. return CURL_FORMADD_DISABLED;
  1394. }
  1395. int curl_formget(struct curl_httppost *form, void *arg,
  1396. curl_formget_callback append)
  1397. {
  1398. (void) form;
  1399. (void) arg;
  1400. (void) append;
  1401. return CURL_FORMADD_DISABLED;
  1402. }
  1403. void curl_formfree(struct curl_httppost *form)
  1404. {
  1405. (void)form;
  1406. /* does nothing HTTP is disabled */
  1407. }
  1408. #endif /* !defined(CURL_DISABLE_HTTP) */