Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
testhttpserver_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
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 <QThread>
21#include <QMutex>
22#include <QWaitCondition>
23#include <private/qglobal_p.h>
24#include <QObject>
25#include <QSet>
26#include <QList>
27#include <QString>
28#include <utility>
29
30QT_BEGIN_NAMESPACE
31
32class TestHTTPServer : public QObject
33{
34 Q_OBJECT
35public:
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 qsizetype chunkSize() const { return m_chunkSize; }
60 void setChunkSize(qsizetype chunkSize) { m_chunkSize = chunkSize; }
61
62private Q_SLOTS:
63 void newConnection();
64 void disconnected();
65 void readyRead();
66 void sendOne();
67 void sendChunk();
68
69private:
70 enum State {
71 AwaitingHeader,
72 AwaitingData,
73 Failed
74 };
75
76 void serveGET(QTcpSocket *, const QByteArray &);
77 bool reply(QTcpSocket *, const QByteArray &);
78
79 QList<std::pair<QString, Mode> > m_directories;
80 QHash<QTcpSocket *, QByteArray> m_dataCache;
81 QList<std::pair<QTcpSocket *, QByteArray> > m_toSend;
82 QSet<QString> m_contentSubstitutedFileNames;
83
84 struct WaitData {
85 QList<QByteArray> headerExactMatches;
86 QList<QByteArray> headerPrefixes;
87 QByteArray body;
88 } m_waitData;
89 QByteArray m_replyData;
90 QByteArray m_bodyData;
91 QByteArray m_data;
92 State m_state;
93
94 QHash<QString, QString> m_aliases;
95 QHash<QString, QString> m_redirects;
96
97 QTcpServer m_server;
98
99 qsizetype m_chunkSize = std::numeric_limits<qsizetype>::max();
100};
101
103{
105public:
107 ThreadedTestHTTPServer(const QHash<QString, TestHTTPServer::Mode> &dirs);
109
110 QUrl baseUrl() const;
111 QUrl url(const QString &documentPath) const;
112 QString urlString(const QString &documentPath) const;
113
114protected:
115 void run() override;
116
117private:
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
126QT_END_NAMESPACE
127
128#endif // TESTHTTPSERVER_P_H
QString urlString(const QString &documentPath) const
ThreadedTestHTTPServer(const QHash< QString, TestHTTPServer::Mode > &dirs)
QUrl url(const QString &documentPath) const
static QList< QByteArrayView > ignoredHeaders
static QUrl localHostUrl(quint16 port)