tufao  1.3.0
An asynchronous web framework for C++ built on top of Qt
Tufao::HttpServerResponse Class Reference

The Tufao::HttpServerResponse is used to respond to a Tufao::HttpServerRequest. More...

+ Inheritance diagram for Tufao::HttpServerResponse:
+ Collaboration diagram for Tufao::HttpServerResponse:

Public Types

enum  Option { HTTP_1_0 = 1, HTTP_1_1 = 1 << 1, KEEP_ALIVE = 1 << 2 }
 This enum represents some aspects of a HTTP response. More...
 

Public Slots

bool writeContinue ()
 Sends a HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent. More...
 
bool writeHead (int statusCode, const QByteArray &reasonPhrase, const Headers &headers)
 Sends a response header to the request. More...
 
bool writeHead (int statusCode, const QByteArray &reasonPhrase)
 This is an overloaded function. More...
 
bool writeHead (HttpResponseStatus statusCode, const QByteArray &reasonPhrase, const Headers &headers)
 This is an overloaded function. More...
 
bool writeHead (HttpResponseStatus statusCode, const QByteArray &reasonPhrase)
 This is an overloaded function. More...
 
bool writeHead (HttpResponseStatus statusCode, const Headers &headers)
 This is an overloaded function. More...
 
bool writeHead (HttpResponseStatus statusCode)
 This is an overloaded function. More...
 
bool write (const QByteArray &chunk)
 This sends a chunk of the response body. More...
 
bool addTrailers (const Headers &headers)
 This method adds HTTP trailing headers (a header but at the end of the message) to the response. More...
 
bool addTrailer (const QByteArray &headerName, const QByteArray &headerValue)
 This method adds one HTTP trailing header (a header but at the end of the message) to the response. More...
 
bool end (const QByteArray &chunk=QByteArray())
 This method signals to the server that all of the response headers and body has been sent; that server should consider this message complete. More...
 

Signals

void finished ()
 This signal is emitted when all bytes from the HTTP response message are written in the device/socket (not confuse with delivered). More...
 

Public Member Functions

 HttpServerResponse (QIODevice &device, Options options=Options(), QObject *parent=0)
 Constructs a Tufao::HttpServerResponse object. More...
 
 ~HttpServerResponse ()
 Destroys the object.
 
Options options () const
 Returns the options passed to the object constructor.
 
bool setOptions (Options options)
 Change the formatting options to options. More...
 
const Headersheaders () const
 Returns a const reference to the headers which will be sent when the first piece of body is written. More...
 
Headersheaders ()
 Returns a reference to the headers which will be sent when the first piece of body is written. More...
 
bool flush ()
 This method calls QAbstractSocket::flush for the object passed in the constructor. More...
 

Detailed Description

The Tufao::HttpServerResponse is used to respond to a Tufao::HttpServerRequest.

A response is built of well defined parts and must be sent ordered. The order to send these parts are:

Note
In HTTP/1.0 connections, it's not possible to send the message body in chunks and you must use Tufao::HttpServerResponse::end to send the full body at once. Additionally, HTTP/1.0 connections don't support trailers too.
See also
Tufao::HttpServer

Member Enumeration Documentation

This enum represents some aspects of a HTTP response.

Enumerator
HTTP_1_0 

A HTTP/1.0 response.

HTTP_1_1 

A HTTP/1.1 response.

KEEP_ALIVE 

The connection should use a persistent stream.

Note
Only supported in HTTP/1.1 connections.

Constructor & Destructor Documentation

Tufao::HttpServerResponse::HttpServerResponse ( QIODevice &  device,
Options  options = Options(),
QObject *  parent = 0 
)
explicit

Constructs a Tufao::HttpServerResponse object.

parent is passed to the QObject constructor.

Parameters
optionsIt controls some aspects of the response.
deviceThe socket used by Tufao::HttpServerResponse to write a HTTP response message.
Note
if options doesn't contain HTTP_1_0 or HTTP_1_1 set, the behaviour is undefined. If you set both flags, the behaviour is undefined also.
Since
1.0

Member Function Documentation

bool Tufao::HttpServerResponse::addTrailer ( const QByteArray &  headerName,
const QByteArray &  headerValue 
)
slot

This method adds one HTTP trailing header (a header but at the end of the message) to the response.

Warning
Trailers will only be emitted if chunked encoding is used for the response; if it is not (e.g., if the request was sent by a HTTP/1.0 user agent), they will be silently discarded.
Note
A server MUST NOT use the trailer for any header fields unless at least one of the following is true:
  • the request included a TE header field that indicates “trailers” is acceptable in the transfer-coding of the response;
  • the server is the origin server (your server is not a proxy or a tunnel) for the response, the trailer fields consist entirely of optional metadata, and the recipient could use the message (in a manner acceptable to the origin server) without receiving this metadata. In other words, the origin server is willing to accept the possibility that the trailer fields might be silently discarded along the path to the client.
See also
Tufao::HttpServerResponse::addTrailers
bool Tufao::HttpServerResponse::addTrailers ( const Headers headers)
slot

