Stellarium  0.16.1
List of all members | Public Member Functions
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 (QByteArray name, QByteArray value)
 Set a HTTP response header. More...
 
void setHeader (QByteArray name, int value)
 Set a HTTP response header. More...
 
bool hasHeader (const QByteArray name) const
 Returns wether this header has already been set. More...
 
QMap< QByteArray, QByteArray > & getHeaders ()
 Get the map of HTTP response headers. More...
 
QMap< QByteArray, HttpCookie > & getCookies ()
 Get the map of cookies. More...
 
void setStatus (int statusCode, QByteArray description=QByteArray())
 Set status code and description. More...
 
int getStatusCode () const
 Return the status code. More...
 
void write (QByteArray data, 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). More...
 
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 35 of file httpresponse.h.

Constructor & Destructor Documentation

HttpResponse::HttpResponse ( QTcpSocket *  socket)

Constructor.

Parameters
socketused to write the response

Member Function Documentation

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.

QMap<QByteArray,HttpCookie>& HttpResponse::getCookies ( )

Get the map of cookies.

QMap<QByteArray,QByteArray>& HttpResponse::getHeaders ( )

Get the map of HTTP response headers.

int HttpResponse::getStatusCode ( ) const

Return the status code.

bool HttpResponse::hasHeader ( const QByteArray  name) const

Returns wether this header has already been set.

bool HttpResponse::hasSentLastPart ( ) const

Indicates whether the body has been sent completely (write() has been called with lastPart=true).

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.

void HttpResponse::redirect ( const QByteArray &  url)

Send a redirect response to the browser.

Cannot be combined with write().

Parameters
urlDestination URL
void HttpResponse::setCookie ( const HttpCookie cookie)

Set a cookie.

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

void HttpResponse::setHeader ( QByteArray  name,
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
void HttpResponse::setHeader ( QByteArray  name,
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
void HttpResponse::setStatus ( int  statusCode,
QByteArray  description = QByteArray() 
)

Set status code and description.

The default is 200,OK. You must call this method before the first write().

void HttpResponse::write ( QByteArray  data,
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.

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