Rename most classes to remove the 'Q' prefix.
This commit is contained in:
@@ -37,7 +37,7 @@ int main(int argc, char * argv[])
|
||||
QCoreApplication a(argc, argv);
|
||||
|
||||
// Attempt to open the local file and read from it
|
||||
QLocalFile file;
|
||||
LocalFile file;
|
||||
if (!file.open()) {
|
||||
qCritical("Unable to open local file - is server running?");
|
||||
return 1;
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
#include "apihandler.h"
|
||||
|
||||
void ApiHandler::messages(QHttpSocket *socket)
|
||||
void ApiHandler::messages(HttpSocket *socket)
|
||||
{
|
||||
QJsonObject object;
|
||||
object.insert("messages", QJsonArray::fromStringList(mMessages));
|
||||
@@ -35,7 +35,7 @@ void ApiHandler::messages(QHttpSocket *socket)
|
||||
}
|
||||
|
||||
|
||||
void ApiHandler::messagesNew(QHttpSocket *socket)
|
||||
void ApiHandler::messagesNew(HttpSocket *socket)
|
||||
{
|
||||
QJsonDocument document;
|
||||
if (socket->readJson(document)) {
|
||||
@@ -49,5 +49,5 @@ void ApiHandler::messagesNew(QHttpSocket *socket)
|
||||
}
|
||||
|
||||
// If execution reaches this point, malformed data was supplied
|
||||
socket->writeError(QHttpSocket::BadRequest);
|
||||
socket->writeError(HttpSocket::BadRequest);
|
||||
}
|
||||
|
||||
@@ -34,8 +34,8 @@ class ApiHandler : public QObject
|
||||
|
||||
public Q_SLOTS:
|
||||
|
||||
void messages(QHttpSocket *socket);
|
||||
void messagesNew(QHttpSocket *socket);
|
||||
void messages(HttpSocket *socket);
|
||||
void messagesNew(HttpSocket *socket);
|
||||
|
||||
private:
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ int main(int argc, char * argv[])
|
||||
quint16 port = parser.value(portOption).toInt();
|
||||
|
||||
// Build the hierarchy of handlers
|
||||
QFilesystemHandler handler(":/static");
|
||||
FilesystemHandler handler(":/static");
|
||||
handler.addRedirect(QRegExp("^$"), "/index.html");
|
||||
|
||||
ApiHandler renameMe;
|
||||
@@ -73,7 +73,7 @@ int main(int argc, char * argv[])
|
||||
apiHandler.registerMethod("messages/new", &renameMe, &ApiHandler::messagesNew);
|
||||
handler.addSubHandler(QRegExp("api/"), &apiHandler);
|
||||
|
||||
QHttpServer server(&handler);
|
||||
HttpServer server(&handler);
|
||||
|
||||
// Attempt to listen on the specified port
|
||||
if (!server.listen(address, port)) {
|
||||
|
||||
@@ -68,8 +68,8 @@ int main(int argc, char * argv[])
|
||||
QString dir = parser.value(dirOption);
|
||||
|
||||
// Create the filesystem handler and server
|
||||
QFilesystemHandler handler(dir);
|
||||
QHttpServer server(&handler);
|
||||
FilesystemHandler handler(dir);
|
||||
HttpServer server(&handler);
|
||||
|
||||
// Attempt to listen on the specified port
|
||||
if (!server.listen(address, port)) {
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
#include "qhttpengine_global.h"
|
||||
|
||||
class QHTTPENGINE_EXPORT QFilesystemHandlerPrivate;
|
||||
class QHTTPENGINE_EXPORT FilesystemHandlerPrivate;
|
||||
|
||||
/**
|
||||
* @brief Handler for filesystem requests
|
||||
@@ -45,7 +45,7 @@ class QHTTPENGINE_EXPORT QFilesystemHandlerPrivate;
|
||||
* can be modified after initialization. It is possible to use a resource
|
||||
* directory for the document root.
|
||||
*/
|
||||
class QHTTPENGINE_EXPORT QFilesystemHandler : public QHttpHandler
|
||||
class QHTTPENGINE_EXPORT FilesystemHandler : public HttpHandler
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -54,12 +54,12 @@ public:
|
||||
/**
|
||||
* @brief Create a new filesystem handler
|
||||
*/
|
||||
explicit QFilesystemHandler(QObject *parent = 0);
|
||||
explicit FilesystemHandler(QObject *parent = 0);
|
||||
|
||||
/**
|
||||
* @brief Create a new filesystem handler from the specified directory
|
||||
*/
|
||||
QFilesystemHandler(const QString &documentRoot, QObject *parent = 0);
|
||||
FilesystemHandler(const QString &documentRoot, QObject *parent = 0);
|
||||
|
||||
/**
|
||||
* @brief Set the document root
|
||||
@@ -74,12 +74,12 @@ protected:
|
||||
/**
|
||||
* @brief Reimplementation of QHttpHandler::process()
|
||||
*/
|
||||
virtual void process(QHttpSocket *socket, const QString &path);
|
||||
virtual void process(HttpSocket *socket, const QString &path);
|
||||
|
||||
private:
|
||||
|
||||
QFilesystemHandlerPrivate *const d;
|
||||
friend class QFilesystemHandlerPrivate;
|
||||
FilesystemHandlerPrivate *const d;
|
||||
friend class FilesystemHandlerPrivate;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QFILESYSTEMHANDLER_H
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
#include "qhttpengine_global.h"
|
||||
|
||||
class QHTTPENGINE_EXPORT QHttpBasicAuthPrivate;
|
||||
class QHTTPENGINE_EXPORT HttpBasicAuthPrivate;
|
||||
|
||||
/**
|
||||
* @brief Middleware for HTTP basic authentication
|
||||
@@ -38,7 +38,7 @@ class QHTTPENGINE_EXPORT QHttpBasicAuthPrivate;
|
||||
* different method of authentication, override the verify() method in a
|
||||
* derived class.
|
||||
*/
|
||||
class QHTTPENGINE_EXPORT QHttpBasicAuth : public QHttpMiddleware
|
||||
class QHTTPENGINE_EXPORT HttpBasicAuth : public HttpMiddleware
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -49,7 +49,7 @@ public:
|
||||
*
|
||||
* The realm string is shown to a client when credentials are requested.
|
||||
*/
|
||||
QHttpBasicAuth(const QString &realm, QObject *parent = Q_NULLPTR);
|
||||
HttpBasicAuth(const QString &realm, QObject *parent = Q_NULLPTR);
|
||||
|
||||
/**
|
||||
* @brief Add credentials to the list
|
||||
@@ -65,7 +65,7 @@ public:
|
||||
* If the verify() method returns true, the client will be granted access
|
||||
* to the resources. Otherwise, 401 Unauthorized will be returned.
|
||||
*/
|
||||
virtual bool process(QHttpSocket *socket);
|
||||
virtual bool process(HttpSocket *socket);
|
||||
|
||||
protected:
|
||||
|
||||
@@ -76,7 +76,7 @@ protected:
|
||||
|
||||
private:
|
||||
|
||||
QHttpBasicAuthPrivate *const d;
|
||||
HttpBasicAuthPrivate *const d;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QHTTPBASICAUTH_H
|
||||
|
||||
@@ -28,9 +28,9 @@
|
||||
#include "qhttpengine_global.h"
|
||||
|
||||
class QRegExp;
|
||||
class QHttpMiddleware;
|
||||
class QHttpSocket;
|
||||
class QHTTPENGINE_EXPORT QHttpHandlerPrivate;
|
||||
class HttpMiddleware;
|
||||
class HttpSocket;
|
||||
class QHTTPENGINE_EXPORT HttpHandlerPrivate;
|
||||
|
||||
/**
|
||||
* @brief Base class for HTTP handlers
|
||||
@@ -68,7 +68,7 @@ class QHTTPENGINE_EXPORT QHttpHandlerPrivate;
|
||||
* the request or write an error to the socket. The default implementation of
|
||||
* process() simply returns an HTTP 404 error.
|
||||
*/
|
||||
class QHTTPENGINE_EXPORT QHttpHandler : public QObject
|
||||
class QHTTPENGINE_EXPORT HttpHandler : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -77,12 +77,12 @@ public:
|
||||
/**
|
||||
* @brief Base constructor for a handler
|
||||
*/
|
||||
explicit QHttpHandler(QObject *parent = 0);
|
||||
explicit HttpHandler(QObject *parent = 0);
|
||||
|
||||
/**
|
||||
* @brief Add middleware to the handler
|
||||
*/
|
||||
void addMiddleware(QHttpMiddleware *middleware);
|
||||
void addMiddleware(HttpMiddleware *middleware);
|
||||
|
||||
/**
|
||||
* @brief Add a redirect for a specific pattern
|
||||
@@ -103,12 +103,12 @@ public:
|
||||
* used when the route() method is invoked to determine whether the
|
||||
* request matches any patterns. The order of the list is preserved.
|
||||
*/
|
||||
void addSubHandler(const QRegExp &pattern, QHttpHandler *handler);
|
||||
void addSubHandler(const QRegExp &pattern, HttpHandler *handler);
|
||||
|
||||
/**
|
||||
* @brief Route an incoming request
|
||||
*/
|
||||
void route(QHttpSocket *socket, const QString &path);
|
||||
void route(HttpSocket *socket, const QString &path);
|
||||
|
||||
protected:
|
||||
|
||||
@@ -119,12 +119,12 @@ protected:
|
||||
* a redirect with QHttpSocket::writeRedirect(), or writing an error to
|
||||
* the socket using QHttpSocket::writeError().
|
||||
*/
|
||||
virtual void process(QHttpSocket *socket, const QString &path);
|
||||
virtual void process(HttpSocket *socket, const QString &path);
|
||||
|
||||
private:
|
||||
|
||||
QHttpHandlerPrivate *const d;
|
||||
friend class QHttpHandlerPrivate;
|
||||
HttpHandlerPrivate *const d;
|
||||
friend class HttpHandlerPrivate;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QHTTPHANDLER_H
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
#include "qhttpengine_global.h"
|
||||
|
||||
class QHttpSocket;
|
||||
class HttpSocket;
|
||||
|
||||
/**
|
||||
* @brief Pre-handler request processor
|
||||
@@ -35,7 +35,7 @@ class QHttpSocket;
|
||||
* Middleware sits between the server and the final request handler,
|
||||
* determining whether the request should be passed on to the handler.
|
||||
*/
|
||||
class QHTTPENGINE_EXPORT QHttpMiddleware : public QObject
|
||||
class QHTTPENGINE_EXPORT HttpMiddleware : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -44,7 +44,7 @@ public:
|
||||
/**
|
||||
* @brief Base constructor for middleware
|
||||
*/
|
||||
explicit QHttpMiddleware(QObject *parent = Q_NULLPTR) : QObject(parent) {}
|
||||
explicit HttpMiddleware(QObject *parent = Q_NULLPTR) : QObject(parent) {}
|
||||
|
||||
/**
|
||||
* @brief Determine if request processing should continue
|
||||
@@ -53,7 +53,7 @@ public:
|
||||
* returned, processing continues. Otherwise, it is assumed that an
|
||||
* appropriate error was written to the socket.
|
||||
*/
|
||||
virtual bool process(QHttpSocket *socket) = 0;
|
||||
virtual bool process(HttpSocket *socket) = 0;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QHTTPMIDDLEWARE_H
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
* response headers. Functionality is broken up into smaller methods in order
|
||||
* to make the unit tests simpler.
|
||||
*/
|
||||
class QHTTPENGINE_EXPORT QHttpParser
|
||||
class QHTTPENGINE_EXPORT HttpParser
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -56,7 +56,7 @@ public:
|
||||
/**
|
||||
* @brief Parse and remove the query string from a path
|
||||
*/
|
||||
static bool parsePath(const QByteArray &rawPath, QString &path, QHttpSocket::QueryStringMap &queryString);
|
||||
static bool parsePath(const QByteArray &rawPath, QString &path, HttpSocket::QueryStringMap &queryString);
|
||||
|
||||
/**
|
||||
* @brief Parse a list of lines containing HTTP headers
|
||||
@@ -64,7 +64,7 @@ public:
|
||||
* Each line is expected to be in the format "name: value". Parsing is
|
||||
* immediately aborted if an invalid line is encountered.
|
||||
*/
|
||||
static bool parseHeaderList(const QList<QByteArray> &lines, QHttpSocket::HeaderMap &headers);
|
||||
static bool parseHeaderList(const QList<QByteArray> &lines, HttpSocket::HeaderMap &headers);
|
||||
|
||||
/**
|
||||
* @brief Parse HTTP headers
|
||||
@@ -73,17 +73,17 @@ public:
|
||||
* into a status line and HTTP headers. The parts list will contain the
|
||||
* parts from the status line.
|
||||
*/
|
||||
static bool parseHeaders(const QByteArray &data, QList<QByteArray> &parts, QHttpSocket::HeaderMap &headers);
|
||||
static bool parseHeaders(const QByteArray &data, QList<QByteArray> &parts, HttpSocket::HeaderMap &headers);
|
||||
|
||||
/**
|
||||
* @brief Parse HTTP request headers
|
||||
*/
|
||||
static bool parseRequestHeaders(const QByteArray &data, QHttpSocket::Method &method, QByteArray &path, QHttpSocket::HeaderMap &headers);
|
||||
static bool parseRequestHeaders(const QByteArray &data, HttpSocket::Method &method, QByteArray &path, HttpSocket::HeaderMap &headers);
|
||||
|
||||
/**
|
||||
* @brief Parse HTTP response headers
|
||||
*/
|
||||
static bool parseResponseHeaders(const QByteArray &data, int &statusCode, QByteArray &statusReason, QHttpSocket::HeaderMap &headers);
|
||||
static bool parseResponseHeaders(const QByteArray &data, int &statusCode, QByteArray &statusReason, HttpSocket::HeaderMap &headers);
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QHTTPPARSER_H
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
#include "qhttpengine_global.h"
|
||||
|
||||
class QHTTPENGINE_EXPORT QHttpRangePrivate;
|
||||
class QHTTPENGINE_EXPORT HttpRangePrivate;
|
||||
|
||||
/**
|
||||
* @brief HTTP range representation
|
||||
@@ -61,7 +61,7 @@ class QHTTPENGINE_EXPORT QHttpRangePrivate;
|
||||
* @endcode
|
||||
*
|
||||
*/
|
||||
class QHTTPENGINE_EXPORT QHttpRange
|
||||
class QHTTPENGINE_EXPORT HttpRange
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -70,7 +70,7 @@ public:
|
||||
*
|
||||
* An empty QHttpRange is considered invalid.
|
||||
*/
|
||||
QHttpRange();
|
||||
HttpRange();
|
||||
|
||||
/**
|
||||
* @brief Construct QHttpRange by parsing range
|
||||
@@ -80,7 +80,7 @@ public:
|
||||
* constructor. dataSize may be supplied so that relative ranges could be
|
||||
* represented as absolute values.
|
||||
*/
|
||||
QHttpRange(const QString &range, qint64 dataSize = -1);
|
||||
HttpRange(const QString &range, qint64 dataSize = -1);
|
||||
|
||||
/**
|
||||
* @brief Construct QHttpRange, using from and to values
|
||||
@@ -89,7 +89,7 @@ public:
|
||||
* supplied so that relative ranges could be represented as
|
||||
* absolute values.
|
||||
*/
|
||||
QHttpRange(qint64 from, qint64 to, qint64 dataSize = -1);
|
||||
HttpRange(qint64 from, qint64 to, qint64 dataSize = -1);
|
||||
|
||||
/**
|
||||
* @brief Construct QHttpRange from other QHttpRange and dataSize
|
||||
@@ -97,17 +97,17 @@ public:
|
||||
* Initialises a new QHttpRange with from and to values of other
|
||||
* QHttpRequest. Supplied dataSize is used instead of other dataSize.
|
||||
*/
|
||||
QHttpRange(const QHttpRange &other, qint64 dataSize);
|
||||
HttpRange(const HttpRange &other, qint64 dataSize);
|
||||
|
||||
/**
|
||||
* @brief Destroy the range
|
||||
*/
|
||||
~QHttpRange();
|
||||
~HttpRange();
|
||||
|
||||
/**
|
||||
* @brief Assignment operator
|
||||
*/
|
||||
QHttpRange& operator=(const QHttpRange &other);
|
||||
HttpRange& operator=(const HttpRange &other);
|
||||
|
||||
/**
|
||||
* @brief Return starting position of range
|
||||
@@ -263,7 +263,7 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
QHttpRangePrivate *const d;
|
||||
HttpRangePrivate *const d;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QHTTPRANGE_H
|
||||
|
||||
@@ -33,8 +33,8 @@
|
||||
class QSslConfiguration;
|
||||
#endif
|
||||
|
||||
class QHttpHandler;
|
||||
class QHTTPENGINE_EXPORT QHttpServerPrivate;
|
||||
class HttpHandler;
|
||||
class QHTTPENGINE_EXPORT HttpServerPrivate;
|
||||
|
||||
/**
|
||||
* @brief TCP server for HTTP requests
|
||||
@@ -60,7 +60,7 @@ class QHTTPENGINE_EXPORT QHttpServerPrivate;
|
||||
* signal is connected to the QHttpSocket's deleteLater() slot to ensure that
|
||||
* the socket is deleted when the client disconnects.
|
||||
*/
|
||||
class QHTTPENGINE_EXPORT QHttpServer : public QTcpServer
|
||||
class QHTTPENGINE_EXPORT HttpServer : public QTcpServer
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -69,17 +69,17 @@ public:
|
||||
/**
|
||||
* @brief Create an HTTP server
|
||||
*/
|
||||
explicit QHttpServer(QObject *parent = 0);
|
||||
explicit HttpServer(QObject *parent = 0);
|
||||
|
||||
/**
|
||||
* @brief Create an HTTP server with the specified handler
|
||||
*/
|
||||
QHttpServer(QHttpHandler *handler, QObject *parent = 0);
|
||||
HttpServer(HttpHandler *handler, QObject *parent = 0);
|
||||
|
||||
/**
|
||||
* @brief Set the root handler for all new requests
|
||||
*/
|
||||
void setHandler(QHttpHandler *handler);
|
||||
void setHandler(HttpHandler *handler);
|
||||
|
||||
#if !defined(QT_NO_SSL)
|
||||
/**
|
||||
@@ -100,8 +100,8 @@ protected:
|
||||
|
||||
private:
|
||||
|
||||
QHttpServerPrivate *const d;
|
||||
friend class QHttpServerPrivate;
|
||||
HttpServerPrivate *const d;
|
||||
friend class HttpServerPrivate;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QHTTPSERVER_H
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
|
||||
class QJsonDocument;
|
||||
class QTcpSocket;
|
||||
class QHTTPENGINE_EXPORT QHttpSocketPrivate;
|
||||
class QHTTPENGINE_EXPORT HttpSocketPrivate;
|
||||
|
||||
/**
|
||||
* @brief Implementation of the HTTP protocol
|
||||
@@ -89,7 +89,7 @@ class QHTTPENGINE_EXPORT QHttpSocketPrivate;
|
||||
* status code to the writeError() method. Both methods will close the socket
|
||||
* once the response is written.
|
||||
*/
|
||||
class QHTTPENGINE_EXPORT QHttpSocket : public QIODevice
|
||||
class QHTTPENGINE_EXPORT HttpSocket : public QIODevice
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -106,7 +106,7 @@ public:
|
||||
* The key used for the map is the QIByteArray class, which allows for
|
||||
* case-insensitive comparison.
|
||||
*/
|
||||
typedef QMultiMap<QIByteArray, QByteArray> HeaderMap;
|
||||
typedef QMultiMap<IByteArray, QByteArray> HeaderMap;
|
||||
|
||||
/**
|
||||
* HTTP methods
|
||||
@@ -177,7 +177,7 @@ public:
|
||||
* This instance will assume ownership of the socket. That is, it will
|
||||
* make itself the parent of the socket.
|
||||
*/
|
||||
QHttpSocket(QTcpSocket *socket, QObject *parent = 0);
|
||||
HttpSocket(QTcpSocket *socket, QObject *parent = 0);
|
||||
|
||||
/**
|
||||
* @brief Retrieve the number of bytes available for reading
|
||||
@@ -354,8 +354,8 @@ protected:
|
||||
|
||||
private:
|
||||
|
||||
QHttpSocketPrivate *const d;
|
||||
friend class QHttpSocketPrivate;
|
||||
HttpSocketPrivate *const d;
|
||||
friend class HttpSocketPrivate;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QHTTPSOCKET_H
|
||||
|
||||
@@ -35,15 +35,15 @@
|
||||
* The QIByteArray is identical to the QByteArray class in all aspects except
|
||||
* that it performs comparisons in a case-insensitive manner.
|
||||
*/
|
||||
class QHTTPENGINE_EXPORT QIByteArray : public QByteArray
|
||||
class QHTTPENGINE_EXPORT IByteArray : public QByteArray
|
||||
{
|
||||
public:
|
||||
|
||||
/// \{
|
||||
QIByteArray() {}
|
||||
QIByteArray(const QByteArray &other) : QByteArray(other) {}
|
||||
QIByteArray(const QIByteArray &other) : QByteArray(other) {}
|
||||
QIByteArray(const char *data, int size = -1) : QByteArray(data, size) {}
|
||||
IByteArray() {}
|
||||
IByteArray(const QByteArray &other) : QByteArray(other) {}
|
||||
IByteArray(const IByteArray &other) : QByteArray(other) {}
|
||||
IByteArray(const char *data, int size = -1) : QByteArray(data, size) {}
|
||||
|
||||
inline bool operator==(const QString &s2) const { return toLower() == s2.toLower(); }
|
||||
inline bool operator!=(const QString &s2) const { return toLower() != s2.toLower(); }
|
||||
@@ -58,40 +58,40 @@ public:
|
||||
/// \}
|
||||
};
|
||||
|
||||
inline bool operator==(const QIByteArray &a1, const char *a2) { return a1.toLower() == QByteArray(a2).toLower(); }
|
||||
inline bool operator==(const char *a1, const QIByteArray &a2) { return QByteArray(a1).toLower() == a2.toLower(); }
|
||||
inline bool operator==(const QIByteArray &a1, const QByteArray &a2) { return a1.toLower() == a2.toLower(); }
|
||||
inline bool operator==(const QByteArray &a1, const QIByteArray &a2) { return a1.toLower() == a2.toLower(); }
|
||||
inline bool operator==(const QIByteArray &a1, const QIByteArray &a2) { return a1.toLower() == a2.toLower(); }
|
||||
inline bool operator==(const IByteArray &a1, const char *a2) { return a1.toLower() == QByteArray(a2).toLower(); }
|
||||
inline bool operator==(const char *a1, const IByteArray &a2) { return QByteArray(a1).toLower() == a2.toLower(); }
|
||||
inline bool operator==(const IByteArray &a1, const QByteArray &a2) { return a1.toLower() == a2.toLower(); }
|
||||
inline bool operator==(const QByteArray &a1, const IByteArray &a2) { return a1.toLower() == a2.toLower(); }
|
||||
inline bool operator==(const IByteArray &a1, const IByteArray &a2) { return a1.toLower() == a2.toLower(); }
|
||||
|
||||
inline bool operator!=(const QIByteArray &a1, const char *a2) { return a1.toLower() != QByteArray(a2).toLower(); }
|
||||
inline bool operator!=(const char *a1, const QIByteArray &a2) { return QByteArray(a1).toLower() != a2.toLower(); }
|
||||
inline bool operator!=(const QIByteArray &a1, const QByteArray &a2) { return a1.toLower() != a2.toLower(); }
|
||||
inline bool operator!=(const QByteArray &a1, const QIByteArray &a2) { return a1.toLower() != a2.toLower(); }
|
||||
inline bool operator!=(const QIByteArray &a1, const QIByteArray &a2) { return a1.toLower() != a2.toLower(); }
|
||||
inline bool operator!=(const IByteArray &a1, const char *a2) { return a1.toLower() != QByteArray(a2).toLower(); }
|
||||
inline bool operator!=(const char *a1, const IByteArray &a2) { return QByteArray(a1).toLower() != a2.toLower(); }
|
||||
inline bool operator!=(const IByteArray &a1, const QByteArray &a2) { return a1.toLower() != a2.toLower(); }
|
||||
inline bool operator!=(const QByteArray &a1, const IByteArray &a2) { return a1.toLower() != a2.toLower(); }
|
||||
inline bool operator!=(const IByteArray &a1, const IByteArray &a2) { return a1.toLower() != a2.toLower(); }
|
||||
|
||||
inline bool operator<(const QIByteArray &a1, const char *a2) { return a1.toLower() < QByteArray(a2).toLower(); }
|
||||
inline bool operator<(const char *a1, const QIByteArray &a2) { return QByteArray(a1).toLower() < a2.toLower(); }
|
||||
inline bool operator<(const QIByteArray &a1, const QByteArray &a2) { return a1.toLower() < a2.toLower(); }
|
||||
inline bool operator<(const QByteArray &a1, const QIByteArray &a2) { return a1.toLower() < a2.toLower(); }
|
||||
inline bool operator<(const QIByteArray &a1, const QIByteArray &a2) { return a1.toLower() < a2.toLower(); }
|
||||
inline bool operator<(const IByteArray &a1, const char *a2) { return a1.toLower() < QByteArray(a2).toLower(); }
|
||||
inline bool operator<(const char *a1, const IByteArray &a2) { return QByteArray(a1).toLower() < a2.toLower(); }
|
||||
inline bool operator<(const IByteArray &a1, const QByteArray &a2) { return a1.toLower() < a2.toLower(); }
|
||||
inline bool operator<(const QByteArray &a1, const IByteArray &a2) { return a1.toLower() < a2.toLower(); }
|
||||
inline bool operator<(const IByteArray &a1, const IByteArray &a2) { return a1.toLower() < a2.toLower(); }
|
||||
|
||||
inline bool operator>(const QIByteArray &a1, const char *a2) { return a1.toLower() > QByteArray(a2).toLower(); }
|
||||
inline bool operator>(const char *a1, const QIByteArray &a2) { return QByteArray(a1).toLower() > a2.toLower(); }
|
||||
inline bool operator>(const QIByteArray &a1, const QByteArray &a2) { return a1.toLower() > a2.toLower(); }
|
||||
inline bool operator>(const QByteArray &a1, const QIByteArray &a2) { return a1.toLower() > a2.toLower(); }
|
||||
inline bool operator>(const QIByteArray &a1, const QIByteArray &a2) { return a1.toLower() > a2.toLower(); }
|
||||
inline bool operator>(const IByteArray &a1, const char *a2) { return a1.toLower() > QByteArray(a2).toLower(); }
|
||||
inline bool operator>(const char *a1, const IByteArray &a2) { return QByteArray(a1).toLower() > a2.toLower(); }
|
||||
inline bool operator>(const IByteArray &a1, const QByteArray &a2) { return a1.toLower() > a2.toLower(); }
|
||||
inline bool operator>(const QByteArray &a1, const IByteArray &a2) { return a1.toLower() > a2.toLower(); }
|
||||
inline bool operator>(const IByteArray &a1, const IByteArray &a2) { return a1.toLower() > a2.toLower(); }
|
||||
|
||||
inline bool operator<=(const QIByteArray &a1, const char *a2) { return a1.toLower() <= QByteArray(a2).toLower(); }
|
||||
inline bool operator<=(const char *a1, const QIByteArray &a2) { return QByteArray(a1).toLower() <= a2.toLower(); }
|
||||
inline bool operator<=(const QIByteArray &a1, const QByteArray &a2) { return a1.toLower() <= a2.toLower(); }
|
||||
inline bool operator<=(const QByteArray &a1, const QIByteArray &a2) { return a1.toLower() <= a2.toLower(); }
|
||||
inline bool operator<=(const QIByteArray &a1, const QIByteArray &a2) { return a1.toLower() <= a2.toLower(); }
|
||||
inline bool operator<=(const IByteArray &a1, const char *a2) { return a1.toLower() <= QByteArray(a2).toLower(); }
|
||||
inline bool operator<=(const char *a1, const IByteArray &a2) { return QByteArray(a1).toLower() <= a2.toLower(); }
|
||||
inline bool operator<=(const IByteArray &a1, const QByteArray &a2) { return a1.toLower() <= a2.toLower(); }
|
||||
inline bool operator<=(const QByteArray &a1, const IByteArray &a2) { return a1.toLower() <= a2.toLower(); }
|
||||
inline bool operator<=(const IByteArray &a1, const IByteArray &a2) { return a1.toLower() <= a2.toLower(); }
|
||||
|
||||
inline bool operator>=(const QIByteArray &a1, const char *a2) { return a1.toLower() >= QByteArray(a2).toLower(); }
|
||||
inline bool operator>=(const char *a1, const QIByteArray &a2) { return QByteArray(a1).toLower() >= a2.toLower(); }
|
||||
inline bool operator>=(const QIByteArray &a1, const QByteArray &a2) { return a1.toLower() >= a2.toLower(); }
|
||||
inline bool operator>=(const QByteArray &a1, const QIByteArray &a2) { return a1.toLower() >= a2.toLower(); }
|
||||
inline bool operator>=(const QIByteArray &a1, const QIByteArray &a2) { return a1.toLower() >= a2.toLower(); }
|
||||
inline bool operator>=(const IByteArray &a1, const char *a2) { return a1.toLower() >= QByteArray(a2).toLower(); }
|
||||
inline bool operator>=(const char *a1, const IByteArray &a2) { return QByteArray(a1).toLower() >= a2.toLower(); }
|
||||
inline bool operator>=(const IByteArray &a1, const QByteArray &a2) { return a1.toLower() >= a2.toLower(); }
|
||||
inline bool operator>=(const QByteArray &a1, const IByteArray &a2) { return a1.toLower() >= a2.toLower(); }
|
||||
inline bool operator>=(const IByteArray &a1, const IByteArray &a2) { return a1.toLower() >= a2.toLower(); }
|
||||
|
||||
#endif // QHTTPENGINE_QIBYTEARRAY_H
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
|
||||
#include "qhttpengine_global.h"
|
||||
|
||||
class QHTTPENGINE_EXPORT QLocalAuthPrivate;
|
||||
class QHTTPENGINE_EXPORT LocalAuthPrivate;
|
||||
|
||||
/**
|
||||
* @brief Middleware for local file-based authentication
|
||||
@@ -49,7 +49,7 @@ class QHTTPENGINE_EXPORT QLocalAuthPrivate;
|
||||
*
|
||||
* Additional data can be added to the object using the setData() method.
|
||||
*/
|
||||
class QHTTPENGINE_EXPORT QLocalAuth : public QHttpMiddleware
|
||||
class QHTTPENGINE_EXPORT LocalAuth : public HttpMiddleware
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -61,7 +61,7 @@ public:
|
||||
* To determine whether the local file was created successfully, call the
|
||||
* exists() method.
|
||||
*/
|
||||
explicit QLocalAuth(QObject *parent = Q_NULLPTR);
|
||||
explicit LocalAuth(QObject *parent = Q_NULLPTR);
|
||||
|
||||
/**
|
||||
* @brief Determine whether the file exists
|
||||
@@ -91,11 +91,11 @@ public:
|
||||
* If the token supplied by the client matches, the request is allowed.
|
||||
* Otherwise, an HTTP 403 error is returned.
|
||||
*/
|
||||
virtual bool process(QHttpSocket *socket);
|
||||
virtual bool process(HttpSocket *socket);
|
||||
|
||||
private:
|
||||
|
||||
QLocalAuthPrivate *const d;
|
||||
LocalAuthPrivate *const d;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QLOCALAUTH_H
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
#include "qhttpengine_global.h"
|
||||
|
||||
class QHTTPENGINE_EXPORT QLocalFilePrivate;
|
||||
class QHTTPENGINE_EXPORT LocalFilePrivate;
|
||||
|
||||
/**
|
||||
* @brief Locally accessible file
|
||||
@@ -49,7 +49,7 @@ class QHTTPENGINE_EXPORT QLocalFilePrivate;
|
||||
* For example, if the application name was "test" and the user's home
|
||||
* directory was `/home/bob`, the absolute path would be `/home/bob/.test`.
|
||||
*/
|
||||
class QHTTPENGINE_EXPORT QLocalFile : public QFile
|
||||
class QHTTPENGINE_EXPORT LocalFile : public QFile
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -58,7 +58,7 @@ public:
|
||||
/**
|
||||
* @brief Create a new local file
|
||||
*/
|
||||
explicit QLocalFile(QObject *parent = 0);
|
||||
explicit LocalFile(QObject *parent = 0);
|
||||
|
||||
/**
|
||||
* @brief Attempt to open the file
|
||||
@@ -71,8 +71,8 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
QLocalFilePrivate *const d;
|
||||
friend class QLocalFilePrivate;
|
||||
LocalFilePrivate *const d;
|
||||
friend class LocalFilePrivate;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QLOCALFILE_H
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
#include "qhttpengine_global.h"
|
||||
|
||||
class QHttpSocket;
|
||||
class HttpSocket;
|
||||
class QHTTPENGINE_EXPORT QObjectHandlerPrivate;
|
||||
|
||||
/**
|
||||
@@ -67,7 +67,7 @@ class QHTTPENGINE_EXPORT QObjectHandlerPrivate;
|
||||
* });
|
||||
* @endcode
|
||||
*/
|
||||
class QHTTPENGINE_EXPORT QObjectHandler : public QHttpHandler
|
||||
class QHTTPENGINE_EXPORT QObjectHandler : public HttpHandler
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -123,7 +123,7 @@ public:
|
||||
"The slot must have exactly one argument.");
|
||||
|
||||
// Ensure the argument is of the correct type
|
||||
Q_STATIC_ASSERT_X((QtPrivate::AreArgumentsCompatible<QHttpSocket*, typename QtPrivate::List_Select<typename SlotType::Arguments, 0>::Value>::value),
|
||||
Q_STATIC_ASSERT_X((QtPrivate::AreArgumentsCompatible<HttpSocket*, typename QtPrivate::List_Select<typename SlotType::Arguments, 0>::Value>::value),
|
||||
"The slot parameters do not match");
|
||||
|
||||
// Invoke the implementation
|
||||
@@ -157,7 +157,7 @@ protected:
|
||||
/**
|
||||
* @brief Reimplementation of QHttpHandler::process()
|
||||
*/
|
||||
virtual void process(QHttpSocket *socket, const QString &path);
|
||||
virtual void process(HttpSocket *socket, const QString &path);
|
||||
|
||||
private:
|
||||
|
||||
@@ -171,7 +171,7 @@ private:
|
||||
"The slot must have exactly one argument.");
|
||||
|
||||
// Ensure the argument is of the correct type
|
||||
Q_STATIC_ASSERT_X((QtPrivate::AreArgumentsCompatible<QHttpSocket*, typename QtPrivate::List_Select<typename SlotType::Arguments, 0>::Value>::value),
|
||||
Q_STATIC_ASSERT_X((QtPrivate::AreArgumentsCompatible<HttpSocket*, typename QtPrivate::List_Select<typename SlotType::Arguments, 0>::Value>::value),
|
||||
"The slot parameters do not match");
|
||||
|
||||
registerMethodImpl(name, context,
|
||||
|
||||
@@ -29,12 +29,12 @@
|
||||
|
||||
#include "qhttpengine_global.h"
|
||||
|
||||
class QHTTPENGINE_EXPORT QProxyHandlerPrivate;
|
||||
class QHTTPENGINE_EXPORT ProxyHandlerPrivate;
|
||||
|
||||
/**
|
||||
* @brief Handler that routes HTTP requests to an upstream server
|
||||
*/
|
||||
class QHTTPENGINE_EXPORT QProxyHandler : public QHttpHandler
|
||||
class QHTTPENGINE_EXPORT ProxyHandler : public HttpHandler
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -43,18 +43,18 @@ public:
|
||||
/**
|
||||
* @brief Create a new proxy handler
|
||||
*/
|
||||
QProxyHandler(const QHostAddress &address, quint16 port, QObject *parent = 0);
|
||||
ProxyHandler(const QHostAddress &address, quint16 port, QObject *parent = 0);
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* @brief Reimplementation of QHttpHandler::process()
|
||||
*/
|
||||
virtual void process(QHttpSocket *socket, const QString &path);
|
||||
virtual void process(HttpSocket *socket, const QString &path);
|
||||
|
||||
private:
|
||||
|
||||
QProxyHandlerPrivate *const d;
|
||||
ProxyHandlerPrivate *const d;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QPROXYHANDLER_H
|
||||
|
||||
@@ -48,12 +48,12 @@ const QString ListTemplate =
|
||||
"</body>"
|
||||
"</html>";
|
||||
|
||||
QFilesystemHandlerPrivate::QFilesystemHandlerPrivate(QFilesystemHandler *handler)
|
||||
FilesystemHandlerPrivate::FilesystemHandlerPrivate(FilesystemHandler *handler)
|
||||
: QObject(handler)
|
||||
{
|
||||
}
|
||||
|
||||
bool QFilesystemHandlerPrivate::absolutePath(const QString &path, QString &absolutePath)
|
||||
bool FilesystemHandlerPrivate::absolutePath(const QString &path, QString &absolutePath)
|
||||
{
|
||||
// Resolve the path according to the document root
|
||||
absolutePath = documentRoot.absoluteFilePath(path);
|
||||
@@ -64,20 +64,20 @@ bool QFilesystemHandlerPrivate::absolutePath(const QString &path, QString &absol
|
||||
return documentRoot.exists(absolutePath) && !documentRoot.relativeFilePath(path).startsWith("../");
|
||||
}
|
||||
|
||||
QByteArray QFilesystemHandlerPrivate::mimeType(const QString &absolutePath)
|
||||
QByteArray FilesystemHandlerPrivate::mimeType(const QString &absolutePath)
|
||||
{
|
||||
// Query the MIME database based on the filename and its contents
|
||||
return database.mimeTypeForFile(absolutePath).name().toUtf8();
|
||||
}
|
||||
|
||||
void QFilesystemHandlerPrivate::processFile(QHttpSocket *socket, const QString &absolutePath)
|
||||
void FilesystemHandlerPrivate::processFile(HttpSocket *socket, const QString &absolutePath)
|
||||
{
|
||||
// Attempt to open the file for reading
|
||||
QFile *file = new QFile(absolutePath);
|
||||
if (!file->open(QIODevice::ReadOnly)) {
|
||||
delete file;
|
||||
|
||||
socket->writeError(QHttpSocket::Forbidden);
|
||||
socket->writeError(HttpSocket::Forbidden);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ void QFilesystemHandlerPrivate::processFile(QHttpSocket *socket, const QString &
|
||||
|
||||
// Checking for partial content request
|
||||
QByteArray rangeHeader = socket->headers().value("Range");
|
||||
QHttpRange range;
|
||||
HttpRange range;
|
||||
|
||||
if (!rangeHeader.isEmpty() && rangeHeader.startsWith("bytes=")) {
|
||||
// Skiping 'bytes=' - first 6 chars and spliting ranges by comma
|
||||
@@ -101,12 +101,12 @@ void QFilesystemHandlerPrivate::processFile(QHttpSocket *socket, const QString &
|
||||
|
||||
// Taking only first range, as multiple ranges require multipart
|
||||
// reply support
|
||||
range = QHttpRange(QString(rangeList.at(0)), fileSize);
|
||||
range = HttpRange(QString(rangeList.at(0)), fileSize);
|
||||
}
|
||||
|
||||
// If range is valid, send partial content
|
||||
if (range.isValid()) {
|
||||
socket->setStatusCode(QHttpSocket::PartialContent);
|
||||
socket->setStatusCode(HttpSocket::PartialContent);
|
||||
socket->setHeader("Content-Length", QByteArray::number(range.length()));
|
||||
socket->setHeader("Content-Range", QByteArray("bytes ") + range.contentRange().toLatin1());
|
||||
copier->setRange(range.from(), range.to());
|
||||
@@ -124,7 +124,7 @@ void QFilesystemHandlerPrivate::processFile(QHttpSocket *socket, const QString &
|
||||
copier->start();
|
||||
}
|
||||
|
||||
void QFilesystemHandlerPrivate::processDirectory(QHttpSocket *socket, const QString &path, const QString &absolutePath)
|
||||
void FilesystemHandlerPrivate::processDirectory(HttpSocket *socket, const QString &path, const QString &absolutePath)
|
||||
{
|
||||
// Add entries for each of the files
|
||||
QString listing;
|
||||
@@ -148,29 +148,29 @@ void QFilesystemHandlerPrivate::processDirectory(QHttpSocket *socket, const QStr
|
||||
socket->close();
|
||||
}
|
||||
|
||||
QFilesystemHandler::QFilesystemHandler(QObject *parent)
|
||||
: QHttpHandler(parent),
|
||||
d(new QFilesystemHandlerPrivate(this))
|
||||
FilesystemHandler::FilesystemHandler(QObject *parent)
|
||||
: HttpHandler(parent),
|
||||
d(new FilesystemHandlerPrivate(this))
|
||||
{
|
||||
}
|
||||
|
||||
QFilesystemHandler::QFilesystemHandler(const QString &documentRoot, QObject *parent)
|
||||
: QHttpHandler(parent),
|
||||
d(new QFilesystemHandlerPrivate(this))
|
||||
FilesystemHandler::FilesystemHandler(const QString &documentRoot, QObject *parent)
|
||||
: HttpHandler(parent),
|
||||
d(new FilesystemHandlerPrivate(this))
|
||||
{
|
||||
setDocumentRoot(documentRoot);
|
||||
}
|
||||
|
||||
void QFilesystemHandler::setDocumentRoot(const QString &documentRoot)
|
||||
void FilesystemHandler::setDocumentRoot(const QString &documentRoot)
|
||||
{
|
||||
d->documentRoot.setPath(documentRoot);
|
||||
}
|
||||
|
||||
void QFilesystemHandler::process(QHttpSocket *socket, const QString &path)
|
||||
void FilesystemHandler::process(HttpSocket *socket, const QString &path)
|
||||
{
|
||||
// If a document root is not set, an error has occurred
|
||||
if (d->documentRoot.path().isNull()) {
|
||||
socket->writeError(QHttpSocket::InternalServerError);
|
||||
socket->writeError(HttpSocket::InternalServerError);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -180,7 +180,7 @@ void QFilesystemHandler::process(QHttpSocket *socket, const QString &path)
|
||||
// Attempt to retrieve the absolute path
|
||||
QString absolutePath;
|
||||
if (!d->absolutePath(decodedPath, absolutePath)) {
|
||||
socket->writeError(QHttpSocket::NotFound);
|
||||
socket->writeError(HttpSocket::NotFound);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,19 +30,19 @@
|
||||
#include <qhttpengine/qfilesystemhandler.h>
|
||||
#include <qhttpengine/qhttpsocket.h>
|
||||
|
||||
class QFilesystemHandlerPrivate : public QObject
|
||||
class FilesystemHandlerPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
QFilesystemHandlerPrivate(QFilesystemHandler *handler);
|
||||
FilesystemHandlerPrivate(FilesystemHandler *handler);
|
||||
|
||||
bool absolutePath(const QString &path, QString &absolutePath);
|
||||
QByteArray mimeType(const QString &path);
|
||||
|
||||
void processFile(QHttpSocket *socket, const QString &absolutePath);
|
||||
void processDirectory(QHttpSocket *socket, const QString &path, const QString &absolutePath);
|
||||
void processFile(HttpSocket*socket, const QString &absolutePath);
|
||||
void processDirectory(HttpSocket*socket, const QString &path, const QString &absolutePath);
|
||||
|
||||
QDir documentRoot;
|
||||
QMimeDatabase database;
|
||||
|
||||
@@ -27,37 +27,37 @@
|
||||
|
||||
#include "qhttpbasicauth_p.h"
|
||||
|
||||
QHttpBasicAuthPrivate::QHttpBasicAuthPrivate(QObject *parent, const QString &realm)
|
||||
HttpBasicAuthPrivate::HttpBasicAuthPrivate(QObject *parent, const QString &realm)
|
||||
: QObject(parent),
|
||||
realm(realm)
|
||||
{
|
||||
}
|
||||
|
||||
QHttpBasicAuth::QHttpBasicAuth(const QString &realm, QObject *parent)
|
||||
: QHttpMiddleware(parent),
|
||||
d(new QHttpBasicAuthPrivate(this, realm))
|
||||
HttpBasicAuth::HttpBasicAuth(const QString &realm, QObject *parent)
|
||||
: HttpMiddleware(parent),
|
||||
d(new HttpBasicAuthPrivate(this, realm))
|
||||
{
|
||||
}
|
||||
|
||||
void QHttpBasicAuth::add(const QString &username, const QString &password)
|
||||
void HttpBasicAuth::add(const QString &username, const QString &password)
|
||||
{
|
||||
d->map.insert(username, password);
|
||||
}
|
||||
|
||||
bool QHttpBasicAuth::verify(const QString &username, const QString &password)
|
||||
bool HttpBasicAuth::verify(const QString &username, const QString &password)
|
||||
{
|
||||
return d->map.contains(username) && d->map.value(username) == password;
|
||||
}
|
||||
|
||||
bool QHttpBasicAuth::process(QHttpSocket *socket)
|
||||
bool HttpBasicAuth::process(HttpSocket *socket)
|
||||
{
|
||||
// Attempt to extract credentials from the header
|
||||
QByteArrayList headerParts = socket->headers().value("Authorization").split(' ');
|
||||
if (headerParts.count() == 2 && headerParts.at(0) == QIByteArray("Basic")) {
|
||||
if (headerParts.count() == 2 && headerParts.at(0) == IByteArray("Basic")) {
|
||||
|
||||
// Decode the credentials and split into username/password
|
||||
QByteArrayList parts;
|
||||
QHttpParser::split(
|
||||
HttpParser::split(
|
||||
QByteArray::fromBase64(headerParts.at(1)),
|
||||
":", 1, parts
|
||||
);
|
||||
@@ -70,6 +70,6 @@ bool QHttpBasicAuth::process(QHttpSocket *socket)
|
||||
|
||||
// Otherwise, inform the client that valid credentials are required
|
||||
socket->setHeader("WWW-Authenticate", QString("Basic realm=\"%1\"").arg(d->realm).toUtf8());
|
||||
socket->writeError(QHttpSocket::Unauthorized);
|
||||
socket->writeError(HttpSocket::Unauthorized);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -26,13 +26,13 @@
|
||||
#include <QMap>
|
||||
#include <QObject>
|
||||
|
||||
class QHttpBasicAuthPrivate : public QObject
|
||||
class HttpBasicAuthPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
explicit QHttpBasicAuthPrivate(QObject *parent, const QString &realm);
|
||||
explicit HttpBasicAuthPrivate(QObject *parent, const QString &realm);
|
||||
|
||||
const QString realm;
|
||||
QMap<QString, QString> map;
|
||||
|
||||
@@ -26,37 +26,37 @@
|
||||
|
||||
#include "qhttphandler_p.h"
|
||||
|
||||
QHttpHandlerPrivate::QHttpHandlerPrivate(QHttpHandler *handler)
|
||||
HttpHandlerPrivate::HttpHandlerPrivate(HttpHandler *handler)
|
||||
: QObject(handler),
|
||||
q(handler)
|
||||
{
|
||||
}
|
||||
|
||||
QHttpHandler::QHttpHandler(QObject *parent)
|
||||
HttpHandler::HttpHandler(QObject *parent)
|
||||
: QObject(parent),
|
||||
d(new QHttpHandlerPrivate(this))
|
||||
d(new HttpHandlerPrivate(this))
|
||||
{
|
||||
}
|
||||
|
||||
void QHttpHandler::addMiddleware(QHttpMiddleware *middleware)
|
||||
void HttpHandler::addMiddleware(HttpMiddleware *middleware)
|
||||
{
|
||||
d->middleware.append(middleware);
|
||||
}
|
||||
|
||||
void QHttpHandler::addRedirect(const QRegExp &pattern, const QString &path)
|
||||
void HttpHandler::addRedirect(const QRegExp &pattern, const QString &path)
|
||||
{
|
||||
d->redirects.append(Redirect(pattern, path));
|
||||
}
|
||||
|
||||
void QHttpHandler::addSubHandler(const QRegExp &pattern, QHttpHandler *handler)
|
||||
void HttpHandler::addSubHandler(const QRegExp &pattern, HttpHandler *handler)
|
||||
{
|
||||
d->subHandlers.append(SubHandler(pattern, handler));
|
||||
}
|
||||
|
||||
void QHttpHandler::route(QHttpSocket *socket, const QString &path)
|
||||
void HttpHandler::route(HttpSocket *socket, const QString &path)
|
||||
{
|
||||
// Run through each of the middleware
|
||||
foreach (QHttpMiddleware *middleware, d->middleware) {
|
||||
foreach (HttpMiddleware *middleware, d->middleware) {
|
||||
if (!middleware->process(socket)) {
|
||||
return;
|
||||
}
|
||||
@@ -86,9 +86,9 @@ void QHttpHandler::route(QHttpSocket *socket, const QString &path)
|
||||
process(socket, path);
|
||||
}
|
||||
|
||||
void QHttpHandler::process(QHttpSocket *socket, const QString &)
|
||||
void HttpHandler::process(HttpSocket *socket, const QString &)
|
||||
{
|
||||
// The default response is simply a 404 error
|
||||
socket->writeError(QHttpSocket::NotFound);
|
||||
socket->writeError(HttpSocket::NotFound);
|
||||
socket->close();
|
||||
}
|
||||
|
||||
@@ -31,23 +31,23 @@
|
||||
#include <qhttpengine/qhttphandler.h>
|
||||
|
||||
typedef QPair<QRegExp, QString> Redirect;
|
||||
typedef QPair<QRegExp, QHttpHandler*> SubHandler;
|
||||
typedef QPair<QRegExp, HttpHandler*> SubHandler;
|
||||
|
||||
class QHttpHandlerPrivate : public QObject
|
||||
class HttpHandlerPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
explicit QHttpHandlerPrivate(QHttpHandler *handler);
|
||||
explicit HttpHandlerPrivate(HttpHandler *handler);
|
||||
|
||||
QList<Redirect> redirects;
|
||||
QList<SubHandler> subHandlers;
|
||||
QList<QHttpMiddleware*> middleware;
|
||||
QList<HttpMiddleware*> middleware;
|
||||
|
||||
private:
|
||||
|
||||
QHttpHandler *const q;
|
||||
HttpHandler*const q;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QHTTPHANDLERPRIVATE_H
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
|
||||
#include <qhttpengine/qhttpparser.h>
|
||||
|
||||
void QHttpParser::split(const QByteArray &data, const QByteArray &delim, int maxSplit, QByteArrayList &parts)
|
||||
void HttpParser::split(const QByteArray &data, const QByteArray &delim, int maxSplit, QByteArrayList &parts)
|
||||
{
|
||||
int index = 0;
|
||||
|
||||
@@ -45,7 +45,7 @@ void QHttpParser::split(const QByteArray &data, const QByteArray &delim, int max
|
||||
parts.append(data.mid(index));
|
||||
}
|
||||
|
||||
bool QHttpParser::parsePath(const QByteArray &rawPath, QString &path, QHttpSocket::QueryStringMap &queryString)
|
||||
bool HttpParser::parsePath(const QByteArray &rawPath, QString &path, HttpSocket::QueryStringMap &queryString)
|
||||
{
|
||||
QUrl url(rawPath);
|
||||
if (!url.isValid()) {
|
||||
@@ -61,7 +61,7 @@ bool QHttpParser::parsePath(const QByteArray &rawPath, QString &path, QHttpSocke
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QHttpParser::parseHeaderList(const QList<QByteArray> &lines, QHttpSocket::HeaderMap &headers)
|
||||
bool HttpParser::parseHeaderList(const QList<QByteArray> &lines, HttpSocket::HeaderMap &headers)
|
||||
{
|
||||
foreach (const QByteArray &line, lines) {
|
||||
|
||||
@@ -80,7 +80,7 @@ bool QHttpParser::parseHeaderList(const QList<QByteArray> &lines, QHttpSocket::H
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QHttpParser::parseHeaders(const QByteArray &data, QList<QByteArray> &parts, QHttpSocket::HeaderMap &headers)
|
||||
bool HttpParser::parseHeaders(const QByteArray &data, QList<QByteArray> &parts, HttpSocket::HeaderMap &headers)
|
||||
{
|
||||
// Split the data into individual lines
|
||||
QList<QByteArray> lines;
|
||||
@@ -95,7 +95,7 @@ bool QHttpParser::parseHeaders(const QByteArray &data, QList<QByteArray> &parts,
|
||||
return parseHeaderList(lines, headers);
|
||||
}
|
||||
|
||||
bool QHttpParser::parseRequestHeaders(const QByteArray &data, QHttpSocket::Method &method, QByteArray &path, QHttpSocket::HeaderMap &headers)
|
||||
bool HttpParser::parseRequestHeaders(const QByteArray &data, HttpSocket::Method &method, QByteArray &path, HttpSocket::HeaderMap &headers)
|
||||
{
|
||||
QList<QByteArray> parts;
|
||||
if (!parseHeaders(data, parts, headers)) {
|
||||
@@ -108,21 +108,21 @@ bool QHttpParser::parseRequestHeaders(const QByteArray &data, QHttpSocket::Metho
|
||||
}
|
||||
|
||||
if (parts[0] == "OPTIONS") {
|
||||
method = QHttpSocket::OPTIONS;
|
||||
method = HttpSocket::OPTIONS;
|
||||
} else if (parts[0] == "GET") {
|
||||
method = QHttpSocket::GET;
|
||||
method = HttpSocket::GET;
|
||||
} else if (parts[0] == "HEAD") {
|
||||
method = QHttpSocket::HEAD;
|
||||
method = HttpSocket::HEAD;
|
||||
} else if (parts[0] == "POST") {
|
||||
method = QHttpSocket::POST;
|
||||
method = HttpSocket::POST;
|
||||
} else if (parts[0] == "PUT") {
|
||||
method = QHttpSocket::PUT;
|
||||
method = HttpSocket::PUT;
|
||||
} else if (parts[0] == "DELETE") {
|
||||
method = QHttpSocket::DELETE;
|
||||
method = HttpSocket::DELETE;
|
||||
} else if (parts[0] == "TRACE") {
|
||||
method = QHttpSocket::TRACE;
|
||||
method = HttpSocket::TRACE;
|
||||
} else if (parts[0] == "CONNECT") {
|
||||
method = QHttpSocket::CONNECT;
|
||||
method = HttpSocket::CONNECT;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
@@ -132,7 +132,7 @@ bool QHttpParser::parseRequestHeaders(const QByteArray &data, QHttpSocket::Metho
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QHttpParser::parseResponseHeaders(const QByteArray &data, int &statusCode, QByteArray &statusReason, QHttpSocket::HeaderMap &headers)
|
||||
bool HttpParser::parseResponseHeaders(const QByteArray &data, int &statusCode, QByteArray &statusReason, HttpSocket::HeaderMap &headers)
|
||||
{
|
||||
QList<QByteArray> parts;
|
||||
if (!parseHeaders(data, parts, headers)) {
|
||||
|
||||
@@ -26,21 +26,21 @@
|
||||
|
||||
#include "qhttprange_p.h"
|
||||
|
||||
QHttpRangePrivate::QHttpRangePrivate(QHttpRange *range)
|
||||
HttpRangePrivate::HttpRangePrivate(HttpRange *range)
|
||||
: q(range)
|
||||
{
|
||||
}
|
||||
|
||||
QHttpRange::QHttpRange()
|
||||
: d(new QHttpRangePrivate(this))
|
||||
HttpRange::HttpRange()
|
||||
: d(new HttpRangePrivate(this))
|
||||
{
|
||||
d->from = 1;
|
||||
d->to = 0;
|
||||
d->dataSize = -1;
|
||||
}
|
||||
|
||||
QHttpRange::QHttpRange(const QString &range, qint64 dataSize)
|
||||
: d(new QHttpRangePrivate(this))
|
||||
HttpRange::HttpRange(const QString &range, qint64 dataSize)
|
||||
: d(new HttpRangePrivate(this))
|
||||
{
|
||||
QRegExp regExp("^(\\d*)-(\\d*)$");
|
||||
|
||||
@@ -101,28 +101,28 @@ QHttpRange::QHttpRange(const QString &range, qint64 dataSize)
|
||||
d->dataSize = dataSize;
|
||||
}
|
||||
|
||||
QHttpRange::QHttpRange(qint64 from, qint64 to, qint64 dataSize)
|
||||
: d(new QHttpRangePrivate(this))
|
||||
HttpRange::HttpRange(qint64 from, qint64 to, qint64 dataSize)
|
||||
: d(new HttpRangePrivate(this))
|
||||
{
|
||||
d->from = from;
|
||||
d->to = to < 0 ? -1 : to;
|
||||
d->dataSize = dataSize < 0 ? -1 : dataSize;
|
||||
}
|
||||
|
||||
QHttpRange::QHttpRange(const QHttpRange &other, qint64 dataSize)
|
||||
: d(new QHttpRangePrivate(this))
|
||||
HttpRange::HttpRange(const HttpRange &other, qint64 dataSize)
|
||||
: d(new HttpRangePrivate(this))
|
||||
{
|
||||
d->from = other.d->from;
|
||||
d->to = other.d->to;
|
||||
d->dataSize = dataSize;
|
||||
}
|
||||
|
||||
QHttpRange::~QHttpRange()
|
||||
HttpRange::~HttpRange()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
QHttpRange& QHttpRange::operator=(const QHttpRange &other)
|
||||
HttpRange& HttpRange::operator=(const HttpRange &other)
|
||||
{
|
||||
if (&other != this) {
|
||||
d->from = other.d->from;
|
||||
@@ -133,7 +133,7 @@ QHttpRange& QHttpRange::operator=(const QHttpRange &other)
|
||||
return *this;
|
||||
}
|
||||
|
||||
qint64 QHttpRange::from() const
|
||||
qint64 HttpRange::from() const
|
||||
{
|
||||
// Last N bytes requested
|
||||
if (d->from < 0 && d->dataSize != -1) {
|
||||
@@ -153,7 +153,7 @@ qint64 QHttpRange::from() const
|
||||
return d->from;
|
||||
}
|
||||
|
||||
qint64 QHttpRange::to() const
|
||||
qint64 HttpRange::to() const
|
||||
{
|
||||
// Last N bytes requested
|
||||
if (d->from < 0 && d->dataSize != -1) {
|
||||
@@ -178,7 +178,7 @@ qint64 QHttpRange::to() const
|
||||
return d->to;
|
||||
}
|
||||
|
||||
qint64 QHttpRange::length() const
|
||||
qint64 HttpRange::length() const
|
||||
{
|
||||
if (!isValid()) {
|
||||
return -1;
|
||||
@@ -202,12 +202,12 @@ qint64 QHttpRange::length() const
|
||||
return -1;
|
||||
}
|
||||
|
||||
qint64 QHttpRange::dataSize() const
|
||||
qint64 HttpRange::dataSize() const
|
||||
{
|
||||
return d->dataSize;
|
||||
}
|
||||
|
||||
bool QHttpRange::isValid() const
|
||||
bool HttpRange::isValid() const
|
||||
{
|
||||
// Valid cases:
|
||||
// 1. "-500/1000" => from: -500, to: -1; dataSize: 1000
|
||||
@@ -253,7 +253,7 @@ bool QHttpRange::isValid() const
|
||||
return false;
|
||||
}
|
||||
|
||||
QString QHttpRange::contentRange() const
|
||||
QString HttpRange::contentRange() const
|
||||
{
|
||||
QString fromStr, toStr, sizeStr = "*";
|
||||
|
||||
|
||||
@@ -25,11 +25,11 @@
|
||||
|
||||
#include <qhttpengine/qhttprange.h>
|
||||
|
||||
class QHttpRangePrivate
|
||||
class HttpRangePrivate
|
||||
{
|
||||
public:
|
||||
|
||||
explicit QHttpRangePrivate(QHttpRange *range);
|
||||
explicit HttpRangePrivate(HttpRange *range);
|
||||
|
||||
qint64 from;
|
||||
qint64 to;
|
||||
@@ -37,7 +37,7 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
QHttpRange *const q;
|
||||
HttpRange *const q;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QHTTPRANGEPRIVATE_H
|
||||
|
||||
@@ -29,56 +29,56 @@
|
||||
|
||||
#include "qhttpserver_p.h"
|
||||
|
||||
QHttpServerPrivate::QHttpServerPrivate(QHttpServer *httpServer)
|
||||
HttpServerPrivate::HttpServerPrivate(HttpServer *httpServer)
|
||||
: QObject(httpServer),
|
||||
q(httpServer),
|
||||
handler(0)
|
||||
{
|
||||
}
|
||||
|
||||
void QHttpServerPrivate::process(QTcpSocket *socket)
|
||||
void HttpServerPrivate::process(QTcpSocket *socket)
|
||||
{
|
||||
QHttpSocket *httpSocket = new QHttpSocket(socket, this);
|
||||
HttpSocket *httpSocket = new HttpSocket(socket, this);
|
||||
|
||||
// Wait until the socket finishes reading the HTTP headers before routing
|
||||
connect(httpSocket, &QHttpSocket::headersParsed, [this, httpSocket]() {
|
||||
connect(httpSocket, &HttpSocket::headersParsed, [this, httpSocket]() {
|
||||
if (handler) {
|
||||
handler->route(httpSocket, QString(httpSocket->path().mid(1)));
|
||||
} else {
|
||||
httpSocket->writeError(QHttpSocket::InternalServerError);
|
||||
httpSocket->writeError(HttpSocket::InternalServerError);
|
||||
}
|
||||
});
|
||||
|
||||
// Destroy the socket once the client is disconnected
|
||||
connect(socket, &QTcpSocket::disconnected, httpSocket, &QHttpSocket::deleteLater);
|
||||
connect(socket, &QTcpSocket::disconnected, httpSocket, &HttpSocket::deleteLater);
|
||||
}
|
||||
|
||||
QHttpServer::QHttpServer(QObject *parent)
|
||||
HttpServer::HttpServer(QObject *parent)
|
||||
: QTcpServer(parent),
|
||||
d(new QHttpServerPrivate(this))
|
||||
d(new HttpServerPrivate(this))
|
||||
{
|
||||
}
|
||||
|
||||
QHttpServer::QHttpServer(QHttpHandler *handler, QObject *parent)
|
||||
HttpServer::HttpServer(HttpHandler *handler, QObject *parent)
|
||||
: QTcpServer(parent),
|
||||
d(new QHttpServerPrivate(this))
|
||||
d(new HttpServerPrivate(this))
|
||||
{
|
||||
setHandler(handler);
|
||||
}
|
||||
|
||||
void QHttpServer::setHandler(QHttpHandler *handler)
|
||||
void HttpServer::setHandler(HttpHandler *handler)
|
||||
{
|
||||
d->handler = handler;
|
||||
}
|
||||
|
||||
#if !defined(QT_NO_SSL)
|
||||
void QHttpServer::setSslConfiguration(const QSslConfiguration &configuration)
|
||||
void HttpServer::setSslConfiguration(const QSslConfiguration &configuration)
|
||||
{
|
||||
d->configuration = configuration;
|
||||
}
|
||||
#endif
|
||||
|
||||
void QHttpServer::incomingConnection(qintptr socketDescriptor)
|
||||
void HttpServer::incomingConnection(qintptr socketDescriptor)
|
||||
{
|
||||
#if !defined(QT_NO_SSL)
|
||||
if (!d->configuration.isNull()) {
|
||||
|
||||
@@ -32,19 +32,19 @@
|
||||
|
||||
#include <qhttpengine/qhttpserver.h>
|
||||
|
||||
class QHttpHandler;
|
||||
class HttpHandler;
|
||||
|
||||
class QHttpServerPrivate : public QObject
|
||||
class HttpServerPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
explicit QHttpServerPrivate(QHttpServer *httpServer);
|
||||
explicit HttpServerPrivate(HttpServer *httpServer);
|
||||
|
||||
void process(QTcpSocket *socket);
|
||||
|
||||
QHttpHandler *handler;
|
||||
HttpHandler*handler;
|
||||
|
||||
#if !defined(QT_NO_SSL)
|
||||
QSslConfiguration configuration;
|
||||
@@ -52,7 +52,7 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
QHttpServer *const q;
|
||||
HttpServer*const q;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QHTTPSERVERPRIVATE_H
|
||||
|
||||
@@ -51,7 +51,7 @@ const QString ErrorTemplate =
|
||||
"</body>"
|
||||
"</html>";
|
||||
|
||||
QHttpSocketPrivate::QHttpSocketPrivate(QHttpSocket *httpSocket, QTcpSocket *tcpSocket)
|
||||
HttpSocketPrivate::HttpSocketPrivate(HttpSocket *httpSocket, QTcpSocket *tcpSocket)
|
||||
: QObject(httpSocket),
|
||||
q(httpSocket),
|
||||
socket(tcpSocket),
|
||||
@@ -64,38 +64,38 @@ QHttpSocketPrivate::QHttpSocketPrivate(QHttpSocket *httpSocket, QTcpSocket *tcpS
|
||||
{
|
||||
socket->setParent(this);
|
||||
|
||||
connect(socket, &QTcpSocket::readyRead, this, &QHttpSocketPrivate::onReadyRead);
|
||||
connect(socket, &QTcpSocket::bytesWritten, this, &QHttpSocketPrivate::onBytesWritten);
|
||||
connect(socket, &QTcpSocket::readChannelFinished, q, &QHttpSocket::readChannelFinished);
|
||||
connect(socket, &QTcpSocket::readyRead, this, &HttpSocketPrivate::onReadyRead);
|
||||
connect(socket, &QTcpSocket::bytesWritten, this, &HttpSocketPrivate::onBytesWritten);
|
||||
connect(socket, &QTcpSocket::readChannelFinished, q, &HttpSocket::readChannelFinished);
|
||||
|
||||
// Process anything already received by the socket
|
||||
onReadyRead();
|
||||
}
|
||||
|
||||
QByteArray QHttpSocketPrivate::statusReason(int statusCode) const
|
||||
QByteArray HttpSocketPrivate::statusReason(int statusCode) const
|
||||
{
|
||||
switch (statusCode) {
|
||||
case QHttpSocket::OK: return "OK";
|
||||
case QHttpSocket::Created: return "CREATED";
|
||||
case QHttpSocket::Accepted: return "ACCEPTED";
|
||||
case QHttpSocket::PartialContent: return "PARTIAL CONTENT";
|
||||
case QHttpSocket::MovedPermanently: return "MOVED PERMANENTLY";
|
||||
case QHttpSocket::Found: return "FOUND";
|
||||
case QHttpSocket::BadRequest: return "BAD REQUEST";
|
||||
case QHttpSocket::Unauthorized: return "UNAUTHORIZED";
|
||||
case QHttpSocket::Forbidden: return "FORBIDDEN";
|
||||
case QHttpSocket::NotFound: return "NOT FOUND";
|
||||
case QHttpSocket::MethodNotAllowed: return "METHOD NOT ALLOWED";
|
||||
case QHttpSocket::Conflict: return "CONFLICT";
|
||||
case QHttpSocket::BadGateway: return "BAD GATEWAY";
|
||||
case QHttpSocket::ServiceUnavailable: return "SERVICE UNAVAILABLE";
|
||||
case QHttpSocket::InternalServerError: return "INTERNAL SERVER ERROR";
|
||||
case QHttpSocket::HttpVersionNotSupported: return "HTTP VERSION NOT SUPPORTED";
|
||||
case HttpSocket::OK: return "OK";
|
||||
case HttpSocket::Created: return "CREATED";
|
||||
case HttpSocket::Accepted: return "ACCEPTED";
|
||||
case HttpSocket::PartialContent: return "PARTIAL CONTENT";
|
||||
case HttpSocket::MovedPermanently: return "MOVED PERMANENTLY";
|
||||
case HttpSocket::Found: return "FOUND";
|
||||
case HttpSocket::BadRequest: return "BAD REQUEST";
|
||||
case HttpSocket::Unauthorized: return "UNAUTHORIZED";
|
||||
case HttpSocket::Forbidden: return "FORBIDDEN";
|
||||
case HttpSocket::NotFound: return "NOT FOUND";
|
||||
case HttpSocket::MethodNotAllowed: return "METHOD NOT ALLOWED";
|
||||
case HttpSocket::Conflict: return "CONFLICT";
|
||||
case HttpSocket::BadGateway: return "BAD GATEWAY";
|
||||
case HttpSocket::ServiceUnavailable: return "SERVICE UNAVAILABLE";
|
||||
case HttpSocket::InternalServerError: return "INTERNAL SERVER ERROR";
|
||||
case HttpSocket::HttpVersionNotSupported: return "HTTP VERSION NOT SUPPORTED";
|
||||
default: return "UNKNOWN ERROR";
|
||||
}
|
||||
}
|
||||
|
||||
void QHttpSocketPrivate::onReadyRead()
|
||||
void HttpSocketPrivate::onReadyRead()
|
||||
{
|
||||
// Append all of the new data to the read buffer
|
||||
readBuffer.append(socket->readAll());
|
||||
@@ -116,7 +116,7 @@ void QHttpSocketPrivate::onReadyRead()
|
||||
}
|
||||
}
|
||||
|
||||
void QHttpSocketPrivate::onBytesWritten(qint64 bytes)
|
||||
void HttpSocketPrivate::onBytesWritten(qint64 bytes)
|
||||
{
|
||||
// Check to see if all of the response header was written
|
||||
if (writeState == WriteHeaders) {
|
||||
@@ -134,7 +134,7 @@ void QHttpSocketPrivate::onBytesWritten(qint64 bytes)
|
||||
}
|
||||
}
|
||||
|
||||
bool QHttpSocketPrivate::readHeaders()
|
||||
bool HttpSocketPrivate::readHeaders()
|
||||
{
|
||||
// Check for the double CRLF that signals the end of the headers and
|
||||
// if it is not found, wait until the next time readyRead is emitted
|
||||
@@ -145,9 +145,9 @@ bool QHttpSocketPrivate::readHeaders()
|
||||
|
||||
// Attempt to parse the headers and if a problem is encountered, abort
|
||||
// the connection (so that no more data is read or written) and return
|
||||
if (!QHttpParser::parseRequestHeaders(readBuffer.left(index), requestMethod, requestRawPath, requestHeaders) ||
|
||||
!QHttpParser::parsePath(requestRawPath, requestPath, requestQueryString)) {
|
||||
q->writeError(QHttpSocket::BadRequest);
|
||||
if (!HttpParser::parseRequestHeaders(readBuffer.left(index), requestMethod, requestRawPath, requestHeaders) ||
|
||||
!HttpParser::parsePath(requestRawPath, requestPath, requestQueryString)) {
|
||||
q->writeError(HttpSocket::BadRequest);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ bool QHttpSocketPrivate::readHeaders()
|
||||
return true;
|
||||
}
|
||||
|
||||
void QHttpSocketPrivate::readData()
|
||||
void HttpSocketPrivate::readData()
|
||||
{
|
||||
// Emit the readyRead() signal if any data is available in the buffer
|
||||
if (readBuffer.size()) {
|
||||
@@ -184,99 +184,99 @@ void QHttpSocketPrivate::readData()
|
||||
}
|
||||
}
|
||||
|
||||
QHttpSocket::QHttpSocket(QTcpSocket *socket, QObject *parent)
|
||||
HttpSocket::HttpSocket(QTcpSocket *socket, QObject *parent)
|
||||
: QIODevice(parent),
|
||||
d(new QHttpSocketPrivate(this, socket))
|
||||
d(new HttpSocketPrivate(this, socket))
|
||||
{
|
||||
// The device is initially open for both reading and writing
|
||||
setOpenMode(QIODevice::ReadWrite);
|
||||
}
|
||||
|
||||
qint64 QHttpSocket::bytesAvailable() const
|
||||
qint64 HttpSocket::bytesAvailable() const
|
||||
{
|
||||
if (d->readState > QHttpSocketPrivate::ReadHeaders) {
|
||||
if (d->readState > HttpSocketPrivate::ReadHeaders) {
|
||||
return d->readBuffer.size() + QIODevice::bytesAvailable();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool QHttpSocket::isSequential() const
|
||||
bool HttpSocket::isSequential() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void QHttpSocket::close()
|
||||
void HttpSocket::close()
|
||||
{
|
||||
// Invoke the parent method
|
||||
QIODevice::close();
|
||||
|
||||
d->readState = QHttpSocketPrivate::ReadFinished;
|
||||
d->writeState = QHttpSocketPrivate::WriteFinished;
|
||||
d->readState = HttpSocketPrivate::ReadFinished;
|
||||
d->writeState = HttpSocketPrivate::WriteFinished;
|
||||
|
||||
d->socket->close();
|
||||
}
|
||||
|
||||
QHostAddress QHttpSocket::peerAddress() const
|
||||
QHostAddress HttpSocket::peerAddress() const
|
||||
{
|
||||
return d->socket->peerAddress();
|
||||
}
|
||||
|
||||
bool QHttpSocket::isHeadersParsed() const
|
||||
bool HttpSocket::isHeadersParsed() const
|
||||
{
|
||||
return d->readState > QHttpSocketPrivate::ReadHeaders;
|
||||
return d->readState > HttpSocketPrivate::ReadHeaders;
|
||||
}
|
||||
|
||||
QHttpSocket::Method QHttpSocket::method() const
|
||||
HttpSocket::Method HttpSocket::method() const
|
||||
{
|
||||
return d->requestMethod;
|
||||
}
|
||||
|
||||
QByteArray QHttpSocket::rawPath() const
|
||||
QByteArray HttpSocket::rawPath() const
|
||||
{
|
||||
return d->requestRawPath;
|
||||
}
|
||||
|
||||
QString QHttpSocket::path() const
|
||||
QString HttpSocket::path() const
|
||||
{
|
||||
return d->requestPath;
|
||||
}
|
||||
|
||||
QHttpSocket::QueryStringMap QHttpSocket::queryString() const
|
||||
HttpSocket::QueryStringMap HttpSocket::queryString() const
|
||||
{
|
||||
return d->requestQueryString;
|
||||
}
|
||||
|
||||
QHttpSocket::HeaderMap QHttpSocket::headers() const
|
||||
HttpSocket::HeaderMap HttpSocket::headers() const
|
||||
{
|
||||
return d->requestHeaders;
|
||||
}
|
||||
|
||||
qint64 QHttpSocket::contentLength() const
|
||||
qint64 HttpSocket::contentLength() const
|
||||
{
|
||||
return d->requestDataTotal;
|
||||
}
|
||||
|
||||
bool QHttpSocket::readJson(QJsonDocument &document)
|
||||
bool HttpSocket::readJson(QJsonDocument &document)
|
||||
{
|
||||
QJsonParseError error;
|
||||
document = QJsonDocument::fromJson(readAll(), &error);
|
||||
|
||||
if (error.error != QJsonParseError::NoError) {
|
||||
writeError(QHttpSocket::BadRequest);
|
||||
writeError(HttpSocket::BadRequest);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void QHttpSocket::setStatusCode(int statusCode, const QByteArray &statusReason)
|
||||
void HttpSocket::setStatusCode(int statusCode, const QByteArray &statusReason)
|
||||
{
|
||||
d->responseStatusCode = statusCode;
|
||||
d->responseStatusReason = statusReason.isNull() ? d->statusReason(statusCode) : statusReason;
|
||||
}
|
||||
|
||||
void QHttpSocket::setHeader(const QByteArray &name, const QByteArray &value, bool replace)
|
||||
void HttpSocket::setHeader(const QByteArray &name, const QByteArray &value, bool replace)
|
||||
{
|
||||
if (replace || d->responseHeaders.count(name)) {
|
||||
d->responseHeaders.replace(name, value);
|
||||
@@ -285,12 +285,12 @@ void QHttpSocket::setHeader(const QByteArray &name, const QByteArray &value, boo
|
||||
}
|
||||
}
|
||||
|
||||
void QHttpSocket::setHeaders(const HeaderMap &headers)
|
||||
void HttpSocket::setHeaders(const HeaderMap &headers)
|
||||
{
|
||||
d->responseHeaders = headers;
|
||||
}
|
||||
|
||||
void QHttpSocket::writeHeaders()
|
||||
void HttpSocket::writeHeaders()
|
||||
{
|
||||
// Use a QByteArray for building the header so that we can later determine
|
||||
// exactly how many bytes were written
|
||||
@@ -312,14 +312,14 @@ void QHttpSocket::writeHeaders()
|
||||
// Append an extra CRLF
|
||||
header.append("\r\n");
|
||||
|
||||
d->writeState = QHttpSocketPrivate::WriteHeaders;
|
||||
d->writeState = HttpSocketPrivate::WriteHeaders;
|
||||
d->responseHeaderRemaining = header.length();
|
||||
|
||||
// Write the header
|
||||
d->socket->write(header);
|
||||
}
|
||||
|
||||
void QHttpSocket::writeRedirect(const QByteArray &path, bool permanent)
|
||||
void HttpSocket::writeRedirect(const QByteArray &path, bool permanent)
|
||||
{
|
||||
setStatusCode(permanent ? MovedPermanently : Found);
|
||||
setHeader("Location", path);
|
||||
@@ -327,7 +327,7 @@ void QHttpSocket::writeRedirect(const QByteArray &path, bool permanent)
|
||||
close();
|
||||
}
|
||||
|
||||
void QHttpSocket::writeError(int statusCode, const QByteArray &statusReason)
|
||||
void HttpSocket::writeError(int statusCode, const QByteArray &statusReason)
|
||||
{
|
||||
setStatusCode(statusCode, statusReason);
|
||||
|
||||
@@ -346,7 +346,7 @@ void QHttpSocket::writeError(int statusCode, const QByteArray &statusReason)
|
||||
close();
|
||||
}
|
||||
|
||||
void QHttpSocket::writeJson(const QJsonDocument &document, int statusCode)
|
||||
void HttpSocket::writeJson(const QJsonDocument &document, int statusCode)
|
||||
{
|
||||
QByteArray data = document.toJson();
|
||||
setStatusCode(statusCode);
|
||||
@@ -356,10 +356,10 @@ void QHttpSocket::writeJson(const QJsonDocument &document, int statusCode)
|
||||
close();
|
||||
}
|
||||
|
||||
qint64 QHttpSocket::readData(char *data, qint64 maxlen)
|
||||
qint64 HttpSocket::readData(char *data, qint64 maxlen)
|
||||
{
|
||||
// Ensure the connection is in the correct state for reading data
|
||||
if (d->readState == QHttpSocketPrivate::ReadHeaders) {
|
||||
if (d->readState == HttpSocketPrivate::ReadHeaders) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -374,11 +374,11 @@ qint64 QHttpSocket::readData(char *data, qint64 maxlen)
|
||||
return size;
|
||||
}
|
||||
|
||||
qint64 QHttpSocket::writeData(const char *data, qint64 len)
|
||||
qint64 HttpSocket::writeData(const char *data, qint64 len)
|
||||
{
|
||||
// If the response headers have not yet been written, they must
|
||||
// immediately be written before the data can be
|
||||
if (d->writeState == QHttpSocketPrivate::WriteNone) {
|
||||
if (d->writeState == HttpSocketPrivate::WriteNone) {
|
||||
writeHeaders();
|
||||
}
|
||||
|
||||
|
||||
@@ -27,13 +27,13 @@
|
||||
|
||||
class QTcpSocket;
|
||||
|
||||
class QHttpSocketPrivate : public QObject
|
||||
class HttpSocketPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
QHttpSocketPrivate(QHttpSocket *httpSocket, QTcpSocket *tcpSocket);
|
||||
HttpSocketPrivate(HttpSocket *httpSocket, QTcpSocket *tcpSocket);
|
||||
|
||||
QByteArray statusReason(int statusCode) const;
|
||||
|
||||
@@ -46,11 +46,11 @@ public:
|
||||
ReadFinished
|
||||
} readState;
|
||||
|
||||
QHttpSocket::Method requestMethod;
|
||||
HttpSocket::Method requestMethod;
|
||||
QByteArray requestRawPath;
|
||||
QString requestPath;
|
||||
QHttpSocket::QueryStringMap requestQueryString;
|
||||
QHttpSocket::HeaderMap requestHeaders;
|
||||
HttpSocket::QueryStringMap requestQueryString;
|
||||
HttpSocket::HeaderMap requestHeaders;
|
||||
qint64 requestDataRead;
|
||||
qint64 requestDataTotal;
|
||||
|
||||
@@ -63,7 +63,7 @@ public:
|
||||
|
||||
int responseStatusCode;
|
||||
QByteArray responseStatusReason;
|
||||
QHttpSocket::HeaderMap responseHeaders;
|
||||
HttpSocket::HeaderMap responseHeaders;
|
||||
qint64 responseHeaderRemaining;
|
||||
|
||||
private Q_SLOTS:
|
||||
@@ -76,7 +76,7 @@ private:
|
||||
bool readHeaders();
|
||||
void readData();
|
||||
|
||||
QHttpSocket *const q;
|
||||
HttpSocket*const q;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QHTTPSOCKETPRIVATE_H
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
|
||||
#include "qlocalauth_p.h"
|
||||
|
||||
QLocalAuthPrivate::QLocalAuthPrivate(QObject *parent)
|
||||
LocalAuthPrivate::LocalAuthPrivate(QObject *parent)
|
||||
: QObject(parent),
|
||||
tokenHeader("X-Auth-Token"),
|
||||
token(QUuid::createUuid().toString())
|
||||
@@ -37,7 +37,7 @@ QLocalAuthPrivate::QLocalAuthPrivate(QObject *parent)
|
||||
updateFile();
|
||||
}
|
||||
|
||||
void QLocalAuthPrivate::updateFile()
|
||||
void LocalAuthPrivate::updateFile()
|
||||
{
|
||||
if (file.open()) {
|
||||
file.write(QJsonDocument(QJsonObject::fromVariantMap(data)).toJson());
|
||||
@@ -45,38 +45,38 @@ void QLocalAuthPrivate::updateFile()
|
||||
}
|
||||
}
|
||||
|
||||
QLocalAuth::QLocalAuth(QObject *parent)
|
||||
: QHttpMiddleware(parent),
|
||||
d(new QLocalAuthPrivate(this))
|
||||
LocalAuth::LocalAuth(QObject *parent)
|
||||
: HttpMiddleware(parent),
|
||||
d(new LocalAuthPrivate(this))
|
||||
{
|
||||
}
|
||||
|
||||
bool QLocalAuth::exists() const
|
||||
bool LocalAuth::exists() const
|
||||
{
|
||||
return d->file.exists();
|
||||
}
|
||||
|
||||
QString QLocalAuth::filename() const
|
||||
QString LocalAuth::filename() const
|
||||
{
|
||||
return d->file.fileName();
|
||||
}
|
||||
|
||||
void QLocalAuth::setData(const QVariantMap &data)
|
||||
void LocalAuth::setData(const QVariantMap &data)
|
||||
{
|
||||
d->data = data;
|
||||
d->data.insert("token", d->token);
|
||||
d->updateFile();
|
||||
}
|
||||
|
||||
void QLocalAuth::setHeaderName(const QByteArray &name)
|
||||
void LocalAuth::setHeaderName(const QByteArray &name)
|
||||
{
|
||||
d->tokenHeader = name;
|
||||
}
|
||||
|
||||
bool QLocalAuth::process(QHttpSocket *socket)
|
||||
bool LocalAuth::process(HttpSocket *socket)
|
||||
{
|
||||
if (socket->headers().value(d->tokenHeader) != d->token) {
|
||||
socket->writeError(QHttpSocket::Forbidden);
|
||||
socket->writeError(HttpSocket::Forbidden);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,17 +28,17 @@
|
||||
|
||||
#include <qhttpengine/qlocalfile.h>
|
||||
|
||||
class QLocalAuthPrivate : public QObject
|
||||
class LocalAuthPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
explicit QLocalAuthPrivate(QObject *parent);
|
||||
explicit LocalAuthPrivate(QObject *parent);
|
||||
|
||||
void updateFile();
|
||||
|
||||
QLocalFile file;
|
||||
LocalFile file;
|
||||
QVariantMap data;
|
||||
QByteArray tokenHeader;
|
||||
QString token;
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
|
||||
#include "qlocalfile_p.h"
|
||||
|
||||
QLocalFilePrivate::QLocalFilePrivate(QLocalFile *localFile)
|
||||
LocalFilePrivate::LocalFilePrivate(LocalFile *localFile)
|
||||
: QObject(localFile),
|
||||
q(localFile)
|
||||
{
|
||||
@@ -43,7 +43,7 @@ QLocalFilePrivate::QLocalFilePrivate(QLocalFile *localFile)
|
||||
q->setFileName(QDir::home().absoluteFilePath("." + QCoreApplication::applicationName()));
|
||||
}
|
||||
|
||||
bool QLocalFilePrivate::setPermission()
|
||||
bool LocalFilePrivate::setPermission()
|
||||
{
|
||||
#if defined(Q_OS_UNIX)
|
||||
return chmod(q->fileName().toUtf8().constData(), S_IRUSR | S_IWUSR) == 0;
|
||||
@@ -87,7 +87,7 @@ bool QLocalFilePrivate::setPermission()
|
||||
#endif
|
||||
}
|
||||
|
||||
bool QLocalFilePrivate::setHidden()
|
||||
bool LocalFilePrivate::setHidden()
|
||||
{
|
||||
#if defined(Q_OS_UNIX)
|
||||
// On Unix, anything beginning with a "." is hidden
|
||||
@@ -100,13 +100,13 @@ bool QLocalFilePrivate::setHidden()
|
||||
#endif
|
||||
}
|
||||
|
||||
QLocalFile::QLocalFile(QObject *parent)
|
||||
LocalFile::LocalFile(QObject *parent)
|
||||
: QFile(parent),
|
||||
d(new QLocalFilePrivate(this))
|
||||
d(new LocalFilePrivate(this))
|
||||
{
|
||||
}
|
||||
|
||||
bool QLocalFile::open()
|
||||
bool LocalFile::open()
|
||||
{
|
||||
return QFile::open(QIODevice::WriteOnly) && d->setPermission() && d->setHidden();
|
||||
}
|
||||
|
||||
@@ -25,22 +25,22 @@
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class QLocalFile;
|
||||
class LocalFile;
|
||||
|
||||
class QLocalFilePrivate : public QObject
|
||||
class LocalFilePrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
explicit QLocalFilePrivate(QLocalFile *localFile);
|
||||
explicit LocalFilePrivate(LocalFile *localFile);
|
||||
|
||||
bool setPermission();
|
||||
bool setHidden();
|
||||
|
||||
private:
|
||||
|
||||
QLocalFile *const q;
|
||||
LocalFile*const q;
|
||||
};
|
||||
|
||||
#endif // QHTTPENGINE_QLOCALFILEPRIVATE_H
|
||||
|
||||
@@ -35,12 +35,12 @@ QObjectHandlerPrivate::QObjectHandlerPrivate(QObjectHandler *handler)
|
||||
}
|
||||
|
||||
QObjectHandler::QObjectHandler(QObject *parent)
|
||||
: QHttpHandler(parent),
|
||||
: HttpHandler(parent),
|
||||
d(new QObjectHandlerPrivate(this))
|
||||
{
|
||||
}
|
||||
|
||||
void QObjectHandlerPrivate::invokeSlot(QHttpSocket *socket, Method m)
|
||||
void QObjectHandlerPrivate::invokeSlot(HttpSocket *socket, Method m)
|
||||
{
|
||||
// Invoke the slot
|
||||
if (m.oldSlot) {
|
||||
@@ -48,7 +48,7 @@ void QObjectHandlerPrivate::invokeSlot(QHttpSocket *socket, Method m)
|
||||
// Obtain the slot index
|
||||
int index = m.receiver->metaObject()->indexOfSlot(m.slot.method + 1);
|
||||
if (index == -1) {
|
||||
socket->writeError(QHttpSocket::InternalServerError);
|
||||
socket->writeError(HttpSocket::InternalServerError);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -57,14 +57,14 @@ void QObjectHandlerPrivate::invokeSlot(QHttpSocket *socket, Method m)
|
||||
// Ensure the parameter is correct
|
||||
QList<QByteArray> params = method.parameterTypes();
|
||||
if (params.count() != 1 || params.at(0) != "QHttpSocket*") {
|
||||
socket->writeError(QHttpSocket::InternalServerError);
|
||||
socket->writeError(HttpSocket::InternalServerError);
|
||||
return;
|
||||
}
|
||||
|
||||
// Invoke the method
|
||||
if (!m.receiver->metaObject()->method(index).invoke(
|
||||
m.receiver, Q_ARG(QHttpSocket*, socket))) {
|
||||
socket->writeError(QHttpSocket::InternalServerError);
|
||||
m.receiver, Q_ARG(HttpSocket*, socket))) {
|
||||
socket->writeError(HttpSocket::InternalServerError);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
@@ -76,11 +76,11 @@ void QObjectHandlerPrivate::invokeSlot(QHttpSocket *socket, Method m)
|
||||
}
|
||||
}
|
||||
|
||||
void QObjectHandler::process(QHttpSocket *socket, const QString &path)
|
||||
void QObjectHandler::process(HttpSocket *socket, const QString &path)
|
||||
{
|
||||
// Ensure the method has been registered
|
||||
if (!d->map.contains(path)) {
|
||||
socket->writeError(QHttpSocket::NotFound);
|
||||
socket->writeError(HttpSocket::NotFound);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ void QObjectHandler::process(QHttpSocket *socket, const QString &path)
|
||||
d->invokeSlot(socket, m);
|
||||
socket->close();
|
||||
} else {
|
||||
connect(socket, &QHttpSocket::readChannelFinished, [this, socket, m]() {
|
||||
connect(socket, &HttpSocket::readChannelFinished, [this, socket, m]() {
|
||||
d->invokeSlot(socket, m);
|
||||
socket->close();
|
||||
});
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
#include <QMap>
|
||||
#include <QObject>
|
||||
|
||||
class QHttpSocket;
|
||||
class HttpSocket;
|
||||
class QObjectHandler;
|
||||
|
||||
class QObjectHandlerPrivate : public QObject
|
||||
@@ -60,7 +60,7 @@ public:
|
||||
bool readAll;
|
||||
};
|
||||
|
||||
void invokeSlot(QHttpSocket *socket, Method m);
|
||||
void invokeSlot(HttpSocket*socket, Method m);
|
||||
|
||||
QMap<QString, Method> map;
|
||||
|
||||
|
||||
@@ -25,20 +25,20 @@
|
||||
#include "qproxyhandler_p.h"
|
||||
#include "qproxysocket.h"
|
||||
|
||||
QProxyHandlerPrivate::QProxyHandlerPrivate(QObject *parent, const QHostAddress &address, quint16 port)
|
||||
ProxyHandlerPrivate::ProxyHandlerPrivate(QObject *parent, const QHostAddress &address, quint16 port)
|
||||
: QObject(parent),
|
||||
address(address),
|
||||
port(port)
|
||||
{
|
||||
}
|
||||
|
||||
QProxyHandler::QProxyHandler(const QHostAddress &address, quint16 port, QObject *parent)
|
||||
: QHttpHandler(parent),
|
||||
d(new QProxyHandlerPrivate(this, address, port))
|
||||
ProxyHandler::ProxyHandler(const QHostAddress &address, quint16 port, QObject *parent)
|
||||
: HttpHandler(parent),
|
||||
d(new ProxyHandlerPrivate(this, address, port))
|
||||
{
|
||||
}
|
||||
|
||||
void QProxyHandler::process(QHttpSocket *socket, const QString &path)
|
||||
void ProxyHandler::process(HttpSocket *socket, const QString &path)
|
||||
{
|
||||
// Parent the socket to the proxy
|
||||
socket->setParent(this);
|
||||
|
||||
@@ -28,13 +28,13 @@
|
||||
|
||||
#include <qhttpengine/qhttpsocket.h>
|
||||
|
||||
class QProxyHandlerPrivate : public QObject
|
||||
class ProxyHandlerPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
QProxyHandlerPrivate(QObject *parent, const QHostAddress &address, quint16 port);
|
||||
ProxyHandlerPrivate(QObject *parent, const QHostAddress &address, quint16 port);
|
||||
|
||||
QHostAddress address;
|
||||
quint16 port;
|
||||
|
||||
@@ -24,13 +24,13 @@
|
||||
|
||||
#include "qproxysocket.h"
|
||||
|
||||
QProxySocket::QProxySocket(QHttpSocket *socket, const QString &path, const QHostAddress &address, quint16 port)
|
||||
QProxySocket::QProxySocket(HttpSocket *socket, const QString &path, const QHostAddress &address, quint16 port)
|
||||
: QObject(socket),
|
||||
mDownstreamSocket(socket),
|
||||
mPath(path),
|
||||
mHeadersParsed(false)
|
||||
{
|
||||
connect(mDownstreamSocket, &QHttpSocket::readyRead, this, &QProxySocket::onDownstreamReadyRead);
|
||||
connect(mDownstreamSocket, &HttpSocket::readyRead, this, &QProxySocket::onDownstreamReadyRead);
|
||||
|
||||
connect(&mUpstreamSocket, &QTcpSocket::connected, this, &QProxySocket::onUpstreamConnected);
|
||||
connect(&mUpstreamSocket, &QTcpSocket::readyRead, this, &QProxySocket::onUpstreamReadyRead);
|
||||
@@ -60,7 +60,7 @@ void QProxySocket::onUpstreamConnected()
|
||||
);
|
||||
|
||||
// Use the existing headers but insert proxy-related ones
|
||||
QHttpSocket::HeaderMap headers = mDownstreamSocket->headers();
|
||||
HttpSocket::HeaderMap headers = mDownstreamSocket->headers();
|
||||
QByteArray peerIP = mDownstreamSocket->peerAddress().toString().toUtf8();
|
||||
QByteArray origFwd = headers.value("X-Forwarded-For");
|
||||
if (origFwd.isNull()) {
|
||||
@@ -94,9 +94,9 @@ void QProxySocket::onUpstreamReadyRead()
|
||||
// Parse the headers
|
||||
int statusCode;
|
||||
QByteArray statusReason;
|
||||
QHttpSocket::HeaderMap headers;
|
||||
if (!QHttpParser::parseResponseHeaders(mBuffer.left(index), statusCode, statusReason, headers)) {
|
||||
mDownstreamSocket->writeError(QHttpSocket::BadGateway);
|
||||
HttpSocket::HeaderMap headers;
|
||||
if (!HttpParser::parseResponseHeaders(mBuffer.left(index), statusCode, statusReason, headers)) {
|
||||
mDownstreamSocket->writeError(HttpSocket::BadGateway);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -120,20 +120,20 @@ void QProxySocket::onUpstreamError(QAbstractSocket::SocketError socketError)
|
||||
if (mHeadersParsed) {
|
||||
mDownstreamSocket->close();
|
||||
} else {
|
||||
mDownstreamSocket->writeError(QHttpSocket::BadGateway);
|
||||
mDownstreamSocket->writeError(HttpSocket::BadGateway);
|
||||
}
|
||||
}
|
||||
|
||||
QString QProxySocket::methodToString(QHttpSocket::Method method) const
|
||||
QString QProxySocket::methodToString(HttpSocket::Method method) const
|
||||
{
|
||||
switch (method) {
|
||||
case QHttpSocket::OPTIONS: return "OPTIONS";
|
||||
case QHttpSocket::GET: return "GET";
|
||||
case QHttpSocket::HEAD: return "HEAD";
|
||||
case QHttpSocket::POST: return "POST";
|
||||
case QHttpSocket::PUT: return "PUT";
|
||||
case QHttpSocket::DELETE: return "DELETE";
|
||||
case QHttpSocket::TRACE: return "TRACE";
|
||||
case QHttpSocket::CONNECT: return "CONNECT";
|
||||
case HttpSocket::OPTIONS: return "OPTIONS";
|
||||
case HttpSocket::GET: return "GET";
|
||||
case HttpSocket::HEAD: return "HEAD";
|
||||
case HttpSocket::POST: return "POST";
|
||||
case HttpSocket::PUT: return "PUT";
|
||||
case HttpSocket::DELETE: return "DELETE";
|
||||
case HttpSocket::TRACE: return "TRACE";
|
||||
case HttpSocket::CONNECT: return "CONNECT";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ class QProxySocket : public QObject
|
||||
|
||||
public:
|
||||
|
||||
explicit QProxySocket(QHttpSocket *socket, const QString &path, const QHostAddress &address, quint16 port);
|
||||
explicit QProxySocket(HttpSocket *socket, const QString &path, const QHostAddress &address, quint16 port);
|
||||
|
||||
private Q_SLOTS:
|
||||
|
||||
@@ -50,9 +50,9 @@ private Q_SLOTS:
|
||||
|
||||
private:
|
||||
|
||||
QString methodToString(QHttpSocket::Method method) const;
|
||||
QString methodToString(HttpSocket::Method method) const;
|
||||
|
||||
QHttpSocket *mDownstreamSocket;
|
||||
HttpSocket *mDownstreamSocket;
|
||||
QTcpSocket mUpstreamSocket;
|
||||
|
||||
QString mPath;
|
||||
|
||||
@@ -72,22 +72,22 @@ void TestQFilesystemHandler::testRequests_data()
|
||||
|
||||
QTest::newRow("nonexistent resource")
|
||||
<< "nonexistent"
|
||||
<< static_cast<int>(QHttpSocket::NotFound)
|
||||
<< static_cast<int>(HttpSocket::NotFound)
|
||||
<< QByteArray();
|
||||
|
||||
QTest::newRow("outside document root")
|
||||
<< "../outside"
|
||||
<< static_cast<int>(QHttpSocket::NotFound)
|
||||
<< static_cast<int>(HttpSocket::NotFound)
|
||||
<< QByteArray();
|
||||
|
||||
QTest::newRow("inside document root")
|
||||
<< "inside"
|
||||
<< static_cast<int>(QHttpSocket::OK)
|
||||
<< static_cast<int>(HttpSocket::OK)
|
||||
<< Data;
|
||||
|
||||
QTest::newRow("directory listing")
|
||||
<< ""
|
||||
<< static_cast<int>(QHttpSocket::OK)
|
||||
<< static_cast<int>(HttpSocket::OK)
|
||||
<< QByteArray();
|
||||
}
|
||||
|
||||
@@ -97,13 +97,13 @@ void TestQFilesystemHandler::testRequests()
|
||||
QFETCH(int, statusCode);
|
||||
QFETCH(QByteArray, data);
|
||||
|
||||
QFilesystemHandler handler(QDir(dir.path()).absoluteFilePath("root"));
|
||||
FilesystemHandler handler(QDir(dir.path()).absoluteFilePath("root"));
|
||||
|
||||
QSocketPair pair;
|
||||
QTRY_VERIFY(pair.isConnected());
|
||||
|
||||
QSimpleHttpClient client(pair.client());
|
||||
QHttpSocket socket(pair.server(), &pair);
|
||||
HttpSocket socket(pair.server(), &pair);
|
||||
|
||||
handler.route(&socket, path);
|
||||
|
||||
@@ -124,37 +124,37 @@ void TestQFilesystemHandler::testRangeRequests_data()
|
||||
|
||||
QTest::newRow("full file")
|
||||
<< "inside" << ""
|
||||
<< static_cast<int>(QHttpSocket::OK)
|
||||
<< static_cast<int>(HttpSocket::OK)
|
||||
<< ""
|
||||
<< Data;
|
||||
|
||||
QTest::newRow("range 0-2")
|
||||
<< "inside" << "0-2"
|
||||
<< static_cast<int>(QHttpSocket::PartialContent)
|
||||
<< static_cast<int>(HttpSocket::PartialContent)
|
||||
<< "bytes 0-2/4"
|
||||
<< Data.mid(0, 3);
|
||||
|
||||
QTest::newRow("range 1-2")
|
||||
<< "inside" << "1-2"
|
||||
<< static_cast<int>(QHttpSocket::PartialContent)
|
||||
<< static_cast<int>(HttpSocket::PartialContent)
|
||||
<< "bytes 1-2/4"
|
||||
<< Data.mid(1, 2);
|
||||
|
||||
QTest::newRow("skip first 1 byte")
|
||||
<< "inside" << "1-"
|
||||
<< static_cast<int>(QHttpSocket::PartialContent)
|
||||
<< static_cast<int>(HttpSocket::PartialContent)
|
||||
<< "bytes 1-3/4"
|
||||
<< Data.mid(1);
|
||||
|
||||
QTest::newRow("last 2 bytes")
|
||||
<< "inside" << "-2"
|
||||
<< static_cast<int>(QHttpSocket::PartialContent)
|
||||
<< static_cast<int>(HttpSocket::PartialContent)
|
||||
<< "bytes 2-3/4"
|
||||
<< Data.mid(2);
|
||||
|
||||
QTest::newRow("bad range request")
|
||||
<< "inside" << "abcd"
|
||||
<< static_cast<int>(QHttpSocket::OK)
|
||||
<< static_cast<int>(HttpSocket::OK)
|
||||
<< ""
|
||||
<< Data;
|
||||
}
|
||||
@@ -167,16 +167,16 @@ void TestQFilesystemHandler::testRangeRequests()
|
||||
QFETCH(QString, contentRange);
|
||||
QFETCH(QByteArray, data);
|
||||
|
||||
QFilesystemHandler handler(QDir(dir.path()).absoluteFilePath("root"));
|
||||
FilesystemHandler handler(QDir(dir.path()).absoluteFilePath("root"));
|
||||
|
||||
QSocketPair pair;
|
||||
QTRY_VERIFY(pair.isConnected());
|
||||
|
||||
QSimpleHttpClient client(pair.client());
|
||||
QHttpSocket socket(pair.server(), &pair);
|
||||
HttpSocket socket(pair.server(), &pair);
|
||||
|
||||
if (!range.isEmpty()) {
|
||||
QHttpSocket::HeaderMap inHeaders;
|
||||
HttpSocket::HeaderMap inHeaders;
|
||||
inHeaders.insert("Range", QByteArray("bytes=") + range.toUtf8());
|
||||
client.sendHeaders("GET", path.toUtf8(), inHeaders);
|
||||
QTRY_VERIFY(socket.isHeadersParsed());
|
||||
|
||||
@@ -49,7 +49,7 @@ private Q_SLOTS:
|
||||
|
||||
private:
|
||||
|
||||
QHttpBasicAuth auth;
|
||||
HttpBasicAuth auth;
|
||||
};
|
||||
|
||||
void TestQHttpBasicAuth::initTestCase()
|
||||
@@ -68,19 +68,19 @@ void TestQHttpBasicAuth::testProcess_data()
|
||||
<< false
|
||||
<< QString()
|
||||
<< QString()
|
||||
<< static_cast<int>(QHttpSocket::Unauthorized);
|
||||
<< static_cast<int>(HttpSocket::Unauthorized);
|
||||
|
||||
QTest::newRow("invalid credentials")
|
||||
<< true
|
||||
<< Username
|
||||
<< QString()
|
||||
<< static_cast<int>(QHttpSocket::Unauthorized);
|
||||
<< static_cast<int>(HttpSocket::Unauthorized);
|
||||
|
||||
QTest::newRow("valid credentials")
|
||||
<< true
|
||||
<< Username
|
||||
<< Password
|
||||
<< static_cast<int>(QHttpSocket::NotFound);
|
||||
<< static_cast<int>(HttpSocket::NotFound);
|
||||
}
|
||||
|
||||
void TestQHttpBasicAuth::testProcess()
|
||||
@@ -94,9 +94,9 @@ void TestQHttpBasicAuth::testProcess()
|
||||
QTRY_VERIFY(pair.isConnected());
|
||||
|
||||
QSimpleHttpClient client(pair.client());
|
||||
QHttpSocket socket(pair.server(), &pair);
|
||||
HttpSocket socket(pair.server(), &pair);
|
||||
|
||||
QHttpSocket::HeaderMap headers;
|
||||
HttpSocket::HeaderMap headers;
|
||||
|
||||
if (header) {
|
||||
headers.insert(
|
||||
@@ -108,7 +108,7 @@ void TestQHttpBasicAuth::testProcess()
|
||||
client.sendHeaders("GET", "/", headers);
|
||||
QTRY_VERIFY(socket.isHeadersParsed());
|
||||
|
||||
QHttpHandler handler;
|
||||
HttpHandler handler;
|
||||
handler.addMiddleware(&auth);
|
||||
handler.route(&socket, "/");
|
||||
|
||||
|
||||
@@ -29,13 +29,13 @@
|
||||
#include "common/qsimplehttpclient.h"
|
||||
#include "common/qsocketpair.h"
|
||||
|
||||
class DummyHandler : public QHttpHandler
|
||||
class DummyHandler : public HttpHandler
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
virtual void process(QHttpSocket *socket, const QString &path) {
|
||||
virtual void process(HttpSocket *socket, const QString &path) {
|
||||
mPathRemainder = path;
|
||||
socket->writeHeaders();
|
||||
socket->close();
|
||||
@@ -69,20 +69,20 @@ void TestQHttpHandler::testRedirect_data()
|
||||
<< QRegExp("\\w+")
|
||||
<< QString("/two")
|
||||
<< QByteArray("one")
|
||||
<< static_cast<int>(QHttpSocket::Found)
|
||||
<< static_cast<int>(HttpSocket::Found)
|
||||
<< QByteArray("/two");
|
||||
|
||||
QTest::newRow("no match")
|
||||
<< QRegExp("\\d+")
|
||||
<< QString("")
|
||||
<< QByteArray("test")
|
||||
<< static_cast<int>(QHttpSocket::NotFound);
|
||||
<< static_cast<int>(HttpSocket::NotFound);
|
||||
|
||||
QTest::newRow("captured texts")
|
||||
<< QRegExp("(\\d+)")
|
||||
<< QString("/path/%1")
|
||||
<< QByteArray("123")
|
||||
<< static_cast<int>(QHttpSocket::Found)
|
||||
<< static_cast<int>(HttpSocket::Found)
|
||||
<< QByteArray("/path/123");
|
||||
}
|
||||
|
||||
@@ -97,18 +97,18 @@ void TestQHttpHandler::testRedirect()
|
||||
QTRY_VERIFY(pair.isConnected());
|
||||
|
||||
QSimpleHttpClient client(pair.client());
|
||||
QHttpSocket socket(pair.server(), &pair);
|
||||
HttpSocket socket(pair.server(), &pair);
|
||||
|
||||
client.sendHeaders("GET", path);
|
||||
QTRY_VERIFY(socket.isHeadersParsed());
|
||||
|
||||
QHttpHandler handler;
|
||||
HttpHandler handler;
|
||||
handler.addRedirect(pattern, destination);
|
||||
handler.route(&socket, socket.path());
|
||||
|
||||
QTRY_COMPARE(client.statusCode(), statusCode);
|
||||
|
||||
if (statusCode == QHttpSocket::Found) {
|
||||
if (statusCode == HttpSocket::Found) {
|
||||
QFETCH(QByteArray, location);
|
||||
QCOMPARE(client.headers().value("Location"), location);
|
||||
}
|
||||
@@ -125,19 +125,19 @@ void TestQHttpHandler::testSubHandler_data()
|
||||
<< QRegExp("\\w+")
|
||||
<< QByteArray("test")
|
||||
<< QString("")
|
||||
<< static_cast<int>(QHttpSocket::OK);
|
||||
<< static_cast<int>(HttpSocket::OK);
|
||||
|
||||
QTest::newRow("no match")
|
||||
<< QRegExp("\\d+")
|
||||
<< QByteArray("test")
|
||||
<< QString("")
|
||||
<< static_cast<int>(QHttpSocket::NotFound);
|
||||
<< static_cast<int>(HttpSocket::NotFound);
|
||||
|
||||
QTest::newRow("path")
|
||||
<< QRegExp("one/")
|
||||
<< QByteArray("one/two")
|
||||
<< QString("two")
|
||||
<< static_cast<int>(QHttpSocket::OK);
|
||||
<< static_cast<int>(HttpSocket::OK);
|
||||
}
|
||||
|
||||
void TestQHttpHandler::testSubHandler()
|
||||
@@ -151,13 +151,13 @@ void TestQHttpHandler::testSubHandler()
|
||||
QTRY_VERIFY(pair.isConnected());
|
||||
|
||||
QSimpleHttpClient client(pair.client());
|
||||
QHttpSocket socket(pair.server(), &pair);
|
||||
HttpSocket socket(pair.server(), &pair);
|
||||
|
||||
client.sendHeaders("GET", path);
|
||||
QTRY_VERIFY(socket.isHeadersParsed());
|
||||
|
||||
DummyHandler subHandler;
|
||||
QHttpHandler handler;
|
||||
HttpHandler handler;
|
||||
handler.addSubHandler(pattern, &subHandler);
|
||||
|
||||
handler.route(&socket, socket.path());
|
||||
|
||||
@@ -29,15 +29,15 @@
|
||||
#include "common/qsimplehttpclient.h"
|
||||
#include "common/qsocketpair.h"
|
||||
|
||||
class DummyMiddleware : public QHttpMiddleware
|
||||
class DummyMiddleware : public HttpMiddleware
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
virtual bool process(QHttpSocket *socket)
|
||||
virtual bool process(HttpSocket *socket)
|
||||
{
|
||||
socket->writeError(QHttpSocket::Forbidden);
|
||||
socket->writeError(HttpSocket::Forbidden);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -57,17 +57,17 @@ void TestQHttpMiddleware::testProcess()
|
||||
QTRY_VERIFY(pair.isConnected());
|
||||
|
||||
QSimpleHttpClient client(pair.client());
|
||||
QHttpSocket socket(pair.server(), &pair);
|
||||
HttpSocket socket(pair.server(), &pair);
|
||||
|
||||
client.sendHeaders("GET", "/");
|
||||
QTRY_VERIFY(socket.isHeadersParsed());
|
||||
|
||||
DummyMiddleware middleware;
|
||||
QHttpHandler handler;
|
||||
HttpHandler handler;
|
||||
handler.addMiddleware(&middleware);
|
||||
handler.route(&socket, "/");
|
||||
|
||||
QTRY_COMPARE(client.statusCode(), static_cast<int>(QHttpSocket::Forbidden));
|
||||
QTRY_COMPARE(client.statusCode(), static_cast<int>(HttpSocket::Forbidden));
|
||||
}
|
||||
|
||||
QTEST_MAIN(TestQHttpMiddleware)
|
||||
|
||||
@@ -30,15 +30,15 @@
|
||||
|
||||
typedef QList<QByteArray> QByteArrayList;
|
||||
|
||||
Q_DECLARE_METATYPE(QHttpSocket::Method)
|
||||
Q_DECLARE_METATYPE(QHttpSocket::QueryStringMap)
|
||||
Q_DECLARE_METATYPE(QHttpSocket::HeaderMap)
|
||||
Q_DECLARE_METATYPE(HttpSocket::Method)
|
||||
Q_DECLARE_METATYPE(HttpSocket::QueryStringMap)
|
||||
Q_DECLARE_METATYPE(HttpSocket::HeaderMap)
|
||||
|
||||
const QIByteArray Key1 = "a";
|
||||
const IByteArray Key1 = "a";
|
||||
const QByteArray Value1 = "b";
|
||||
const QByteArray Line1 = Key1 + ": " + Value1;
|
||||
|
||||
const QIByteArray Key2 = "c";
|
||||
const IByteArray Key2 = "c";
|
||||
const QByteArray Value2 = "d";
|
||||
const QByteArray Line2 = Key2 + ": " + Value2;
|
||||
|
||||
@@ -72,7 +72,7 @@ private Q_SLOTS:
|
||||
|
||||
private:
|
||||
|
||||
QHttpSocket::HeaderMap headers;
|
||||
HttpSocket::HeaderMap headers;
|
||||
};
|
||||
|
||||
TestQHttpParser::TestQHttpParser()
|
||||
@@ -127,7 +127,7 @@ void TestQHttpParser::testSplit()
|
||||
QFETCH(QByteArrayList, parts);
|
||||
|
||||
QByteArrayList outParts;
|
||||
QHttpParser::split(data, delim, maxSplit, outParts);
|
||||
HttpParser::split(data, delim, maxSplit, outParts);
|
||||
|
||||
QCOMPARE(outParts, parts);
|
||||
}
|
||||
@@ -136,29 +136,29 @@ void TestQHttpParser::testParsePath_data()
|
||||
{
|
||||
QTest::addColumn<QByteArray>("rawPath");
|
||||
QTest::addColumn<QString>("path");
|
||||
QTest::addColumn<QHttpSocket::QueryStringMap>("map");
|
||||
QTest::addColumn<HttpSocket::QueryStringMap>("map");
|
||||
|
||||
QTest::newRow("no query string")
|
||||
<< QByteArray("/path")
|
||||
<< QString("/path")
|
||||
<< QHttpSocket::QueryStringMap();
|
||||
<< HttpSocket::QueryStringMap();
|
||||
|
||||
QTest::newRow("single parameter")
|
||||
<< QByteArray("/path?a=b")
|
||||
<< QString("/path")
|
||||
<< QHttpSocket::QueryStringMap{{"a", "b"}};
|
||||
<< HttpSocket::QueryStringMap{{"a", "b"}};
|
||||
}
|
||||
|
||||
void TestQHttpParser::testParsePath()
|
||||
{
|
||||
QFETCH(QByteArray, rawPath);
|
||||
QFETCH(QString, path);
|
||||
QFETCH(QHttpSocket::QueryStringMap, map);
|
||||
QFETCH(HttpSocket::QueryStringMap, map);
|
||||
|
||||
QString outPath;
|
||||
QHttpSocket::QueryStringMap outMap;
|
||||
HttpSocket::QueryStringMap outMap;
|
||||
|
||||
QVERIFY(QHttpParser::parsePath(rawPath, outPath, outMap));
|
||||
QVERIFY(HttpParser::parsePath(rawPath, outPath, outMap));
|
||||
|
||||
QCOMPARE(path, outPath);
|
||||
QCOMPARE(map, outMap);
|
||||
@@ -168,7 +168,7 @@ void TestQHttpParser::testParseHeaderList_data()
|
||||
{
|
||||
QTest::addColumn<bool>("success");
|
||||
QTest::addColumn<QByteArrayList>("lines");
|
||||
QTest::addColumn<QHttpSocket::HeaderMap>("headers");
|
||||
QTest::addColumn<HttpSocket::HeaderMap>("headers");
|
||||
|
||||
QTest::newRow("empty line")
|
||||
<< false
|
||||
@@ -185,11 +185,11 @@ void TestQHttpParser::testParseHeaderList()
|
||||
QFETCH(bool, success);
|
||||
QFETCH(QByteArrayList, lines);
|
||||
|
||||
QHttpSocket::HeaderMap outHeaders;
|
||||
QCOMPARE(QHttpParser::parseHeaderList(lines, outHeaders), success);
|
||||
HttpSocket::HeaderMap outHeaders;
|
||||
QCOMPARE(HttpParser::parseHeaderList(lines, outHeaders), success);
|
||||
|
||||
if (success) {
|
||||
QFETCH(QHttpSocket::HeaderMap, headers);
|
||||
QFETCH(HttpSocket::HeaderMap, headers);
|
||||
QCOMPARE(outHeaders, headers);
|
||||
}
|
||||
}
|
||||
@@ -216,9 +216,9 @@ void TestQHttpParser::testParseHeaders()
|
||||
QFETCH(QByteArray, data);
|
||||
|
||||
QByteArrayList outParts;
|
||||
QHttpSocket::HeaderMap outHeaders;
|
||||
HttpSocket::HeaderMap outHeaders;
|
||||
|
||||
QCOMPARE(QHttpParser::parseHeaders(data, outParts, outHeaders), success);
|
||||
QCOMPARE(HttpParser::parseHeaders(data, outParts, outHeaders), success);
|
||||
|
||||
if (success) {
|
||||
QFETCH(QByteArrayList, parts);
|
||||
@@ -230,7 +230,7 @@ void TestQHttpParser::testParseRequestHeaders_data()
|
||||
{
|
||||
QTest::addColumn<bool>("success");
|
||||
QTest::addColumn<QByteArray>("data");
|
||||
QTest::addColumn<QHttpSocket::Method>("method");
|
||||
QTest::addColumn<HttpSocket::Method>("method");
|
||||
QTest::addColumn<QByteArray>("path");
|
||||
|
||||
QTest::newRow("bad HTTP version")
|
||||
@@ -240,7 +240,7 @@ void TestQHttpParser::testParseRequestHeaders_data()
|
||||
QTest::newRow("GET request")
|
||||
<< true
|
||||
<< QByteArray("GET / HTTP/1.0")
|
||||
<< QHttpSocket::GET
|
||||
<< HttpSocket::GET
|
||||
<< QByteArray("/");
|
||||
}
|
||||
|
||||
@@ -249,14 +249,14 @@ void TestQHttpParser::testParseRequestHeaders()
|
||||
QFETCH(bool, success);
|
||||
QFETCH(QByteArray, data);
|
||||
|
||||
QHttpSocket::Method outMethod;
|
||||
HttpSocket::Method outMethod;
|
||||
QByteArray outPath;
|
||||
QHttpSocket::HeaderMap outHeaders;
|
||||
HttpSocket::HeaderMap outHeaders;
|
||||
|
||||
QCOMPARE(QHttpParser::parseRequestHeaders(data, outMethod, outPath, outHeaders), success);
|
||||
QCOMPARE(HttpParser::parseRequestHeaders(data, outMethod, outPath, outHeaders), success);
|
||||
|
||||
if (success) {
|
||||
QFETCH(QHttpSocket::Method, method);
|
||||
QFETCH(HttpSocket::Method, method);
|
||||
QFETCH(QByteArray, path);
|
||||
|
||||
QCOMPARE(method, outMethod);
|
||||
@@ -289,9 +289,9 @@ void TestQHttpParser::testParseResponseHeaders()
|
||||
|
||||
int outStatusCode;
|
||||
QByteArray outStatusReason;
|
||||
QHttpSocket::HeaderMap outHeaders;
|
||||
HttpSocket::HeaderMap outHeaders;
|
||||
|
||||
QCOMPARE(QHttpParser::parseResponseHeaders(data, outStatusCode, outStatusReason, outHeaders), success);
|
||||
QCOMPARE(HttpParser::parseResponseHeaders(data, outStatusCode, outStatusReason, outHeaders), success);
|
||||
|
||||
if (success) {
|
||||
QFETCH(int, statusCode);
|
||||
|
||||
@@ -58,15 +58,15 @@ TestQHttpRange::TestQHttpRange()
|
||||
|
||||
void TestQHttpRange::testDefaultConstructor()
|
||||
{
|
||||
QHttpRange range;
|
||||
HttpRange range;
|
||||
|
||||
QCOMPARE(range.isValid(), false);
|
||||
}
|
||||
|
||||
void TestQHttpRange::testAssignmentOperator()
|
||||
{
|
||||
QHttpRange range;
|
||||
QHttpRange otherRange(100, 200, -1);
|
||||
HttpRange range;
|
||||
HttpRange otherRange(100, 200, -1);
|
||||
|
||||
range = otherRange;
|
||||
|
||||
@@ -110,7 +110,7 @@ void TestQHttpRange::testFromToLength()
|
||||
QFETCH(int, to);
|
||||
QFETCH(int, length);
|
||||
|
||||
QHttpRange range(inFrom, inTo, inDataSize);
|
||||
HttpRange range(inFrom, inTo, inDataSize);
|
||||
|
||||
QCOMPARE(range.from(), from);
|
||||
QCOMPARE(range.to(), to);
|
||||
@@ -162,7 +162,7 @@ void TestQHttpRange::testIsValid()
|
||||
QFETCH(int, dataSize);
|
||||
QFETCH(bool, valid);
|
||||
|
||||
QHttpRange range(from, to, dataSize);
|
||||
HttpRange range(from, to, dataSize);
|
||||
|
||||
QCOMPARE(range.isValid(), valid);
|
||||
}
|
||||
@@ -235,7 +235,7 @@ void TestQHttpRange::testParseFromString()
|
||||
QFETCH(int, dataSize);
|
||||
QFETCH(bool, valid);
|
||||
|
||||
QHttpRange range(data, dataSize);
|
||||
HttpRange range(data, dataSize);
|
||||
|
||||
QCOMPARE(range.isValid(), valid);
|
||||
|
||||
@@ -281,7 +281,7 @@ void TestQHttpRange::testContentRange()
|
||||
QFETCH(int, dataSize);
|
||||
QFETCH(QString, contentRange);
|
||||
|
||||
QHttpRange range(from, to, dataSize);
|
||||
HttpRange range(from, to, dataSize);
|
||||
|
||||
QCOMPARE(range.contentRange(), contentRange);
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
|
||||
#include "common/qsimplehttpclient.h"
|
||||
|
||||
class TestHandler : public QHttpHandler
|
||||
class TestHandler : public HttpHandler
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -45,12 +45,12 @@ public:
|
||||
|
||||
TestHandler() : mSocket(0) {}
|
||||
|
||||
virtual void process(QHttpSocket *socket, const QString &path) {
|
||||
virtual void process(HttpSocket *socket, const QString &path) {
|
||||
mSocket = socket;
|
||||
mPath = path;
|
||||
}
|
||||
|
||||
QHttpSocket *mSocket;
|
||||
HttpSocket *mSocket;
|
||||
QString mPath;
|
||||
};
|
||||
|
||||
@@ -70,7 +70,7 @@ private Q_SLOTS:
|
||||
void TestQHttpServer::testServer()
|
||||
{
|
||||
TestHandler handler;
|
||||
QHttpServer server(&handler);
|
||||
HttpServer server(&handler);
|
||||
|
||||
QVERIFY(server.listen(QHostAddress::LocalHost));
|
||||
|
||||
@@ -102,7 +102,7 @@ void TestQHttpServer::testSsl()
|
||||
config.setPrivateKey(key);
|
||||
config.setLocalCertificateChain(certs);
|
||||
|
||||
QHttpServer server;
|
||||
HttpServer server;
|
||||
server.setSslConfiguration(config);
|
||||
|
||||
QVERIFY(server.listen(QHostAddress::LocalHost));
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
#include "common/qsimplehttpclient.h"
|
||||
#include "common/qsocketpair.h"
|
||||
|
||||
Q_DECLARE_METATYPE(QHttpSocket::QueryStringMap)
|
||||
Q_DECLARE_METATYPE(HttpSocket::QueryStringMap)
|
||||
|
||||
// Utility macro (avoids duplication) that creates a pair of connected
|
||||
// sockets, a QSimpleHttpClient for the client and a QHttpSocket for the
|
||||
@@ -41,7 +41,7 @@ Q_DECLARE_METATYPE(QHttpSocket::QueryStringMap)
|
||||
QSocketPair pair; \
|
||||
QTRY_VERIFY(pair.isConnected()); \
|
||||
QSimpleHttpClient client(pair.client()); \
|
||||
QHttpSocket server(pair.server())
|
||||
HttpSocket server(pair.server())
|
||||
|
||||
const QByteArray Method = "POST";
|
||||
const QByteArray Path = "/test";
|
||||
@@ -67,7 +67,7 @@ private Q_SLOTS:
|
||||
|
||||
private:
|
||||
|
||||
QHttpSocket::HeaderMap headers;
|
||||
HttpSocket::HeaderMap headers;
|
||||
};
|
||||
|
||||
TestQHttpSocket::TestQHttpSocket()
|
||||
@@ -82,7 +82,7 @@ void TestQHttpSocket::testProperties()
|
||||
|
||||
client.sendHeaders(Method, Path, headers);
|
||||
|
||||
QTRY_COMPARE(server.method(), QHttpSocket::POST);
|
||||
QTRY_COMPARE(server.method(), HttpSocket::POST);
|
||||
QCOMPARE(server.rawPath(), Path);
|
||||
QCOMPARE(server.headers(), headers);
|
||||
|
||||
@@ -120,7 +120,7 @@ void TestQHttpSocket::testRedirect()
|
||||
|
||||
server.writeRedirect(Path, true);
|
||||
|
||||
QTRY_COMPARE(client.statusCode(), static_cast<int>(QHttpSocket::MovedPermanently));
|
||||
QTRY_COMPARE(client.statusCode(), static_cast<int>(HttpSocket::MovedPermanently));
|
||||
QCOMPARE(client.headers().value("Location"), Path);
|
||||
QTRY_COMPARE(disconnectedSpy.count(), 1);
|
||||
}
|
||||
@@ -170,7 +170,7 @@ void TestQHttpSocket::testJson()
|
||||
QJsonObject object{{"a", "b"}, {"c", 123}};
|
||||
QByteArray data = QJsonDocument(object).toJson();
|
||||
|
||||
client.sendHeaders(Method, Path, QHttpSocket::HeaderMap{
|
||||
client.sendHeaders(Method, Path, HttpSocket::HeaderMap{
|
||||
{"Content-Length", QByteArray::number(data.length())},
|
||||
{"Content-Type", "application/json"}
|
||||
});
|
||||
|
||||
@@ -31,8 +31,8 @@ const char *Value2 = "TEST";
|
||||
// Helpful macros to cut down on the amount of duplicated code
|
||||
#define TEST_OPERATOR(tn,t,on,o,v) void test##tn##on() \
|
||||
{ \
|
||||
QCOMPARE(QIByteArray(Value1) o static_cast<t>(Value2), v); \
|
||||
QCOMPARE(static_cast<t>(Value1) o QIByteArray(Value1), v); \
|
||||
QCOMPARE(IByteArray(Value1) o static_cast<t>(Value2), v); \
|
||||
QCOMPARE(static_cast<t>(Value1) o IByteArray(Value1), v); \
|
||||
}
|
||||
#define TEST_TYPE(tn,t) \
|
||||
TEST_OPERATOR(tn, t, Equals, ==, true) \
|
||||
@@ -50,7 +50,7 @@ private Q_SLOTS:
|
||||
|
||||
TEST_TYPE(ConstChar, const char *)
|
||||
TEST_TYPE(QByteArray, QByteArray)
|
||||
TEST_TYPE(QIByteArray, QIByteArray)
|
||||
TEST_TYPE(IByteArray, IByteArray)
|
||||
TEST_TYPE(QString, QString)
|
||||
|
||||
void testContains();
|
||||
@@ -58,7 +58,7 @@ private Q_SLOTS:
|
||||
|
||||
void TestQIByteArray::testContains()
|
||||
{
|
||||
QIByteArray v(Value1);
|
||||
IByteArray v(Value1);
|
||||
|
||||
QVERIFY(v.contains('t'));
|
||||
QVERIFY(v.contains(Value2));
|
||||
|
||||
@@ -51,9 +51,9 @@ void TestQLocalAuth::testAuth()
|
||||
QTRY_VERIFY(pair.isConnected());
|
||||
|
||||
QSimpleHttpClient client(pair.client());
|
||||
QHttpSocket socket(pair.server(), &pair);
|
||||
HttpSocket socket(pair.server(), &pair);
|
||||
|
||||
QLocalAuth localAuth;
|
||||
LocalAuth localAuth;
|
||||
localAuth.setData(QVariantMap{
|
||||
{CustomName, CustomData}
|
||||
});
|
||||
@@ -67,7 +67,7 @@ void TestQLocalAuth::testAuth()
|
||||
QVERIFY(data.contains("token"));
|
||||
QCOMPARE(data.value(CustomName).toByteArray(), CustomData);
|
||||
|
||||
client.sendHeaders("GET", "/", QHttpSocket::HeaderMap{
|
||||
client.sendHeaders("GET", "/", HttpSocket::HeaderMap{
|
||||
{HeaderName, data.value("token").toByteArray()}
|
||||
});
|
||||
QTRY_VERIFY(socket.isHeadersParsed());
|
||||
|
||||
@@ -46,7 +46,7 @@ void TestQLocalFile::initTestCase()
|
||||
|
||||
void TestQLocalFile::testOpen()
|
||||
{
|
||||
QLocalFile file;
|
||||
LocalFile file;
|
||||
QVERIFY(file.open());
|
||||
QVERIFY(file.remove());
|
||||
}
|
||||
|
||||
@@ -37,8 +37,8 @@ public Q_SLOTS:
|
||||
|
||||
void wrongArgumentCount() {}
|
||||
void wrongArgumentType(int) {}
|
||||
void valid(QHttpSocket *socket) {
|
||||
socket->writeError(QHttpSocket::OK);
|
||||
void valid(HttpSocket *socket) {
|
||||
socket->writeError(HttpSocket::OK);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -60,19 +60,19 @@ void TestQObjectHandler::testOldConnection_data()
|
||||
|
||||
QTest::newRow("invalid slot")
|
||||
<< QByteArray(SLOT(invalid()))
|
||||
<< static_cast<int>(QHttpSocket::InternalServerError);
|
||||
<< static_cast<int>(HttpSocket::InternalServerError);
|
||||
|
||||
QTest::newRow("wrong argument count")
|
||||
<< QByteArray(SLOT(wrongArgumentCount()))
|
||||
<< static_cast<int>(QHttpSocket::InternalServerError);
|
||||
<< static_cast<int>(HttpSocket::InternalServerError);
|
||||
|
||||
QTest::newRow("wrong argument type")
|
||||
<< QByteArray(SLOT(wrongArgumentType(int)))
|
||||
<< static_cast<int>(QHttpSocket::InternalServerError);
|
||||
<< static_cast<int>(HttpSocket::InternalServerError);
|
||||
|
||||
QTest::newRow("valid")
|
||||
<< QByteArray(SLOT(valid(QHttpSocket*)))
|
||||
<< static_cast<int>(QHttpSocket::OK);
|
||||
<< QByteArray(SLOT(valid(HttpSocket*)))
|
||||
<< static_cast<int>(HttpSocket::OK);
|
||||
}
|
||||
|
||||
void TestQObjectHandler::testOldConnection()
|
||||
@@ -89,7 +89,7 @@ void TestQObjectHandler::testOldConnection()
|
||||
QTRY_VERIFY(pair.isConnected());
|
||||
|
||||
QSimpleHttpClient client(pair.client());
|
||||
QHttpSocket socket(pair.server(), &pair);
|
||||
HttpSocket socket(pair.server(), &pair);
|
||||
|
||||
client.sendHeaders("GET", "test");
|
||||
QTRY_VERIFY(socket.isHeadersParsed());
|
||||
@@ -107,11 +107,11 @@ void TestQObjectHandler::testNewConnection()
|
||||
handler.registerMethod("0", &api, &DummyAPI::valid);
|
||||
|
||||
// Connect to functor
|
||||
handler.registerMethod("1", [](QHttpSocket *socket) {
|
||||
socket->writeError(QHttpSocket::OK);
|
||||
handler.registerMethod("1", [](HttpSocket *socket) {
|
||||
socket->writeError(HttpSocket::OK);
|
||||
});
|
||||
handler.registerMethod("2", &api, [](QHttpSocket *socket) {
|
||||
socket->writeError(QHttpSocket::OK);
|
||||
handler.registerMethod("2", &api, [](HttpSocket *socket) {
|
||||
socket->writeError(HttpSocket::OK);
|
||||
});
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
@@ -119,13 +119,13 @@ void TestQObjectHandler::testNewConnection()
|
||||
QTRY_VERIFY(pair.isConnected());
|
||||
|
||||
QSimpleHttpClient client(pair.client());
|
||||
QHttpSocket socket(pair.server(), &pair);
|
||||
HttpSocket socket(pair.server(), &pair);
|
||||
|
||||
client.sendHeaders("GET", QByteArray::number(i));
|
||||
QTRY_VERIFY(socket.isHeadersParsed());
|
||||
|
||||
handler.route(&socket, socket.path());
|
||||
QTRY_COMPARE(client.statusCode(), static_cast<int>(QHttpSocket::OK));
|
||||
QTRY_COMPARE(client.statusCode(), static_cast<int>(HttpSocket::OK));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,26 +48,26 @@ void TestQProxyHandler::testDataPassthrough()
|
||||
{
|
||||
// Create the upstream handler (simple echo)
|
||||
QObjectHandler upstreamHandler;
|
||||
upstreamHandler.registerMethod(Path, [](QHttpSocket *socket) {
|
||||
upstreamHandler.registerMethod(Path, [](HttpSocket *socket) {
|
||||
socket->write(socket->readAll());
|
||||
socket->close();
|
||||
}, true);
|
||||
|
||||
// Create the upstream server and begin listening
|
||||
QHttpServer upstreamServer(&upstreamHandler);
|
||||
HttpServer upstreamServer(&upstreamHandler);
|
||||
QVERIFY(upstreamServer.listen(QHostAddress::LocalHost));
|
||||
|
||||
// Create the proxy handler
|
||||
QProxyHandler handler(upstreamServer.serverAddress(), upstreamServer.serverPort());
|
||||
ProxyHandler handler(upstreamServer.serverAddress(), upstreamServer.serverPort());
|
||||
|
||||
QSocketPair pair;
|
||||
QTRY_VERIFY(pair.isConnected());
|
||||
|
||||
QSimpleHttpClient client(pair.client());
|
||||
QHttpSocket socket(pair.server(), &pair);
|
||||
HttpSocket socket(pair.server());
|
||||
|
||||
// Send the headers and wait for them to be parsed
|
||||
QHttpSocket::HeaderMap headers{
|
||||
HttpSocket::HeaderMap headers{
|
||||
{"Content-Length", QByteArray::number(Data.length())}
|
||||
};
|
||||
client.sendHeaders("POST", QString("/%1").arg(Path).toUtf8(), headers);
|
||||
|
||||
@@ -33,7 +33,7 @@ QSimpleHttpClient::QSimpleHttpClient(QTcpSocket *socket)
|
||||
onReadyRead();
|
||||
}
|
||||
|
||||
void QSimpleHttpClient::sendHeaders(const QByteArray &method, const QByteArray &path, const QHttpSocket::HeaderMap &headers)
|
||||
void QSimpleHttpClient::sendHeaders(const QByteArray &method, const QByteArray &path, const HttpSocket::HeaderMap &headers)
|
||||
{
|
||||
QByteArray data = method + " " + path + " HTTP/1.0\r\n";
|
||||
for (auto i = headers.constBegin(); i != headers.constEnd(); ++i) {
|
||||
@@ -59,7 +59,7 @@ void QSimpleHttpClient::onReadyRead()
|
||||
// Parse the headers if the double CRLF sequence was found
|
||||
int index = mBuffer.indexOf("\r\n\r\n");
|
||||
if (index != -1) {
|
||||
QHttpParser::parseResponseHeaders(mBuffer.left(index), mStatusCode, mStatusReason, mHeaders);
|
||||
HttpParser::parseResponseHeaders(mBuffer.left(index), mStatusCode, mStatusReason, mHeaders);
|
||||
|
||||
mHeadersParsed = true;
|
||||
mData.append(mBuffer.mid(index + 4));
|
||||
|
||||
@@ -45,7 +45,7 @@ public:
|
||||
|
||||
QSimpleHttpClient(QTcpSocket *socket);
|
||||
|
||||
void sendHeaders(const QByteArray &method, const QByteArray &path, const QHttpSocket::HeaderMap &headers=QHttpSocket::HeaderMap());
|
||||
void sendHeaders(const QByteArray &method, const QByteArray &path, const HttpSocket::HeaderMap &headers=HttpSocket::HeaderMap());
|
||||
void sendData(const QByteArray &data);
|
||||
|
||||
int statusCode() const {
|
||||
@@ -56,7 +56,7 @@ public:
|
||||
return mStatusReason;
|
||||
}
|
||||
|
||||
QHttpSocket::HeaderMap headers() const {
|
||||
HttpSocket::HeaderMap headers() const {
|
||||
return mHeaders;
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ private:
|
||||
|
||||
int mStatusCode;
|
||||
QByteArray mStatusReason;
|
||||
QHttpSocket::HeaderMap mHeaders;
|
||||
HttpSocket::HeaderMap mHeaders;
|
||||
QByteArray mData;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user