5 #include "multipartformdataparser_p.h"
14 if (body->isSequential()) {
15 qCWarning(CUTELYST_MULTIPART) <<
"Parsing sequential body is not supported" << body;
19 int start = contentType.indexOf(u
"boundary=");
21 qCWarning(CUTELYST_MULTIPART) <<
"No boundary match" << contentType;
27 const int len = contentType.length();
28 boundary.reserve(contentType.length() - start + 2);
30 for (
int i = start, quotes = 0; i < len; ++i) {
31 const QChar ch = contentType.at(i);
33 if ((quotes == 0 && i > start) || ++quotes == 2) {
36 }
else if (ch == u
';') {
39 boundary.append(ch.toLatin1());
43 if (boundary.isEmpty()) {
44 qCWarning(CUTELYST_MULTIPART) <<
"Boundary match was empty" << contentType;
47 boundary.prepend(
"--", 2);
49 if (bufferSize < 1024) {
52 char *buffer =
new char[bufferSize];
54 ret = MultiPartFormDataParserPrivate::execute(buffer, bufferSize, body, boundary);
61 Uploads MultiPartFormDataParserPrivate::execute(
char *buffer,
int bufferSize, QIODevice *body,
const QByteArray &boundary)
64 QByteArray headerLine;
68 qint64 contentLength = body->size();
70 int boundarySize = boundary.size();
71 ParserState state = FindBoundary;
72 QByteArrayMatcher matcher(boundary);
74 while (pos < contentLength) {
75 qint64 len = body->read(buffer + bufferSkip, bufferSize - bufferSkip);
77 qCWarning(CUTELYST_MULTIPART) <<
"Error while reading POST body" << body->errorString();
88 i += findBoundary(buffer + i, len - i, matcher, boundarySize, state);
92 if (buffer[i] !=
'\r') {
96 state = EndBoundaryLF;
99 if (buffer[i] !=
'\n') {
103 state = StartHeaders;
106 if (headerLine.isEmpty() && buffer[i] ==
'\r') {
110 char *pch =
static_cast<char *
>(memchr(buffer + i,
'\r', len - i));
112 headerLine.append(buffer + i, len - i);
115 headerLine.append(buffer + i, pch - buffer - i);
117 state = FinishHeader;
122 if (buffer[i] ==
'\n') {
123 int dotdot = headerLine.indexOf(
':');
124 headers.
setHeader(QString::fromLatin1(headerLine.left(dotdot)),
125 QString::fromUtf8(headerLine.mid(dotdot + 1).trimmed()));
126 headerLine = QByteArray();
127 state = StartHeaders;
134 if (buffer[i] ==
'\n') {
143 startOffset = pos - len + i;
146 i += findBoundary(buffer + i, len - i, matcher, boundarySize, state);
148 if (state == EndBoundaryCR) {
150 const qint64 endOffset = pos - len + i - boundarySize - 1;
151 auto upload =
new Upload(
new UploadPrivate(body, headers, startOffset, endOffset));
158 bufferSkip = boundarySize - 1;
159 memmove(buffer, buffer + len - bufferSkip, bufferSkip);
171 int MultiPartFormDataParserPrivate::findBoundary(
char *buffer,
int len,
const QByteArrayMatcher &matcher,
int boundarySize, MultiPartFormDataParserPrivate::ParserState &state)
173 int i = matcher.indexIn(buffer, len);
177 state = EndBoundaryCR;
178 return i + boundarySize - 1;
183 #include "moc_multipartformdataparser_p.cpp"
Cutelyst Upload handles file upload request
The Cutelyst namespace holds all public Cutelyst API.
QVector< Upload * > Uploads