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 | |
11 | #include <QtNetwork/qhostaddress.h> |
12 | |
13 | #if QT_CONFIG(ssl) |
14 | #include <QtNetwork/qssl.h> |
15 | #endif |
16 | |
17 | #if defined(QT_WEBSOCKETS_LIB) |
18 | #include <QtWebSockets/qwebsocket.h> |
19 | #endif // defined(QT_WEBSOCKETS_LIB) |
20 | |
21 | #include <memory> |
22 | |
23 | QT_BEGIN_NAMESPACE |
24 | |
25 | class QHttpServerRequest; |
26 | class QHttpServerResponder; |
27 | class QSslCertificate; |
28 | class QSslConfiguration; |
29 | class QSslKey; |
30 | class QTcpServer; |
31 | |
32 | class QAbstractHttpServerPrivate; |
33 | class Q_HTTPSERVER_EXPORT QAbstractHttpServer : public QObject |
34 | { |
35 | Q_OBJECT |
36 | friend class QHttpServerStream; |
37 | |
38 | public: |
39 | explicit QAbstractHttpServer(QObject *parent = nullptr); |
40 | ~QAbstractHttpServer() override; |
41 | |
42 | quint16 listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0); |
43 | QList<quint16> serverPorts(); |
44 | |
45 | void bind(QTcpServer *server = nullptr); |
46 | QList<QTcpServer *> servers() const; |
47 | |
48 | #if QT_CONFIG(ssl) |
49 | void sslSetup(const QSslCertificate &certificate, const QSslKey &privateKey, |
50 | QSsl::SslProtocol protocol = QSsl::SecureProtocols); |
51 | void sslSetup(const QSslConfiguration &sslConfiguration); |
52 | #endif |
53 | |
54 | #if defined(QT_WEBSOCKETS_LIB) |
55 | Q_SIGNALS: |
56 | void newWebSocketConnection(); |
57 | |
58 | public: |
59 | bool hasPendingWebSocketConnections() const; |
60 | std::unique_ptr<QWebSocket> nextPendingWebSocketConnection(); |
61 | #endif // defined(QT_WEBSOCKETS_LIB) |
62 | |
63 | protected: |
64 | QAbstractHttpServer(QAbstractHttpServerPrivate &dd, QObject *parent = nullptr); |
65 | |
66 | virtual bool handleRequest(const QHttpServerRequest &request, |
67 | QHttpServerResponder &responder) = 0; |
68 | virtual void missingHandler(const QHttpServerRequest &request, |
69 | QHttpServerResponder &&responder) = 0; |
70 | |
71 | private: |
72 | Q_DECLARE_PRIVATE(QAbstractHttpServer) |
73 | }; |
74 | |
75 | QT_END_NAMESPACE |
76 | |
77 | #endif // QABSTRACTHTTPSERVER_H |
78 |