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