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


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15