Commit fadb843a authored by Thomas BOUTROUE's avatar Thomas BOUTROUE

Code style / cleanup

parent b3b01229
......@@ -43,7 +43,6 @@ void ExampleStaticFileServing::onServerError (QString msg) {
static inline void printErrorToReply (QtHttpReply * reply, QString errorMessage) {
reply->addHeader ("Content-Type", QByteArrayLiteral ("text/plain"));
reply->addHeader ("Content-Length", QByteArray::number (errorMessage.size ()));
reply->appendRawData (errorMessage.toLocal8Bit ());
}
......@@ -57,7 +56,6 @@ void ExampleStaticFileServing::onRequestNeedsReply (QtHttpRequest * request, QtH
if (file.open (QFile::ReadOnly)) {
QByteArray data = file.readAll ();
reply->addHeader ("Content-Type", mime.name ().toLocal8Bit ());
reply->addHeader ("Content-Length", QByteArray::number (data.size ()));
reply->appendRawData (data);
file.close ();
}
......
......@@ -3,10 +3,13 @@ import qbs 1.0;
Project {
name: "The Qt5 HTTP Server";
StaticLibrary {
property bool buildAsSharedLib : true;
Product {
name: "qt5-http-server-lib";
type: (project.buildAsSharedLib ? "dynamiclibrary" : "staticlibrary");
targetName: "Qt5HttpServer";
cpp.includePaths: ["."];
cpp.includePaths: ".";
Depends {
name: "cpp";
......@@ -23,17 +26,14 @@ Project {
name: "C++ Headers";
files: ["Qt*.h"]
}
Group {
qbs.install: true;
fileTagsFilter: (project.buildAsSharedLib ? "dynamiclibrary" : "staticlibrary");
}
}
Application {
name: "qt5-http-server-test-app";
targetName: "Qt5HttpServerTestApp";
files: [
"ExampleRequestParsing.cpp",
"ExampleRequestParsing.h",
"ExampleStaticFileServing.cpp",
"ExampleStaticFileServing.h",
"main.cpp",
]
Depends {
name: "cpp";
......@@ -45,5 +45,18 @@ Project {
Depends {
name: "qt5-http-server-lib";
}
Group {
name: "C++ Sources";
files: ["Example*.cpp", "main.cpp"];
}
Group {
name: "C++ Headers";
files: ["Example*.h"]
}
Group {
qbs.install: true;
fileTagsFilter: "application";
}
}
}
......@@ -6,8 +6,10 @@
#include "QtHttpHeader.h"
#include <QCryptographicHash>
#include <QDateTime>
#include <QTcpSocket>
#include <QStringBuilder>
#include <QStringList>
#include <QDateTime>
#define CRLF QByteArrayLiteral ("\r\n")
#define SPACE char (' ')
......@@ -49,7 +51,7 @@ void QtHttpClientWrapper::onClientDataReceived () {
QString command = parts.at (0);
QString url = parts.at (1);
QString version = parts.at (2);
if (version == m_serverHandle->getHttpVersion ()) {
if (version == QtHttpServer::HTTP_VERSION) {
//qDebug () << "Debug : HTTP"
// << "command :" << command
// << "url :" << url
......@@ -149,7 +151,7 @@ void QtHttpClientWrapper::onReplySendHeadersRequested (void) {
if (reply != Q_NULLPTR) {
QByteArray data;
// HTTP Version + Status Code + Status Msg
data.append (m_serverHandle->getHttpVersion ());
data.append (QtHttpServer::HTTP_VERSION);
data.append (SPACE);
data.append (QByteArray::number (reply->getStatusCode ()));
data.append (SPACE);
......
......@@ -2,7 +2,9 @@
#define QTHTTPCLIENTWRAPPER_H
#include <QObject>
#include <QTcpSocket>
#include <QString>
class QTcpSocket;
class QtHttpRequest;
class QtHttpReply;
......
#include "QtHttpHeader.h"
#include <QByteArray>
const QByteArray & QtHttpHeader::Server = QByteArrayLiteral ("Server");
const QByteArray & QtHttpHeader::Date = QByteArrayLiteral ("Date");
const QByteArray & QtHttpHeader::Host = QByteArrayLiteral ("Host");
......
#ifndef QTHTTPHEADER_H
#define QTHTTPHEADER_H
#include <QByteArray>
class QByteArray;
class QtHttpHeader {
public:
......
......@@ -2,9 +2,12 @@
#define QTHTTPREPLY_H
#include <QObject>
#include <QByteArray>
#include <QHash>
#include <QList>
class QtHttpServer;
class QtHttpReply : public QObject {
Q_OBJECT
Q_ENUMS (StatusCode)
......@@ -19,11 +22,11 @@ public:
NotFound = 404
};
int getRawDataSize (void) const;
bool useChunked (void) const;
StatusCode getStatusCode (void) const;
QByteArray getRawData (void) const;
QList<QByteArray> getHeadersList (void) const;
int getRawDataSize (void) const;
bool useChunked (void) const;
StatusCode getStatusCode (void) const;
QByteArray getRawData (void) const;
QList<QByteArray> getHeadersList (void) const;
QByteArray getHeader (const QByteArray & header) const;
......@@ -44,7 +47,7 @@ private:
bool m_useChunked;
StatusCode m_statusCode;
QByteArray m_data;
QtHttpServer * m_serverHandle;
QtHttpServer * m_serverHandle;
QHash<QByteArray, QByteArray> m_headersHash;
};
......
......@@ -2,22 +2,24 @@
#define QTHTTPREQUEST_H
#include <QObject>
#include <QHash>
#include <QString>
#include <QByteArray>
#include <QHash>
#include <QUrl>
class QtHttpServer;
class QtHttpRequest : public QObject {
Q_OBJECT
public:
explicit QtHttpRequest (QtHttpServer * parent);
int getRawDataSize (void) const;
QUrl getUrl (void) const;
QString getCommand (void) const;
QByteArray getRawData (void) const;
QList<QByteArray> getHeadersList (void) const;
int getRawDataSize (void) const;
QUrl getUrl (void) const;
QString getCommand (void) const;
QByteArray getRawData (void) const;
QList<QByteArray> getHeadersList (void) const;
QByteArray getHeader (const QByteArray & header) const;
......@@ -31,7 +33,7 @@ private:
QUrl m_url;
QString m_command;
QByteArray m_data;
QtHttpServer * m_serverHandle;
QtHttpServer * m_serverHandle;
QHash<QByteArray, QByteArray> m_headersHash;
};
......
......@@ -4,9 +4,12 @@
#include "QtHttpReply.h"
#include "QtHttpClientWrapper.h"
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include <QDebug>
const QString QtHttpServer::s_httpVersion = QStringLiteral ("HTTP/1.1");
const QString & QtHttpServer::HTTP_VERSION = QStringLiteral ("HTTP/1.1");
QtHttpServer::QtHttpServer (QObject * parent)
: QObject (parent)
......@@ -24,16 +27,12 @@ const QString QtHttpServer::getServerName (void) const {
return m_serverName;
}
const QString QtHttpServer::getHttpVersion (void) {
return s_httpVersion;
}
void QtHttpServer::start (quint16 port) {
if (m_sockServer->listen (QHostAddress::Any, port)) {
emit started (m_sockServer->serverPort ());
}
else {
emit error (m_sockServer->errorString ());
emit error (m_sockServer->errorString ());
}
}
......
......@@ -2,9 +2,11 @@
#define QTHTTPSERVER_H
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include <QString>
#include <QHash>
class QTcpSocket;
class QTcpServer;
class QtHttpRequest;
class QtHttpReply;
......@@ -14,26 +16,25 @@ class QtHttpServer : public QObject {
Q_OBJECT
public:
explicit QtHttpServer (QObject * parent = Q_NULLPTR);
virtual ~QtHttpServer (void);
explicit QtHttpServer (QObject * parent = Q_NULLPTR);
virtual ~QtHttpServer (void);
const QString getServerName (void) const;
static const QString & HTTP_VERSION;
static const QString getHttpVersion (void);
const QString getServerName (void) const;
public slots:
void start (quint16 port = 0);
void stop (void);
void setServerName (const QString & serverName);
void start (quint16 port = 0);
void stop (void);
void setServerName (const QString & serverName);
signals:
void started (quint16 port);
void stopped (void);
void error (const QString & msg);
void clientConnected (const QString & guid);
void clientDisconnected (const QString & guid);
void requestNeedsReply (QtHttpRequest * request,
QtHttpReply * reply);
void started (quint16 port);
void stopped (void);
void error (const QString & msg);
void clientConnected (const QString & guid);
void clientDisconnected (const QString & guid);
void requestNeedsReply (QtHttpRequest * request, QtHttpReply * reply);
private slots:
void onClientConnected (void);
......@@ -41,10 +42,8 @@ private slots:
private:
QString m_serverName;
QTcpServer * m_sockServer;
QTcpServer * m_sockServer;
QHash<QTcpSocket *, QtHttpClientWrapper *> m_socksClientsHash;
static const QString s_httpVersion;
};
#endif // QTHTTPSERVER_H
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