mime.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "mime.h"
  27. #include "warnless.h"
  28. #include "urldata.h"
  29. #include "sendf.h"
  30. #include "strdup.h"
  31. #if !defined(CURL_DISABLE_MIME) && (!defined(CURL_DISABLE_HTTP) || \
  32. !defined(CURL_DISABLE_SMTP) || \
  33. !defined(CURL_DISABLE_IMAP))
  34. #if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
  35. #include <libgen.h>
  36. #endif
  37. #include "rand.h"
  38. #include "slist.h"
  39. #include "strcase.h"
  40. #include "dynbuf.h"
  41. /* The last 3 #include files should be in this order */
  42. #include "curl_printf.h"
  43. #include "curl_memory.h"
  44. #include "memdebug.h"
  45. #ifdef _WIN32
  46. # ifndef R_OK
  47. # define R_OK 4
  48. # endif
  49. #endif
  50. #define READ_ERROR ((size_t) -1)
  51. #define STOP_FILLING ((size_t) -2)
  52. static size_t mime_subparts_read(char *buffer, size_t size, size_t nitems,
  53. void *instream, bool *hasread);
  54. /* Encoders. */
  55. static size_t encoder_nop_read(char *buffer, size_t size, bool ateof,
  56. curl_mimepart *part);
  57. static curl_off_t encoder_nop_size(curl_mimepart *part);
  58. static size_t encoder_7bit_read(char *buffer, size_t size, bool ateof,
  59. curl_mimepart *part);
  60. static size_t encoder_base64_read(char *buffer, size_t size, bool ateof,
  61. curl_mimepart *part);
  62. static curl_off_t encoder_base64_size(curl_mimepart *part);
  63. static size_t encoder_qp_read(char *buffer, size_t size, bool ateof,
  64. curl_mimepart *part);
  65. static curl_off_t encoder_qp_size(curl_mimepart *part);
  66. static curl_off_t mime_size(curl_mimepart *part);
  67. static const struct mime_encoder encoders[] = {
  68. {"binary", encoder_nop_read, encoder_nop_size},
  69. {"8bit", encoder_nop_read, encoder_nop_size},
  70. {"7bit", encoder_7bit_read, encoder_nop_size},
  71. {"base64", encoder_base64_read, encoder_base64_size},
  72. {"quoted-printable", encoder_qp_read, encoder_qp_size},
  73. {ZERO_NULL, ZERO_NULL, ZERO_NULL}
  74. };
  75. /* Base64 encoding table */
  76. static const char base64enc[] =
  77. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  78. /* Quoted-printable character class table.
  79. *
  80. * We cannot rely on ctype functions since quoted-printable input data
  81. * is assumed to be ascii-compatible, even on non-ascii platforms. */
  82. #define QP_OK 1 /* Can be represented by itself. */
  83. #define QP_SP 2 /* Space or tab. */
  84. #define QP_CR 3 /* Carriage return. */
  85. #define QP_LF 4 /* Line-feed. */
  86. static const unsigned char qp_class[] = {
  87. 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 07 */
  88. 0, QP_SP, QP_LF, 0, 0, QP_CR, 0, 0, /* 08 - 0F */
  89. 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 17 */
  90. 0, 0, 0, 0, 0, 0, 0, 0, /* 18 - 1F */
  91. QP_SP, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 20 - 27 */
  92. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 28 - 2F */
  93. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 30 - 37 */
  94. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, 0 , QP_OK, QP_OK, /* 38 - 3F */
  95. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 40 - 47 */
  96. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 48 - 4F */
  97. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 50 - 57 */
  98. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 58 - 5F */
  99. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 60 - 67 */
  100. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 68 - 6F */
  101. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 70 - 77 */
  102. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, 0, /* 78 - 7F */
  103. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
  104. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
  105. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
  106. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
  107. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
  108. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
  109. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
  110. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
  111. };
  112. /* Binary --> hexadecimal ASCII table. */
  113. static const char aschex[] =
  114. "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x41\x42\x43\x44\x45\x46";
  115. #ifndef __VMS
  116. #define filesize(name, stat_data) (stat_data.st_size)
  117. #define fopen_read fopen
  118. #else
  119. #include <fabdef.h>
  120. /*
  121. * get_vms_file_size does what it takes to get the real size of the file
  122. *
  123. * For fixed files, find out the size of the EOF block and adjust.
  124. *
  125. * For all others, have to read the entire file in, discarding the contents.
  126. * Most posted text files will be small, and binary files like zlib archives
  127. * and CD/DVD images should be either a STREAM_LF format or a fixed format.
  128. *
  129. */
  130. curl_off_t VmsRealFileSize(const char *name,
  131. const struct_stat *stat_buf)
  132. {
  133. char buffer[8192];
  134. curl_off_t count;
  135. int ret_stat;
  136. FILE * file;
  137. file = fopen(name, FOPEN_READTEXT); /* VMS */
  138. if(!file)
  139. return 0;
  140. count = 0;
  141. ret_stat = 1;
  142. while(ret_stat > 0) {
  143. ret_stat = fread(buffer, 1, sizeof(buffer), file);
  144. if(ret_stat)
  145. count += ret_stat;
  146. }
  147. fclose(file);
  148. return count;
  149. }
  150. /*
  151. *
  152. * VmsSpecialSize checks to see if the stat st_size can be trusted and
  153. * if not to call a routine to get the correct size.
  154. *
  155. */
  156. static curl_off_t VmsSpecialSize(const char *name,
  157. const struct_stat *stat_buf)
  158. {
  159. switch(stat_buf->st_fab_rfm) {
  160. case FAB$C_VAR:
  161. case FAB$C_VFC:
  162. return VmsRealFileSize(name, stat_buf);
  163. break;
  164. default:
  165. return stat_buf->st_size;
  166. }
  167. }
  168. #define filesize(name, stat_data) VmsSpecialSize(name, &stat_data)
  169. /*
  170. * vmsfopenread
  171. *
  172. * For upload to work as expected on VMS, different optional
  173. * parameters must be added to the fopen command based on
  174. * record format of the file.
  175. *
  176. */
  177. static FILE * vmsfopenread(const char *file, const char *mode)
  178. {
  179. struct_stat statbuf;
  180. int result;
  181. result = stat(file, &statbuf);
  182. switch(statbuf.st_fab_rfm) {
  183. case FAB$C_VAR:
  184. case FAB$C_VFC:
  185. case FAB$C_STMCR:
  186. return fopen(file, FOPEN_READTEXT); /* VMS */
  187. break;
  188. default:
  189. return fopen(file, FOPEN_READTEXT, "rfm=stmlf", "ctx=stm");
  190. }
  191. }
  192. #define fopen_read vmsfopenread
  193. #endif
  194. #ifndef HAVE_BASENAME
  195. /*
  196. (Quote from The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004
  197. Edition)
  198. The basename() function shall take the pathname pointed to by path and
  199. return a pointer to the final component of the pathname, deleting any
  200. trailing '/' characters.
  201. If the string pointed to by path consists entirely of the '/' character,
  202. basename() shall return a pointer to the string "/". If the string pointed
  203. to by path is exactly "//", it is implementation-defined whether '/' or "//"
  204. is returned.
  205. If path is a null pointer or points to an empty string, basename() shall
  206. return a pointer to the string ".".
  207. The basename() function may modify the string pointed to by path, and may
  208. return a pointer to static storage that may then be overwritten by a
  209. subsequent call to basename().
  210. The basename() function need not be reentrant. A function that is not
  211. required to be reentrant is not required to be thread-safe.
  212. */
  213. static char *Curl_basename(char *path)
  214. {
  215. /* Ignore all the details above for now and make a quick and simple
  216. implementation here */
  217. char *s1;
  218. char *s2;
  219. s1 = strrchr(path, '/');
  220. s2 = strrchr(path, '\\');
  221. if(s1 && s2) {
  222. path = (s1 > s2? s1 : s2) + 1;
  223. }
  224. else if(s1)
  225. path = s1 + 1;
  226. else if(s2)
  227. path = s2 + 1;
  228. return path;
  229. }
  230. #define basename(x) Curl_basename((x))
  231. #endif
  232. /* Set readback state. */
  233. static void mimesetstate(struct mime_state *state,
  234. enum mimestate tok, void *ptr)
  235. {
  236. state->state = tok;
  237. state->ptr = ptr;
  238. state->offset = 0;
  239. }
  240. /* Escape header string into allocated memory. */
  241. static char *escape_string(struct Curl_easy *data,
  242. const char *src, enum mimestrategy strategy)
  243. {
  244. CURLcode result;
  245. struct dynbuf db;
  246. const char * const *table;
  247. const char * const *p;
  248. /* replace first character by rest of string. */
  249. static const char * const mimetable[] = {
  250. "\\\\\\",
  251. "\"\\\"",
  252. NULL
  253. };
  254. /* WHATWG HTML living standard 4.10.21.8 2 specifies:
  255. For field names and filenames for file fields, the result of the
  256. encoding in the previous bullet point must be escaped by replacing
  257. any 0x0A (LF) bytes with the byte sequence `%0A`, 0x0D (CR) with `%0D`
  258. and 0x22 (") with `%22`.
  259. The user agent must not perform any other escapes. */
  260. static const char * const formtable[] = {
  261. "\"%22",
  262. "\r%0D",
  263. "\n%0A",
  264. NULL
  265. };
  266. table = formtable;
  267. /* data can be NULL when this function is called indirectly from
  268. curl_formget(). */
  269. if(strategy == MIMESTRATEGY_MAIL || (data && (data->set.mime_formescape)))
  270. table = mimetable;
  271. Curl_dyn_init(&db, CURL_MAX_INPUT_LENGTH);
  272. for(result = Curl_dyn_addn(&db, STRCONST("")); !result && *src; src++) {
  273. for(p = table; *p && **p != *src; p++)
  274. ;
  275. if(*p)
  276. result = Curl_dyn_add(&db, *p + 1);
  277. else
  278. result = Curl_dyn_addn(&db, src, 1);
  279. }
  280. return Curl_dyn_ptr(&db);
  281. }
  282. /* Check if header matches. */
  283. static char *match_header(struct curl_slist *hdr, const char *lbl, size_t len)
  284. {
  285. char *value = NULL;
  286. if(strncasecompare(hdr->data, lbl, len) && hdr->data[len] == ':')
  287. for(value = hdr->data + len + 1; *value == ' '; value++)
  288. ;
  289. return value;
  290. }
  291. /* Get a header from an slist. */
  292. static char *search_header(struct curl_slist *hdrlist,
  293. const char *hdr, size_t len)
  294. {
  295. char *value = NULL;
  296. for(; !value && hdrlist; hdrlist = hdrlist->next)
  297. value = match_header(hdrlist, hdr, len);
  298. return value;
  299. }
  300. static char *strippath(const char *fullfile)
  301. {
  302. char *filename;
  303. char *base;
  304. filename = strdup(fullfile); /* duplicate since basename() may ruin the
  305. buffer it works on */
  306. if(!filename)
  307. return NULL;
  308. base = strdup(basename(filename));
  309. free(filename); /* free temporary buffer */
  310. return base; /* returns an allocated string or NULL ! */
  311. }
  312. /* Initialize data encoder state. */
  313. static void cleanup_encoder_state(struct mime_encoder_state *p)
  314. {
  315. p->pos = 0;
  316. p->bufbeg = 0;
  317. p->bufend = 0;
  318. }
  319. /* Dummy encoder. This is used for 8bit and binary content encodings. */
  320. static size_t encoder_nop_read(char *buffer, size_t size, bool ateof,
  321. struct curl_mimepart *part)
  322. {
  323. struct mime_encoder_state *st = &part->encstate;
  324. size_t insize = st->bufend - st->bufbeg;
  325. (void) ateof;
  326. if(!size)
  327. return STOP_FILLING;
  328. if(size > insize)
  329. size = insize;
  330. if(size)
  331. memcpy(buffer, st->buf + st->bufbeg, size);
  332. st->bufbeg += size;
  333. return size;
  334. }
  335. static curl_off_t encoder_nop_size(curl_mimepart *part)
  336. {
  337. return part->datasize;
  338. }
  339. /* 7bit encoder: the encoder is just a data validity check. */
  340. static size_t encoder_7bit_read(char *buffer, size_t size, bool ateof,
  341. curl_mimepart *part)
  342. {
  343. struct mime_encoder_state *st = &part->encstate;
  344. size_t cursize = st->bufend - st->bufbeg;
  345. (void) ateof;
  346. if(!size)
  347. return STOP_FILLING;
  348. if(size > cursize)
  349. size = cursize;
  350. for(cursize = 0; cursize < size; cursize++) {
  351. *buffer = st->buf[st->bufbeg];
  352. if(*buffer++ & 0x80)
  353. return cursize? cursize: READ_ERROR;
  354. st->bufbeg++;
  355. }
  356. return cursize;
  357. }
  358. /* Base64 content encoder. */
  359. static size_t encoder_base64_read(char *buffer, size_t size, bool ateof,
  360. curl_mimepart *part)
  361. {
  362. struct mime_encoder_state *st = &part->encstate;
  363. size_t cursize = 0;
  364. int i;
  365. char *ptr = buffer;
  366. while(st->bufbeg < st->bufend) {
  367. /* Line full ? */
  368. if(st->pos > MAX_ENCODED_LINE_LENGTH - 4) {
  369. /* Yes, we need 2 characters for CRLF. */
  370. if(size < 2) {
  371. if(!cursize)
  372. return STOP_FILLING;
  373. break;
  374. }
  375. *ptr++ = '\r';
  376. *ptr++ = '\n';
  377. st->pos = 0;
  378. cursize += 2;
  379. size -= 2;
  380. }
  381. /* Be sure there is enough space and input data for a base64 group. */
  382. if(size < 4) {
  383. if(!cursize)
  384. return STOP_FILLING;
  385. break;
  386. }
  387. if(st->bufend - st->bufbeg < 3)
  388. break;
  389. /* Encode three bytes as four characters. */
  390. i = st->buf[st->bufbeg++] & 0xFF;
  391. i = (i << 8) | (st->buf[st->bufbeg++] & 0xFF);
  392. i = (i << 8) | (st->buf[st->bufbeg++] & 0xFF);
  393. *ptr++ = base64enc[(i >> 18) & 0x3F];
  394. *ptr++ = base64enc[(i >> 12) & 0x3F];
  395. *ptr++ = base64enc[(i >> 6) & 0x3F];
  396. *ptr++ = base64enc[i & 0x3F];
  397. cursize += 4;
  398. st->pos += 4;
  399. size -= 4;
  400. }
  401. /* If at eof, we have to flush the buffered data. */
  402. if(ateof) {
  403. if(size < 4) {
  404. if(!cursize)
  405. return STOP_FILLING;
  406. }
  407. else {
  408. /* Buffered data size can only be 0, 1 or 2. */
  409. ptr[2] = ptr[3] = '=';
  410. i = 0;
  411. /* If there is buffered data */
  412. if(st->bufend != st->bufbeg) {
  413. if(st->bufend - st->bufbeg == 2)
  414. i = (st->buf[st->bufbeg + 1] & 0xFF) << 8;
  415. i |= (st->buf[st->bufbeg] & 0xFF) << 16;
  416. ptr[0] = base64enc[(i >> 18) & 0x3F];
  417. ptr[1] = base64enc[(i >> 12) & 0x3F];
  418. if(++st->bufbeg != st->bufend) {
  419. ptr[2] = base64enc[(i >> 6) & 0x3F];
  420. st->bufbeg++;
  421. }
  422. cursize += 4;
  423. st->pos += 4;
  424. }
  425. }
  426. }
  427. return cursize;
  428. }
  429. static curl_off_t encoder_base64_size(curl_mimepart *part)
  430. {
  431. curl_off_t size = part->datasize;
  432. if(size <= 0)
  433. return size; /* Unknown size or no data. */
  434. /* Compute base64 character count. */
  435. size = 4 * (1 + (size - 1) / 3);
  436. /* Effective character count must include CRLFs. */
  437. return size + 2 * ((size - 1) / MAX_ENCODED_LINE_LENGTH);
  438. }
  439. /* Quoted-printable lookahead.
  440. *
  441. * Check if a CRLF or end of data is in input buffer at current position + n.
  442. * Return -1 if more data needed, 1 if CRLF or end of data, else 0.
  443. */
  444. static int qp_lookahead_eol(struct mime_encoder_state *st, int ateof, size_t n)
  445. {
  446. n += st->bufbeg;
  447. if(n >= st->bufend && ateof)
  448. return 1;
  449. if(n + 2 > st->bufend)
  450. return ateof? 0: -1;
  451. if(qp_class[st->buf[n] & 0xFF] == QP_CR &&
  452. qp_class[st->buf[n + 1] & 0xFF] == QP_LF)
  453. return 1;
  454. return 0;
  455. }
  456. /* Quoted-printable encoder. */
  457. static size_t encoder_qp_read(char *buffer, size_t size, bool ateof,
  458. curl_mimepart *part)
  459. {
  460. struct mime_encoder_state *st = &part->encstate;
  461. char *ptr = buffer;
  462. size_t cursize = 0;
  463. int softlinebreak;
  464. char buf[4];
  465. /* On all platforms, input is supposed to be ASCII compatible: for this
  466. reason, we use hexadecimal ASCII codes in this function rather than
  467. character constants that can be interpreted as non-ascii on some
  468. platforms. Preserve ASCII encoding on output too. */
  469. while(st->bufbeg < st->bufend) {
  470. size_t len = 1;
  471. size_t consumed = 1;
  472. int i = st->buf[st->bufbeg];
  473. buf[0] = (char) i;
  474. buf[1] = aschex[(i >> 4) & 0xF];
  475. buf[2] = aschex[i & 0xF];
  476. switch(qp_class[st->buf[st->bufbeg] & 0xFF]) {
  477. case QP_OK: /* Not a special character. */
  478. break;
  479. case QP_SP: /* Space or tab. */
  480. /* Spacing must be escaped if followed by CRLF. */
  481. switch(qp_lookahead_eol(st, ateof, 1)) {
  482. case -1: /* More input data needed. */
  483. return cursize;
  484. case 0: /* No encoding needed. */
  485. break;
  486. default: /* CRLF after space or tab. */
  487. buf[0] = '\x3D'; /* '=' */
  488. len = 3;
  489. break;
  490. }
  491. break;
  492. case QP_CR: /* Carriage return. */
  493. /* If followed by a line-feed, output the CRLF pair.
  494. Else escape it. */
  495. switch(qp_lookahead_eol(st, ateof, 0)) {
  496. case -1: /* Need more data. */
  497. return cursize;
  498. case 1: /* CRLF found. */
  499. buf[len++] = '\x0A'; /* Append '\n'. */
  500. consumed = 2;
  501. break;
  502. default: /* Not followed by LF: escape. */
  503. buf[0] = '\x3D'; /* '=' */
  504. len = 3;
  505. break;
  506. }
  507. break;
  508. default: /* Character must be escaped. */
  509. buf[0] = '\x3D'; /* '=' */
  510. len = 3;
  511. break;
  512. }
  513. /* Be sure the encoded character fits within maximum line length. */
  514. if(buf[len - 1] != '\x0A') { /* '\n' */
  515. softlinebreak = st->pos + len > MAX_ENCODED_LINE_LENGTH;
  516. if(!softlinebreak && st->pos + len == MAX_ENCODED_LINE_LENGTH) {
  517. /* We may use the current line only if end of data or followed by
  518. a CRLF. */
  519. switch(qp_lookahead_eol(st, ateof, consumed)) {
  520. case -1: /* Need more data. */
  521. return cursize;
  522. case 0: /* Not followed by a CRLF. */
  523. softlinebreak = 1;
  524. break;
  525. }
  526. }
  527. if(softlinebreak) {
  528. strcpy(buf, "\x3D\x0D\x0A"); /* "=\r\n" */
  529. len = 3;
  530. consumed = 0;
  531. }
  532. }
  533. /* If the output buffer would overflow, do not store. */
  534. if(len > size) {
  535. if(!cursize)
  536. return STOP_FILLING;
  537. break;
  538. }
  539. /* Append to output buffer. */
  540. memcpy(ptr, buf, len);
  541. cursize += len;
  542. ptr += len;
  543. size -= len;
  544. st->pos += len;
  545. if(buf[len - 1] == '\x0A') /* '\n' */
  546. st->pos = 0;
  547. st->bufbeg += consumed;
  548. }
  549. return cursize;
  550. }
  551. static curl_off_t encoder_qp_size(curl_mimepart *part)
  552. {
  553. /* Determining the size can only be done by reading the data: unless the
  554. data size is 0, we return it as unknown (-1). */
  555. return part->datasize? -1: 0;
  556. }
  557. /* In-memory data callbacks. */
  558. /* Argument is a pointer to the mime part. */
  559. static size_t mime_mem_read(char *buffer, size_t size, size_t nitems,
  560. void *instream)
  561. {
  562. curl_mimepart *part = (curl_mimepart *) instream;
  563. size_t sz = curlx_sotouz(part->datasize - part->state.offset);
  564. (void) size; /* Always 1.*/
  565. if(!nitems)
  566. return STOP_FILLING;
  567. if(sz > nitems)
  568. sz = nitems;
  569. if(sz)
  570. memcpy(buffer, part->data + curlx_sotouz(part->state.offset), sz);
  571. return sz;
  572. }
  573. static int mime_mem_seek(void *instream, curl_off_t offset, int whence)
  574. {
  575. curl_mimepart *part = (curl_mimepart *) instream;
  576. switch(whence) {
  577. case SEEK_CUR:
  578. offset += part->state.offset;
  579. break;
  580. case SEEK_END:
  581. offset += part->datasize;
  582. break;
  583. }
  584. if(offset < 0 || offset > part->datasize)
  585. return CURL_SEEKFUNC_FAIL;
  586. part->state.offset = offset;
  587. return CURL_SEEKFUNC_OK;
  588. }
  589. static void mime_mem_free(void *ptr)
  590. {
  591. Curl_safefree(((curl_mimepart *) ptr)->data);
  592. }
  593. /* Named file callbacks. */
  594. /* Argument is a pointer to the mime part. */
  595. static int mime_open_file(curl_mimepart *part)
  596. {
  597. /* Open a MIMEKIND_FILE part. */
  598. if(part->fp)
  599. return 0;
  600. part->fp = fopen_read(part->data, "rb");
  601. return part->fp? 0: -1;
  602. }
  603. static size_t mime_file_read(char *buffer, size_t size, size_t nitems,
  604. void *instream)
  605. {
  606. curl_mimepart *part = (curl_mimepart *) instream;
  607. if(!nitems)
  608. return STOP_FILLING;
  609. if(mime_open_file(part))
  610. return READ_ERROR;
  611. return fread(buffer, size, nitems, part->fp);
  612. }
  613. static int mime_file_seek(void *instream, curl_off_t offset, int whence)
  614. {
  615. curl_mimepart *part = (curl_mimepart *) instream;
  616. if(whence == SEEK_SET && !offset && !part->fp)
  617. return CURL_SEEKFUNC_OK; /* Not open: implicitly already at BOF. */
  618. if(mime_open_file(part))
  619. return CURL_SEEKFUNC_FAIL;
  620. return fseek(part->fp, (long) offset, whence)?
  621. CURL_SEEKFUNC_CANTSEEK: CURL_SEEKFUNC_OK;
  622. }
  623. static void mime_file_free(void *ptr)
  624. {
  625. curl_mimepart *part = (curl_mimepart *) ptr;
  626. if(part->fp) {
  627. fclose(part->fp);
  628. part->fp = NULL;
  629. }
  630. Curl_safefree(part->data);
  631. }
  632. /* Subparts callbacks. */
  633. /* Argument is a pointer to the mime structure. */
  634. /* Readback a byte string segment. */
  635. static size_t readback_bytes(struct mime_state *state,
  636. char *buffer, size_t bufsize,
  637. const char *bytes, size_t numbytes,
  638. const char *trail, size_t traillen)
  639. {
  640. size_t sz;
  641. size_t offset = curlx_sotouz(state->offset);
  642. if(numbytes > offset) {
  643. sz = numbytes - offset;
  644. bytes += offset;
  645. }
  646. else {
  647. sz = offset - numbytes;
  648. if(sz >= traillen)
  649. return 0;
  650. bytes = trail + sz;
  651. sz = traillen - sz;
  652. }
  653. if(sz > bufsize)
  654. sz = bufsize;
  655. memcpy(buffer, bytes, sz);
  656. state->offset += sz;
  657. return sz;
  658. }
  659. /* Read a non-encoded part content. */
  660. static size_t read_part_content(curl_mimepart *part,
  661. char *buffer, size_t bufsize, bool *hasread)
  662. {
  663. size_t sz = 0;
  664. switch(part->lastreadstatus) {
  665. case 0:
  666. case CURL_READFUNC_ABORT:
  667. case CURL_READFUNC_PAUSE:
  668. case READ_ERROR:
  669. return part->lastreadstatus;
  670. default:
  671. break;
  672. }
  673. /* If we can determine we are at end of part data, spare a read. */
  674. if(part->datasize != (curl_off_t) -1 &&
  675. part->state.offset >= part->datasize) {
  676. /* sz is already zero. */
  677. }
  678. else {
  679. switch(part->kind) {
  680. case MIMEKIND_MULTIPART:
  681. /*
  682. * Cannot be processed as other kinds since read function requires
  683. * an additional parameter and is highly recursive.
  684. */
  685. sz = mime_subparts_read(buffer, 1, bufsize, part->arg, hasread);
  686. break;
  687. case MIMEKIND_FILE:
  688. if(part->fp && feof(part->fp))
  689. break; /* At EOF. */
  690. FALLTHROUGH();
  691. default:
  692. if(part->readfunc) {
  693. if(!(part->flags & MIME_FAST_READ)) {
  694. if(*hasread)
  695. return STOP_FILLING;
  696. *hasread = TRUE;
  697. }
  698. sz = part->readfunc(buffer, 1, bufsize, part->arg);
  699. }
  700. break;
  701. }
  702. }
  703. switch(sz) {
  704. case STOP_FILLING:
  705. break;
  706. case 0:
  707. case CURL_READFUNC_ABORT:
  708. case CURL_READFUNC_PAUSE:
  709. case READ_ERROR:
  710. part->lastreadstatus = sz;
  711. break;
  712. default:
  713. part->state.offset += sz;
  714. part->lastreadstatus = sz;
  715. break;
  716. }
  717. return sz;
  718. }
  719. /* Read and encode part content. */
  720. static size_t read_encoded_part_content(curl_mimepart *part, char *buffer,
  721. size_t bufsize, bool *hasread)
  722. {
  723. struct mime_encoder_state *st = &part->encstate;
  724. size_t cursize = 0;
  725. size_t sz;
  726. bool ateof = FALSE;
  727. for(;;) {
  728. if(st->bufbeg < st->bufend || ateof) {
  729. /* Encode buffered data. */
  730. sz = part->encoder->encodefunc(buffer, bufsize, ateof, part);
  731. switch(sz) {
  732. case 0:
  733. if(ateof)
  734. return cursize;
  735. break;
  736. case READ_ERROR:
  737. case STOP_FILLING:
  738. return cursize? cursize: sz;
  739. default:
  740. cursize += sz;
  741. buffer += sz;
  742. bufsize -= sz;
  743. continue;
  744. }
  745. }
  746. /* We need more data in input buffer. */
  747. if(st->bufbeg) {
  748. size_t len = st->bufend - st->bufbeg;
  749. if(len)
  750. memmove(st->buf, st->buf + st->bufbeg, len);
  751. st->bufbeg = 0;
  752. st->bufend = len;
  753. }
  754. if(st->bufend >= sizeof(st->buf))
  755. return cursize? cursize: READ_ERROR; /* Buffer full. */
  756. sz = read_part_content(part, st->buf + st->bufend,
  757. sizeof(st->buf) - st->bufend, hasread);
  758. switch(sz) {
  759. case 0:
  760. ateof = TRUE;
  761. break;
  762. case CURL_READFUNC_ABORT:
  763. case CURL_READFUNC_PAUSE:
  764. case READ_ERROR:
  765. case STOP_FILLING:
  766. return cursize? cursize: sz;
  767. default:
  768. st->bufend += sz;
  769. break;
  770. }
  771. }
  772. /* NOTREACHED */
  773. }
  774. /* Readback a mime part. */
  775. static size_t readback_part(curl_mimepart *part,
  776. char *buffer, size_t bufsize, bool *hasread)
  777. {
  778. size_t cursize = 0;
  779. /* Readback from part. */
  780. while(bufsize) {
  781. size_t sz = 0;
  782. struct curl_slist *hdr = (struct curl_slist *) part->state.ptr;
  783. switch(part->state.state) {
  784. case MIMESTATE_BEGIN:
  785. mimesetstate(&part->state,
  786. (part->flags & MIME_BODY_ONLY)?
  787. MIMESTATE_BODY: MIMESTATE_CURLHEADERS,
  788. part->curlheaders);
  789. break;
  790. case MIMESTATE_USERHEADERS:
  791. if(!hdr) {
  792. mimesetstate(&part->state, MIMESTATE_EOH, NULL);
  793. break;
  794. }
  795. if(match_header(hdr, "Content-Type", 12)) {
  796. mimesetstate(&part->state, MIMESTATE_USERHEADERS, hdr->next);
  797. break;
  798. }
  799. FALLTHROUGH();
  800. case MIMESTATE_CURLHEADERS:
  801. if(!hdr)
  802. mimesetstate(&part->state, MIMESTATE_USERHEADERS, part->userheaders);
  803. else {
  804. sz = readback_bytes(&part->state, buffer, bufsize,
  805. hdr->data, strlen(hdr->data), STRCONST("\r\n"));
  806. if(!sz)
  807. mimesetstate(&part->state, part->state.state, hdr->next);
  808. }
  809. break;
  810. case MIMESTATE_EOH:
  811. sz = readback_bytes(&part->state, buffer, bufsize, STRCONST("\r\n"),
  812. STRCONST(""));
  813. if(!sz)
  814. mimesetstate(&part->state, MIMESTATE_BODY, NULL);
  815. break;
  816. case MIMESTATE_BODY:
  817. cleanup_encoder_state(&part->encstate);
  818. mimesetstate(&part->state, MIMESTATE_CONTENT, NULL);
  819. break;
  820. case MIMESTATE_CONTENT:
  821. if(part->encoder)
  822. sz = read_encoded_part_content(part, buffer, bufsize, hasread);
  823. else
  824. sz = read_part_content(part, buffer, bufsize, hasread);
  825. switch(sz) {
  826. case 0:
  827. mimesetstate(&part->state, MIMESTATE_END, NULL);
  828. /* Try sparing open file descriptors. */
  829. if(part->kind == MIMEKIND_FILE && part->fp) {
  830. fclose(part->fp);
  831. part->fp = NULL;
  832. }
  833. FALLTHROUGH();
  834. case CURL_READFUNC_ABORT:
  835. case CURL_READFUNC_PAUSE:
  836. case READ_ERROR:
  837. case STOP_FILLING:
  838. return cursize? cursize: sz;
  839. }
  840. break;
  841. case MIMESTATE_END:
  842. return cursize;
  843. default:
  844. break; /* Other values not in part state. */
  845. }
  846. /* Bump buffer and counters according to read size. */
  847. cursize += sz;
  848. buffer += sz;
  849. bufsize -= sz;
  850. }
  851. return cursize;
  852. }
  853. /* Readback from mime. Warning: not a read callback function. */
  854. static size_t mime_subparts_read(char *buffer, size_t size, size_t nitems,
  855. void *instream, bool *hasread)
  856. {
  857. curl_mime *mime = (curl_mime *) instream;
  858. size_t cursize = 0;
  859. (void) size; /* Always 1. */
  860. while(nitems) {
  861. size_t sz = 0;
  862. curl_mimepart *part = mime->state.ptr;
  863. switch(mime->state.state) {
  864. case MIMESTATE_BEGIN:
  865. case MIMESTATE_BODY:
  866. mimesetstate(&mime->state, MIMESTATE_BOUNDARY1, mime->firstpart);
  867. /* The first boundary always follows the header termination empty line,
  868. so is always preceded by a CRLF. We can then spare 2 characters
  869. by skipping the leading CRLF in boundary. */
  870. mime->state.offset += 2;
  871. break;
  872. case MIMESTATE_BOUNDARY1:
  873. sz = readback_bytes(&mime->state, buffer, nitems, STRCONST("\r\n--"),
  874. STRCONST(""));
  875. if(!sz)
  876. mimesetstate(&mime->state, MIMESTATE_BOUNDARY2, part);
  877. break;
  878. case MIMESTATE_BOUNDARY2:
  879. if(part)
  880. sz = readback_bytes(&mime->state, buffer, nitems, mime->boundary,
  881. MIME_BOUNDARY_LEN, STRCONST("\r\n"));
  882. else
  883. sz = readback_bytes(&mime->state, buffer, nitems, mime->boundary,
  884. MIME_BOUNDARY_LEN, STRCONST("--\r\n"));
  885. if(!sz) {
  886. mimesetstate(&mime->state, MIMESTATE_CONTENT, part);
  887. }
  888. break;
  889. case MIMESTATE_CONTENT:
  890. if(!part) {
  891. mimesetstate(&mime->state, MIMESTATE_END, NULL);
  892. break;
  893. }
  894. sz = readback_part(part, buffer, nitems, hasread);
  895. switch(sz) {
  896. case CURL_READFUNC_ABORT:
  897. case CURL_READFUNC_PAUSE:
  898. case READ_ERROR:
  899. case STOP_FILLING:
  900. return cursize? cursize: sz;
  901. case 0:
  902. mimesetstate(&mime->state, MIMESTATE_BOUNDARY1, part->nextpart);
  903. break;
  904. }
  905. break;
  906. case MIMESTATE_END:
  907. return cursize;
  908. default:
  909. break; /* other values not used in mime state. */
  910. }
  911. /* Bump buffer and counters according to read size. */
  912. cursize += sz;
  913. buffer += sz;
  914. nitems -= sz;
  915. }
  916. return cursize;
  917. }
  918. static int mime_part_rewind(curl_mimepart *part)
  919. {
  920. int res = CURL_SEEKFUNC_OK;
  921. enum mimestate targetstate = MIMESTATE_BEGIN;
  922. if(part->flags & MIME_BODY_ONLY)
  923. targetstate = MIMESTATE_BODY;
  924. cleanup_encoder_state(&part->encstate);
  925. if(part->state.state > targetstate) {
  926. res = CURL_SEEKFUNC_CANTSEEK;
  927. if(part->seekfunc) {
  928. res = part->seekfunc(part->arg, (curl_off_t) 0, SEEK_SET);
  929. switch(res) {
  930. case CURL_SEEKFUNC_OK:
  931. case CURL_SEEKFUNC_FAIL:
  932. case CURL_SEEKFUNC_CANTSEEK:
  933. break;
  934. case -1: /* For fseek() error. */
  935. res = CURL_SEEKFUNC_CANTSEEK;
  936. break;
  937. default:
  938. res = CURL_SEEKFUNC_FAIL;
  939. break;
  940. }
  941. }
  942. }
  943. if(res == CURL_SEEKFUNC_OK)
  944. mimesetstate(&part->state, targetstate, NULL);
  945. part->lastreadstatus = 1; /* Successful read status. */
  946. return res;
  947. }
  948. static int mime_subparts_seek(void *instream, curl_off_t offset, int whence)
  949. {
  950. curl_mime *mime = (curl_mime *) instream;
  951. curl_mimepart *part;
  952. int result = CURL_SEEKFUNC_OK;
  953. if(whence != SEEK_SET || offset)
  954. return CURL_SEEKFUNC_CANTSEEK; /* Only support full rewind. */
  955. if(mime->state.state == MIMESTATE_BEGIN)
  956. return CURL_SEEKFUNC_OK; /* Already rewound. */
  957. for(part = mime->firstpart; part; part = part->nextpart) {
  958. int res = mime_part_rewind(part);
  959. if(res != CURL_SEEKFUNC_OK)
  960. result = res;
  961. }
  962. if(result == CURL_SEEKFUNC_OK)
  963. mimesetstate(&mime->state, MIMESTATE_BEGIN, NULL);
  964. return result;
  965. }
  966. /* Release part content. */
  967. static void cleanup_part_content(curl_mimepart *part)
  968. {
  969. if(part->freefunc)
  970. part->freefunc(part->arg);
  971. part->readfunc = NULL;
  972. part->seekfunc = NULL;
  973. part->freefunc = NULL;
  974. part->arg = (void *) part; /* Defaults to part itself. */
  975. part->data = NULL;
  976. part->fp = NULL;
  977. part->datasize = (curl_off_t) 0; /* No size yet. */
  978. cleanup_encoder_state(&part->encstate);
  979. part->kind = MIMEKIND_NONE;
  980. part->flags &= ~MIME_FAST_READ;
  981. part->lastreadstatus = 1; /* Successful read status. */
  982. part->state.state = MIMESTATE_BEGIN;
  983. }
  984. static void mime_subparts_free(void *ptr)
  985. {
  986. curl_mime *mime = (curl_mime *) ptr;
  987. if(mime && mime->parent) {
  988. mime->parent->freefunc = NULL; /* Be sure we won't be called again. */
  989. cleanup_part_content(mime->parent); /* Avoid dangling pointer in part. */
  990. }
  991. curl_mime_free(mime);
  992. }
  993. /* Do not free subparts: unbind them. This is used for the top level only. */
  994. static void mime_subparts_unbind(void *ptr)
  995. {
  996. curl_mime *mime = (curl_mime *) ptr;
  997. if(mime && mime->parent) {
  998. mime->parent->freefunc = NULL; /* Be sure we won't be called again. */
  999. cleanup_part_content(mime->parent); /* Avoid dangling pointer in part. */
  1000. mime->parent = NULL;
  1001. }
  1002. }
  1003. void Curl_mime_cleanpart(curl_mimepart *part)
  1004. {
  1005. if(part) {
  1006. cleanup_part_content(part);
  1007. curl_slist_free_all(part->curlheaders);
  1008. if(part->flags & MIME_USERHEADERS_OWNER)
  1009. curl_slist_free_all(part->userheaders);
  1010. Curl_safefree(part->mimetype);
  1011. Curl_safefree(part->name);
  1012. Curl_safefree(part->filename);
  1013. Curl_mime_initpart(part);
  1014. }
  1015. }
  1016. /* Recursively delete a mime handle and its parts. */
  1017. void curl_mime_free(curl_mime *mime)
  1018. {
  1019. curl_mimepart *part;
  1020. if(mime) {
  1021. mime_subparts_unbind(mime); /* Be sure it's not referenced anymore. */
  1022. while(mime->firstpart) {
  1023. part = mime->firstpart;
  1024. mime->firstpart = part->nextpart;
  1025. Curl_mime_cleanpart(part);
  1026. free(part);
  1027. }
  1028. free(mime);
  1029. }
  1030. }
  1031. CURLcode Curl_mime_duppart(struct Curl_easy *data,
  1032. curl_mimepart *dst, const curl_mimepart *src)
  1033. {
  1034. curl_mime *mime;
  1035. curl_mimepart *d;
  1036. const curl_mimepart *s;
  1037. CURLcode res = CURLE_OK;
  1038. DEBUGASSERT(dst);
  1039. /* Duplicate content. */
  1040. switch(src->kind) {
  1041. case MIMEKIND_NONE:
  1042. break;
  1043. case MIMEKIND_DATA:
  1044. res = curl_mime_data(dst, src->data, (size_t) src->datasize);
  1045. break;
  1046. case MIMEKIND_FILE:
  1047. res = curl_mime_filedata(dst, src->data);
  1048. /* Do not abort duplication if file is not readable. */
  1049. if(res == CURLE_READ_ERROR)
  1050. res = CURLE_OK;
  1051. break;
  1052. case MIMEKIND_CALLBACK:
  1053. res = curl_mime_data_cb(dst, src->datasize, src->readfunc,
  1054. src->seekfunc, src->freefunc, src->arg);
  1055. break;
  1056. case MIMEKIND_MULTIPART:
  1057. /* No one knows about the cloned subparts, thus always attach ownership
  1058. to the part. */
  1059. mime = curl_mime_init(data);
  1060. res = mime? curl_mime_subparts(dst, mime): CURLE_OUT_OF_MEMORY;
  1061. /* Duplicate subparts. */
  1062. for(s = ((curl_mime *) src->arg)->firstpart; !res && s; s = s->nextpart) {
  1063. d = curl_mime_addpart(mime);
  1064. res = d? Curl_mime_duppart(data, d, s): CURLE_OUT_OF_MEMORY;
  1065. }
  1066. break;
  1067. default: /* Invalid kind: should not occur. */
  1068. DEBUGF(infof(data, "invalid MIMEKIND* attempt"));
  1069. res = CURLE_BAD_FUNCTION_ARGUMENT; /* Internal error? */
  1070. break;
  1071. }
  1072. /* Duplicate headers. */
  1073. if(!res && src->userheaders) {
  1074. struct curl_slist *hdrs = Curl_slist_duplicate(src->userheaders);
  1075. if(!hdrs)
  1076. res = CURLE_OUT_OF_MEMORY;
  1077. else {
  1078. /* No one but this procedure knows about the new header list,
  1079. so always take ownership. */
  1080. res = curl_mime_headers(dst, hdrs, TRUE);
  1081. if(res)
  1082. curl_slist_free_all(hdrs);
  1083. }
  1084. }
  1085. if(!res) {
  1086. /* Duplicate other fields. */
  1087. dst->encoder = src->encoder;
  1088. res = curl_mime_type(dst, src->mimetype);
  1089. }
  1090. if(!res)
  1091. res = curl_mime_name(dst, src->name);
  1092. if(!res)
  1093. res = curl_mime_filename(dst, src->filename);
  1094. /* If an error occurred, rollback. */
  1095. if(res)
  1096. Curl_mime_cleanpart(dst);
  1097. return res;
  1098. }
  1099. /*
  1100. * Mime build functions.
  1101. */
  1102. /* Create a mime handle. */
  1103. curl_mime *curl_mime_init(struct Curl_easy *easy)
  1104. {
  1105. curl_mime *mime;
  1106. mime = (curl_mime *) malloc(sizeof(*mime));
  1107. if(mime) {
  1108. mime->parent = NULL;
  1109. mime->firstpart = NULL;
  1110. mime->lastpart = NULL;
  1111. memset(mime->boundary, '-', MIME_BOUNDARY_DASHES);
  1112. if(Curl_rand_alnum(easy,
  1113. (unsigned char *) &mime->boundary[MIME_BOUNDARY_DASHES],
  1114. MIME_RAND_BOUNDARY_CHARS + 1)) {
  1115. /* failed to get random separator, bail out */
  1116. free(mime);
  1117. return NULL;
  1118. }
  1119. mimesetstate(&mime->state, MIMESTATE_BEGIN, NULL);
  1120. }
  1121. return mime;
  1122. }
  1123. /* Initialize a mime part. */
  1124. void Curl_mime_initpart(curl_mimepart *part)
  1125. {
  1126. memset((char *) part, 0, sizeof(*part));
  1127. part->lastreadstatus = 1; /* Successful read status. */
  1128. mimesetstate(&part->state, MIMESTATE_BEGIN, NULL);
  1129. }
  1130. /* Create a mime part and append it to a mime handle's part list. */
  1131. curl_mimepart *curl_mime_addpart(curl_mime *mime)
  1132. {
  1133. curl_mimepart *part;
  1134. if(!mime)
  1135. return NULL;
  1136. part = (curl_mimepart *) malloc(sizeof(*part));
  1137. if(part) {
  1138. Curl_mime_initpart(part);
  1139. part->parent = mime;
  1140. if(mime->lastpart)
  1141. mime->lastpart->nextpart = part;
  1142. else
  1143. mime->firstpart = part;
  1144. mime->lastpart = part;
  1145. }
  1146. return part;
  1147. }
  1148. /* Set mime part name. */
  1149. CURLcode curl_mime_name(curl_mimepart *part, const char *name)
  1150. {
  1151. if(!part)
  1152. return CURLE_BAD_FUNCTION_ARGUMENT;
  1153. Curl_safefree(part->name);
  1154. if(name) {
  1155. part->name = strdup(name);
  1156. if(!part->name)
  1157. return CURLE_OUT_OF_MEMORY;
  1158. }
  1159. return CURLE_OK;
  1160. }
  1161. /* Set mime part remote file name. */
  1162. CURLcode curl_mime_filename(curl_mimepart *part, const char *filename)
  1163. {
  1164. if(!part)
  1165. return CURLE_BAD_FUNCTION_ARGUMENT;
  1166. Curl_safefree(part->filename);
  1167. if(filename) {
  1168. part->filename = strdup(filename);
  1169. if(!part->filename)
  1170. return CURLE_OUT_OF_MEMORY;
  1171. }
  1172. return CURLE_OK;
  1173. }
  1174. /* Set mime part content from memory data. */
  1175. CURLcode curl_mime_data(curl_mimepart *part,
  1176. const char *ptr, size_t datasize)
  1177. {
  1178. if(!part)
  1179. return CURLE_BAD_FUNCTION_ARGUMENT;
  1180. cleanup_part_content(part);
  1181. if(ptr) {
  1182. if(datasize == CURL_ZERO_TERMINATED)
  1183. datasize = strlen(ptr);
  1184. part->data = Curl_memdup0(ptr, datasize);
  1185. if(!part->data)
  1186. return CURLE_OUT_OF_MEMORY;
  1187. part->datasize = datasize;
  1188. part->readfunc = mime_mem_read;
  1189. part->seekfunc = mime_mem_seek;
  1190. part->freefunc = mime_mem_free;
  1191. part->flags |= MIME_FAST_READ;
  1192. part->kind = MIMEKIND_DATA;
  1193. }
  1194. return CURLE_OK;
  1195. }
  1196. /* Set mime part content from named local file. */
  1197. CURLcode curl_mime_filedata(curl_mimepart *part, const char *filename)
  1198. {
  1199. CURLcode result = CURLE_OK;
  1200. if(!part)
  1201. return CURLE_BAD_FUNCTION_ARGUMENT;
  1202. cleanup_part_content(part);
  1203. if(filename) {
  1204. char *base;
  1205. struct_stat sbuf;
  1206. if(stat(filename, &sbuf) || access(filename, R_OK))
  1207. result = CURLE_READ_ERROR;
  1208. part->data = strdup(filename);
  1209. if(!part->data)
  1210. result = CURLE_OUT_OF_MEMORY;
  1211. part->datasize = -1;
  1212. if(!result && S_ISREG(sbuf.st_mode)) {
  1213. part->datasize = filesize(filename, sbuf);
  1214. part->seekfunc = mime_file_seek;
  1215. }
  1216. part->readfunc = mime_file_read;
  1217. part->freefunc = mime_file_free;
  1218. part->kind = MIMEKIND_FILE;
  1219. /* As a side effect, set the filename to the current file's base name.
  1220. It is possible to withdraw this by explicitly calling
  1221. curl_mime_filename() with a NULL filename argument after the current
  1222. call. */
  1223. base = strippath(filename);
  1224. if(!base)
  1225. result = CURLE_OUT_OF_MEMORY;
  1226. else {
  1227. CURLcode res = curl_mime_filename(part, base);
  1228. if(res)
  1229. result = res;
  1230. free(base);
  1231. }
  1232. }
  1233. return result;
  1234. }
  1235. /* Set mime part type. */
  1236. CURLcode curl_mime_type(curl_mimepart *part, const char *mimetype)
  1237. {
  1238. if(!part)
  1239. return CURLE_BAD_FUNCTION_ARGUMENT;
  1240. Curl_safefree(part->mimetype);
  1241. if(mimetype) {
  1242. part->mimetype = strdup(mimetype);
  1243. if(!part->mimetype)
  1244. return CURLE_OUT_OF_MEMORY;
  1245. }
  1246. return CURLE_OK;
  1247. }
  1248. /* Set mime data transfer encoder. */
  1249. CURLcode curl_mime_encoder(curl_mimepart *part, const char *encoding)
  1250. {
  1251. CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
  1252. const struct mime_encoder *mep;
  1253. if(!part)
  1254. return result;
  1255. part->encoder = NULL;
  1256. if(!encoding)
  1257. return CURLE_OK; /* Removing current encoder. */
  1258. for(mep = encoders; mep->name; mep++)
  1259. if(strcasecompare(encoding, mep->name)) {
  1260. part->encoder = mep;
  1261. result = CURLE_OK;
  1262. }
  1263. return result;
  1264. }
  1265. /* Set mime part headers. */
  1266. CURLcode curl_mime_headers(curl_mimepart *part,
  1267. struct curl_slist *headers, int take_ownership)
  1268. {
  1269. if(!part)
  1270. return CURLE_BAD_FUNCTION_ARGUMENT;
  1271. if(part->flags & MIME_USERHEADERS_OWNER) {
  1272. if(part->userheaders != headers) /* Allow setting twice the same list. */
  1273. curl_slist_free_all(part->userheaders);
  1274. part->flags &= ~MIME_USERHEADERS_OWNER;
  1275. }
  1276. part->userheaders = headers;
  1277. if(headers && take_ownership)
  1278. part->flags |= MIME_USERHEADERS_OWNER;
  1279. return CURLE_OK;
  1280. }
  1281. /* Set mime part content from callback. */
  1282. CURLcode curl_mime_data_cb(curl_mimepart *part, curl_off_t datasize,
  1283. curl_read_callback readfunc,
  1284. curl_seek_callback seekfunc,
  1285. curl_free_callback freefunc, void *arg)
  1286. {
  1287. if(!part)
  1288. return CURLE_BAD_FUNCTION_ARGUMENT;
  1289. cleanup_part_content(part);
  1290. if(readfunc) {
  1291. part->readfunc = readfunc;
  1292. part->seekfunc = seekfunc;
  1293. part->freefunc = freefunc;
  1294. part->arg = arg;
  1295. part->datasize = datasize;
  1296. part->kind = MIMEKIND_CALLBACK;
  1297. }
  1298. return CURLE_OK;
  1299. }
  1300. /* Set mime part content from subparts. */
  1301. CURLcode Curl_mime_set_subparts(curl_mimepart *part,
  1302. curl_mime *subparts, int take_ownership)
  1303. {
  1304. curl_mime *root;
  1305. if(!part)
  1306. return CURLE_BAD_FUNCTION_ARGUMENT;
  1307. /* Accept setting twice the same subparts. */
  1308. if(part->kind == MIMEKIND_MULTIPART && part->arg == subparts)
  1309. return CURLE_OK;
  1310. cleanup_part_content(part);
  1311. if(subparts) {
  1312. /* Should not have been attached already. */
  1313. if(subparts->parent)
  1314. return CURLE_BAD_FUNCTION_ARGUMENT;
  1315. /* Should not be the part's root. */
  1316. root = part->parent;
  1317. if(root) {
  1318. while(root->parent && root->parent->parent)
  1319. root = root->parent->parent;
  1320. if(subparts == root) {
  1321. /* Can't add as a subpart of itself. */
  1322. return CURLE_BAD_FUNCTION_ARGUMENT;
  1323. }
  1324. }
  1325. subparts->parent = part;
  1326. /* Subparts are processed internally: no read callback. */
  1327. part->seekfunc = mime_subparts_seek;
  1328. part->freefunc = take_ownership? mime_subparts_free: mime_subparts_unbind;
  1329. part->arg = subparts;
  1330. part->datasize = -1;
  1331. part->kind = MIMEKIND_MULTIPART;
  1332. }
  1333. return CURLE_OK;
  1334. }
  1335. CURLcode curl_mime_subparts(curl_mimepart *part, curl_mime *subparts)
  1336. {
  1337. return Curl_mime_set_subparts(part, subparts, TRUE);
  1338. }
  1339. /* Readback from top mime. */
  1340. /* Argument is the dummy top part. */
  1341. size_t Curl_mime_read(char *buffer, size_t size, size_t nitems, void *instream)
  1342. {
  1343. curl_mimepart *part = (curl_mimepart *) instream;
  1344. size_t ret;
  1345. bool hasread;
  1346. (void) size; /* Always 1. */
  1347. do {
  1348. hasread = FALSE;
  1349. ret = readback_part(part, buffer, nitems, &hasread);
  1350. /*
  1351. * If this is not possible to get some data without calling more than
  1352. * one read callback (probably because a content encoder is not able to
  1353. * deliver a new bunch for the few data accumulated so far), force another
  1354. * read until we get enough data or a special exit code.
  1355. */
  1356. } while(ret == STOP_FILLING);
  1357. return ret;
  1358. }
  1359. /* Rewind mime stream. */
  1360. static CURLcode mime_rewind(curl_mimepart *part)
  1361. {
  1362. return mime_part_rewind(part) == CURL_SEEKFUNC_OK?
  1363. CURLE_OK: CURLE_SEND_FAIL_REWIND;
  1364. }
  1365. /* Compute header list size. */
  1366. static size_t slist_size(struct curl_slist *s,
  1367. size_t overhead, const char *skip, size_t skiplen)
  1368. {
  1369. size_t size = 0;
  1370. for(; s; s = s->next)
  1371. if(!skip || !match_header(s, skip, skiplen))
  1372. size += strlen(s->data) + overhead;
  1373. return size;
  1374. }
  1375. /* Get/compute multipart size. */
  1376. static curl_off_t multipart_size(curl_mime *mime)
  1377. {
  1378. curl_off_t size;
  1379. curl_off_t boundarysize;
  1380. curl_mimepart *part;
  1381. if(!mime)
  1382. return 0; /* Not present -> empty. */
  1383. boundarysize = 4 + MIME_BOUNDARY_LEN + 2;
  1384. size = boundarysize; /* Final boundary - CRLF after headers. */
  1385. for(part = mime->firstpart; part; part = part->nextpart) {
  1386. curl_off_t sz = mime_size(part);
  1387. if(sz < 0)
  1388. size = sz;
  1389. if(size >= 0)
  1390. size += boundarysize + sz;
  1391. }
  1392. return size;
  1393. }
  1394. /* Get/compute mime size. */
  1395. static curl_off_t mime_size(curl_mimepart *part)
  1396. {
  1397. curl_off_t size;
  1398. if(part->kind == MIMEKIND_MULTIPART)
  1399. part->datasize = multipart_size(part->arg);
  1400. size = part->datasize;
  1401. if(part->encoder)
  1402. size = part->encoder->sizefunc(part);
  1403. if(size >= 0 && !(part->flags & MIME_BODY_ONLY)) {
  1404. /* Compute total part size. */
  1405. size += slist_size(part->curlheaders, 2, NULL, 0);
  1406. size += slist_size(part->userheaders, 2, STRCONST("Content-Type"));
  1407. size += 2; /* CRLF after headers. */
  1408. }
  1409. return size;
  1410. }
  1411. /* Add a header. */
  1412. /* VARARGS2 */
  1413. CURLcode Curl_mime_add_header(struct curl_slist **slp, const char *fmt, ...)
  1414. {
  1415. struct curl_slist *hdr = NULL;
  1416. char *s = NULL;
  1417. va_list ap;
  1418. va_start(ap, fmt);
  1419. s = curl_mvaprintf(fmt, ap);
  1420. va_end(ap);
  1421. if(s) {
  1422. hdr = Curl_slist_append_nodup(*slp, s);
  1423. if(hdr)
  1424. *slp = hdr;
  1425. else
  1426. free(s);
  1427. }
  1428. return hdr? CURLE_OK: CURLE_OUT_OF_MEMORY;
  1429. }
  1430. /* Add a content type header. */
  1431. static CURLcode add_content_type(struct curl_slist **slp,
  1432. const char *type, const char *boundary)
  1433. {
  1434. return Curl_mime_add_header(slp, "Content-Type: %s%s%s", type,
  1435. boundary? "; boundary=": "",
  1436. boundary? boundary: "");
  1437. }
  1438. const char *Curl_mime_contenttype(const char *filename)
  1439. {
  1440. /*
  1441. * If no content type was specified, we scan through a few well-known
  1442. * extensions and pick the first we match!
  1443. */
  1444. struct ContentType {
  1445. const char *extension;
  1446. const char *type;
  1447. };
  1448. static const struct ContentType ctts[] = {
  1449. {".gif", "image/gif"},
  1450. {".jpg", "image/jpeg"},
  1451. {".jpeg", "image/jpeg"},
  1452. {".png", "image/png"},
  1453. {".svg", "image/svg+xml"},
  1454. {".txt", "text/plain"},
  1455. {".htm", "text/html"},
  1456. {".html", "text/html"},
  1457. {".pdf", "application/pdf"},
  1458. {".xml", "application/xml"}
  1459. };
  1460. if(filename) {
  1461. size_t len1 = strlen(filename);
  1462. const char *nameend = filename + len1;
  1463. unsigned int i;
  1464. for(i = 0; i < sizeof(ctts) / sizeof(ctts[0]); i++) {
  1465. size_t len2 = strlen(ctts[i].extension);
  1466. if(len1 >= len2 && strcasecompare(nameend - len2, ctts[i].extension))
  1467. return ctts[i].type;
  1468. }
  1469. }
  1470. return NULL;
  1471. }
  1472. static bool content_type_match(const char *contenttype,
  1473. const char *target, size_t len)
  1474. {
  1475. if(contenttype && strncasecompare(contenttype, target, len))
  1476. switch(contenttype[len]) {
  1477. case '\0':
  1478. case '\t':
  1479. case '\r':
  1480. case '\n':
  1481. case ' ':
  1482. case ';':
  1483. return TRUE;
  1484. }
  1485. return FALSE;
  1486. }
  1487. CURLcode Curl_mime_prepare_headers(struct Curl_easy *data,
  1488. curl_mimepart *part,
  1489. const char *contenttype,
  1490. const char *disposition,
  1491. enum mimestrategy strategy)
  1492. {
  1493. curl_mime *mime = NULL;
  1494. const char *boundary = NULL;
  1495. char *customct;
  1496. const char *cte = NULL;
  1497. CURLcode ret = CURLE_OK;
  1498. /* Get rid of previously prepared headers. */
  1499. curl_slist_free_all(part->curlheaders);
  1500. part->curlheaders = NULL;
  1501. /* Be sure we won't access old headers later. */
  1502. if(part->state.state == MIMESTATE_CURLHEADERS)
  1503. mimesetstate(&part->state, MIMESTATE_CURLHEADERS, NULL);
  1504. /* Check if content type is specified. */
  1505. customct = part->mimetype;
  1506. if(!customct)
  1507. customct = search_header(part->userheaders, STRCONST("Content-Type"));
  1508. if(customct)
  1509. contenttype = customct;
  1510. /* If content type is not specified, try to determine it. */
  1511. if(!contenttype) {
  1512. switch(part->kind) {
  1513. case MIMEKIND_MULTIPART:
  1514. contenttype = MULTIPART_CONTENTTYPE_DEFAULT;
  1515. break;
  1516. case MIMEKIND_FILE:
  1517. contenttype = Curl_mime_contenttype(part->filename);
  1518. if(!contenttype)
  1519. contenttype = Curl_mime_contenttype(part->data);
  1520. if(!contenttype && part->filename)
  1521. contenttype = FILE_CONTENTTYPE_DEFAULT;
  1522. break;
  1523. default:
  1524. contenttype = Curl_mime_contenttype(part->filename);
  1525. break;
  1526. }
  1527. }
  1528. if(part->kind == MIMEKIND_MULTIPART) {
  1529. mime = (curl_mime *) part->arg;
  1530. if(mime)
  1531. boundary = mime->boundary;
  1532. }
  1533. else if(contenttype && !customct &&
  1534. content_type_match(contenttype, STRCONST("text/plain")))
  1535. if(strategy == MIMESTRATEGY_MAIL || !part->filename)
  1536. contenttype = NULL;
  1537. /* Issue content-disposition header only if not already set by caller. */
  1538. if(!search_header(part->userheaders, STRCONST("Content-Disposition"))) {
  1539. if(!disposition)
  1540. if(part->filename || part->name ||
  1541. (contenttype && !strncasecompare(contenttype, "multipart/", 10)))
  1542. disposition = DISPOSITION_DEFAULT;
  1543. if(disposition && curl_strequal(disposition, "attachment") &&
  1544. !part->name && !part->filename)
  1545. disposition = NULL;
  1546. if(disposition) {
  1547. char *name = NULL;
  1548. char *filename = NULL;
  1549. if(part->name) {
  1550. name = escape_string(data, part->name, strategy);
  1551. if(!name)
  1552. ret = CURLE_OUT_OF_MEMORY;
  1553. }
  1554. if(!ret && part->filename) {
  1555. filename = escape_string(data, part->filename, strategy);
  1556. if(!filename)
  1557. ret = CURLE_OUT_OF_MEMORY;
  1558. }
  1559. if(!ret)
  1560. ret = Curl_mime_add_header(&part->curlheaders,
  1561. "Content-Disposition: %s%s%s%s%s%s%s",
  1562. disposition,
  1563. name? "; name=\"": "",
  1564. name? name: "",
  1565. name? "\"": "",
  1566. filename? "; filename=\"": "",
  1567. filename? filename: "",
  1568. filename? "\"": "");
  1569. Curl_safefree(name);
  1570. Curl_safefree(filename);
  1571. if(ret)
  1572. return ret;
  1573. }
  1574. }
  1575. /* Issue Content-Type header. */
  1576. if(contenttype) {
  1577. ret = add_content_type(&part->curlheaders, contenttype, boundary);
  1578. if(ret)
  1579. return ret;
  1580. }
  1581. /* Content-Transfer-Encoding header. */
  1582. if(!search_header(part->userheaders,
  1583. STRCONST("Content-Transfer-Encoding"))) {
  1584. if(part->encoder)
  1585. cte = part->encoder->name;
  1586. else if(contenttype && strategy == MIMESTRATEGY_MAIL &&
  1587. part->kind != MIMEKIND_MULTIPART)
  1588. cte = "8bit";
  1589. if(cte) {
  1590. ret = Curl_mime_add_header(&part->curlheaders,
  1591. "Content-Transfer-Encoding: %s", cte);
  1592. if(ret)
  1593. return ret;
  1594. }
  1595. }
  1596. /* If we were reading curl-generated headers, restart with new ones (this
  1597. should not occur). */
  1598. if(part->state.state == MIMESTATE_CURLHEADERS)
  1599. mimesetstate(&part->state, MIMESTATE_CURLHEADERS, part->curlheaders);
  1600. /* Process subparts. */
  1601. if(part->kind == MIMEKIND_MULTIPART && mime) {
  1602. curl_mimepart *subpart;
  1603. disposition = NULL;
  1604. if(content_type_match(contenttype, STRCONST("multipart/form-data")))
  1605. disposition = "form-data";
  1606. for(subpart = mime->firstpart; subpart; subpart = subpart->nextpart) {
  1607. ret = Curl_mime_prepare_headers(data, subpart, NULL,
  1608. disposition, strategy);
  1609. if(ret)
  1610. return ret;
  1611. }
  1612. }
  1613. return ret;
  1614. }
  1615. /* Recursively reset paused status in the given part. */
  1616. static void mime_unpause(curl_mimepart *part)
  1617. {
  1618. if(part) {
  1619. if(part->lastreadstatus == CURL_READFUNC_PAUSE)
  1620. part->lastreadstatus = 1; /* Successful read status. */
  1621. if(part->kind == MIMEKIND_MULTIPART) {
  1622. curl_mime *mime = (curl_mime *) part->arg;
  1623. if(mime) {
  1624. curl_mimepart *subpart;
  1625. for(subpart = mime->firstpart; subpart; subpart = subpart->nextpart)
  1626. mime_unpause(subpart);
  1627. }
  1628. }
  1629. }
  1630. }
  1631. struct cr_mime_ctx {
  1632. struct Curl_creader super;
  1633. curl_mimepart *part;
  1634. curl_off_t total_len;
  1635. curl_off_t read_len;
  1636. CURLcode error_result;
  1637. BIT(seen_eos);
  1638. BIT(errored);
  1639. };
  1640. static CURLcode cr_mime_init(struct Curl_easy *data,
  1641. struct Curl_creader *reader)
  1642. {
  1643. struct cr_mime_ctx *ctx = reader->ctx;
  1644. (void)data;
  1645. ctx->total_len = -1;
  1646. ctx->read_len = 0;
  1647. return CURLE_OK;
  1648. }
  1649. /* Real client reader to installed client callbacks. */
  1650. static CURLcode cr_mime_read(struct Curl_easy *data,
  1651. struct Curl_creader *reader,
  1652. char *buf, size_t blen,
  1653. size_t *pnread, bool *peos)
  1654. {
  1655. struct cr_mime_ctx *ctx = reader->ctx;
  1656. size_t nread;
  1657. /* Once we have errored, we will return the same error forever */
  1658. if(ctx->errored) {
  1659. *pnread = 0;
  1660. *peos = FALSE;
  1661. return ctx->error_result;
  1662. }
  1663. if(ctx->seen_eos) {
  1664. *pnread = 0;
  1665. *peos = TRUE;
  1666. return CURLE_OK;
  1667. }
  1668. /* respect length limitations */
  1669. if(ctx->total_len >= 0) {
  1670. curl_off_t remain = ctx->total_len - ctx->read_len;
  1671. if(remain <= 0)
  1672. blen = 0;
  1673. else if(remain < (curl_off_t)blen)
  1674. blen = (size_t)remain;
  1675. }
  1676. nread = 0;
  1677. if(blen) {
  1678. nread = Curl_mime_read(buf, 1, blen, ctx->part);
  1679. }
  1680. switch(nread) {
  1681. case 0:
  1682. if((ctx->total_len >= 0) && (ctx->read_len < ctx->total_len)) {
  1683. failf(data, "client mime read EOF fail, only "
  1684. "only %"CURL_FORMAT_CURL_OFF_T"/%"CURL_FORMAT_CURL_OFF_T
  1685. " of needed bytes read", ctx->read_len, ctx->total_len);
  1686. return CURLE_READ_ERROR;
  1687. }
  1688. *pnread = 0;
  1689. *peos = TRUE;
  1690. ctx->seen_eos = TRUE;
  1691. break;
  1692. case CURL_READFUNC_ABORT:
  1693. failf(data, "operation aborted by callback");
  1694. *pnread = 0;
  1695. *peos = FALSE;
  1696. ctx->errored = TRUE;
  1697. ctx->error_result = CURLE_ABORTED_BY_CALLBACK;
  1698. return CURLE_ABORTED_BY_CALLBACK;
  1699. case CURL_READFUNC_PAUSE:
  1700. /* CURL_READFUNC_PAUSE pauses read callbacks that feed socket writes */
  1701. data->req.keepon |= KEEP_SEND_PAUSE; /* mark socket send as paused */
  1702. *pnread = 0;
  1703. *peos = FALSE;
  1704. break; /* nothing was read */
  1705. default:
  1706. if(nread > blen) {
  1707. /* the read function returned a too large value */
  1708. failf(data, "read function returned funny value");
  1709. *pnread = 0;
  1710. *peos = FALSE;
  1711. ctx->errored = TRUE;
  1712. ctx->error_result = CURLE_READ_ERROR;
  1713. return CURLE_READ_ERROR;
  1714. }
  1715. ctx->read_len += nread;
  1716. if(ctx->total_len >= 0)
  1717. ctx->seen_eos = (ctx->read_len >= ctx->total_len);
  1718. *pnread = nread;
  1719. *peos = ctx->seen_eos;
  1720. break;
  1721. }
  1722. DEBUGF(infof(data, "cr_mime_read(len=%zu, total=%"CURL_FORMAT_CURL_OFF_T
  1723. ", read=%"CURL_FORMAT_CURL_OFF_T") -> %d, %zu, %d",
  1724. blen, ctx->total_len, ctx->read_len, CURLE_OK, *pnread, *peos));
  1725. return CURLE_OK;
  1726. }
  1727. static bool cr_mime_needs_rewind(struct Curl_easy *data,
  1728. struct Curl_creader *reader)
  1729. {
  1730. struct cr_mime_ctx *ctx = reader->ctx;
  1731. (void)data;
  1732. return ctx->read_len > 0;
  1733. }
  1734. static curl_off_t cr_mime_total_length(struct Curl_easy *data,
  1735. struct Curl_creader *reader)
  1736. {
  1737. struct cr_mime_ctx *ctx = reader->ctx;
  1738. (void)data;
  1739. return ctx->total_len;
  1740. }
  1741. static CURLcode cr_mime_resume_from(struct Curl_easy *data,
  1742. struct Curl_creader *reader,
  1743. curl_off_t offset)
  1744. {
  1745. struct cr_mime_ctx *ctx = reader->ctx;
  1746. if(offset > 0) {
  1747. curl_off_t passed = 0;
  1748. do {
  1749. char scratch[4*1024];
  1750. size_t readthisamountnow =
  1751. (offset - passed > (curl_off_t)sizeof(scratch)) ?
  1752. sizeof(scratch) :
  1753. curlx_sotouz(offset - passed);
  1754. size_t nread;
  1755. nread = Curl_mime_read(scratch, 1, readthisamountnow, ctx->part);
  1756. passed += (curl_off_t)nread;
  1757. if((nread == 0) || (nread > readthisamountnow)) {
  1758. /* this checks for greater-than only to make sure that the
  1759. CURL_READFUNC_ABORT return code still aborts */
  1760. failf(data, "Could only read %" CURL_FORMAT_CURL_OFF_T
  1761. " bytes from the mime post", passed);
  1762. return CURLE_READ_ERROR;
  1763. }
  1764. } while(passed < offset);
  1765. /* now, decrease the size of the read */
  1766. if(ctx->total_len > 0) {
  1767. ctx->total_len -= offset;
  1768. if(ctx->total_len <= 0) {
  1769. failf(data, "Mime post already completely uploaded");
  1770. return CURLE_PARTIAL_FILE;
  1771. }
  1772. }
  1773. /* we've passed, proceed as normal */
  1774. }
  1775. return CURLE_OK;
  1776. }
  1777. static CURLcode cr_mime_rewind(struct Curl_easy *data,
  1778. struct Curl_creader *reader)
  1779. {
  1780. struct cr_mime_ctx *ctx = reader->ctx;
  1781. CURLcode result = mime_rewind(ctx->part);
  1782. if(result)
  1783. failf(data, "Cannot rewind mime/post data");
  1784. return result;
  1785. }
  1786. static CURLcode cr_mime_unpause(struct Curl_easy *data,
  1787. struct Curl_creader *reader)
  1788. {
  1789. struct cr_mime_ctx *ctx = reader->ctx;
  1790. (void)data;
  1791. mime_unpause(ctx->part);
  1792. return CURLE_OK;
  1793. }
  1794. static const struct Curl_crtype cr_mime = {
  1795. "cr-mime",
  1796. cr_mime_init,
  1797. cr_mime_read,
  1798. Curl_creader_def_close,
  1799. cr_mime_needs_rewind,
  1800. cr_mime_total_length,
  1801. cr_mime_resume_from,
  1802. cr_mime_rewind,
  1803. cr_mime_unpause,
  1804. Curl_creader_def_done,
  1805. sizeof(struct cr_mime_ctx)
  1806. };
  1807. CURLcode Curl_creader_set_mime(struct Curl_easy *data, curl_mimepart *part)
  1808. {
  1809. struct Curl_creader *r;
  1810. struct cr_mime_ctx *ctx;
  1811. CURLcode result;
  1812. result = Curl_creader_create(&r, data, &cr_mime, CURL_CR_CLIENT);
  1813. if(result)
  1814. return result;
  1815. ctx = r->ctx;
  1816. ctx->part = part;
  1817. /* Make sure we will read the entire mime structure. */
  1818. result = mime_rewind(ctx->part);
  1819. if(result) {
  1820. Curl_creader_free(data, r);
  1821. return result;
  1822. }
  1823. ctx->total_len = mime_size(ctx->part);
  1824. return Curl_creader_set(data, r);
  1825. }
  1826. #else /* !CURL_DISABLE_MIME && (!CURL_DISABLE_HTTP ||
  1827. !CURL_DISABLE_SMTP || !CURL_DISABLE_IMAP) */
  1828. /* Mime not compiled in: define stubs for externally-referenced functions. */
  1829. curl_mime *curl_mime_init(CURL *easy)
  1830. {
  1831. (void) easy;
  1832. return NULL;
  1833. }
  1834. void curl_mime_free(curl_mime *mime)
  1835. {
  1836. (void) mime;
  1837. }
  1838. curl_mimepart *curl_mime_addpart(curl_mime *mime)
  1839. {
  1840. (void) mime;
  1841. return NULL;
  1842. }
  1843. CURLcode curl_mime_name(curl_mimepart *part, const char *name)
  1844. {
  1845. (void) part;
  1846. (void) name;
  1847. return CURLE_NOT_BUILT_IN;
  1848. }
  1849. CURLcode curl_mime_filename(curl_mimepart *part, const char *filename)
  1850. {
  1851. (void) part;
  1852. (void) filename;
  1853. return CURLE_NOT_BUILT_IN;
  1854. }
  1855. CURLcode curl_mime_type(curl_mimepart *part, const char *mimetype)
  1856. {
  1857. (void) part;
  1858. (void) mimetype;
  1859. return CURLE_NOT_BUILT_IN;
  1860. }
  1861. CURLcode curl_mime_encoder(curl_mimepart *part, const char *encoding)
  1862. {
  1863. (void) part;
  1864. (void) encoding;
  1865. return CURLE_NOT_BUILT_IN;
  1866. }
  1867. CURLcode curl_mime_data(curl_mimepart *part,
  1868. const char *data, size_t datasize)
  1869. {
  1870. (void) part;
  1871. (void) data;
  1872. (void) datasize;
  1873. return CURLE_NOT_BUILT_IN;
  1874. }
  1875. CURLcode curl_mime_filedata(curl_mimepart *part, const char *filename)
  1876. {
  1877. (void) part;
  1878. (void) filename;
  1879. return CURLE_NOT_BUILT_IN;
  1880. }
  1881. CURLcode curl_mime_data_cb(curl_mimepart *part,
  1882. curl_off_t datasize,
  1883. curl_read_callback readfunc,
  1884. curl_seek_callback seekfunc,
  1885. curl_free_callback freefunc,
  1886. void *arg)
  1887. {
  1888. (void) part;
  1889. (void) datasize;
  1890. (void) readfunc;
  1891. (void) seekfunc;
  1892. (void) freefunc;
  1893. (void) arg;
  1894. return CURLE_NOT_BUILT_IN;
  1895. }
  1896. CURLcode curl_mime_subparts(curl_mimepart *part, curl_mime *subparts)
  1897. {
  1898. (void) part;
  1899. (void) subparts;
  1900. return CURLE_NOT_BUILT_IN;
  1901. }
  1902. CURLcode curl_mime_headers(curl_mimepart *part,
  1903. struct curl_slist *headers, int take_ownership)
  1904. {
  1905. (void) part;
  1906. (void) headers;
  1907. (void) take_ownership;
  1908. return CURLE_NOT_BUILT_IN;
  1909. }
  1910. CURLcode Curl_mime_add_header(struct curl_slist **slp, const char *fmt, ...)
  1911. {
  1912. (void)slp;
  1913. (void)fmt;
  1914. return CURLE_NOT_BUILT_IN;
  1915. }
  1916. #endif /* if disabled */