From 43a04e17a1e10425da84b6c725a03dc87834b64a Mon Sep 17 00:00:00 2001 From: Mikhail Svetkin Date: Sun, 1 Sep 2019 21:33:22 +0200 Subject: [PATCH] Allow using QHttpServerRequest/Responder at the same time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: QTBUG-77088 Change-Id: I2208cee290d086339aba705f959c57e0f6a73a87 Reviewed-by: MÃ¥rten Nordheim --- src/httpserver/qhttpserver.h | 24 +++++++++++++++-- src/httpserver/qhttpserverrouterviewtraits.h | 4 ++- tests/auto/qhttpserver/tst_qhttpserver.cpp | 27 +++++++++++++++++++ .../tst_qhttpserverrouter.cpp | 12 ++++----- 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/httpserver/qhttpserver.h b/src/httpserver/qhttpserver.h index d5ebdcb..80bad2c 100644 --- a/src/httpserver/qhttpserver.h +++ b/src/httpserver/qhttpserver.h @@ -112,7 +112,16 @@ private: } template - typename std::enable_if::type + typename std::enable_if::type + responseImpl(T &boundViewHandler, const QHttpServerRequest &request, QTcpSocket *socket) + { + boundViewHandler(makeResponder(request, socket), request); + } + + template + typename std::enable_if::type responseImpl(T &boundViewHandler, const QHttpServerRequest &request, QTcpSocket *socket) { const QHttpServerResponse response(boundViewHandler(request)); @@ -120,7 +129,18 @@ private: } template - typename std::enable_if::type + typename std::enable_if::type + responseImpl(T &boundViewHandler, + const QHttpServerRequest &request, + QTcpSocket *socket) + { + boundViewHandler(request, makeResponder(request, socket)); + } + + template + typename std::enable_if::type responseImpl(T &boundViewHandler, const QHttpServerRequest &request, QTcpSocket *socket) diff --git a/src/httpserver/qhttpserverrouterviewtraits.h b/src/httpserver/qhttpserverrouterviewtraits.h index b4ce4fb..377811c 100644 --- a/src/httpserver/qhttpserverrouterviewtraits.h +++ b/src/httpserver/qhttpserverrouterviewtraits.h @@ -169,8 +169,10 @@ struct ViewTraitsHelper { static constexpr bool TypeMatched = isType(); static constexpr bool TypeCVRefMatched = isType(); + static constexpr bool ValidPosition = - (I == FunctionTraits::ArgumentIndexMax); + (I == FunctionTraits::ArgumentIndexMax || + I == FunctionTraits::ArgumentIndexMax - 1); static constexpr bool ValidAll = TypeCVRefMatched && ValidPosition; static constexpr bool assertCondition = diff --git a/tests/auto/qhttpserver/tst_qhttpserver.cpp b/tests/auto/qhttpserver/tst_qhttpserver.cpp index ef5290e..ec4fad3 100644 --- a/tests/auto/qhttpserver/tst_qhttpserver.cpp +++ b/tests/auto/qhttpserver/tst_qhttpserver.cpp @@ -149,6 +149,19 @@ struct CustomArg { void tst_QHttpServer::initTestCase() { + + httpserver.route("/req-and-resp", [] (QHttpServerResponder &&resp, + const QHttpServerRequest &req) { + resp.write(req.body(), + QHttpServerLiterals::contentTypeTextHtml()); + }); + + httpserver.route("/resp-and-req", [] (const QHttpServerRequest &req, + QHttpServerResponder &&resp) { + resp.write(req.body(), + QHttpServerLiterals::contentTypeTextHtml()); + }); + httpserver.route("/test", [] (QHttpServerResponder &&responder) { responder.write("test msg", QHttpServerLiterals::contentTypeTextHtml()); @@ -655,6 +668,20 @@ void tst_QHttpServer::routePost_data() << body << body; + QTest::addRow("req-and-resp") + << urlBase.arg("/req-and-resp") + << 200 + << "text/html" + << "test" + << "test"; + + QTest::addRow("resp-and-req") + << urlBase.arg("/resp-and-req") + << 200 + << "text/html" + << "test" + << "test"; + #if QT_CONFIG(ssl) QTest::addRow("post-and-get, post, ssl") diff --git a/tests/auto/qhttpserverrouter/tst_qhttpserverrouter.cpp b/tests/auto/qhttpserverrouter/tst_qhttpserverrouter.cpp index 4f70018..5536408 100644 --- a/tests/auto/qhttpserverrouter/tst_qhttpserverrouter.cpp +++ b/tests/auto/qhttpserverrouter/tst_qhttpserverrouter.cpp @@ -486,21 +486,21 @@ void tst_QHttpServerRouter::viewHandlerLastTwoSpecials() using Arg0 = typename Args::template Arg<0>; static_assert(Arg0::IsRequest::Value, "viewTwoSpecialArgs: Args::Arg0::IsRequest::Value"); - static_assert(Arg0::IsRequest::Valid == 0, - "viewTwoSpecialArgs: Args::Arg0::IsRequest::Valid == 0"); + static_assert(Arg0::IsRequest::Valid, + "viewTwoSpecialArgs: Args::Arg0::IsRequest::Valid"); static_assert(Arg0::IsResponder::Value == 0, "viewTwoSpecialArgs: Args::Arg0::IsResponder::Value == 0"); static_assert(Arg0::IsResponder::Valid == 0, "viewTwoSpecialArgs: Args::Arg0::IsResponder::Valid == 0"); static_assert(Arg0::IsSpecial::Value, "viewTwoSpecialArgs: Args::Arg0::IsSpecial::Value"); - static_assert(Arg0::IsSpecial::Valid == 0, - "viewTwoSpecialArgs: Args::Arg0::IsSpecial::Valid == 0"); + static_assert(Arg0::IsSpecial::Valid, + "viewTwoSpecialArgs: Args::Arg0::IsSpecial::Valid"); static_assert(Arg0::IsSimple::Value == 0, "viewTwoSpecialArgs: Args::Arg0::IsSimple::Value == 0"); static_assert(Arg0::IsSimple::Valid == 0, "viewTwoSpecialArgs: Args::Arg0::IsSimple::Valid == 0"); - static_assert(Arg0::Valid == 0, + static_assert(Arg0::Valid, "viewTwoSpecialArgs: Args::Arg0::Valid"); // StaticAssert is disabled in tests static_assert(Arg0::StaticAssert, @@ -532,7 +532,7 @@ void tst_QHttpServerRouter::viewHandlerLastTwoSpecials() static_assert(Arg1::isType(), "viewTwoSpecialArgs: Args::Arg1::isType()"); - static_assert(Args::Valid == 0, "viewTwoSpecialArgs: Args::Valid"); + static_assert(Args::Valid, "viewTwoSpecialArgs: Args::Valid"); // StaticAssert is disabled in tests static_assert(Args::StaticAssert, "viewTwoSpecialArgs: Args::StaticAssert"); }