| 1 | // Copyright (C) 2019 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QABSTRACTHTTPSERVER_H |
| 5 | #define QABSTRACTHTTPSERVER_H |
| 6 | |
| 7 | #include <QtCore/qobject.h> |
| 8 | |
| 9 | #include <QtHttpServer/qthttpserverglobal.h> |
| 10 | #include <QtHttpServer/qhttpserverwebsocketupgraderesponse.h> |
| 11 | |
| 12 | #include <QtNetwork/qhostaddress.h> |
| 13 | |
| 14 | #if QT_CONFIG(ssl) |
| 15 | #include <QtNetwork/qhttp2configuration.h> |
| 16 | #endif |
| 17 | |
| 18 | #if defined(QT_WEBSOCKETS_LIB) |
| 19 | #include <QtWebSockets/qwebsocket.h> |
| 20 | #endif // defined(QT_WEBSOCKETS_LIB) |
| 21 | |
| 22 | #include <functional> |
| 23 | #include <memory> |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | class QHttpServerRequest; |
| 28 | class QHttpServerResponder; |
| 29 | class QLocalServer; |
| 30 | class QTcpServer; |
| 31 | |
| 32 | class QAbstractHttpServerPrivate; |
| 33 | class Q_HTTPSERVER_EXPORT QAbstractHttpServer : public QObject |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | friend class QHttpServerHttp1ProtocolHandler; |
| 37 | friend class QHttpServerHttp2ProtocolHandler; |
| 38 | |
| 39 | public: |
| 40 | explicit QAbstractHttpServer(QObject *parent = nullptr); |
| 41 | ~QAbstractHttpServer() override; |
| 42 | |
| 43 | QList<quint16> serverPorts() const; |
| 44 | bool bind(QTcpServer *server); |
| 45 | QList<QTcpServer *> servers() const; |
| 46 | |
| 47 | #if QT_CONFIG(localserver) |
| 48 | bool bind(QLocalServer *server); |
| 49 | QList<QLocalServer *> localServers() const; |
| 50 | #endif |
| 51 | |
| 52 | #if QT_CONFIG(ssl) |
| 53 | QHttp2Configuration http2Configuration() const; |
| 54 | void setHttp2Configuration(const QHttp2Configuration &configuration); |
| 55 | #endif |
| 56 | |
| 57 | #if defined(QT_WEBSOCKETS_LIB) |
| 58 | Q_SIGNALS: |
| 59 | void newWebSocketConnection(); |
| 60 | |
| 61 | private: |
| 62 | using WebSocketUpgradeVerifierPrototype = |
| 63 | QHttpServerWebSocketUpgradeResponse (*)(const QHttpServerRequest &request); |
| 64 | template <typename T> |
| 65 | using if_compatible_callable = typename std::enable_if< |
| 66 | QtPrivate::AreFunctionsCompatible<WebSocketUpgradeVerifierPrototype, T>::value, |
| 67 | bool>::type; |
| 68 | |
| 69 | void addWebSocketUpgradeVerifierImpl(const QObject *context, |
| 70 | QtPrivate::QSlotObjectBase *slotObjRaw); |
| 71 | |
| 72 | public: |
| 73 | bool hasPendingWebSocketConnections() const; |
| 74 | std::unique_ptr<QWebSocket> nextPendingWebSocketConnection(); |
| 75 | |
| 76 | template <typename Handler, if_compatible_callable<Handler> = true> |
| 77 | void addWebSocketUpgradeVerifier( |
| 78 | const typename QtPrivate::ContextTypeForFunctor<Handler>::ContextType *context, |
| 79 | Handler &&func) |
| 80 | { |
| 81 | addWebSocketUpgradeVerifierImpl( |
| 82 | context, |
| 83 | QtPrivate::makeCallableObject<WebSocketUpgradeVerifierPrototype>( |
| 84 | std::forward<Handler>(func))); |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | QHttpServerWebSocketUpgradeResponse |
| 89 | verifyWebSocketUpgrade(const QHttpServerRequest &request) const; |
| 90 | #endif // defined(QT_WEBSOCKETS_LIB) |
| 91 | |
| 92 | protected: |
| 93 | QAbstractHttpServer(QAbstractHttpServerPrivate &dd, QObject *parent = nullptr); |
| 94 | |
| 95 | virtual bool handleRequest(const QHttpServerRequest &request, |
| 96 | QHttpServerResponder &responder) = 0; |
| 97 | virtual void missingHandler(const QHttpServerRequest &request, |
| 98 | QHttpServerResponder &responder) = 0; |
| 99 | |
| 100 | private: |
| 101 | Q_DECLARE_PRIVATE(QAbstractHttpServer) |
| 102 | }; |
| 103 | |
| 104 | QT_END_NAMESPACE |
| 105 | |
| 106 | #endif // QABSTRACTHTTPSERVER_H |
| 107 | |