-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cpp
272 lines (218 loc) · 8.93 KB
/
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
// This file is distributed under GPLv3 licence
// Author: Gorelov Grigory (gorelov@grigory.info)
//
// Contacts and other info are on the WEB page: grigory.info/MPFDParser
#include "includes/Parser.h"
std::map<std::string, MPFD::Field *> MPFD::Parser::GetFieldsMap() {
return Fields;
}
MPFD::Field * MPFD::Parser::GetField(std::string Name) {
if (Fields.count(Name)) {
return Fields[Name];
} else {
return NULL;
}
}
MPFD::Parser::Parser() {
DataCollector = NULL;
DataCollectorLength = 0;
_HeadersOfTheFieldAreProcessed = false;
CurrentStatus = Status_LookingForStartingBoundary;
MaxDataCollectorLength = 16 * 1024 * 1024; // 16 Mb default data collector size.
SetUploadedFilesStorage(StoreUploadedFilesInFilesystem);
}
MPFD::Parser::~Parser() {
std::map<std::string, Field *>::iterator it;
for (it = Fields.begin(); it != Fields.end(); it++) {
delete it->second;
}
if (DataCollector) {
delete DataCollector;
}
}
void MPFD::Parser::SetContentType(const std::string type) {
if (type.find("multipart/form-data;") != 0) {
throw MPFD::Exception(std::string("Content type is not \"multipart/form-data\"\nIt is \"") + type + std::string("\""));
}
int bp = type.find("boundary=");
if (bp == std::string::npos) {
throw MPFD::Exception(std::string("Cannot find boundary in Content-type: \"") + type + std::string("\""));
}
Boundary = std::string("--") + type.substr(bp + 9, type.length() - bp);
}
void MPFD::Parser::AcceptSomeData(const char *data, const long length) {
if (Boundary.length() > 0) {
// Append data to existing accumulator
if (DataCollector == NULL) {
DataCollector = new char[length];
memcpy(DataCollector, data, length);
DataCollectorLength = length;
} else {
DataCollector = (char*) realloc(DataCollector, DataCollectorLength + length);
memcpy(DataCollector + DataCollectorLength, data, length);
DataCollectorLength += length;
}
if (DataCollectorLength > MaxDataCollectorLength) {
throw Exception("Maximum data collector length reached.");
}
_ProcessData();
} else {
throw MPFD::Exception("Accepting data, but content type was not set.");
}
}
void MPFD::Parser::_ProcessData() {
// If some data left after truncate, process it right now.
// Do not wait for AcceptSomeData called again
bool NeedToRepeat;
do {
NeedToRepeat = false;
switch (CurrentStatus) {
case Status_LookingForStartingBoundary:
if (FindStartingBoundaryAndTruncData()) {
CurrentStatus = Status_ProcessingHeaders;
NeedToRepeat = true;
}
break;
case Status_ProcessingHeaders:
if (WaitForHeadersEndAndParseThem()) {
CurrentStatus = Status_ProcessingContentOfTheField;
NeedToRepeat = true;
}
break;
case Status_ProcessingContentOfTheField:
if (ProcessContentOfTheField()) {
CurrentStatus = Status_LookingForStartingBoundary;
NeedToRepeat = true;
}
break;
}
} while (NeedToRepeat);
}
bool MPFD::Parser::ProcessContentOfTheField() {
long BoundaryPosition = BoundaryPositionInDataCollector();
long DataLengthToSendToField;
if (BoundaryPosition >= 0) {
// 2 is the \r\n before boundary we do not need them
DataLengthToSendToField = BoundaryPosition - 2;
} else {
// We need to save +2 chars for \r\n chars before boundary
DataLengthToSendToField = DataCollectorLength - (Boundary.length() + 2);
}
if (DataLengthToSendToField > 0) {
Fields[ProcessingFieldName]->AcceptSomeData(DataCollector, DataLengthToSendToField);
TruncateDataCollectorFromTheBeginning(DataLengthToSendToField);
}
if (BoundaryPosition >= 0) {
CurrentStatus = Status_LookingForStartingBoundary;
return true;
} else {
return false;
}
}
bool MPFD::Parser::WaitForHeadersEndAndParseThem() {
for (int i = 0; i < DataCollectorLength - 3; i++) {
if ((DataCollector[i] == 13) && (DataCollector[i + 1] == 10) && (DataCollector[i + 2] == 13) && (DataCollector[i + 3] == 10)) {
long headers_length = i;
char *headers = new char[headers_length + 1];
memset(headers, 0, headers_length + 1);
memcpy(headers, DataCollector, headers_length);
_ParseHeaders(std::string(headers));
TruncateDataCollectorFromTheBeginning(i + 4);
delete headers;
return true;
}
}
return false;
}
void MPFD::Parser::SetUploadedFilesStorage(int where) {
WhereToStoreUploadedFiles = where;
}
void MPFD::Parser::SetTempDirForFileUpload(std::string dir) {
TempDirForFileUpload = dir;
}
void MPFD::Parser::_ParseHeaders(std::string headers) {
// Check if it is form data
if (headers.find("Content-Disposition: form-data;") == std::string::npos) {
throw Exception(std::string("Accepted headers of field does not contain \"Content-Disposition: form-data;\"\nThe headers are: \"") + headers + std::string("\""));
}
// Find name
long name_pos = headers.find("name=\"");
if (name_pos == std::string::npos) {
throw Exception(std::string("Accepted headers of field does not contain \"name=\".\nThe headers are: \"") + headers + std::string("\""));
} else {
long name_end_pos = headers.find("\"", name_pos + 6);
if (name_end_pos == std::string::npos) {
throw Exception(std::string("Cannot find closing quote of \"name=\" attribute.\nThe headers are: \"") + headers + std::string("\""));
} else {
ProcessingFieldName = headers.substr(name_pos + 6, name_end_pos - (name_pos + 6));
Fields[ProcessingFieldName] = new Field();
}
// find filename if exists
long filename_pos = headers.find("filename=\"");
if (filename_pos == std::string::npos) {
Fields[ProcessingFieldName]->SetType(Field::TextType);
} else {
Fields[ProcessingFieldName]->SetType(Field::FileType);
Fields[ProcessingFieldName]->SetTempDir(TempDirForFileUpload);
Fields[ProcessingFieldName]->SetUploadedFilesStorage(WhereToStoreUploadedFiles);
long filename_end_pos = headers.find("\"", filename_pos + 10);
if (filename_end_pos == std::string::npos) {
throw Exception(std::string("Cannot find closing quote of \"filename=\" attribute.\nThe headers are: \"") + headers + std::string("\""));
} else {
std::string filename = headers.substr(filename_pos + 10, filename_end_pos - (filename_pos + 10));
Fields[ProcessingFieldName]->SetFileName(filename);
}
// find Content-Type if exists
long content_type_pos = headers.find("Content-Type: ");
if (content_type_pos != std::string::npos) {
long content_type_end_pos = 0;
for (int i = content_type_pos + 14; (i < headers.length()) && (!content_type_end_pos); i++) {
if ((headers[i] == ' ') || (headers[i] == 10) || (headers[i] == 13)) {
content_type_end_pos = i - 1;
}
}
std::string content_type = headers.substr(content_type_pos + 14, content_type_end_pos - (content_type_pos + 14));
Fields[ProcessingFieldName]->SetFileContentType(content_type);
}
}
}
}
void MPFD::Parser::SetMaxCollectedDataLength(long max) {
MaxDataCollectorLength = max;
}
void MPFD::Parser::TruncateDataCollectorFromTheBeginning(long n) {
long TruncatedDataCollectorLength = DataCollectorLength - n;
char *tmp = DataCollector;
DataCollector = new char[TruncatedDataCollectorLength];
memcpy(DataCollector, tmp + n, TruncatedDataCollectorLength);
DataCollectorLength = TruncatedDataCollectorLength;
delete tmp;
}
long MPFD::Parser::BoundaryPositionInDataCollector() {
const char *b = Boundary.c_str();
int bl = Boundary.length();
if (DataCollectorLength >= bl) {
bool found = false;
for (int i = 0; (i <= DataCollectorLength - bl) && (!found); i++) {
found = true;
for (int j = 0; (j < bl) && (found); j++) {
if (DataCollector[i + j] != b[j]) {
found = false;
}
}
if (found) {
return i;
}
}
}
return -1;
}
bool MPFD::Parser::FindStartingBoundaryAndTruncData() {
long n = BoundaryPositionInDataCollector();
if (n >= 0) {
TruncateDataCollectorFromTheBeginning(n + Boundary.length());
return true;
} else {
return false;
}
}