Commit a7c93dc5 authored by Thomas BOUTROUE's avatar Thomas BOUTROUE

Lot of code cleanup and optimizations

Don't reject HTTP 1.0 requests
parent 927618d5
Pipeline #1004 passed with stage
in 19 seconds
This diff is collapsed.
......@@ -30,7 +30,7 @@ public:
RequestParsed = 3
};
QString getGuid (void);
const QByteArray & getGuid (void) const;
private slots:
void onClientDataReceived (void);
......@@ -43,7 +43,7 @@ protected slots:
void onReplySendDataRequested (void);
private:
QString m_guid;
QByteArray m_guid;
ParsingStatus m_parsingStatus;
QTcpSocket * m_sockClient;
QtHttpRequest * m_currentRequest;
......
#include "QtHttpReply.h"
#include "QtHttpHeader.h"
#include "QtHttpServer.h"
#include <QDateTime>
#include <QLocale>
QtHttpReply::QtHttpReply (QtHttpServer * parent)
QtHttpReply::QtHttpReply (QObject * parent)
: QObject (parent)
, m_useChunked (false)
, m_statusCode (Ok)
, m_data (QByteArray ())
, m_serverHandle (parent)
{
// set some additional headers
addHeader (QtHttpHeader::Date, QLocale ("C").toString (QDateTime::currentDateTimeUtc (), "ddd, dd MMM yyyy hh:mm:ss t").toUtf8 ());
addHeader (QtHttpHeader::Server, m_serverHandle->getServerName ().toUtf8 ());
}
, m_request (Q_NULLPTR)
{ }
int QtHttpReply::getRawDataSize (void) const {
return m_data.size ();
......@@ -30,7 +25,7 @@ QtHttpReply::StatusCode QtHttpReply::getStatusCode (void) const {
return m_statusCode;
}
QByteArray QtHttpReply::getRawData (void) const {
const QByteArray & QtHttpReply::getRawData (void) const {
return m_data;
}
......@@ -38,10 +33,18 @@ QList<QByteArray> QtHttpReply::getHeadersList (void) const {
return m_headersHash.keys ();
}
const QHash<QByteArray, QByteArray> & QtHttpReply::getHeaders (void) const {
return m_headersHash;
}
QByteArray QtHttpReply::getHeader (const QByteArray & header) const {
return m_headersHash.value (header, QByteArray ());
}
const QtHttpRequest * QtHttpReply::getRequest (void) const {
return m_request;
}
const QByteArray QtHttpReply::getStatusTextForCode (QtHttpReply::StatusCode statusCode) {
switch (statusCode) {
case Ok: return QByteArrayLiteral ("OK.");
......@@ -52,6 +55,10 @@ const QByteArray QtHttpReply::getStatusTextForCode (QtHttpReply::StatusCode stat
}
}
void QtHttpReply::setRequest (QtHttpRequest * request) {
m_request = request;
}
void QtHttpReply::setUseChunked (bool chunked){
m_useChunked = chunked;
}
......
......@@ -6,14 +6,14 @@
#include <QHash>
#include <QList>
class QtHttpServer;
class QtHttpRequest;
class QtHttpReply : public QObject {
Q_OBJECT
Q_ENUMS (StatusCode)
public:
explicit QtHttpReply (QtHttpServer * parent);
explicit QtHttpReply (QObject * parent = Q_NULLPTR);
enum StatusCode {
Ok = 200,
......@@ -28,17 +28,22 @@ public:
ServiceUnavailable = 503,
};
int getRawDataSize (void) const;
bool useChunked (void) const;
StatusCode getStatusCode (void) const;
QByteArray getRawData (void) const;
int getRawDataSize (void) const;
bool useChunked (void) const;
StatusCode getStatusCode (void) const;
const QByteArray & getRawData (void) const;
const QHash<QByteArray, QByteArray> & getHeaders (void) const;
QList<QByteArray> getHeadersList (void) const;
QByteArray getHeader (const QByteArray & header) const;
const QtHttpRequest * getRequest (void) const;
static const QByteArray getStatusTextForCode (StatusCode statusCode);
public slots:
void setRequest (QtHttpRequest * request);
void setUseChunked (bool chunked = false);
void setStatusCode (StatusCode statusCode);
void appendRawData (const QByteArray & data);
......@@ -53,7 +58,7 @@ private:
bool m_useChunked;
StatusCode m_statusCode;
QByteArray m_data;
QtHttpServer * m_serverHandle;
QtHttpRequest * m_request;
QHash<QByteArray, QByteArray> m_headersHash;
};
......
#include "QtHttpRequest.h"
#include "QtHttpHeader.h"
#include "QtHttpServer.h"
QtHttpRequest::QtHttpRequest (QtHttpClientWrapper * client, QtHttpServer * parent)
: QObject (parent)
, m_url (QUrl ())
, m_command (QString ())
, m_data (QByteArray ())
, m_serverHandle (parent)
, m_clientHandle (client)
{
// set some additional headers
addHeader (QtHttpHeader::ContentLength, QByteArrayLiteral ("0"));
addHeader (QtHttpHeader::Connection, QByteArrayLiteral ("Keep-Alive"));
}
QtHttpRequest::QtHttpRequest (QObject * parent)
: QObject (parent)
{ }
QtHttpRequest::~QtHttpRequest (void) { }
QUrl QtHttpRequest::getUrl (void) const {
const QUrl & QtHttpRequest::getUrl (void) const {
return m_url;
}
QString QtHttpRequest::getCommand (void) const {
const QByteArray & QtHttpRequest::getCommand (void) const {
return m_command;
}
......@@ -30,32 +20,40 @@ int QtHttpRequest::getRawDataSize (void) const {
return m_data.size ();
}
QByteArray QtHttpRequest::getRawData (void) const {
const QByteArray & QtHttpRequest::getVersion (void) const {
return m_version;
}
const QByteArray & QtHttpRequest::getRawData (void) const {
return m_data;
}
QList<QByteArray> QtHttpRequest::getHeadersList (void) const {
return m_headersHash.keys ();
const QHash<QByteArray, QByteArray> & QtHttpRequest::getHeaders (void) const {
return m_headersHash;
}
QtHttpClientWrapper * QtHttpRequest::getClient (void) const {
return m_clientHandle;
QList<QByteArray> QtHttpRequest::getHeadersList (void) const {
return m_headersHash.keys ();
}
QByteArray QtHttpRequest::getHeader (const QByteArray & header) const {
return m_headersHash.value (header, QByteArray ());
}
void QtHttpRequest::setVersion (const QByteArray & version) {
m_version = version;
}
void QtHttpRequest::setUrl (const QUrl & url) {
m_url = url;
}
void QtHttpRequest::setCommand (const QString & command) {
void QtHttpRequest::setCommand (const QByteArray & command) {
m_command = command;
}
void QtHttpRequest::addHeader (const QByteArray & header, const QByteArray & value) {
QByteArray key = header.trimmed ();
const QByteArray key = header.trimmed ();
if (!key.isEmpty ()) {
m_headersHash.insert (key, value);
}
......
......@@ -7,37 +7,36 @@
#include <QHash>
#include <QUrl>
class QtHttpServer;
class QtHttpClientWrapper;
class QtHttpRequest : public QObject {
Q_OBJECT
public:
explicit QtHttpRequest (QtHttpClientWrapper * client, QtHttpServer * parent);
explicit QtHttpRequest (QObject * parent = Q_NULLPTR);
virtual ~QtHttpRequest (void);
int getRawDataSize (void) const;
QUrl getUrl (void) const;
QString getCommand (void) const;
QByteArray getRawData (void) const;
QList<QByteArray> getHeadersList (void) const;
QtHttpClientWrapper * getClient (void) const;
int getRawDataSize (void) const;
const QUrl & getUrl (void) const;
const QByteArray & getVersion (void) const;
const QByteArray & getCommand (void) const;
const QByteArray & getRawData (void) const;
const QHash<QByteArray, QByteArray> & getHeaders (void) const;
QList<QByteArray> getHeadersList (void) const;
QByteArray getHeader (const QByteArray & header) const;
public slots:
void setVersion (const QByteArray & version);
void setUrl (const QUrl & url);
void setCommand (const QString & command);
void setCommand (const QByteArray & command);
void addHeader (const QByteArray & header, const QByteArray & value);
void appendRawData (const QByteArray & data);
private:
QByteArray m_version;
QUrl m_url;
QString m_command;
QByteArray m_command;
QByteArray m_data;
QtHttpServer * m_serverHandle;
QtHttpClientWrapper * m_clientHandle;
QHash<QByteArray, QByteArray> m_headersHash;
};
......
......@@ -6,7 +6,8 @@
#include <QDebug>
const QByteArray QtHttpServer::HTTP_VERSION ("HTTP/1.1");
const QByteArray QtHttpServer::HTTP_VERSION_1_1 ("HTTP/1.1");
const QByteArray QtHttpServer::HTTP_VERSION_1_0 ("HTTP/1.0");
QtHttpServerWrapper::QtHttpServerWrapper (QObject * parent)
: QTcpServer (parent)
......
......@@ -40,7 +40,8 @@ class QtHttpServer : public QObject {
public:
explicit QtHttpServer (QObject * parent = Q_NULLPTR);
static const QByteArray HTTP_VERSION;
static const QByteArray HTTP_VERSION_1_1;
static const QByteArray HTTP_VERSION_1_0;
typedef void (QSslSocket::* SslErrorSignal) (const QList<QSslError> &);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment