1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #ifndef TESTHTTPSERVER_P_H |
5 | #define TESTHTTPSERVER_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QTcpServer> |
19 | #include <QUrl> |
20 | #include <QPair> |
21 | #include <QThread> |
22 | #include <QMutex> |
23 | #include <QWaitCondition> |
24 | #include <private/qglobal_p.h> |
25 | #include <QObject> |
26 | #include <QSet> |
27 | #include <QList> |
28 | #include <QString> |
29 | |
30 | QT_BEGIN_NAMESPACE |
31 | |
32 | class TestHTTPServer : public QObject |
33 | { |
34 | Q_OBJECT |
35 | public: |
36 | TestHTTPServer(); |
37 | |
38 | bool listen(); |
39 | quint16 port() const; |
40 | QUrl baseUrl() const; |
41 | QUrl url(const QString &documentPath) const; |
42 | QString urlString(const QString &documentPath) const; |
43 | QString errorString() const; |
44 | |
45 | enum Mode { Normal, Delay, Disconnect }; |
46 | bool serveDirectory(const QString &, Mode = Normal); |
47 | |
48 | bool wait(const QUrl &expect, const QUrl &reply, const QUrl &body); |
49 | bool hasFailed() const; |
50 | |
51 | void addAlias(const QString &filename, const QString &aliasName); |
52 | void addRedirect(const QString &filename, const QString &redirectName); |
53 | |
54 | void registerFileNameForContentSubstitution(const QString &fileName); |
55 | |
56 | // In Delay mode, each item needs one call to this function to be sent |
57 | void sendDelayedItem(); |
58 | |
59 | private Q_SLOTS: |
60 | void newConnection(); |
61 | void disconnected(); |
62 | void readyRead(); |
63 | void sendOne(); |
64 | |
65 | private: |
66 | enum State { |
67 | , |
68 | AwaitingData, |
69 | Failed |
70 | }; |
71 | |
72 | void serveGET(QTcpSocket *, const QByteArray &); |
73 | bool reply(QTcpSocket *, const QByteArray &); |
74 | |
75 | QList<QPair<QString, Mode> > m_directories; |
76 | QHash<QTcpSocket *, QByteArray> m_dataCache; |
77 | QList<QPair<QTcpSocket *, QByteArray> > m_toSend; |
78 | QSet<QString> m_contentSubstitutedFileNames; |
79 | |
80 | struct WaitData { |
81 | QList<QByteArray> ; |
82 | QList<QByteArray> ; |
83 | QByteArray body; |
84 | } m_waitData; |
85 | QByteArray m_replyData; |
86 | QByteArray m_bodyData; |
87 | QByteArray m_data; |
88 | State m_state; |
89 | |
90 | QHash<QString, QString> m_aliases; |
91 | QHash<QString, QString> m_redirects; |
92 | |
93 | QTcpServer m_server; |
94 | }; |
95 | |
96 | class ThreadedTestHTTPServer : public QThread |
97 | { |
98 | Q_OBJECT |
99 | public: |
100 | ThreadedTestHTTPServer(const QString &dir, TestHTTPServer::Mode mode = TestHTTPServer::Normal); |
101 | ThreadedTestHTTPServer(const QHash<QString, TestHTTPServer::Mode> &dirs); |
102 | ~ThreadedTestHTTPServer(); |
103 | |
104 | QUrl baseUrl() const; |
105 | QUrl url(const QString &documentPath) const; |
106 | QString urlString(const QString &documentPath) const; |
107 | |
108 | protected: |
109 | void run() override; |
110 | |
111 | private: |
112 | void start(); |
113 | |
114 | QHash<QString, TestHTTPServer::Mode> m_dirs; |
115 | quint16 m_port; |
116 | QMutex m_mutex; |
117 | QWaitCondition m_condition; |
118 | }; |
119 | |
120 | QT_END_NAMESPACE |
121 | |
122 | #endif // TESTHTTPSERVER_P_H |
123 | |
124 | |