| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #ifndef TESTHTTPSERVER_H |
| 30 | #define TESTHTTPSERVER_H |
| 31 | |
| 32 | #include <QTcpServer> |
| 33 | #include <QUrl> |
| 34 | #include <QPair> |
| 35 | #include <QThread> |
| 36 | #include <QMutex> |
| 37 | #include <QWaitCondition> |
| 38 | |
| 39 | class TestHTTPServer : public QObject |
| 40 | { |
| 41 | Q_OBJECT |
| 42 | public: |
| 43 | TestHTTPServer(); |
| 44 | |
| 45 | bool listen(); |
| 46 | quint16 port() const; |
| 47 | QUrl baseUrl() const; |
| 48 | QUrl url(const QString &documentPath) const; |
| 49 | QString urlString(const QString &documentPath) const; |
| 50 | QString errorString() const; |
| 51 | |
| 52 | enum Mode { Normal, Delay, Disconnect }; |
| 53 | bool serveDirectory(const QString &, Mode = Normal); |
| 54 | |
| 55 | bool wait(const QUrl &expect, const QUrl &reply, const QUrl &body); |
| 56 | bool hasFailed() const; |
| 57 | |
| 58 | void addAlias(const QString &filename, const QString &aliasName); |
| 59 | void addRedirect(const QString &filename, const QString &redirectName); |
| 60 | |
| 61 | void registerFileNameForContentSubstitution(const QString &fileName); |
| 62 | |
| 63 | // In Delay mode, each item needs one call to this function to be sent |
| 64 | void sendDelayedItem(); |
| 65 | |
| 66 | private slots: |
| 67 | void newConnection(); |
| 68 | void disconnected(); |
| 69 | void readyRead(); |
| 70 | void sendOne(); |
| 71 | |
| 72 | private: |
| 73 | enum State { |
| 74 | , |
| 75 | AwaitingData, |
| 76 | Failed |
| 77 | }; |
| 78 | |
| 79 | void serveGET(QTcpSocket *, const QByteArray &); |
| 80 | bool reply(QTcpSocket *, const QByteArray &); |
| 81 | |
| 82 | QList<QPair<QString, Mode> > m_directories; |
| 83 | QHash<QTcpSocket *, QByteArray> m_dataCache; |
| 84 | QList<QPair<QTcpSocket *, QByteArray> > m_toSend; |
| 85 | QSet<QString> m_contentSubstitutedFileNames; |
| 86 | |
| 87 | struct WaitData { |
| 88 | QList <QByteArray>; |
| 89 | QByteArray body; |
| 90 | } m_waitData; |
| 91 | QByteArray m_replyData; |
| 92 | QByteArray m_bodyData; |
| 93 | QByteArray m_data; |
| 94 | State m_state; |
| 95 | |
| 96 | QHash<QString, QString> m_aliases; |
| 97 | QHash<QString, QString> m_redirects; |
| 98 | |
| 99 | QTcpServer m_server; |
| 100 | }; |
| 101 | |
| 102 | class ThreadedTestHTTPServer : public QThread |
| 103 | { |
| 104 | Q_OBJECT |
| 105 | public: |
| 106 | ThreadedTestHTTPServer(const QString &dir, TestHTTPServer::Mode mode = TestHTTPServer::Normal); |
| 107 | ThreadedTestHTTPServer(const QHash<QString, TestHTTPServer::Mode> &dirs); |
| 108 | ~ThreadedTestHTTPServer(); |
| 109 | |
| 110 | QUrl baseUrl() const; |
| 111 | QUrl url(const QString &documentPath) const; |
| 112 | QString urlString(const QString &documentPath) const; |
| 113 | |
| 114 | protected: |
| 115 | void run(); |
| 116 | |
| 117 | private: |
| 118 | void start(); |
| 119 | |
| 120 | QHash<QString, TestHTTPServer::Mode> m_dirs; |
| 121 | quint16 m_port; |
| 122 | QMutex m_mutex; |
| 123 | QWaitCondition m_condition; |
| 124 | }; |
| 125 | |
| 126 | #endif // TESTHTTPSERVER_H |
| 127 | |
| 128 | |