mime.h 6.2 KB

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