forked from FooBarWidget/multipart-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultipartParser.h
429 lines (382 loc) · 9.36 KB
/
MultipartParser.h
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
#ifndef _MULTIPART_PARSER_H_
#define _MULTIPART_PARSER_H_
#include <sys/types.h>
#include <string>
#include <stdexcept>
class MultipartParser {
public:
typedef void (*Callback)(const char *buffer, size_t start, size_t end, void *userData);
private:
static const char CR = 13;
static const char LF = 10;
static const char SPACE = 32;
static const char HYPHEN = 45;
static const char COLON = 58;
static const size_t UNMARKED = (size_t) -1;
enum State {
ERROR,
START,
START_BOUNDARY,
HEADER_FIELD_START,
HEADER_FIELD,
HEADER_VALUE_START,
HEADER_VALUE,
HEADER_VALUE_ALMOST_DONE,
HEADERS_ALMOST_DONE,
PART_DATA_START,
PART_DATA,
PART_END,
END
};
enum Flags {
PART_BOUNDARY = 1,
LAST_BOUNDARY = 2
};
std::string boundary;
const char *boundaryData;
size_t boundarySize;
bool boundaryIndex[256];
char *lookbehind;
size_t lookbehindSize;
State state;
int flags;
size_t index;
size_t headerFieldMark;
size_t headerValueMark;
size_t partDataMark;
const char *errorReason;
void resetCallbacks() {
onPartBegin = NULL;
onHeaderField = NULL;
onHeaderValue = NULL;
onHeaderEnd = NULL;
onHeadersEnd = NULL;
onPartData = NULL;
onPartEnd = NULL;
onEnd = NULL;
userData = NULL;
}
void indexBoundary() {
const char *current;
const char *end = boundaryData + boundarySize;
memset(boundaryIndex, 0, sizeof(boundaryIndex));
for (current = boundaryData; current < end; current++) {
boundaryIndex[(unsigned char) *current] = true;
}
}
void callback(Callback cb, const char *buffer = NULL, size_t start = UNMARKED,
size_t end = UNMARKED, bool allowEmpty = false)
{
if (start != UNMARKED && start == end && !allowEmpty) {
return;
}
if (cb != NULL) {
cb(buffer, start, end, userData);
}
}
void dataCallback(Callback cb, size_t &mark, const char *buffer, size_t i, size_t bufferLen,
bool clear, bool allowEmpty = false)
{
if (mark == UNMARKED) {
return;
}
if (!clear) {
callback(cb, buffer, mark, bufferLen, allowEmpty);
mark = 0;
} else {
callback(cb, buffer, mark, i, allowEmpty);
mark = UNMARKED;
}
}
char lower(char c) const {
return c | 0x20;
}
inline bool isBoundaryChar(char c) const {
return boundaryIndex[(unsigned char) c];
}
bool isHeaderFieldCharacter(char c) const {
return (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| c == HYPHEN;
}
void setError(const char *message) {
state = ERROR;
errorReason = message;
}
void processPartData(size_t &prevIndex, size_t &index, const char *buffer,
size_t len, size_t boundaryEnd, size_t &i, char c, State &state, int &flags)
{
prevIndex = index;
if (index == 0) {
// boyer-moore derived algorithm to safely skip non-boundary data
while (i + boundarySize <= len) {
if (isBoundaryChar(buffer[i + boundaryEnd])) {
break;
}
i += boundarySize;
}
c = buffer[i];
}
if (index < boundarySize) {
if (boundary[index] == c) {
if (index == 0) {
dataCallback(onPartData, partDataMark, buffer, i, len, true);
}
index++;
} else {
index = 0;
}
} else if (index == boundarySize) {
index++;
if (c == CR) {
// CR = part boundary
flags |= PART_BOUNDARY;
} else if (c == HYPHEN) {
// HYPHEN = end boundary
flags |= LAST_BOUNDARY;
} else {
index = 0;
}
} else if (index - 1 == boundarySize) {
if (flags & PART_BOUNDARY) {
index = 0;
if (c == LF) {
// unset the PART_BOUNDARY flag
flags &= ~PART_BOUNDARY;
callback(onPartEnd);
callback(onPartBegin);
state = HEADER_FIELD_START;
return;
}
} else if (flags & LAST_BOUNDARY) {
if (c == HYPHEN) {
callback(onPartEnd);
callback(onEnd);
state = END;
} else {
index = 0;
}
} else {
index = 0;
}
} else if (index - 2 == boundarySize) {
if (c == CR) {
index++;
} else {
index = 0;
}
} else if (index - boundarySize == 3) {
index = 0;
if (c == LF) {
callback(onPartEnd);
callback(onEnd);
state = END;
return;
}
}
if (index > 0) {
// when matching a possible boundary, keep a lookbehind reference
// in case it turns out to be a false lead
if (index - 1 >= lookbehindSize) {
setError("Parser bug: index overflows lookbehind buffer. "
"Please send bug report with input file attached.");
throw std::out_of_range("index overflows lookbehind buffer");
} else if (index - 1 < 0) {
setError("Parser bug: index underflows lookbehind buffer. "
"Please send bug report with input file attached.");
throw std::out_of_range("index underflows lookbehind buffer");
}
lookbehind[index - 1] = c;
} else if (prevIndex > 0) {
// if our boundary turned out to be rubbish, the captured lookbehind
// belongs to partData
callback(onPartData, lookbehind, 0, prevIndex);
prevIndex = 0;
partDataMark = i;
// reconsider the current character even so it interrupted the sequence
// it could be the beginning of a new sequence
i--;
}
}
public:
Callback onPartBegin;
Callback onHeaderField;
Callback onHeaderValue;
Callback onHeaderEnd;
Callback onHeadersEnd;
Callback onPartData;
Callback onPartEnd;
Callback onEnd;
void *userData;
MultipartParser() {
lookbehind = NULL;
resetCallbacks();
reset();
}
MultipartParser(const std::string &boundary) {
lookbehind = NULL;
resetCallbacks();
setBoundary(boundary);
}
~MultipartParser() {
delete[] lookbehind;
}
void reset() {
delete[] lookbehind;
state = ERROR;
boundary.clear();
boundaryData = boundary.c_str();
boundarySize = 0;
lookbehind = NULL;
lookbehindSize = 0;
flags = 0;
index = 0;
headerFieldMark = UNMARKED;
headerValueMark = UNMARKED;
partDataMark = UNMARKED;
errorReason = "Parser uninitialized.";
}
void setBoundary(const std::string &boundary) {
reset();
this->boundary = "\r\n--" + boundary;
boundaryData = this->boundary.c_str();
boundarySize = this->boundary.size();
indexBoundary();
lookbehind = new char[boundarySize + 8];
lookbehindSize = boundarySize + 8;
state = START;
errorReason = "No error.";
}
size_t feed(const char *buffer, size_t len) {
if (state == ERROR || len == 0) {
return 0;
}
State state = this->state;
int flags = this->flags;
size_t prevIndex = this->index;
size_t index = this->index;
size_t boundaryEnd = boundarySize - 1;
size_t i;
char c, cl;
for (i = 0; i < len; i++) {
c = buffer[i];
switch (state) {
case ERROR:
return i;
case START:
index = 0;
state = START_BOUNDARY;
case START_BOUNDARY:
if (index == boundarySize - 2) {
if (c != CR) {
setError("Malformed. Expected CR after boundary.");
return i;
}
index++;
break;
} else if (index - 1 == boundarySize - 2) {
if (c != LF) {
setError("Malformed. Expected LF after boundary CR.");
return i;
}
index = 0;
callback(onPartBegin);
state = HEADER_FIELD_START;
break;
}
if (c != boundary[index + 2]) {
setError("Malformed. Found different boundary data than the given one.");
return i;
}
index++;
break;
case HEADER_FIELD_START:
state = HEADER_FIELD;
headerFieldMark = i;
index = 0;
case HEADER_FIELD:
if (c == CR) {
headerFieldMark = UNMARKED;
state = HEADERS_ALMOST_DONE;
break;
}
index++;
if (c == HYPHEN) {
break;
}
if (c == COLON) {
if (index == 1) {
// empty header field
setError("Malformed first header name character.");
return i;
}
dataCallback(onHeaderField, headerFieldMark, buffer, i, len, true);
state = HEADER_VALUE_START;
break;
}
cl = lower(c);
if (cl < 'a' || cl > 'z') {
setError("Malformed header name.");
return i;
}
break;
case HEADER_VALUE_START:
if (c == SPACE) {
break;
}
headerValueMark = i;
state = HEADER_VALUE;
case HEADER_VALUE:
if (c == CR) {
dataCallback(onHeaderValue, headerValueMark, buffer, i, len, true, true);
callback(onHeaderEnd);
state = HEADER_VALUE_ALMOST_DONE;
}
break;
case HEADER_VALUE_ALMOST_DONE:
if (c != LF) {
setError("Malformed header value: LF expected after CR");
return i;
}
state = HEADER_FIELD_START;
break;
case HEADERS_ALMOST_DONE:
if (c != LF) {
setError("Malformed header ending: LF expected after CR");
return i;
}
callback(onHeadersEnd);
state = PART_DATA_START;
break;
case PART_DATA_START:
state = PART_DATA;
partDataMark = i;
case PART_DATA:
processPartData(prevIndex, index, buffer, len, boundaryEnd, i, c, state, flags);
break;
default:
return i;
}
}
dataCallback(onHeaderField, headerFieldMark, buffer, i, len, false);
dataCallback(onHeaderValue, headerValueMark, buffer, i, len, false);
dataCallback(onPartData, partDataMark, buffer, i, len, false);
this->index = index;
this->state = state;
this->flags = flags;
return len;
}
bool succeeded() const {
return state == END;
}
bool hasError() const {
return state == ERROR;
}
bool stopped() const {
return state == ERROR || state == END;
}
const char *getErrorMessage() const {
return errorReason;
}
};
#endif /* _MULTIPART_PARSER_H_ */