formdata.c 46 KB

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