This object represents a HTTP response, in particular the response headers. More...
#include <httpresponse.h>
Public Member Functions | |
HttpResponse (QTcpSocket *socket) | |
Constructor. | |
void | setHeader (QByteArray name, QByteArray value) |
Set a HTTP response header. | |
void | setHeader (QByteArray name, int value) |
Set a HTTP response header. | |
QMap< QByteArray, QByteArray > & | getHeaders () |
Get the map of HTTP response headers. | |
QMap< QByteArray, HttpCookie > & | getCookies () |
Get the map of cookies. | |
void | setStatus (int statusCode, QByteArray description=QByteArray()) |
Set status code and description. | |
void | write (QByteArray data, bool lastPart=false) |
Write body data to the socket. | |
bool | hasSentLastPart () const |
Indicates wheter the body has been sent completely. | |
void | setCookie (const HttpCookie &cookie) |
Set a cookie. |
This object represents a HTTP response, in particular the response headers.
Example code for proper response generation:
response.setStatus(200,"OK"); // optional, because this is the default response.writeBody("Hello"); response.writeBody("World!",true);
Example how to return an error:
response.setStatus(500,"server error"); response.write("The request cannot be processed because the servers is broken",true);
For performance reason, writing a single or few large packets is better than writing many small packets. In case of large responses (e.g. file downloads), a Content-Length header should be set before calling write(). Web Browsers use that information to display a progress bar.
Definition at line 36 of file httpresponse.h.
HttpResponse::HttpResponse | ( | QTcpSocket * | socket | ) |
Constructor.
socket | used to write the response |
Definition at line 8 of file httpresponse.cpp.
bool HttpResponse::hasSentLastPart | ( | ) | const |
Indicates wheter the body has been sent completely.
Used by the connection handler to terminate the body automatically when necessary.
Definition at line 108 of file httpresponse.cpp.
void HttpResponse::setCookie | ( | const HttpCookie & | cookie | ) |
Set a cookie.
Cookies are sent together with the headers when the first call to write() occurs.
Definition at line 113 of file httpresponse.cpp.
void HttpResponse::setHeader | ( | QByteArray | name, | |
int | value | |||
) |
Set a HTTP response header.
name | name of the header | |
value | value of the header |
Definition at line 21 of file httpresponse.cpp.
void HttpResponse::setHeader | ( | QByteArray | name, | |
QByteArray | value | |||
) |
Set a HTTP response header.
name | name of the header | |
value | value of the header |
Definition at line 16 of file httpresponse.cpp.
void HttpResponse::setStatus | ( | int | statusCode, | |
QByteArray | description = QByteArray() | |||
) |
Set status code and description.
The default is 200,OK.
Definition at line 30 of file httpresponse.cpp.
void HttpResponse::write | ( | QByteArray | data, | |
bool | lastPart = false | |||
) |
Write body data to the socket.
The HTTP status line and headers are sent automatically before the first byte of the body gets sent.
If the response contains only a single chunk (indicated by lastPart=true), the response is transferred in traditional mode with a Content-Length header, which is automatically added if not already set before.
Otherwise, each part is transferred in chunked mode.
data | Data bytes of the body | |
lastPart | Indicator, if this is the last part of the response. |
Definition at line 69 of file httpresponse.cpp.