QtWebApp
stefanfrings::HttpResponse Class Reference

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

Detailed Description

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.

Constructor & Destructor Documentation

◆ HttpResponse()

HttpResponse::HttpResponse ( QTcpSocket *  socket)

Constructor.

Parameters
socketused to write the response

Definition at line 10 of file httpresponse.cpp.

Member Function Documentation

◆ flush()

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.

◆ getStatusCode()

int HttpResponse::getStatusCode ( ) const

Return the status code.

Definition at line 43 of file httpresponse.cpp.

◆ isConnected()

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.

◆ redirect()

void HttpResponse::redirect ( const QByteArray &  url)

Send a redirect response to the browser.

Cannot be combined with write().

Parameters
urlDestination URL

Definition at line 184 of file httpresponse.cpp.

◆ setCookie()

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.

◆ setHeader() [1/2]

void HttpResponse::setHeader ( const QByteArray  name,
const QByteArray  value 
)

Set a HTTP response header.

You must call this method before the first write().

Parameters
namename of the header
valuevalue of the header

Definition at line 20 of file httpresponse.cpp.

◆ setHeader() [2/2]

void HttpResponse::setHeader ( const QByteArray  name,
const int  value 
)

Set a HTTP response header.

You must call this method before the first write().

Parameters
namename of the header
valuevalue of the header

Definition at line 26 of file httpresponse.cpp.

◆ setStatus()

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.

◆ write()

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.

Parameters
dataData bytes of the body
lastPartIndicates that this is the last chunk of data and flushes the output buffer.

Definition at line 99 of file httpresponse.cpp.


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