1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "testhttpserver.h"
#include <QTcpSocket>
#include <QDebug>
#include <QFile>
#include <QTimer>
/*!
\internal
\class TestHTTPServer
\brief provides a very, very basic HTTP server for testing.
Inside the test case, an instance of TestHTTPServer should be created, with the
appropriate port to listen on. The server will listen on the localhost interface.
Directories to serve can then be added to server, which will be added as "roots".
Each root can be added as a Normal, Delay or Disconnect root. Requests for files
within a Normal root are returned immediately. Request for files within a Delay
root are delayed for 500ms, and then served. Requests for files within a Disconnect
directory cause the server to disconnect immediately. A request for a file that isn't
found in any root will return a 404 error.
If you have the following directory structure:
\code
disconnect/disconnectTest.qml
files/main.qml
files/Button.qml
files/content/WebView.qml
slowFiles/slowMain.qml
\endcode
it can be added like this:
\code
TestHTTPServer server(14445);
server.serveDirectory("disconnect", TestHTTPServer::Disconnect);
server.serveDirectory("files");
server.serveDirectory("slowFiles", TestHTTPServer::Delay);
\endcode
The following request urls will then result in the appropriate action:
\table
\header \li URL \li Action
\row \li http://localhost:14445/disconnectTest.qml \li Disconnection
\row \li http://localhost:14445/main.qml \li main.qml returned immediately
\row \li http://localhost:14445/Button.qml \li Button.qml returned immediately
\row \li http://localhost:14445/content/WebView.qml \li content/WebView.qml returned immediately
\row \li http://localhost:14445/slowMain.qml \li slowMain.qml returned after 500ms
\endtable
*/
TestHTTPServer::TestHTTPServer(quint16 port)
: m_hasFailed(false)
{
QObject::connect(&server, SIGNAL(newConnection()), this, SLOT(newConnection()));
server.listen(QHostAddress::LocalHost, port);
}
bool TestHTTPServer::isValid() const
{
return server.isListening();
}
bool TestHTTPServer::serveDirectory(const QString &dir, Mode mode)
{
dirs.append(qMakePair(dir, mode));
return true;
}
/*
Add an alias, so that if filename is requested and does not exist,
alias may be returned.
*/
void TestHTTPServer::addAlias(const QString &filename, const QString &alias)
{
aliases.insert(filename, alias);
}
void TestHTTPServer::addRedirect(const QString &filename, const QString &redirectName)
{
redirects.insert(filename, redirectName);
}
bool TestHTTPServer::wait(const QUrl &expect, const QUrl &reply, const QUrl &body)
{
m_hasFailed = false;
QFile expectFile(expect.toLocalFile());
if (!expectFile.open(QIODevice::ReadOnly)) return false;
QFile replyFile(reply.toLocalFile());
if (!replyFile.open(QIODevice::ReadOnly)) return false;
bodyData = QByteArray();
if (body.isValid()) {
QFile bodyFile(body.toLocalFile());
if (!bodyFile.open(QIODevice::ReadOnly)) return false;
bodyData = bodyFile.readAll();
}
waitData = expectFile.readAll();
/*
while (waitData.endsWith('\n'))
waitData = waitData.left(waitData.count() - 1);
*/
replyData = replyFile.readAll();
if (!replyData.endsWith('\n'))
replyData.append("\n");
replyData.append("Content-length: " + QByteArray::number(bodyData.length()));
replyData .append("\n\n");
for (int ii = 0; ii < replyData.count(); ++ii) {
if (replyData.at(ii) == '\n' && (!ii || replyData.at(ii - 1) != '\r')) {
replyData.insert(ii, '\r');
++ii;
}
}
replyData.append(bodyData);
return true;
}
bool TestHTTPServer::hasFailed() const
{
return m_hasFailed;
}
void TestHTTPServer::newConnection()
{
QTcpSocket *socket = server.nextPendingConnection();
if (!socket) return;
if (!dirs.isEmpty())
dataCache.insert(socket, QByteArray());
QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
void TestHTTPServer::disconnected()
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
if (!socket) return;
dataCache.remove(socket);
for (int ii = 0; ii < toSend.count(); ++ii) {
if (toSend.at(ii).first == socket) {
toSend.removeAt(ii);
--ii;
}
}
socket->disconnect();
socket->deleteLater();
}
void TestHTTPServer::readyRead()
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
if (!socket || socket->state() == QTcpSocket::ClosingState) return;
QByteArray ba = socket->readAll();
if (!dirs.isEmpty()) {
serveGET(socket, ba);
return;
}
if (m_hasFailed || waitData.isEmpty()) {
qWarning() << "TestHTTPServer: Unexpected data" << ba;
return;
}
for (int ii = 0; ii < ba.count(); ++ii) {
const char c = ba.at(ii);
if (c == '\r' && waitData.isEmpty())
continue;
else if (!waitData.isEmpty() && c == waitData.at(0))
waitData = waitData.mid(1);
else if (c == '\r')
continue;
else {
QByteArray data = ba.mid(ii);
qWarning() << "TestHTTPServer: Unexpected data" << data << "\nExpected: " << waitData;
m_hasFailed = true;
socket->disconnectFromHost();
return;
}
}
if (waitData.isEmpty()) {
socket->write(replyData);
socket->disconnectFromHost();
}
}
bool TestHTTPServer::reply(QTcpSocket *socket, const QByteArray &fileName)
{
if (redirects.contains(fileName)) {
QByteArray response = "HTTP/1.1 302 Found\r\nContent-length: 0\r\nContent-type: text/html; charset=UTF-8\r\nLocation: " + redirects[fileName].toUtf8() + "\r\n\r\n";
socket->write(response);
return true;
}
for (int ii = 0; ii < dirs.count(); ++ii) {
QString dir = dirs.at(ii).first;
Mode mode = dirs.at(ii).second;
QString dirFile = dir + QLatin1String("/") + QLatin1String(fileName);
if (!QFile::exists(dirFile)) {
if (aliases.contains(fileName))
dirFile = dir + QLatin1String("/") + aliases.value(fileName);
}
QFile file(dirFile);
if (file.open(QIODevice::ReadOnly)) {
if (mode == Disconnect)
return true;
QByteArray data = file.readAll();
QByteArray response = "HTTP/1.0 200 OK\r\nContent-type: text/html; charset=UTF-8\r\nContent-length: ";
response += QByteArray::number(data.count());
response += "\r\n\r\n";
response += data;
if (mode == Delay) {
toSend.append(qMakePair(socket, response));
QTimer::singleShot(500, this, SLOT(sendOne()));
return false;
} else {
socket->write(response);
return true;
}
}
}
QByteArray response = "HTTP/1.0 404 Not found\r\nContent-type: text/html; charset=UTF-8\r\n\r\n";
socket->write(response);
return true;
}
void TestHTTPServer::sendOne()
{
if (!toSend.isEmpty()) {
toSend.first().first->write(toSend.first().second);
toSend.first().first->close();
toSend.removeFirst();
}
}
void TestHTTPServer::serveGET(QTcpSocket *socket, const QByteArray &data)
{
if (!dataCache.contains(socket))
return;
QByteArray total = dataCache[socket] + data;
dataCache[socket] = total;
if (total.contains("\n\r\n")) {
bool close = true;
if (total.startsWith("GET /")) {
int space = total.indexOf(' ', 4);
if (space != -1) {
QByteArray req = total.mid(5, space - 5);
close = reply(socket, req);
}
}
dataCache.remove(socket);
if (close)
socket->disconnectFromHost();
}
}
|