QHttpEngine
1.0.0
Simple and secure HTTP server for Qt applications
|
Implementation of the HTTP protocol. More...
#include <qhttpengine/socket.h>
Public Types | |
enum | { OK = 200, Created = 201, Accepted = 202, PartialContent = 206, MovedPermanently = 301, Found = 302, BadRequest = 400, Unauthorized = 401, Forbidden = 403, NotFound = 404, MethodNotAllowed = 405, Conflict = 409, InternalServerError = 500, BadGateway = 502, ServiceUnavailable = 503, HttpVersionNotSupported = 505 } |
typedef QMultiMap< IByteArray, QByteArray > | HeaderMap |
Map consisting of HTTP headers. More... | |
enum | Method { OPTIONS = 1, GET = 1 << 1, HEAD = 1 << 2, POST = 1 << 3, PUT = 1 << 4, DELETE = 1 << 5, TRACE = 1 << 6, CONNECT = 1 << 7 } |
typedef QMultiMap< QString, QString > | QueryStringMap |
Map consisting of query string values. | |
Signals | |
void | disconnected () |
Indicate that the client has disconnected. | |
void | headersParsed () |
Indicate that request headers have been parsed. More... | |
Public Member Functions | |
Socket (QTcpSocket *socket, QObject *parent=0) | |
Create a new socket from a QTcpSocket. More... | |
virtual qint64 | bytesAvailable () const |
Retrieve the number of bytes available for reading. More... | |
virtual void | close () |
Close the device and underlying socket. More... | |
qint64 | contentLength () const |
Retrieve the length of the content. More... | |
HeaderMap | headers () const |
Retrieve a map of request headers. More... | |
bool | isHeadersParsed () const |
Determine if the request headers have been parsed yet. | |
virtual bool | isSequential () const |
Determine if the device is sequential. More... | |
Method | method () const |
Retrieve the request method. More... | |
QString | path () const |
Retrieve the decoded path with the query string removed. More... | |
QHostAddress | peerAddress () const |
Retrive the address of the remote peer. | |
QueryStringMap | queryString () const |
Retrieve the query string. More... | |
QByteArray | rawPath () const |
Retrieve the raw request path. More... | |
bool | readJson (QJsonDocument &document) |
Parse the request body as a JSON document. More... | |
void | setHeader (const QByteArray &name, const QByteArray &value, bool replace=true) |
Set a response header to a specific value. More... | |
void | setHeaders (const HeaderMap &headers) |
Set the response headers. More... | |
void | setStatusCode (int statusCode, const QByteArray &statusReason=QByteArray()) |
Set the response code. More... | |
void | writeError (int statusCode, const QByteArray &statusReason=QByteArray()) |
Write an HTTP error to the socket and close it. | |
void | writeHeaders () |
Write response headers to the socket. More... | |
void | writeJson (const QJsonDocument &document, int statusCode=OK) |
Write the specified JSON document to the socket and close it. | |
void | writeRedirect (const QByteArray &path, bool permanent=false) |
Write an HTTP 3xx redirect to the socket and close it. | |
Protected Member Functions | |
virtual qint64 | readData (char *data, qint64 maxlen) |
Implementation of QIODevice::readData() | |
virtual qint64 | writeData (const char *data, qint64 len) |
Implementation of QIODevice::writeData() | |
Friends | |
class | SocketPrivate |
This class provides a class derived from QIODevice that can be used to read data from and write data to an HTTP client through a QTcpSocket provided in the constructor. The socket will assume ownership of the QTcpSocket and ensure it is properly deleted. Consequently, the QTcpSocket must have been allocated on the heap:
Once the headersParsed() signal is emitted, information about the request can be retrieved using the appropriate methods. As data is received, the readyRead() signal is emitted and any available data can be read using QIODevice::read():
If the client sets the Content-Length
header, the readChannelFinished() signal will be emitted when the specified amount of data is read from the client. Otherwise the readChannelFinished() signal will be emitted immediately after the headers are read.
The status code and headers may be set as long as no data has been written to the device and the writeHeaders() method has not been called. The headers are written either when the writeHeaders() method is called or when data is first written to the device:
This class also provides methods that simplify writing a redirect or an HTTP error to the socket. To write a redirect, simply pass a path to the writeRedirect() method. To write an error, simply pass the desired HTTP status code to the writeError() method. Both methods will close the socket once the response is written.
typedef QMultiMap<IByteArray, QByteArray> QHttpEngine::Socket::HeaderMap |
The key used for the map is the IByteArray class, which allows for case-insensitive comparison.
anonymous enum |
Predefined constants for HTTP status codes
HTTP methods
An integer constant is provided for each of the methods described in RFC 2616 (HTTP/1.1).
QHttpEngine::Socket::Socket | ( | QTcpSocket * | socket, |
QObject * | parent = 0 |
||
) |
This instance will assume ownership of the QTcpSocket. That is, it will make itself the parent of the socket.
|
virtual |
This method indicates the number of bytes that could immediately be read by a call to QIODevice::readAll().
|
virtual |
Invoking this method signifies that no more data will be written to the device. It will also close the underlying QTcpSocket and destroy this object.
qint64 QHttpEngine::Socket::contentLength | ( | ) | const |
This value is provided by the Content-Length
HTTP header (if present) and returns -1 if the value is not available.
HeaderMap QHttpEngine::Socket::headers | ( | ) | const |
This method may only be called after the request headers have been parsed. The original case of the headers is preserved but comparisons are performed in a case-insensitive manner.
|
signal |
This signal is emitted when the request headers have been received from the client and parsing is complete. It is then safe to begin reading request data. The readyRead() signal will be emitted as request data is received.
|
virtual |
This method will always return true.
Method QHttpEngine::Socket::method | ( | ) | const |
This method may only be called after the request headers have been parsed.
QString QHttpEngine::Socket::path | ( | ) | const |
This method may only be called after the request headers have been parsed.
QueryStringMap QHttpEngine::Socket::queryString | ( | ) | const |
This method may only be called after the request headers have been parsed.
QByteArray QHttpEngine::Socket::rawPath | ( | ) | const |
This method may only be called after the request headers have been parsed.
bool QHttpEngine::Socket::readJson | ( | QJsonDocument & | document | ) |
This method may only be called after the request headers and the request body have been received. The most effective way to confirm that this is the case is by using:
If the JSON received is invalid, an error will be immediately written to the socket. The return value indicates whether the JSON was valid.
void QHttpEngine::Socket::setHeader | ( | const QByteArray & | name, |
const QByteArray & | value, | ||
bool | replace = true |
||
) |
This method may only be called before the response headers are written. Duplicate values will be either appended to the header or used to replace the original value, depending on the third parameter.
void QHttpEngine::Socket::setHeaders | ( | const HeaderMap & | headers | ) |
This method may only be called before the response headers are written. All existing headers will be overwritten.
void QHttpEngine::Socket::setStatusCode | ( | int | statusCode, |
const QByteArray & | statusReason = QByteArray() |
||
) |
This method may only be called before the response headers are written.
The statusReason parameter may be omitted if one of the predefined status code constants is used. If no response status code is explicitly set, it will assume a default value of "200 OK".
void QHttpEngine::Socket::writeHeaders | ( | ) |
This method should not be invoked after the response headers have been written.