summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMikhail Svetkin <mikhail.svetkin@qt.io>2019-03-30 12:48:47 +0300
committerJesus Fernandez <Jesus.Fernandez@qt.io>2019-04-03 15:26:38 +0000
commit6f7e8d28b46dea47c9179f4a69bc693e844f4d1b (patch)
tree24d592e92b0fa92dd0b5b51521dc0f3f4e93b1f1
parent5ad0dd40505f93f0c7cec17adebfd3bcaf9ee3a8 (diff)
Fix HTTP chunked request body handling
Task: QTBUG-74843 Change-Id: I4978c80195246c99a5e6d1e608b3980f56b39207 Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
-rw-r--r--src/httpserver/qhttpserverrequest.cpp8
-rw-r--r--tests/auto/qhttpserver/tst_qhttpserver.cpp45
2 files changed, 50 insertions, 3 deletions
diff --git a/src/httpserver/qhttpserverrequest.cpp b/src/httpserver/qhttpserverrequest.cpp
index f48864b..f6c52dd 100644
--- a/src/httpserver/qhttpserverrequest.cpp
+++ b/src/httpserver/qhttpserverrequest.cpp
@@ -209,7 +209,13 @@ int QHttpServerRequestPrivate::onBody(http_parser *httpParser, const char *at, s
qCDebug(lc) << httpParser << QString::fromUtf8(at, int(length));
auto i = instance(httpParser);
i->state = State::OnBody;
- i->body = QByteArray(at, int(length));
+ if (i->body.isEmpty()) {
+ i->body.reserve(
+ static_cast<int>(httpParser->content_length) +
+ static_cast<int>(length));
+ }
+
+ i->body.append(at, int(length));
return 0;
}
diff --git a/tests/auto/qhttpserver/tst_qhttpserver.cpp b/tests/auto/qhttpserver/tst_qhttpserver.cpp
index 6193fb5..2934dc0 100644
--- a/tests/auto/qhttpserver/tst_qhttpserver.cpp
+++ b/tests/auto/qhttpserver/tst_qhttpserver.cpp
@@ -166,6 +166,12 @@ void tst_QHttpServer::initTestCase()
httpserver.route("/check-custom-type/", [] (const CustomArg &customArg) {
return QString("data = %1").arg(customArg.data);
});
+
+ httpserver.route("/post-body", "POST", [] (const QHttpServerRequest &request) {
+ return request.body();
+ });
+
+ urlBase = QStringLiteral("http://localhost:%1%2").arg(httpserver.listen());
}
void tst_QHttpServer::routeGet_data()
@@ -334,6 +340,38 @@ void tst_QHttpServer::routePost_data()
<< "text/html"
<< ""
<< "Hello world post";
+
+ QTest::addRow("post-and-get, post")
+ << "/post-and-get"
+ << 200
+ << "text/html"
+ << ""
+ << "Hello world post";
+
+ QTest::addRow("any, post")
+ << "/any"
+ << 200
+ << "text/html"
+ << ""
+ << "Post";
+
+ QTest::addRow("post-body")
+ << "/post-body"
+ << 200
+ << "text/html"
+ << "some post data"
+ << "some post data";
+
+ QString body;
+ for (int i = 0; i < 10000; i++)
+ body.append(QString::number(i));
+
+ QTest::addRow("post-body - huge body, chunk test")
+ << "/post-body"
+ << 200
+ << "text/html"
+ << body
+ << body;
}
void tst_QHttpServer::routePost()
@@ -345,8 +383,11 @@ void tst_QHttpServer::routePost()
QFETCH(QString, body);
QNetworkAccessManager networkAccessManager;
- const QUrl requestUrl(urlBase.arg(url));
- auto reply = networkAccessManager.post(QNetworkRequest(requestUrl), data.toUtf8());
+ QNetworkRequest request(QUrl(urlBase.arg(url)));
+ if (data.size())
+ request.setHeader(QNetworkRequest::ContentTypeHeader, "text/html");
+
+ auto reply = networkAccessManager.post(request, data.toUtf8());
QTRY_VERIFY(reply->isFinished());