QHttpEngine 0.1.0
Simple and secure HTTP server for Qt applications
|
Implementation of the HTTP protocol. More...
#include <QHttpEngine/QHttpSocket>
Public Types | |
enum | { OK = 200 , MovedPermanently = 301 , Found = 302 , BadRequest = 400 , Forbidden = 403 , NotFound = 404 , MethodNotAllowed = 405 , InternalServerError = 500 } |
Signals | |
void | headersParsed () |
Indicate that request headers have been parsed. | |
Public Member Functions | |
QHttpSocket (QTcpSocket *socket, QObject *parent=0) | |
Create a new QHttpSocket from a QTcpSocket. | |
virtual qint64 | bytesAvailable () const |
Retrieve the number of bytes available for reading. | |
virtual void | close () |
Close the device and underlying socket. | |
qint64 | contentLength () const |
Retrieve the length of the content. | |
QHttpHeaderMap | headers () const |
Retrieve a map of request headers. | |
bool | isHeadersParsed () const |
Determine if the request headers have been parsed yet. | |
virtual bool | isSequential () const |
Determine if the device is sequential. | |
QByteArray | method () const |
Retrieve the request method. | |
QByteArray | path () const |
Retrieve the request path. | |
void | setHeader (const QByteArray &name, const QByteArray &value) |
Set a response header to a specific value. | |
void | setHeaders (const QHttpHeaderMap &headers) |
Set the response headers. | |
void | setStatusCode (int statusCode, const QByteArray &statusReason=QByteArray()) |
Set the response code. | |
void | writeError (int statusCode, const QByteArray &statusReason=QByteArray()) |
Write an HTTP error to the socket. | |
void | writeHeaders () |
Write response headers to the socket. | |
void | writeRedirect (const QByteArray &path, bool permanent=false) |
Write an HTTP 3xx redirect to the socket. | |
QHttpSocket 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 QHttpSocket will assume ownership of the socket 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.
anonymous enum |
Predefined constants for HTTP status codes
QHttpSocket::QHttpSocket | ( | QTcpSocket * | socket, |
QObject * | parent = 0 |
||
) |
This instance will assume ownership of the socket. 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.
qint64 QHttpSocket::contentLength | ( | ) | const |
This value is provided by the Content-Length
HTTP header (if present) and returns -1 if the value is not available.
QHttpHeaderMap QHttpSocket::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.
QByteArray QHttpSocket::method | ( | ) | const |
This method may only be called after the request headers have been parsed.
QByteArray QHttpSocket::path | ( | ) | const |
This method may only be called after the request headers have been parsed.
void QHttpSocket::setHeader | ( | const QByteArray & | name, |
const QByteArray & | value | ||
) |
This method may only be called before the response headers are written. If the specified header already has a value set, it will be overwritten.
void QHttpSocket::setHeaders | ( | const QHttpHeaderMap & | headers | ) |
This method may only be called before the response headers are written. All existing headers will be overwritten.
void QHttpSocket::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 QHttpSocket::writeHeaders | ( | ) |
This method should not be invoked after the response headers have been written.