formdata.c 49 KB

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