cleaup commenting and style across new HTTP classes

This commit is contained in:
Stephen Birarda 2014-01-17 09:00:34 -08:00
parent 036dba9c2f
commit e68dc1b142
3 changed files with 47 additions and 105 deletions

View file

@ -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<FormData> HttpConnection::parseFormData () const
{
QList<FormData> HttpConnection::parseFormData() const {
// make sure we have the correct MIME type
QList<QByteArray> elements = _requestHeaders.value("Content-Type").split(';');
if (elements.at(0).trimmed() != "multipart/form-data") {
@ -64,6 +57,7 @@ QList<FormData> HttpConnection::parseFormData () const
break;
}
}
QByteArray start = "--" + boundary;
QByteArray end = "\r\n--" + boundary + "--\r\n";
@ -103,7 +97,7 @@ QList<FormData> 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();

View file

@ -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 <QHash>
#include <QHostAddress>
@ -27,128 +27,89 @@ class HttpManager;
class MaskFilter;
class ServerApp;
/** Header hash. */
/// Header hash
typedef QHash<QByteArray, QByteArray> Headers;
/** A form data element. */
/// A form data element
typedef QPair<Headers, QByteArray> 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<FormData> 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__) */

View file

@ -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 <QByteArray>
#include <QHash>
@ -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__) */