/**************************************************************************** ** ** Copyright (C) 2020 Mikhail Svetkin ** Copyright (C) 2019 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtHttpServer module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:GPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3 or (at your option) any later version ** approved by the KDE Free Qt Foundation. The licenses are as published by ** the Free Software Foundation and appearing in the file LICENSE.GPL3 ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QHTTPSERVER_H #define QHTTPSERVER_H #include #include #include #include #include #include #include QT_BEGIN_NAMESPACE class QTcpSocket; class QHttpServerRequest; class QHttpServerPrivate; class Q_HTTPSERVER_EXPORT QHttpServer final : public QAbstractHttpServer { Q_OBJECT Q_DECLARE_PRIVATE(QHttpServer) template struct VariadicTypeAt { using Type = typename std::tuple_element>::type; }; template struct VariadicTypeLast { using Type = typename VariadicTypeAt::Type; }; template using ResponseType = typename std::conditional< std::is_base_of::value, T, QHttpServerResponse >::type; public: explicit QHttpServer(QObject *parent = nullptr); ~QHttpServer(); QHttpServerRouter *router(); template bool route(Args && ... args) { using ViewHandler = typename VariadicTypeLast::Type; using ViewTraits = QHttpServerRouterViewTraits; static_assert(ViewTraits::Arguments::StaticAssert, "ViewHandler arguments are in the wrong order or not supported"); return routeHelper( QtPrivate::makeIndexSequence{}, std::forward(args)...); } template void afterRequest(ViewHandler &&viewHandler) { using ViewTraits = QHttpServerAfterRequestViewTraits; static_assert(ViewTraits::Arguments::StaticAssert, "ViewHandler arguments are in the wrong order or not supported"); afterRequestHelper(std::move(viewHandler)); } using AfterRequestHandler = std::function; private: template typename std::enable_if::type afterRequestHelper(ViewHandler &&viewHandler) { auto handler = [viewHandler](QHttpServerResponse &&resp, const QHttpServerRequest &request) { return std::move(viewHandler(std::move(resp), request)); }; afterRequestImpl(std::move(handler)); } template typename std::enable_if::type afterRequestHelper(ViewHandler &&viewHandler) { auto handler = [viewHandler](QHttpServerResponse &&resp, const QHttpServerRequest &) { return std::move(viewHandler(std::move(resp))); }; afterRequestImpl(std::move(handler)); } template typename std::enable_if::type afterRequestHelper(ViewHandler &&viewHandler) { auto handler = [viewHandler](QHttpServerResponse &&resp, const QHttpServerRequest &request) { return std::move(viewHandler(request, std::move(resp))); }; afterRequestImpl(std::move(handler)); } void afterRequestImpl(AfterRequestHandler &&afterRequestHandler); private: template bool routeHelper(QtPrivate::IndexesList, Args &&... args) { return routeImpl::Type...>(std::forward(args)...); } template bool routeImpl(Args &&...args, ViewHandler &&viewHandler) { auto routerHandler = [this, viewHandler] ( const QRegularExpressionMatch &match, const QHttpServerRequest &request, QTcpSocket *socket) mutable { auto boundViewHandler = router()->bindCaptured( std::move(viewHandler), match); responseImpl(boundViewHandler, request, socket); }; return router()->addRule( new Rule(std::forward(args)..., std::move(routerHandler))); } template typename std::enable_if::type responseImpl(T &boundViewHandler, const QHttpServerRequest &request, QTcpSocket *socket) { ResponseType response(boundViewHandler()); sendResponse(std::move(response), request, socket); } template typename std::enable_if::type responseImpl(T &boundViewHandler, const QHttpServerRequest &request, QTcpSocket *socket) { ResponseType response(boundViewHandler(request)); sendResponse(std::move(response), request, socket); } template 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) { boundViewHandler(request, makeResponder(request, socket)); } template typename std::enable_if::type responseImpl(T &boundViewHandler, const QHttpServerRequest &request, QTcpSocket *socket) { boundViewHandler(makeResponder(request, socket)); } bool handleRequest(const QHttpServerRequest &request, QTcpSocket *socket) override final; void sendResponse(QHttpServerResponse &&response, const QHttpServerRequest &request, QTcpSocket *socket); }; QT_END_NAMESPACE #endif // QHTTPSERVER_H