This method adds HTTP trailing headers (a header but at the end of the message) to the response.

Warning
Trailers will only be emitted if chunked encoding is used for the response; if it is not (e.g., if the request was sent by a HTTP/1.0 user agent), they will be silently discarded.
Note
A server MUST NOT use the trailer for any header fields unless at least one of the following is true:
  • the request included a TE header field that indicates “trailers” is acceptable in the transfer-coding of the response;
  • the server is the origin server (your server is not a proxy or a tunnel) for the response, the trailer fields consist entirely of optional metadata, and the recipient could use the message (in a manner acceptable to the origin server) without receiving this metadata. In other words, the origin server is willing to accept the possibility that the trailer fields might be silently discarded along the path to the client.
See also
Tufao::HttpServerResponse::addTrailer
bool Tufao::HttpServerResponse::end ( const QByteArray &  chunk = QByteArray())
slot

This method signals to the server that all of the response headers and body has been sent; that server should consider this message complete.

It MUST be called on each response exactly one time.

Parameters
chunkIf specified, it is equivalent to calling Tufao::HttpServerResponse::write followed by Tufao::HttpServerResponse::end
void Tufao::HttpServerResponse::finished ( )
signal

This signal is emitted when all bytes from the HTTP response message are written in the device/socket (not confuse with delivered).

Call Tufao::HttpServerResponse::end will cause this signal to be emitted.

bool Tufao::HttpServerResponse::flush ( )

This method calls QAbstractSocket::flush for the object passed in the constructor.

It'll do nothing and return false if the object passed in the constructor isn't an actual QAbstractSocket instance.

Since
0.3
const Headers& Tufao::HttpServerResponse::headers ( ) const

Returns a const reference to the headers which will be sent when the first piece of body is written.

Since
0.4
Headers& Tufao::HttpServerResponse::headers ( )

Returns a reference to the headers which will be sent when the first piece of body is written.

Use this reference to send custom headers.

Note
Change this object when the first piece of the message body was already written won't take any effects. However the object will retain the changes.
bool Tufao::HttpServerResponse::setOptions ( Options  options)

Change the formatting options to options.

Note
if options doesn't contain HTTP_1_0 or HTTP_1_1 set, the behaviour is undefined. If you set both flags, the behaviour is undefined also.
Returns
true if successful.
false if wasn't possible to change options. This can happen if the first chunk of data was already sent.
Since
1.0
bool Tufao::HttpServerResponse::write ( const QByteArray &  chunk)
slot

This sends a chunk of the response body.

This method may be called multiple times to provide successive parts of the body.

Note
This is the raw HTTP body and has nothing to do with higher-level multi-part body encodings that may be used.

The first time Tufao::HttpServerResponse::write is called, it will send the buffered headers and the first body chunk to the client. The second time Tufao::HttpServerResponse::write is called, it assumes you're going to streaming data, and sends that separately. That is, the response is buffered up to the first chunk of body.

If you call this function with a empty byte array, it will do nothing.

Note
HTTP/1.0 user agents don't support chunked entities. To overcome this limitation, Tufao::HttpServerResponse will buffer the chunks and send the full message at once. If you want a performance boost, treat HTTP/1.0 clients diferently.
See also
operator<<(HttpServerResponse&,const QByteArray&)
bool Tufao::HttpServerResponse::writeContinue ( )
slot

Sends a HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent.

You should write a HTTP/1.1 100 Continue response to requests that include 100-continue in the header Expect if you are willing to accept the request body based on the headers sent.

If you don't want to accept the request body, you should respond it with an Tufao::HttpServerResponse::EXPECTATION_FAILED response status.

The purpose of the 100 Continue status is to decrease the network traffic by avoiding the transfer of data that the server would reject anyway.

Warning
It's not possible to send a HTTP/1.1 100 Continue to HTTP/1.0 clients.
bool Tufao::HttpServerResponse::writeHead ( int  statusCode,
const QByteArray &  reasonPhrase,
const Headers headers 
)
slot

Sends a response header to the request.

Call this function after the first chunk of entity body data was already sent (calling Tufao::HttpServerResponse::write ) will have no effect.

Parameters
statusCodeThe status code is a 3-digit HTTP status code.
reasonPhraseA human-readable reasonPhrase.
headersThe response headers.
bool Tufao::HttpServerResponse::writeHead ( int  statusCode,
const QByteArray &  reasonPhrase 
)
slot
bool Tufao::HttpServerResponse::writeHead ( HttpResponseStatus  statusCode,
const QByteArray &  reasonPhrase,
const Headers headers 
)
slot
bool Tufao::HttpServerResponse::writeHead ( HttpResponseStatus  statusCode,
const QByteArray &  reasonPhrase 
)
slot
bool Tufao::HttpServerResponse::writeHead ( HttpResponseStatus  statusCode,
const Headers headers 
)
slot
bool Tufao::HttpServerResponse::writeHead ( HttpResponseStatus  statusCode)
slot

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