mime.c 54 KB

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