From e68dc1b1425d779ae19372a8dbebabe368b02ac2 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 17 Jan 2014 09:00:34 -0800 Subject: [PATCH] cleaup commenting and style across new HTTP classes --- .../embedded-webserver/src/HttpConnection.cpp | 26 ++--- .../embedded-webserver/src/HttpConnection.h | 97 ++++++------------- .../embedded-webserver/src/HttpManager.h | 29 ++---- 3 files changed, 47 insertions(+), 105 deletions(-) diff --git a/libraries/embedded-webserver/src/HttpConnection.cpp b/libraries/embedded-webserver/src/HttpConnection.cpp index 7923e8ec40..ffb763cee3 100755 --- a/libraries/embedded-webserver/src/HttpConnection.cpp +++ b/libraries/embedded-webserver/src/HttpConnection.cpp @@ -23,8 +23,8 @@ HttpConnection::HttpConnection (QTcpSocket* socket, HttpManager* parentManager) _parentManager(parentManager), _socket(socket), _stream(socket), - _address(socket->peerAddress()) -{ + _address(socket->peerAddress()) { + // take over ownership of the socket _socket->setParent(this); @@ -32,23 +32,16 @@ HttpConnection::HttpConnection (QTcpSocket* socket, HttpManager* parentManager) connect(socket, SIGNAL(readyRead()), SLOT(readRequest())); connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(deleteLater())); connect(socket, SIGNAL(disconnected()), SLOT(deleteLater())); - - // log the connection - qDebug() << "HTTP connection opened." << _address; } -HttpConnection::~HttpConnection () -{ +HttpConnection::~HttpConnection() { // log the destruction - QString error; - QDebug base = qDebug() << "HTTP connection closed." << _address; if (_socket->error() != QAbstractSocket::UnknownSocketError) { - base << _socket->errorString(); + qDebug() << _socket->errorString(); } } -QList HttpConnection::parseFormData () const -{ +QList HttpConnection::parseFormData() const { // make sure we have the correct MIME type QList elements = _requestHeaders.value("Content-Type").split(';'); if (elements.at(0).trimmed() != "multipart/form-data") { @@ -64,6 +57,7 @@ QList HttpConnection::parseFormData () const break; } } + QByteArray start = "--" + boundary; QByteArray end = "\r\n--" + boundary + "--\r\n"; @@ -103,7 +97,7 @@ QList HttpConnection::parseFormData () const return data; } -void HttpConnection::respond (const char* code, const QByteArray& content, const char* contentType, const Headers& headers) { +void HttpConnection::respond(const char* code, const QByteArray& content, const char* contentType, const Headers& headers) { _socket->write("HTTP/1.1 "); _socket->write(code); _socket->write("\r\n"); @@ -139,8 +133,7 @@ void HttpConnection::respond (const char* code, const QByteArray& content, const _socket->disconnectFromHost(); } -void HttpConnection::readRequest () -{ +void HttpConnection::readRequest() { if (!_socket->canReadLine()) { return; } @@ -177,8 +170,7 @@ void HttpConnection::readRequest () readHeaders(); } -void HttpConnection::readHeaders () -{ +void HttpConnection::readHeaders() { while (_socket->canReadLine()) { QByteArray line = _socket->readLine(); QByteArray trimmed = line.trimmed(); diff --git a/libraries/embedded-webserver/src/HttpConnection.h b/libraries/embedded-webserver/src/HttpConnection.h index 22ab0ddbb5..c791a93a07 100755 --- a/libraries/embedded-webserver/src/HttpConnection.h +++ b/libraries/embedded-webserver/src/HttpConnection.h @@ -10,8 +10,8 @@ // (https://github.com/ey6es/witgap/tree/master/src/cpp/server/http) // -#ifndef HTTP_CONNECTION -#define HTTP_CONNECTION +#ifndef __hifi__HttpConnection__ +#define __hifi__HttpConnection__ #include #include @@ -27,128 +27,89 @@ class HttpManager; class MaskFilter; class ServerApp; -/** Header hash. */ +/// Header hash typedef QHash Headers; -/** A form data element. */ +/// A form data element typedef QPair FormData; -/** - * Handles a single HTTP connection. - */ -class HttpConnection : public QObject -{ +/// Handles a single HTTP connection. +class HttpConnection : public QObject { Q_OBJECT public: - /** WebSocket close status codes. */ + /// WebSocket close status codes. enum ReasonCode { NoReason = 0, NormalClosure = 1000, GoingAway = 1001 }; - /** - * Initializes the connection. - */ + /// Initializes the connection. HttpConnection (QTcpSocket* socket, HttpManager* parentManager); - /** - * Destroys the connection. - */ + /// Destroys the connection. virtual ~HttpConnection (); - /** - * Returns a pointer to the underlying socket, to which WebSocket message bodies should be - * written. - */ + /// Returns a pointer to the underlying socket, to which WebSocket message bodies should be written. QTcpSocket* socket () const { return _socket; } - /** - * Returns the request operation. - */ + /// Returns the request operation. QNetworkAccessManager::Operation requestOperation () const { return _requestOperation; } - /** - * Returns a reference to the request URL. - */ + /// Returns a reference to the request URL. const QUrl& requestUrl () const { return _requestUrl; } - /** - * Returns a reference to the request headers. - */ + /// Returns a reference to the request headers. const Headers& requestHeaders () const { return _requestHeaders; } - /** - * Returns a reference to the request content. - */ + /// Returns a reference to the request content. const QByteArray& requestContent () const { return _requestContent; } - /** - * Parses the request content as form data, returning a list of header/content pairs. - */ + /// Parses the request content as form data, returning a list of header/content pairs. QList parseFormData () const; - /** - * Sends a response and closes the connection. - */ + /// Sends a response and closes the connection. void respond (const char* code, const QByteArray& content = QByteArray(), const char* contentType = "text/plain; charset=ISO-8859-1", const Headers& headers = Headers()); -signals: - - /** - * Fired when a WebSocket message of the specified size is available to read. - */ - void webSocketMessageAvailable (QIODevice* device, int length, bool text); - - /** - * Fired when the WebSocket has been closed by the other side. - */ - void webSocketClosed (quint16 reasonCode, QByteArray reason); protected slots: - /** - * Reads the request line. - */ + /// Reads the request line. void readRequest (); - /** - * Reads the headers. - */ + /// Reads the headers. void readHeaders (); - /** - * Reads the content. - */ + /// Reads the content. void readContent (); protected: - /** The parent HTTP manager. */ + /// The parent HTTP manager HttpManager* _parentManager; - /** The underlying socket. */ + /// The underlying socket. QTcpSocket* _socket; - /** The data stream for writing to the socket. */ + /// The data stream for writing to the socket. QDataStream _stream; - /** The stored address. */ + /// The stored address. QHostAddress _address; - /** The requested operation. */ + /// The requested operation. QNetworkAccessManager::Operation _requestOperation; - /** The requested URL. */ + /// The requested URL. QUrl _requestUrl; - /** The request headers. */ + /// The request headers. Headers _requestHeaders; - /** The last request header processed (used for continuations). */ + /// The last request header processed (used for continuations). QByteArray _lastRequestHeader; - /** The content of the request. */ + /// The content of the request. QByteArray _requestContent; }; -#endif // HTTP_CONNECTION +#endif /* defined(__hifi__HttpConnection__) */ diff --git a/libraries/embedded-webserver/src/HttpManager.h b/libraries/embedded-webserver/src/HttpManager.h index 5dbfefc8df..1c5cb7ee65 100755 --- a/libraries/embedded-webserver/src/HttpManager.h +++ b/libraries/embedded-webserver/src/HttpManager.h @@ -10,8 +10,8 @@ // (https://github.com/ey6es/witgap/tree/master/src/cpp/server/http) // -#ifndef HTTP_MANAGER -#define HTTP_MANAGER +#ifndef __hifi__HttpManager__ +#define __hifi__HttpManager__ #include #include @@ -20,34 +20,23 @@ class HttpConnection; class HttpRequestHandler; -/** - * Handles HTTP connections. - */ -class HttpManager : public QTcpServer -{ +/// Handles HTTP connections +class HttpManager : public QTcpServer { Q_OBJECT public: - /** - * Initializes the manager. - */ + /// Initializes the manager. HttpManager(quint16 port, const QString& documentRoot, QObject* parent = 0); - /** - * Handles an HTTP request. - */ + /// Handles an HTTP request. virtual bool handleRequest (HttpConnection* connection, const QString& path); - protected slots: - - /** - * Accepts all pending connections. - */ - void acceptConnections (); + /// Accepts all pending connections + void acceptConnections(); protected: QString _documentRoot; }; -#endif // HTTP_MANAGER +#endif /* defined(__hifi__HttpManager__) */