-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathMultipartReader.h
157 lines (132 loc) · 4.12 KB
/
MultipartReader.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
#ifndef _MULTIPART_READER_H_
#define _MULTIPART_READER_H_
#include <map>
#include <utility>
#include "MultipartParser.h"
class MultipartHeaders: public std::multimap<std::string, std::string> {
private:
std::string empty;
public:
const std::string &operator[](const std::string &key) const {
const_iterator it = find(key);
if (it == end()) {
return empty;
} else {
return it->second;
}
}
};
class MultipartReader {
public:
typedef void (*PartBeginCallback)(const MultipartHeaders &headers, void *userData);
typedef void (*PartDataCallback)(const char *buffer, size_t size, void *userData);
typedef void (*Callback)(void *userData);
private:
MultipartParser parser;
bool headersProcessed;
MultipartHeaders currentHeaders;
std::string currentHeaderName, currentHeaderValue;
void resetReaderCallbacks() {
onPartBegin = NULL;
onPartData = NULL;
onPartEnd = NULL;
onEnd = NULL;
userData = NULL;
}
void setParserCallbacks() {
parser.onPartBegin = cbPartBegin;
parser.onHeaderField = cbHeaderField;
parser.onHeaderValue = cbHeaderValue;
parser.onHeaderEnd = cbHeaderEnd;
parser.onHeadersEnd = cbHeadersEnd;
parser.onPartData = cbPartData;
parser.onPartEnd = cbPartEnd;
parser.onEnd = cbEnd;
parser.userData = this;
}
static void cbPartBegin(const char *buffer, size_t start, size_t end, void *userData) {
MultipartReader *self = (MultipartReader *) userData;
self->headersProcessed = false;
self->currentHeaders.clear();
self->currentHeaderName.clear();
self->currentHeaderValue.clear();
}
static void cbHeaderField(const char *buffer, size_t start, size_t end, void *userData) {
MultipartReader *self = (MultipartReader *) userData;
self->currentHeaderName.append(buffer + start, end - start);
}
static void cbHeaderValue(const char *buffer, size_t start, size_t end, void *userData) {
MultipartReader *self = (MultipartReader *) userData;
self->currentHeaderValue.append(buffer + start, end - start);
}
static void cbHeaderEnd(const char *buffer, size_t start, size_t end, void *userData) {
MultipartReader *self = (MultipartReader *) userData;
self->currentHeaders.insert(std::make_pair(self->currentHeaderName,
self->currentHeaderValue));
self->currentHeaderName.clear();
self->currentHeaderValue.clear();
}
static void cbHeadersEnd(const char *buffer, size_t start, size_t end, void *userData) {
MultipartReader *self = (MultipartReader *) userData;
if (self->onPartBegin != NULL) {
self->onPartBegin(self->currentHeaders, self->userData);
}
self->currentHeaders.clear();
self->currentHeaderName.clear();
self->currentHeaderValue.clear();
}
static void cbPartData(const char *buffer, size_t start, size_t end, void *userData) {
MultipartReader *self = (MultipartReader *) userData;
if (self->onPartData != NULL) {
self->onPartData(buffer + start, end - start, self->userData);
}
}
static void cbPartEnd(const char *buffer, size_t start, size_t end, void *userData) {
MultipartReader *self = (MultipartReader *) userData;
if (self->onPartEnd != NULL) {
self->onPartEnd(self->userData);
}
}
static void cbEnd(const char *buffer, size_t start, size_t end, void *userData) {
MultipartReader *self = (MultipartReader *) userData;
if (self->onEnd != NULL) {
self->onEnd(self->userData);
}
}
public:
PartBeginCallback onPartBegin;
PartDataCallback onPartData;
Callback onPartEnd;
Callback onEnd;
void *userData;
MultipartReader() {
resetReaderCallbacks();
setParserCallbacks();
}
MultipartReader(const std::string &boundary): parser(boundary) {
resetReaderCallbacks();
setParserCallbacks();
}
void reset() {
parser.reset();
}
void setBoundary(const std::string &boundary) {
parser.setBoundary(boundary);
}
size_t feed(const char *buffer, size_t len) {
return parser.feed(buffer, len);
}
bool succeeded() const {
return parser.succeeded();
}
bool hasError() const {
return parser.hasError();
}
bool stopped() const {
return parser.stopped();
}
const char *getErrorMessage() const {
return parser.getErrorMessage();
}
};
#endif /* _MULTIPART_READER_H_ */