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