forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultipartformdataparser.cpp
196 lines (177 loc) · 6.99 KB
/
multipartformdataparser.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
/*
* Copyright (C) 2014-2017 Daniel Nicoletti <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "multipartformdataparser_p.h"
#include "upload_p.h"
#include "common.h"
using namespace Cutelyst;
Uploads MultiPartFormDataParser::parse(QIODevice *body, const QString &contentType, int bufferSize)
{
Uploads ret;
if (body->isSequential()) {
qCWarning(CUTELYST_MULTIPART) << "Parsing sequential body is not supported" << body;
return ret;
}
int start = contentType.indexOf(QLatin1String("boundary="));
if (start == -1) {
qCWarning(CUTELYST_MULTIPART) << "No boudary match" << contentType;
return ret;
}
start += 9;
QByteArray boundary;
const int len = contentType.length();
boundary.reserve(contentType.length() - start + 2);
for (int i = start, quotes = 0; i < len; ++i) {
const QChar ch = contentType.at(i);
if (ch == QLatin1Char('\"')) {
if ((quotes == 0 && i > start) || ++quotes == 2) {
break;
}
} else if (ch == QLatin1Char(';')) {
break;
} else {
boundary.append(ch.toLatin1());
}
}
if (boundary.isEmpty()) {
qCWarning(CUTELYST_MULTIPART) << "Boudary match was empty" << contentType;
return ret;
}
boundary.prepend("--", 2);
if (bufferSize < 1024) {
bufferSize = 1024;
}
char *buffer = new char[bufferSize];
ret = MultiPartFormDataParserPrivate::execute(buffer, bufferSize, body, boundary);
delete [] buffer;
return ret;
}
Uploads MultiPartFormDataParserPrivate::execute(char *buffer, int bufferSize, QIODevice *body, const QByteArray &boundary)
{
Uploads ret;
QByteArray headerLine;
Headers headers;
qint64 startOffset;
qint64 pos = 0;
qint64 contentLength = body->size();
int bufferSkip = 0;
int boundarySize = boundary.size();
ParserState state = FindBoundary;
QByteArrayMatcher matcher(boundary);
while (pos < contentLength) {
qint64 len = body->read(buffer + bufferSkip, bufferSize - bufferSkip);
if (len < 0) {
qCWarning(CUTELYST_MULTIPART) << "Error while reading POST body" << body->errorString();
return ret;
}
pos += len;
len += bufferSkip;
bufferSkip = 0;
int i = 0;
while (i < len) {
switch (state) {
case FindBoundary:
i += findBoundary(buffer + i, len - i, matcher, boundarySize, state);
break;
case EndBoundaryCR:
// TODO the "--" case
if (buffer[i] != '\r') {
// qCDebug(CUTELYST_MULTIPART) << "EndBoundaryCR return!";
return ret;
}
state = EndBoundaryLF;
break;
case EndBoundaryLF:
if (buffer[i] != '\n') {
// qCDebug(CUTELYST_MULTIPART) << "EndBoundaryLF return!";
return ret;
}
state = StartHeaders;
break;
case StartHeaders:
if (headerLine.isEmpty() && buffer[i] == '\r') {
// nothing was read
state = EndHeaders;
} else {
char *pch = static_cast<char *>(memchr(buffer + i, '\r', len - i));
if (pch == NULL) {
headerLine.append(buffer + i, len - i);
i = len;
} else {
headerLine.append(buffer + i, pch - buffer - i);
i = pch - buffer;
state = FinishHeader;
}
}
break;
case FinishHeader:
if (buffer[i] == '\n') {
int dotdot = headerLine.indexOf(':');
headers.setHeader(QString::fromLatin1(headerLine.left(dotdot)),
QString::fromLatin1(headerLine.mid(dotdot + 1).trimmed()));
headerLine = QByteArray();
state = StartHeaders;
} else {
// qCDebug(CUTELYST_MULTIPART) << "FinishHeader return!";
return ret;
}
break;
case EndHeaders:
if (buffer[i] == '\n') {
state = StartData;
} else {
// qCDebug(CUTELYST_MULTIPART) << "EndHeaders return!";
return ret;
}
break;
case StartData:
// qCDebug(CUTELYST_MULTIPART) << "StartData" << body->pos() - len + i;
startOffset = pos - len + i;
state = EndData;
case EndData:
i += findBoundary(buffer + i, len - i, matcher, boundarySize, state);
if (state == EndBoundaryCR) {
// qCDebug(CUTELYST_MULTIPART) << "EndData" << body->pos() - len + i - boundaryLength - 1;
int endOffset = pos - len + i - boundarySize - 1;
auto upload = new Upload(new UploadPrivate(body, headers, startOffset, endOffset));
ret.append(upload);
headers = Headers();
} else {
// Boundary was not found so move the boundary size at end of the buffer
// to be sure we don't have the boundary in the middle of two chunks
bufferSkip = boundarySize - 1;
memmove(buffer, buffer + len - bufferSkip, bufferSkip);
}
break;
}
++i;
}
}
return ret;
}
int MultiPartFormDataParserPrivate::findBoundary(char *buffer, int len, const QByteArrayMatcher &matcher, int boundarySize, MultiPartFormDataParserPrivate::ParserState &state)
{
int i = matcher.indexIn(buffer, len);
// qCDebug(CUTELYST_MULTIPART) << "findBoundary" << QByteArray(buffer, len);
if (i != -1) {
// qCDebug(CUTELYST_MULTIPART) << "FindBoundary: found at" << i << body->pos() << len << body->pos() - len + i << i + boundaryLength;
state = EndBoundaryCR;
return i + boundarySize - 1;
}
return len;
}
#include "moc_multipartformdataparser_p.cpp"