formdata.c 45 KB

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