formdata.c 45 KB

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