QtWebApp
|
This object represents a HTTP response, used to return something to the web client. More...
#include <httpresponse.h>
Public Member Functions | |
HttpResponse (QTcpSocket *socket) | |
Constructor. More... | |
void | setHeader (const QByteArray name, const QByteArray value) |
Set a HTTP response header. More... | |
void | setHeader (const QByteArray name, const int value) |
Set a HTTP response header. More... | |
QMap< QByteArray, QByteArray > & | getHeaders () |
Get the map of HTTP response headers. | |
QMap< QByteArray, HttpCookie > & | getCookies () |
Get the map of cookies. | |
void | setStatus (const int statusCode, const QByteArray description=QByteArray()) |
Set status code and description. More... | |
int | getStatusCode () const |
Return the status code. | |
void | write (const QByteArray data, const bool lastPart=false) |
Write body data to the socket. More... | |
bool | hasSentLastPart () const |
Indicates whether the body has been sent completely (write() has been called with lastPart=true). | |
void | setCookie (const HttpCookie &cookie) |
Set a cookie. More... | |
void | redirect (const QByteArray &url) |
Send a redirect response to the browser. More... | |
void | flush () |
Flush the output buffer (of the underlying socket). More... | |
bool | isConnected () const |
May be used to check whether the connection to the web client has been lost. More... | |
This object represents a HTTP response, used to return something to the web client.
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);
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 10 of file httpresponse.cpp.
void HttpResponse::flush | ( | ) |
Flush the output buffer (of the underlying socket).
You normally don't need to call this method because flush is automatically called after HttpRequestHandler::service() returns.
Definition at line 192 of file httpresponse.cpp.
bool HttpResponse::isConnected | ( | ) | const |
May be used to check whether the connection to the web client has been lost.
This might be useful to cancel the generation of large or slow responses.
Definition at line 198 of file httpresponse.cpp.
void HttpResponse::redirect | ( | const QByteArray & | url | ) |
Send a redirect response to the browser.
Cannot be combined with write().
url | Destination URL |
Definition at line 184 of file httpresponse.cpp.
void HttpResponse::setCookie | ( | const HttpCookie & | cookie | ) |
Set a cookie.
You must call this method before the first write().
Definition at line 168 of file httpresponse.cpp.
void HttpResponse::setHeader | ( | const QByteArray | name, |
const int | value | ||
) |
Set a HTTP response header.
You must call this method before the first write().
name | name of the header |
value | value of the header |
Definition at line 26 of file httpresponse.cpp.
void HttpResponse::setHeader | ( | const QByteArray | name, |
const QByteArray | value | ||
) |
Set a HTTP response header.
You must call this method before the first write().
name | name of the header |
value | value of the header |
Definition at line 20 of file httpresponse.cpp.
void HttpResponse::setStatus | ( | const int | statusCode, |
const QByteArray | description = QByteArray() |
||
) |
Set status code and description.
The default is 200,OK. You must call this method before the first write().
Definition at line 37 of file httpresponse.cpp.
void HttpResponse::write | ( | const QByteArray | data, |
const bool | lastPart = false |
||
) |
Write body data to the socket.
The HTTP status line, headers and cookies are sent automatically before the body.
If the response contains only a single chunk (indicated by lastPart=true), then a Content-Length header is automatically set.
Chunked mode is automatically selected if there is no Content-Length header and also no Connection:close header.
data | Data bytes of the body |
lastPart | Indicates that this is the last chunk of data and flushes the output buffer. |
Definition at line 99 of file httpresponse.cpp.