73 : m_state(AwaitingHeader)
120 m_aliases.
insert(filename, alias);
125 m_redirects.
insert(filename, redirectName);
135 m_state = AwaitingHeader;
151 m_bodyData = bodyFile.
readAll();
158 bool headers_done =
false;
162 m_waitData.body.append(
line);
163 }
else if (
line.
at(0) ==
'\n') {
166 m_waitData.headerPrefixes.append(
line.
left(
line.
size() - strlen(
"{{Ignore}}\n")));
169 m_waitData.headerExactMatches.append(
line);
173 m_replyData = replyFile.
readAll();
177 m_replyData.
append(
"Content-length: ");
179 m_replyData.
append(
"\n\n");
181 for (
int ii = 0; ii < m_replyData.
size(); ++ii) {
182 if (m_replyData.
at(ii) ==
'\n' && (!ii || m_replyData.
at(ii - 1) !=
'\r')) {
183 m_replyData.
insert(ii,
'\r');
187 m_replyData.
append(m_bodyData);
194 return m_state == Failed;
197void TestHTTPServer::newConnection()
210void TestHTTPServer::disconnected()
217 for (
int ii = 0; ii < m_toSend.
size(); ++ii) {
218 if (m_toSend.
at(ii).first ==
socket) {
227void TestHTTPServer::readyRead()
233 if (!m_directories.
isEmpty()) {
238 if (m_state == Failed || (m_waitData.body.isEmpty() && m_waitData.headerExactMatches.size() == 0)) {
243 if (m_state == AwaitingHeader) {
248 m_state = AwaitingData;
252 bool prefixFound =
false;
253 for (
const QByteArray &prefix : m_waitData.headerPrefixes) {
266 if (!prefixFound && !m_waitData.headerExactMatches.contains(
line)) {
267 qWarning() <<
"TestHTTPServer: Unexpected header:" <<
line
268 <<
"\nExpected exact headers: " << m_waitData.headerExactMatches
269 <<
"\nExpected header prefixes: " << m_waitData.headerPrefixes;
280 if (!m_data.
isEmpty() || m_waitData.body.isEmpty()) {
281 if (m_waitData.body != m_data) {
282 qWarning() <<
"TestHTTPServer: Unexpected data" << m_data <<
"\nExpected: " << m_waitData.body;
296 =
"HTTP/1.1 302 Found\r\nContent-length: 0\r\nContent-type: text/html; charset=UTF-8\r\nLocation: "
302 for (
int ii = 0; ii < m_directories.
size(); ++ii) {
304 const Mode mode = m_directories.
at(ii).second;
325 =
"HTTP/1.0 200 OK\r\nContent-type: text/html; charset=UTF-8\r\nContent-length: ";
327 response +=
"\r\n\r\n";
341 socket->
write(
"HTTP/1.0 404 Not found\r\nContent-type: text/html; charset=UTF-8\r\n\r\n");
351void TestHTTPServer::sendOne()
354 m_toSend.
first().first->write(m_toSend.
first().second);
355 m_toSend.
first().first->close();
363 if (
it == m_dataCache.
end())
372 const int space = total.
indexOf(
' ', 4);
390 m_dirs(dirs), m_port(0)
425 server.serveDirectory(
i.key(),
i.value());
432void ThreadedTestHTTPServer::start()
436 m_condition.
wait(&m_mutex);
441#include "moc_testhttpserver_p.cpp"
virtual void disconnectFromHost()
Attempts to close the socket.
SocketState state() const
Returns the state of the socket.
void disconnected()
This signal is emitted when the socket has been disconnected.
bool endsWith(char c) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
qsizetype indexOf(char c, qsizetype from=0) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool startsWith(QByteArrayView bv) const
char at(qsizetype i) const
Returns the byte at index position i in the byte array.
QByteArray & insert(qsizetype i, QByteArrayView data)
bool contains(char c) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
static QByteArray number(int, int base=10)
Returns a byte-array representing the whole number n as text.
void clear()
Clears the contents of the byte array and makes it null.
QByteArray & append(char c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QByteArray mid(qsizetype index, qsizetype len=-1) const
Returns a byte array containing len bytes from this byte array, starting at position pos.
QByteArray & replace(qsizetype index, qsizetype len, const char *s, qsizetype alen)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
bool exists() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool remove(const Key &key)
Removes the item that has the key from the hash.
const_iterator constFind(const Key &key) const noexcept
const_iterator constEnd() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
const_iterator constBegin() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
iterator erase(const_iterator it)
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
T value(const Key &key) const noexcept
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
void readyRead()
This signal is emitted once every time new data is available for reading from the device's current re...
qint64 readLine(char *data, qint64 maxlen)
This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes,...
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
qsizetype size() const noexcept
void removeFirst() noexcept
bool isEmpty() const noexcept
void removeAt(qsizetype i)
const_reference at(qsizetype i) const noexcept
void append(parameter_type t)
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
void deleteLater()
\threadsafe
static QString urlToLocalFileOrQrc(const QString &)
If url is a local file returns a path suitable for passing to QFile.
bool contains(const T &value) const
iterator insert(const T &value)
\macro QT_RESTRICTED_CAST_FROM_ASCII
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
QString & replace(qsizetype i, qsizetype len, QChar after)
qsizetype size() const
Returns the number of characters in this string.
const QChar at(qsizetype i) const
Returns the character at the given index position in the string.
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
QString left(qsizetype n) const
Returns a substring that contains the n leftmost characters of the string.
QByteArray toUtf8() const &
void newConnection()
This signal is emitted every time a new connection is available, regardless of whether it has been ad...
QString errorString() const
Returns a human readable description of the last error that occurred.
virtual QTcpSocket * nextPendingConnection()
Returns the next pending connection as a connected QTcpSocket object.
bool listen(const QHostAddress &address=QHostAddress::Any, quint16 port=0)
Tells the server to listen for incoming connections on address address and port port.
quint16 serverPort() const
Returns the server's port if the server is listening for connections; otherwise returns 0.
The QTcpSocket class provides a TCP socket.
void start(Priority=InheritPriority)
bool wait(QDeadlineTimer deadline=QDeadlineTimer(QDeadlineTimer::Forever))
bool singleShot
whether the timer is a single-shot timer
QUrl resolved(const QUrl &relative) const
Returns the result of the merge of this URL with relative.
bool isValid() const
Returns true if the URL is non-empty and valid; otherwise returns false.
void setScheme(const QString &scheme)
Sets the scheme of the URL to scheme.
void setHost(const QString &host, ParsingMode mode=DecodedMode)
Sets the host of the URL to host.
QString toString(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Returns a string representation of the URL.
void setPort(int port)
Sets the port of the URL to port.
bool wait(QMutex *, QDeadlineTimer=QDeadlineTimer(QDeadlineTimer::Forever))
provides a very, very basic HTTP server for testing.
QString errorString() const
void addAlias(const QString &filename, const QString &aliasName)
bool wait(const QUrl &expect, const QUrl &reply, const QUrl &body)
QUrl url(const QString &documentPath) const
bool serveDirectory(const QString &, Mode=Normal)
void registerFileNameForContentSubstitution(const QString &fileName)
void addRedirect(const QString &filename, const QString &redirectName)
QString urlString(const QString &documentPath) const
QString urlString(const QString &documentPath) const
QUrl url(const QString &documentPath) const
~ThreadedTestHTTPServer()
ThreadedTestHTTPServer(const QString &dir, TestHTTPServer::Mode mode=TestHTTPServer::Normal)
QSet< QString >::iterator it
Combined button and popup list for selecting options.
int toUtf8(char16_t u, OutputPtr &dst, InputPtr &src, InputPtr end)
#define QByteArrayLiteral(str)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage return DBusPendingCall DBusPendingCall return DBusPendingCall return dbus_int32_t return DBusServer * server
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
constexpr decltype(auto) qMakePair(T1 &&value1, T2 &&value2) noexcept(noexcept(std::make_pair(std::forward< T1 >(value1), std::forward< T2 >(value2))))
#define qPrintable(string)
QLatin1StringView QLatin1String
#define QStringLiteral(str)
#define QVERIFY2(statement, description)
QUrl url("example.com")
[constructor-url-reference]
char * toString(const MyType &t)
[31]
\inmodule QtCore \reentrant
static QUrl localHostUrl(quint16 port)
static QList< QByteArrayView > ignoredHeaders