-
Notifications
You must be signed in to change notification settings - Fork 2
/
mime.cpp
465 lines (382 loc) · 14.2 KB
/
mime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/**
Copyright 1999-2020 Red Anchor Trading Co. Ltd.
Distributed under the Boost Software License, Version 1.0.
See <http://www.boost.org/LICENSE_1_0.txt>
*/
#include "fost-inet.hpp"
#include <fost/mime.hpp>
#include <fost/insert>
#include <fost/string>
#include <fost/exception/parse_error.hpp>
#include <fost/exception/unexpected_eof.hpp>
using namespace fostlib;
/**
## fostlib::mime
*/
fostlib::mime::mime(mime_headers h, f5::u8view content_type)
: content_type{content_type}, headers{std::move(h)} {
if (not headers().exists("Content-Type")) {
headers().set(
"Content-Type", mime::mime_headers::content{content_type});
}
}
fostlib::mime::~mime() {}
f5::u8string fostlib::mime::body_as_string() const {
std::string str;
for (auto const &i : *this) {
str.append(
reinterpret_cast<char const *>(i.first),
reinterpret_cast<char const *>(i.second));
}
fostlib::utf8_string_tag::check_encoded(str);
return f5::u8string{std::move(str)};
}
fostlib::json fostlib::mime::body_as_json() const {
return fostlib::json::parse(body_as_string());
}
fostlib::mime::const_iterator fostlib::mime::begin() const {
return const_iterator(iterator());
}
fostlib::mime::const_iterator fostlib::mime::end() const {
return const_iterator(
std::unique_ptr<fostlib::mime::iterator_implementation>());
}
/**
## fostlib::mime::const_iterator
*/
fostlib::mime::const_iterator::const_iterator(
std::unique_ptr<fostlib::mime::iterator_implementation> i)
: underlying(i.release()) {
if (underlying.get())
current = (*underlying)();
else
current = std::pair<const unsigned char *, const unsigned char *>();
}
const_memory_block fostlib::mime::const_iterator::operator*() const {
return current;
}
const mime::const_iterator &fostlib::mime::const_iterator::operator++() {
if (!underlying.get())
throw exceptions::unexpected_eof(
"fostlib::mime::const_iterator represents end of sequence");
current = (*underlying)();
return *this;
}
bool fostlib::mime::const_iterator::operator==(const const_iterator &o) const {
return current == *o;
}
bool fostlib::mime::const_iterator::operator!=(const const_iterator &o) const {
return current != *o;
}
/**
## fostlib::mime::iterator_implementation
*/
fostlib::mime::iterator_implementation::~iterator_implementation() = default;
/**
## fostlib::mime::mime_headers
*/
std::pair<string, headers_base::content> fostlib::mime::mime_headers::value(
const string &name, const string &value) {
if (name == "Content-Disposition" || name == "content-disposition"
|| name == "Content-Type") {
std::map<string, string> args;
// Parse the value from the format
// form-data; name="aname"; extra="value"
std::pair<string, nullable<string>> disp(partition(value, ";"));
if (disp.second) {
for (std::pair<string, nullable<string>> para(
partition(disp.second, ";"));
!para.first.empty(); para = partition(para.second, ";")) {
// Normally the extra argument values should be surrounded by
// double quotes, but sometimes not
std::pair<string, nullable<string>> argument =
partition(para.first, "=");
if (argument.second && argument.second.value().at(0) == '"'
&& argument.second.value().at(
argument.second.value().code_points() - 1)
== '"') {
argument.second = argument.second.value().substr(
1, argument.second.value().code_points() - 2);
}
if (not argument.second)
throw exceptions::parse_error(
"Message header " + name
+ " does not have a value for an argument",
para.first);
args[argument.first] = argument.second.value();
}
}
if (name == "Content-Disposition") // RFC 1867 uses lower case
return std::make_pair(
"content-disposition", content(disp.first, args));
else
return std::make_pair(name, content(disp.first, args));
} else
return std::make_pair(name, content(value));
}
/**
## fostlib::empty_mime
*/
fostlib::empty_mime::empty_mime(
const mime_headers &headers, const string &mime_type)
: mime(headers, mime_type) {
if (not this->headers().exists("Content-Length")) {
this->headers().set("Content-Length", "0");
}
}
bool fostlib::empty_mime::boundary_is_ok(const string & /*boundary*/) const {
return true;
}
std::ostream &fostlib::empty_mime::print_on(std::ostream &o) const {
return o << headers() << "\r\n";
}
struct fostlib::empty_mime::empty_mime_iterator :
public mime::iterator_implementation {
const_memory_block operator()() { return const_memory_block(); }
};
std::unique_ptr<mime::iterator_implementation>
fostlib::empty_mime::iterator() const {
return std::unique_ptr<mime::iterator_implementation>(
new fostlib::empty_mime::empty_mime_iterator);
}
/**
## fostlib::mime_envelope
*/
fostlib::mime_envelope::mime_envelope(
const mime_headers &headers, const string &type)
: mime(headers, type) {}
const fostlib::string &fostlib::mime_envelope::boundary() {
if (m_boundary.empty()) {
do { m_boundary = guid(); } while (!boundary_is_ok(m_boundary));
mime_headers::content with_boundary(headers()["Content-Type"]);
with_boundary.subvalue("boundary", boundary());
headers().set("Content-Type", with_boundary);
}
return m_boundary;
}
bool fostlib::mime_envelope::boundary_is_ok(const string &boundary) const {
bool ok = true;
for (std::list<boost::shared_ptr<mime>>::const_iterator part(
items().begin());
ok && part != items().end(); ++part)
ok = (*part)->boundary_is_ok(boundary);
return ok;
}
std::ostream &fostlib::mime_envelope::print_on(std::ostream &o) const {
o << headers() << "\r\n";
for (const_iterator c(begin()); c != end(); ++c) {
auto const &starc = *c;
const char *first = reinterpret_cast<const char *>(starc.first);
const char *second = reinterpret_cast<const char *>(starc.second);
o.write(first, second - first);
}
return o;
}
struct fostlib::mime_envelope::mime_envelope_iterator :
public mime::iterator_implementation {
enum { e_start, e_attachments, e_final } stage;
mime_envelope::items_type::const_iterator current_attachment,
end_attachment;
boost::scoped_ptr<mime::const_iterator> current, end;
utf8_string internal_buffer, boundary;
mime_envelope_iterator(
const string &boundary,
mime_envelope::items_type::const_iterator b,
mime_envelope::items_type::const_iterator e)
: stage(e_start),
current_attachment(b),
end_attachment(e),
boundary(fostlib::coerce<utf8_string>(boundary)) {}
const_memory_block operator()() {
static utf8_string dashes("--"), crlf("\r\n");
switch (stage) {
case e_start:
internal_buffer = dashes + boundary + crlf;
stage = e_attachments;
break;
case e_attachments:
if (!current.get()) {
std::stringstream ss;
ss << (*current_attachment)->headers();
internal_buffer = utf8_string(ss.str()) + crlf;
current.reset(new mime::const_iterator(
(*current_attachment)->begin()));
end.reset(
new mime::const_iterator((*current_attachment)->end()));
} else if (*current != *end) {
const_memory_block r = **current;
++(*current);
return r;
} else {
current_attachment++;
current.reset(NULL);
end.reset(NULL);
internal_buffer = crlf + dashes + boundary;
if (current_attachment == end_attachment) {
internal_buffer += dashes + crlf;
stage = e_final;
} else
internal_buffer += crlf;
}
break;
case e_final: return const_memory_block();
default:
throw exceptions::not_implemented(
"Impossible MIME envelope write state");
}
auto const u{internal_buffer.memory()};
return const_memory_block{u.data(), u.data() + u.size()};
}
};
std::unique_ptr<mime::iterator_implementation>
fostlib::mime_envelope::iterator() const {
return std::unique_ptr<mime::iterator_implementation>(
new mime_envelope_iterator(
m_boundary, items().begin(), items().end()));
}
/**
## fostlib::text_body
*/
namespace {
void do_headers(
text_body &tb, const utf8_string &body, const string &mime_type) {
if (!tb.headers().exists("Content-Type"))
tb.headers().set(
"Content-Type", mime::mime_headers::content(mime_type));
tb.headers().set_subvalue("Content-Type", "charset", "utf-8");
tb.headers().set("Content-Transfer-Encoding", "8bit");
tb.headers().set("Content-Length", body.memory().size());
}
}
fostlib::text_body::text_body(
f5::u8view t, mime_headers headers, f5::u8view mime_type)
: mime(std::move(headers), mime_type), text(t) {
do_headers(*this, text(), mime_type);
}
std::ostream &fostlib::text_body::print_on(std::ostream &o) const {
return o << headers() << "\r\n" << text().underlying();
}
bool fostlib::text_body::boundary_is_ok(const string &boundary) const {
return text().underlying().find(coerce<utf8_string>(boundary).underlying())
== string::npos;
}
struct fostlib::text_body::text_body_iterator :
public mime::iterator_implementation {
const utf8_string &body;
bool sent;
text_body_iterator(const utf8_string &b) : body(b), sent(false) {}
const_memory_block operator()() {
if (body.underlying().empty() || sent) {
return const_memory_block();
} else {
sent = true;
return const_memory_block{
body.memory().data(),
body.memory().data() + body.memory().size()};
}
}
};
std::unique_ptr<mime::iterator_implementation>
fostlib::text_body::iterator() const {
return std::unique_ptr<mime::iterator_implementation>(
new text_body_iterator(text()));
}
/**
## fostlib::binary_body
*/
fostlib::binary_body::binary_body(
const mime_headers &headers, const string &mime_type)
: mime(headers, mime_type) {}
fostlib::binary_body::binary_body(
const data_type &data,
const mime_headers &headers,
const string &mime_type)
: mime(headers, mime_type), data(data) {}
fostlib::binary_body::binary_body(
const char *begin,
const char *end,
const mime_headers &headers,
const string &mime_type)
: mime(headers, mime_type), data(data_type(begin, end)) {}
fostlib::binary_body::binary_body(
const signed char *begin,
const signed char *end,
const mime_headers &headers,
const string &mime_type)
: mime(headers, mime_type), data(data_type(begin, end)) {}
fostlib::binary_body::binary_body(
const unsigned char *begin,
const unsigned char *end,
const mime_headers &headers,
const string &mime_type)
: mime(headers, mime_type), data(data_type(begin, end)) {}
std::ostream &fostlib::binary_body::print_on(std::ostream &o) const {
for (data_type::const_iterator i(data().begin()); i != data().end(); ++i)
o << int(*i) << ' ';
return o;
}
bool fostlib::binary_body::boundary_is_ok(const string &boundary) const {
throw exceptions::not_implemented(
"fostlib::binary_body::boundary_is_ok( const string &boundary ) "
"const");
}
struct fostlib::binary_body::binary_body_iterator :
public mime::iterator_implementation {
const data_type &data;
bool sent;
binary_body_iterator(const data_type &d) : data(d), sent(false) {}
const_memory_block operator()() {
if (data.size() == 0 || sent) {
return const_memory_block{};
} else {
sent = true;
return const_memory_block{data.data(), data.data() + data.size()};
}
}
};
std::unique_ptr<mime::iterator_implementation>
fostlib::binary_body::iterator() const {
return std::unique_ptr<mime::iterator_implementation>(
new binary_body_iterator(data()));
}
/**
## fostlib::file_body
*/
fostlib::file_body::file_body(
const fostlib::fs::path &p,
const mime_headers &headers,
const string &mime_type)
: mime(headers, mime_type), filename(p) {
this->headers().set("Content-Transfer-Encoding", "8bit");
this->headers().set(
"Content-Length", coerce<string>(fostlib::fs::file_size(p)));
}
std::ostream &fostlib::file_body::print_on(std::ostream &o) const {
fostlib::ifstream file(filename(), std::ios::binary);
return o << headers() << "\r\n" << file.rdbuf();
}
bool fostlib::file_body::boundary_is_ok(const string &boundary) const {
throw exceptions::not_implemented(
"fostlib::file_body::boundary_is_ok( const string &boundary ) "
"const");
}
struct fostlib::file_body::file_body_iteration :
public mime::iterator_implementation {
fostlib::ifstream file;
std::array<char, 2048> buffer;
file_body_iteration(const fostlib::fs::path &p)
: file(p, std::ios::binary) {}
const_memory_block operator()() {
if (!file.eof() && file.good()) {
file.read(buffer.data(), buffer.size());
return const_memory_block(
buffer.data(), buffer.data() + file.gcount());
} else
return const_memory_block();
}
};
std::unique_ptr<mime::iterator_implementation>
fostlib::file_body::iterator() const {
return std::unique_ptr<mime::iterator_implementation>(
new file_body_iteration(filename()));
}