mime.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef HEADER_CURL_MIME_H
  2. #define HEADER_CURL_MIME_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #define MIME_BOUNDARY_DASHES 24 /* leading boundary dashes */
  28. #define MIME_RAND_BOUNDARY_CHARS 16 /* Nb. of random boundary chars. */
  29. #define MAX_ENCODED_LINE_LENGTH 76 /* Maximum encoded line length. */
  30. #define ENCODING_BUFFER_SIZE 256 /* Encoding temp buffers size. */
  31. /* Part flags. */
  32. #define MIME_USERHEADERS_OWNER (1 << 0)
  33. #define MIME_BODY_ONLY (1 << 1)
  34. #define MIME_FAST_READ (1 << 2)
  35. #define FILE_CONTENTTYPE_DEFAULT "application/octet-stream"
  36. #define MULTIPART_CONTENTTYPE_DEFAULT "multipart/mixed"
  37. #define DISPOSITION_DEFAULT "attachment"
  38. /* Part source kinds. */
  39. enum mimekind {
  40. MIMEKIND_NONE = 0, /* Part not set. */
  41. MIMEKIND_DATA, /* Allocated mime data. */
  42. MIMEKIND_FILE, /* Data from file. */
  43. MIMEKIND_CALLBACK, /* Data from `read' callback. */
  44. MIMEKIND_MULTIPART, /* Data is a mime subpart. */
  45. MIMEKIND_LAST
  46. };
  47. /* Readback state tokens. */
  48. enum mimestate {
  49. MIMESTATE_BEGIN, /* Readback has not yet started. */
  50. MIMESTATE_CURLHEADERS, /* In curl-generated headers. */
  51. MIMESTATE_USERHEADERS, /* In caller's supplied headers. */
  52. MIMESTATE_EOH, /* End of headers. */
  53. MIMESTATE_BODY, /* Placeholder. */
  54. MIMESTATE_BOUNDARY1, /* In boundary prefix. */
  55. MIMESTATE_BOUNDARY2, /* In boundary. */
  56. MIMESTATE_CONTENT, /* In content. */
  57. MIMESTATE_END, /* End of part reached. */
  58. MIMESTATE_LAST
  59. };
  60. /* Mime headers strategies. */
  61. enum mimestrategy {
  62. MIMESTRATEGY_MAIL, /* Mime mail. */
  63. MIMESTRATEGY_FORM, /* HTTP post form. */
  64. MIMESTRATEGY_LAST
  65. };
  66. /* Content transfer encoder. */
  67. struct mime_encoder {
  68. const char * name; /* Encoding name. */
  69. size_t (*encodefunc)(char *buffer, size_t size, bool ateof,
  70. curl_mimepart *part); /* Encoded read. */
  71. curl_off_t (*sizefunc)(curl_mimepart *part); /* Encoded size. */
  72. };
  73. /* Content transfer encoder state. */
  74. struct mime_encoder_state {
  75. size_t pos; /* Position on output line. */
  76. size_t bufbeg; /* Next data index in input buffer. */
  77. size_t bufend; /* First unused byte index in input buffer. */
  78. char buf[ENCODING_BUFFER_SIZE]; /* Input buffer. */
  79. };
  80. /* Mime readback state. */
  81. struct mime_state {
  82. enum mimestate state; /* Current state token. */
  83. void *ptr; /* State-dependent pointer. */
  84. curl_off_t offset; /* State-dependent offset. */
  85. };
  86. /* Boundary string length. */
  87. #define MIME_BOUNDARY_LEN (MIME_BOUNDARY_DASHES + MIME_RAND_BOUNDARY_CHARS)
  88. /* A mime multipart. */
  89. struct curl_mime {
  90. curl_mimepart *parent; /* Parent part. */
  91. curl_mimepart *firstpart; /* First part. */
  92. curl_mimepart *lastpart; /* Last part. */
  93. char boundary[MIME_BOUNDARY_LEN + 1]; /* The part boundary. */
  94. struct mime_state state; /* Current readback state. */
  95. };
  96. /* A mime part. */
  97. struct curl_mimepart {
  98. curl_mime *parent; /* Parent mime structure. */
  99. curl_mimepart *nextpart; /* Forward linked list. */
  100. enum mimekind kind; /* The part kind. */
  101. unsigned int flags; /* Flags. */
  102. char *data; /* Memory data or file name. */
  103. curl_read_callback readfunc; /* Read function. */
  104. curl_seek_callback seekfunc; /* Seek function. */
  105. curl_free_callback freefunc; /* Argument free function. */
  106. void *arg; /* Argument to callback functions. */
  107. FILE *fp; /* File pointer. */
  108. struct curl_slist *curlheaders; /* Part headers. */
  109. struct curl_slist *userheaders; /* Part headers. */
  110. char *mimetype; /* Part mime type. */
  111. char *filename; /* Remote file name. */
  112. char *name; /* Data name. */
  113. curl_off_t datasize; /* Expected data size. */
  114. struct mime_state state; /* Current readback state. */
  115. const struct mime_encoder *encoder; /* Content data encoder. */
  116. struct mime_encoder_state encstate; /* Data encoder state. */
  117. size_t lastreadstatus; /* Last read callback returned status. */
  118. };
  119. CURLcode Curl_mime_add_header(struct curl_slist **slp, const char *fmt, ...);
  120. #if !defined(CURL_DISABLE_MIME) && (!defined(CURL_DISABLE_HTTP) || \
  121. !defined(CURL_DISABLE_SMTP) || \
  122. !defined(CURL_DISABLE_IMAP))
  123. /* Prototypes. */
  124. void Curl_mime_initpart(struct curl_mimepart *part);
  125. void Curl_mime_cleanpart(struct curl_mimepart *part);
  126. CURLcode Curl_mime_duppart(struct Curl_easy *data,
  127. struct curl_mimepart *dst,
  128. const curl_mimepart *src);
  129. CURLcode Curl_mime_set_subparts(struct curl_mimepart *part,
  130. struct curl_mime *subparts,
  131. int take_ownership);
  132. CURLcode Curl_mime_prepare_headers(struct Curl_easy *data,
  133. struct curl_mimepart *part,
  134. const char *contenttype,
  135. const char *disposition,
  136. enum mimestrategy strategy);
  137. curl_off_t Curl_mime_size(struct curl_mimepart *part);
  138. size_t Curl_mime_read(char *buffer, size_t size, size_t nitems,
  139. void *instream);
  140. CURLcode Curl_mime_rewind(struct curl_mimepart *part);
  141. const char *Curl_mime_contenttype(const char *filename);
  142. void Curl_mime_unpause(struct curl_mimepart *part);
  143. #else
  144. /* if disabled */
  145. #define Curl_mime_initpart(x)
  146. #define Curl_mime_cleanpart(x)
  147. #define Curl_mime_duppart(x,y,z) CURLE_OK /* Nothing to duplicate. Succeed */
  148. #define Curl_mime_set_subparts(a,b,c) CURLE_NOT_BUILT_IN
  149. #define Curl_mime_prepare_headers(a,b,c,d,e) CURLE_NOT_BUILT_IN
  150. #define Curl_mime_size(x) (curl_off_t) -1
  151. #define Curl_mime_read NULL
  152. #define Curl_mime_rewind(x) ((void)x, CURLE_NOT_BUILT_IN)
  153. #define Curl_mime_unpause(x)
  154. #endif
  155. #endif /* HEADER_CURL_MIME_H */