mime.h 7.1 KB

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