mime.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef HEADER_CURL_MIME_H
  2. #define HEADER_CURL_MIME_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2022, 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. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #define MIME_BOUNDARY_DASHES 24 /* leading boundary dashes */
  26. #define MIME_RAND_BOUNDARY_CHARS 16 /* Nb. of random boundary chars. */
  27. #define MAX_ENCODED_LINE_LENGTH 76 /* Maximum encoded line length. */
  28. #define ENCODING_BUFFER_SIZE 256 /* Encoding temp buffers size. */
  29. /* Part flags. */
  30. #define MIME_USERHEADERS_OWNER (1 << 0)
  31. #define MIME_BODY_ONLY (1 << 1)
  32. #define MIME_FAST_READ (1 << 2)
  33. #define FILE_CONTENTTYPE_DEFAULT "application/octet-stream"
  34. #define MULTIPART_CONTENTTYPE_DEFAULT "multipart/mixed"
  35. #define DISPOSITION_DEFAULT "attachment"
  36. /* Part source kinds. */
  37. enum mimekind {
  38. MIMEKIND_NONE = 0, /* Part not set. */
  39. MIMEKIND_DATA, /* Allocated mime data. */
  40. MIMEKIND_FILE, /* Data from file. */
  41. MIMEKIND_CALLBACK, /* Data from `read' callback. */
  42. MIMEKIND_MULTIPART, /* Data is a mime subpart. */
  43. MIMEKIND_LAST
  44. };
  45. /* Readback state tokens. */
  46. enum mimestate {
  47. MIMESTATE_BEGIN, /* Readback has not yet started. */
  48. MIMESTATE_CURLHEADERS, /* In curl-generated headers. */
  49. MIMESTATE_USERHEADERS, /* In caller's supplied headers. */
  50. MIMESTATE_EOH, /* End of headers. */
  51. MIMESTATE_BODY, /* Placeholder. */
  52. MIMESTATE_BOUNDARY1, /* In boundary prefix. */
  53. MIMESTATE_BOUNDARY2, /* In boundary. */
  54. MIMESTATE_CONTENT, /* In content. */
  55. MIMESTATE_END, /* End of part reached. */
  56. MIMESTATE_LAST
  57. };
  58. /* Mime headers strategies. */
  59. enum mimestrategy {
  60. MIMESTRATEGY_MAIL, /* Mime mail. */
  61. MIMESTRATEGY_FORM, /* HTTP post form. */
  62. MIMESTRATEGY_LAST
  63. };
  64. /* Content transfer encoder. */
  65. struct mime_encoder {
  66. const char * name; /* Encoding name. */
  67. size_t (*encodefunc)(char *buffer, size_t size, bool ateof,
  68. curl_mimepart *part); /* Encoded read. */
  69. curl_off_t (*sizefunc)(curl_mimepart *part); /* Encoded size. */
  70. };
  71. /* Content transfer encoder state. */
  72. struct mime_encoder_state {
  73. size_t pos; /* Position on output line. */
  74. size_t bufbeg; /* Next data index in input buffer. */
  75. size_t bufend; /* First unused byte index in input buffer. */
  76. char buf[ENCODING_BUFFER_SIZE]; /* Input buffer. */
  77. };
  78. /* Mime readback state. */
  79. struct mime_state {
  80. enum mimestate state; /* Current state token. */
  81. void *ptr; /* State-dependent pointer. */
  82. curl_off_t offset; /* State-dependent offset. */
  83. };
  84. /* Boundary string length. */
  85. #define MIME_BOUNDARY_LEN (MIME_BOUNDARY_DASHES + MIME_RAND_BOUNDARY_CHARS)
  86. /* A mime multipart. */
  87. struct curl_mime {
  88. struct Curl_easy *easy; /* The associated easy handle. */
  89. curl_mimepart *parent; /* Parent part. */
  90. curl_mimepart *firstpart; /* First part. */
  91. curl_mimepart *lastpart; /* Last part. */
  92. char boundary[MIME_BOUNDARY_LEN + 1]; /* The part boundary. */
  93. struct mime_state state; /* Current readback state. */
  94. };
  95. /* A mime part. */
  96. struct curl_mimepart {
  97. struct Curl_easy *easy; /* The associated easy handle. */
  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_HTTP) && !defined(CURL_DISABLE_MIME)) || \
  121. !defined(CURL_DISABLE_SMTP) || !defined(CURL_DISABLE_IMAP)
  122. /* Prototypes. */
  123. void Curl_mime_initpart(struct curl_mimepart *part, struct Curl_easy *easy);
  124. void Curl_mime_cleanpart(struct curl_mimepart *part);
  125. CURLcode Curl_mime_duppart(struct curl_mimepart *dst,
  126. const curl_mimepart *src);
  127. CURLcode Curl_mime_set_subparts(struct curl_mimepart *part,
  128. struct curl_mime *subparts,
  129. int take_ownership);
  130. CURLcode Curl_mime_prepare_headers(struct curl_mimepart *part,
  131. const char *contenttype,
  132. const char *disposition,
  133. enum mimestrategy strategy);
  134. curl_off_t Curl_mime_size(struct curl_mimepart *part);
  135. size_t Curl_mime_read(char *buffer, size_t size, size_t nitems,
  136. void *instream);
  137. CURLcode Curl_mime_rewind(struct curl_mimepart *part);
  138. const char *Curl_mime_contenttype(const char *filename);
  139. void Curl_mime_unpause(struct curl_mimepart *part);
  140. #else
  141. /* if disabled */
  142. #define Curl_mime_initpart(x,y)
  143. #define Curl_mime_cleanpart(x)
  144. #define Curl_mime_duppart(x,y) CURLE_OK /* Nothing to duplicate. Succeed */
  145. #define Curl_mime_set_subparts(a,b,c) CURLE_NOT_BUILT_IN
  146. #define Curl_mime_prepare_headers(a,b,c,d) CURLE_NOT_BUILT_IN
  147. #define Curl_mime_size(x) (curl_off_t) -1
  148. #define Curl_mime_read NULL
  149. #define Curl_mime_rewind(x) ((void)x, CURLE_NOT_BUILT_IN)
  150. #define Curl_mime_unpause(x)
  151. #endif
  152. #endif /* HEADER_CURL_MIME_H */