mime.c 51 KB

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