-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultipart_parser.cpp
298 lines (276 loc) · 9.86 KB
/
multipart_parser.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
#include <string.h>
#include <algorithm>
#include "parse_common.hpp"
#include "multipart_parser.hpp"
namespace beauty {
MultiPartParser::MultiPartParser(std::vector<char> &lastBuffer)
: state_(expecting_hyphen_1), lastBuffer_(lastBuffer) {}
void MultiPartParser::reset() {
state_ = expecting_hyphen_1;
boundaryStr_.clear();
lastBuffer_.clear();
lastParts_.clear();
}
bool MultiPartParser::parseHeader(const Request &req) {
reset();
std::string contentTypeVal = req.getHeaderValue("Content-Type");
auto it = contentTypeVal.find("multipart");
if (it == std::string::npos) {
return false;
}
const std::string key = "boundary=";
std::size_t foundStart = contentTypeVal.find(key);
std::size_t foundEnd = contentTypeVal.find(";", foundStart + key.size());
if (foundStart != std::string::npos && foundEnd > foundStart) {
if (foundEnd == std::string::npos) {
boundaryStr_ = contentTypeVal.substr(foundStart + key.size());
} else {
boundaryStr_ =
contentTypeVal.substr(foundStart + key.size(), foundEnd - foundStart - key.size());
}
} else {
return false;
}
return true;
}
MultiPartParser::result_type MultiPartParser::parse(const Request &req,
std::vector<char> &content,
std::deque<ContentPart> &parts) {
result_type result;
if (content.empty()) {
return indeterminate;
}
parts.clear();
auto begin = content.begin();
auto end = content.end();
while (begin != end) {
result = consume(begin++, parts);
if (result != indeterminate) {
break;
}
}
if (result == bad) {
return result;
}
// if no filename_/start_/end_ was found in consume() it will not create
// a part, so must then assume we're in the middle of content somewhere and
// create a part
if (result == indeterminate && parts.empty()) {
parts.push_back(ContentPart());
}
// return the previous content (that have been adjusted below)
parts.swap(lastParts_);
content.swap(lastBuffer_);
// make adjustments to the stored lastParts_ so they are correct when
// swapped out in the next call
bool containEndForPartToLeave = false;
for (auto &lastPart : lastParts_) {
// if start_ not found, assume buffer start
if (!lastPart.foundStart_) {
lastPart.start_ = lastBuffer_.begin();
}
// if end_ not found assume buffer end
if (!lastPart.foundEnd_) {
lastPart.end_ = lastBuffer_.end();
} else {
// lastPart.end_ is assigned +1 char after boundary so:
// subtract "crlf--boundarySize" to get position after
// last data byte
lastPart.end_ = lastPart.end_ - (boundaryStr_.size() + 4);
if (lastPart.end_ < lastPart.start_) {
// end was in the last part of the returned "parts".
// "Luckily" (actually the whole reason why we're having
// a lastPart/Buffer memory) we can adjust it here!
ContentPart &partToLeave = parts.back();
partToLeave.end_ = partToLeave.end_ - (lastPart.start_ - lastPart.end_);
partToLeave.foundEnd_ = true;
// this part needs removing as it was created
// by consume() but only contained end_ for previous part
containEndForPartToLeave = true;
}
}
}
if (containEndForPartToLeave) {
lastParts_.pop_front();
}
return result;
}
void MultiPartParser::flush(std::vector<char> &content, std::deque<ContentPart> &parts) {
parts.swap(lastParts_);
content.swap(lastBuffer_);
lastParts_.clear();
lastBuffer_.clear();
}
const std::deque<MultiPartParser::ContentPart> &MultiPartParser::peakLastPart() const {
return lastParts_;
}
MultiPartParser::result_type MultiPartParser::consume(std::vector<char>::iterator inputPtr,
std::deque<ContentPart> &parts) {
char input = *inputPtr;
switch (state_) {
case expecting_hyphen_1:
if (input != '-') {
return bad;
}
state_ = expecting_hyphen_2;
return indeterminate;
case expecting_hyphen_2:
if (input != '-') {
return bad;
}
state_ = boundary_first;
return indeterminate;
case boundary_first:
if (input == '\r') {
state_ = expecting_newline_1;
}
return indeterminate;
case expecting_newline_1:
if (input == '\n') {
state_ = header_line_start;
} else {
return bad;
}
return indeterminate;
case header_line_start:
if (input == '\r') {
state_ = expecting_newline_3;
} else if (!headers_.empty() && (input == ' ' || input == '\t')) {
state_ = header_lws;
} else if (!isChar(input) || isCtl(input) || isTsspecial(input)) {
return bad;
} else {
headers_.push_back(Header());
headers_.back().name_.reserve(20);
headers_.back().value_.reserve(50);
headers_.back().name_.push_back(input);
state_ = header_name;
}
return indeterminate;
case header_lws:
if (input == '\r') {
state_ = expecting_newline_2;
} else if (input == ' ' || input == '\t') {
// skip
} else if (isCtl(input)) {
return bad;
} else {
state_ = header_value;
headers_.back().value_.push_back(input);
}
return indeterminate;
case header_name:
if (input == ':') {
state_ = space_before_header_value;
} else if (!isChar(input) || isCtl(input) || isTsspecial(input)) {
return bad;
} else {
headers_.back().name_.push_back(input);
}
return indeterminate;
case space_before_header_value:
if (input == ' ') {
state_ = header_value;
} else {
return bad;
}
return indeterminate;
case header_value: {
if (input == '\r') {
Header &h = headers_.back();
if (strcasecmp(h.name_.c_str(), "Content-Disposition") == 0) {
const std::string key = "filename=\"";
std::size_t foundStart = h.value_.rfind(key);
std::size_t foundEnd = h.value_.find("\"", foundStart + key.size());
if (foundStart != std::string::npos && foundEnd != std::string::npos &&
foundEnd > foundStart) {
if (parts.empty()) {
parts.push_back(ContentPart());
}
parts.back().filename_ = h.value_.substr(
foundStart + key.size(), foundEnd - foundStart - key.size());
headers_.clear();
} else {
return bad;
}
}
state_ = expecting_newline_2;
} else if (isCtl(input)) {
return bad;
} else {
headers_.back().value_.push_back(input);
}
return indeterminate;
}
case expecting_newline_2:
if (input == '\n') {
state_ = header_line_start;
} else {
return bad;
}
return indeterminate;
case expecting_newline_3: {
if (input == '\n') {
if (parts.empty()) {
parts.push_back(ContentPart());
}
parts.back().headerOnly_ = true;
state_ = part_data_start;
} else {
return bad;
}
return indeterminate;
}
case part_data_start:
if (parts.empty()) {
parts.push_back(ContentPart());
}
parts.back().headerOnly_ = false;
parts.back().start_ = inputPtr;
parts.back().foundStart_ = true;
state_ = part_data_cont;
return indeterminate;
case part_data_cont:
if (input == '-') {
state_ = expecting_hyphen_3;
}
return indeterminate;
case expecting_hyphen_3:
if (input == '-') {
state_ = boundary_next;
boundaryCount_ = 0;
} else {
state_ = part_data_cont;
}
return indeterminate;
case boundary_next:
if (input == boundaryStr_[boundaryCount_++]) {
if (boundaryCount_ == boundaryStr_.size()) {
state_ = boundary_close;
}
} else {
boundaryCount_ = 0;
state_ = part_data_cont;
}
return indeterminate;
case boundary_close: {
if (parts.empty()) {
parts.push_back(ContentPart());
}
parts.back().end_ = inputPtr;
parts.back().foundEnd_ = true;
if (input == '-') {
return done;
} else if (input == '\r') {
parts.push_back(ContentPart());
state_ = expecting_newline_1;
} else {
return bad;
}
return indeterminate;
}
default:
return bad;
}
}
} // namespace beauty