Loading...
Searching...
No Matches
QHttpSocket Class Reference

Implementation of the HTTP protocol. More...

`#include <QHttpEngine/QHttpSocket>`

Inheritance diagram for 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.

## 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()

## Detailed Description

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:

QTcpSocket *tcpSock = new QTcpSocket;
tcpSock->connectToHost(...);
tcpSock->waitForConnected();
QHttpSocket *httpSock = new QHttpSocket(tcpSock);
QHttpSocket
Implementation of the HTTP protocol.
Definition qhttpsocket.h:89

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():

QByteArray data;
connect(httpSock, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
void MyClass::onReadyRead()
{
data.append(httpSock->readAll());
}

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:

httpSock->setStatusCode(QHttpSocket::OK);
httpSock->setHeader("Content-Length", 13);
httpSock->write("Hello, world!");
QHttpSocket::OK
@ OK
Request was successful.
Definition qhttpsocket.h:99
QHttpSocket::setStatusCode
void setStatusCode(int statusCode, const QByteArray &statusReason=QByteArray())
Set the response code.
QHttpSocket::setHeader
void setHeader(const QByteArray &name, const QByteArray &value)
Set a response header to a specific value.

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.

## Member Enumeration Documentation

## anonymous enum

anonymous enum

Predefined constants for HTTP status codes

Enumerator
OK | Request was successful.
MovedPermanently | Resource has moved permanently.
Found | Resource is available at an alternate URI.
BadRequest | Bad client request.
Forbidden | Access to the resource is forbidden.
NotFound | Resource was not found.
MethodNotAllowed | Method is not valid for the resource.
InternalServerError | An internal server error occurred.

## Constructor & Destructor Documentation

## QHttpSocket()

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.

## Member Function Documentation

## bytesAvailable()

virtual qint64 QHttpSocket::bytesAvailable | ( |  | ) | const

virtual

This method indicates the number of bytes that could immediately be read by a call to QIODevice::readAll().

## close()

virtual void QHttpSocket::close | ( |  | ) |

virtual

Invoking this method signifies that no more data will be written to the device. It will also close the underlying QTcpSocket.

## contentLength()

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.

## headers()

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.

## headersParsed

void QHttpSocket::headersParsed | ( |  | ) |

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.

## isSequential()

virtual bool QHttpSocket::isSequential | ( |  | ) | const

virtual

This method will always return true.

## method()

QByteArray QHttpSocket::method | ( |  | ) | const

This method may only be called after the request headers have been parsed.

## path()

QByteArray QHttpSocket::path | ( |  | ) | const

This method may only be called after the request headers have been parsed.

## setHeader()

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.

## setHeaders()

void QHttpSocket::setHeaders | ( | const QHttpHeaderMap & | headers | ) |

This method may only be called before the response headers are written. All existing headers will be overwritten.

## setStatusCode()

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".

## writeHeaders()

void QHttpSocket::writeHeaders | ( |  | ) |

This method should not be invoked after the response headers have been written.

The documentation for this class was generated from the following file:

- qhttpsocket.h