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

source code of qthttpserver/src/httpserver/qabstracthttpserver.h