Allow using QHttpServerRequest/Responder at the same time

Fixes: QTBUG-77088
Change-Id: I2208cee290d086339aba705f959c57e0f6a73a87
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Mikhail Svetkin 2019-09-01 21:33:22 +02:00
parent e35fdc4763
commit 43a04e17a1
4 changed files with 58 additions and 9 deletions

View File

@ -112,7 +112,16 @@ private:
} }
template<typename ViewTraits, typename T> template<typename ViewTraits, typename T>
typename std::enable_if<ViewTraits::Arguments::Last::IsRequest::Value, void>::type typename std::enable_if<ViewTraits::Arguments::Last::IsRequest::Value &&
ViewTraits::Arguments::PlaceholdersCount == 2, void>::type
responseImpl(T &boundViewHandler, const QHttpServerRequest &request, QTcpSocket *socket)
{
boundViewHandler(makeResponder(request, socket), request);
}
template<typename ViewTraits, typename T>
typename std::enable_if<ViewTraits::Arguments::Last::IsRequest::Value &&
ViewTraits::Arguments::PlaceholdersCount == 1, void>::type
responseImpl(T &boundViewHandler, const QHttpServerRequest &request, QTcpSocket *socket) responseImpl(T &boundViewHandler, const QHttpServerRequest &request, QTcpSocket *socket)
{ {
const QHttpServerResponse response(boundViewHandler(request)); const QHttpServerResponse response(boundViewHandler(request));
@ -120,7 +129,18 @@ private:
} }
template<typename ViewTraits, typename T> template<typename ViewTraits, typename T>
typename std::enable_if<ViewTraits::Arguments::Last::IsResponder::Value, void>::type typename std::enable_if<ViewTraits::Arguments::Last::IsResponder::Value &&
ViewTraits::Arguments::PlaceholdersCount == 2, void>::type
responseImpl(T &boundViewHandler,
const QHttpServerRequest &request,
QTcpSocket *socket)
{
boundViewHandler(request, makeResponder(request, socket));
}
template<typename ViewTraits, typename T>
typename std::enable_if<ViewTraits::Arguments::Last::IsResponder::Value &&
ViewTraits::Arguments::PlaceholdersCount == 1, void>::type
responseImpl(T &boundViewHandler, responseImpl(T &boundViewHandler,
const QHttpServerRequest &request, const QHttpServerRequest &request,
QTcpSocket *socket) QTcpSocket *socket)

View File

@ -169,8 +169,10 @@ struct ViewTraitsHelper {
static constexpr bool TypeMatched = isType<CleanTypeT, true>(); static constexpr bool TypeMatched = isType<CleanTypeT, true>();
static constexpr bool TypeCVRefMatched = isType<T>(); static constexpr bool TypeCVRefMatched = isType<T>();
static constexpr bool ValidPosition = static constexpr bool ValidPosition =
(I == FunctionTraits::ArgumentIndexMax); (I == FunctionTraits::ArgumentIndexMax ||
I == FunctionTraits::ArgumentIndexMax - 1);
static constexpr bool ValidAll = TypeCVRefMatched && ValidPosition; static constexpr bool ValidAll = TypeCVRefMatched && ValidPosition;
static constexpr bool assertCondition = static constexpr bool assertCondition =

View File

@ -149,6 +149,19 @@ struct CustomArg {
void tst_QHttpServer::initTestCase() 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) { httpserver.route("/test", [] (QHttpServerResponder &&responder) {
responder.write("test msg", responder.write("test msg",
QHttpServerLiterals::contentTypeTextHtml()); QHttpServerLiterals::contentTypeTextHtml());
@ -655,6 +668,20 @@ void tst_QHttpServer::routePost_data()
<< body << body
<< 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) #if QT_CONFIG(ssl)
QTest::addRow("post-and-get, post, ssl") QTest::addRow("post-and-get, post, ssl")

View File

@ -486,21 +486,21 @@ void tst_QHttpServerRouter::viewHandlerLastTwoSpecials()
using Arg0 = typename Args::template Arg<0>; using Arg0 = typename Args::template Arg<0>;
static_assert(Arg0::IsRequest::Value, static_assert(Arg0::IsRequest::Value,
"viewTwoSpecialArgs: Args::Arg0::IsRequest::Value"); "viewTwoSpecialArgs: Args::Arg0::IsRequest::Value");
static_assert(Arg0::IsRequest::Valid == 0, static_assert(Arg0::IsRequest::Valid,
"viewTwoSpecialArgs: Args::Arg0::IsRequest::Valid == 0"); "viewTwoSpecialArgs: Args::Arg0::IsRequest::Valid");
static_assert(Arg0::IsResponder::Value == 0, static_assert(Arg0::IsResponder::Value == 0,
"viewTwoSpecialArgs: Args::Arg0::IsResponder::Value == 0"); "viewTwoSpecialArgs: Args::Arg0::IsResponder::Value == 0");
static_assert(Arg0::IsResponder::Valid == 0, static_assert(Arg0::IsResponder::Valid == 0,
"viewTwoSpecialArgs: Args::Arg0::IsResponder::Valid == 0"); "viewTwoSpecialArgs: Args::Arg0::IsResponder::Valid == 0");
static_assert(Arg0::IsSpecial::Value, static_assert(Arg0::IsSpecial::Value,
"viewTwoSpecialArgs: Args::Arg0::IsSpecial::Value"); "viewTwoSpecialArgs: Args::Arg0::IsSpecial::Value");
static_assert(Arg0::IsSpecial::Valid == 0, static_assert(Arg0::IsSpecial::Valid,
"viewTwoSpecialArgs: Args::Arg0::IsSpecial::Valid == 0"); "viewTwoSpecialArgs: Args::Arg0::IsSpecial::Valid");
static_assert(Arg0::IsSimple::Value == 0, static_assert(Arg0::IsSimple::Value == 0,
"viewTwoSpecialArgs: Args::Arg0::IsSimple::Value == 0"); "viewTwoSpecialArgs: Args::Arg0::IsSimple::Value == 0");
static_assert(Arg0::IsSimple::Valid == 0, static_assert(Arg0::IsSimple::Valid == 0,
"viewTwoSpecialArgs: Args::Arg0::IsSimple::Valid == 0"); "viewTwoSpecialArgs: Args::Arg0::IsSimple::Valid == 0");
static_assert(Arg0::Valid == 0, static_assert(Arg0::Valid,
"viewTwoSpecialArgs: Args::Arg0::Valid"); "viewTwoSpecialArgs: Args::Arg0::Valid");
// StaticAssert is disabled in tests // StaticAssert is disabled in tests
static_assert(Arg0::StaticAssert, static_assert(Arg0::StaticAssert,
@ -532,7 +532,7 @@ void tst_QHttpServerRouter::viewHandlerLastTwoSpecials()
static_assert(Arg1::isType<QHttpServerResponder &&>(), static_assert(Arg1::isType<QHttpServerResponder &&>(),
"viewTwoSpecialArgs: Args::Arg1::isType<QHttpServerResponder &&>()"); "viewTwoSpecialArgs: Args::Arg1::isType<QHttpServerResponder &&>()");
static_assert(Args::Valid == 0, "viewTwoSpecialArgs: Args::Valid"); static_assert(Args::Valid, "viewTwoSpecialArgs: Args::Valid");
// StaticAssert is disabled in tests // StaticAssert is disabled in tests
static_assert(Args::StaticAssert, "viewTwoSpecialArgs: Args::StaticAssert"); static_assert(Args::StaticAssert, "viewTwoSpecialArgs: Args::StaticAssert");
} }