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