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