Cutelyst  1.11.0
Public Types | Public Member Functions | Protected Member Functions | Friends | List of all members
Cutelyst::Response Class Reference
Inheritance diagram for Cutelyst::Response:
Inheritance graph
[legend]

Public Types

enum  CloseCode {
  CloseCodeNormal, CloseCodeGoingAway, CloseCodeProtocolError, CloseCodeDatatypeNotSupported,
  CloseCodeReserved1004, CloseCodeMissingStatusCode, CloseCodeAbnormalDisconnection, CloseCodeWrongDatatype,
  CloseCodePolicyViolated, CloseCodeTooMuchData, CloseCodeMissingExtension, CloseCodeBadOperation,
  CloseCodeTlsHandshakeFailed
}
 
enum  HttpStatus {
  Continue, SwitchingProtocols, OK, Created,
  Accepted, NonAuthoritativeInformation, NoContent, ResetContent,
  PartialContent, MultipleChoices, MovedPermanently, Found,
  SeeOther, NotModified, UseProxy, TemporaryRedirect,
  PermanentRedirect, BadRequest, Unauthorized, PaymentRequired,
  Forbidden, NotFound, MethodNotAllowed, NotAcceptable,
  ProxyAuthenticationRequired, RequestTimeout, Conflict, Gone,
  LengthRequired, PreconditionFailed, RequestEntityTooLarge, RequestURITooLong,
  UnsupportedMediaType, RequestedRangeNotSatisfiable, ExpectationFailed, InternalServerError,
  NotImplemented, BadGateway, ServiceUnavailable, GatewayTimeout,
  HTTPVersionNotSupported, BandwidthLimitExceeded
}
 

Public Member Functions

Q_REQUIRED_RESULT QByteArray & body ()
 
QIODevice * bodyDevice () const
 
QString contentEncoding () const
 
qint64 contentLength () const
 
QString contentType () const
 
QString contentTypeCharset () const
 
QVariant cookie (const QByteArray &name) const
 
QList< QNetworkCookie > cookies () const
 
bool hasBody () const
 
QString header (const QString &field) const
 
Headersheaders ()
 
virtual bool isSequential () const override
 
QUrl location () const
 
void redirect (const QUrl &url, quint16 status=Found)
 
void redirect (const QString &url, quint16 status=Found)
 
int removeCookies (const QByteArray &name)
 
void setBody (QIODevice *body)
 
void setBody (const QByteArray &body)
 
void setBody (const QString &body)
 
void setContentEncoding (const QString &encoding)
 
void setContentLength (qint64 length)
 
void setContentType (const QString &type)
 
void setCookie (const QNetworkCookie &cookie)
 
void setCookies (const QList< QNetworkCookie > &cookies)
 
void setHeader (const QString &field, const QString &value)
 
void setJsonBody (const QJsonDocument &documment)
 
void setJsonBody (const QJsonObject &object)
 
void setJsonBody (const QJsonArray &array)
 
void setStatus (quint16 status)
 
virtual qint64 size () const override
 
quint16 status () const
 
bool webSocketBinaryMessage (const QByteArray &message)
 Sends a WebSocket binary message.
 
bool webSocketClose (quint16 code=Response::CloseCodeNormal, const QString &reason=QString())
 Sends a WebSocket close frame, with both optional close code and a string reason.
 
bool webSocketHandshake (const QString &key=QString(), const QString &origin=QString(), const QString &protocol=QString())
 Sends the websocket handshake, if no parameters are defined it will use header data. More...
 
bool webSocketPing (const QByteArray &payload=QByteArray())
 Sends a WebSocket ping with an optional payload limited to 125 bytes, which will be truncated if larger.
 
bool webSocketTextMessage (const QString &message)
 Sends a WebSocket text message.
 

Protected Member Functions

 Response (Context *c, Engine *engine, const Headers &defaultHeaders)
 
 Response (Context *c, EngineRequest *conn, const Headers &defaultHeaders)
 
virtual qint64 readData (char *data, qint64 maxlen) override
 
virtual qint64 writeData (const char *data, qint64 len) override
 

Friends

class Application
 
class Context
 
class ContextPrivate
 
class Engine
 
class EngineConnection
 

Detailed Description

Definition at line 34 of file response.h.

Member Enumeration Documentation

This enum type specifies the status response to be sent to the client

Definition at line 87 of file response.h.

This enum type specifies the status response to be sent to the client

Definition at line 40 of file response.h.

Constructor & Destructor Documentation

Response::Response ( Context c,
Engine engine,
const Headers defaultHeaders 
)
explicitprotected

Constructs a Response object, for this context c, engine and defaultHeaders.

Definition at line 32 of file response.cpp.

Member Function Documentation

QByteArray & Response::body ( )

This function returns a reference to a QByteArray which implicity sets the body device to a QBuffer, even if one was already set.

Definition at line 102 of file response.cpp.

Referenced by Cutelyst::ActionREST::doExecute(), Cutelyst::EngineRequest::finalizeBody(), setBody(), setJsonBody(), and webSocketClose().

QIODevice * Response::bodyDevice ( ) const

Returns the body IO device (if any) of this response.

Definition at line 113 of file response.cpp.

Referenced by Cutelyst::EngineRequest::finalizeBody().

QString Response::contentEncoding ( ) const

Short for headers().contentEncoding();

Definition at line 162 of file response.cpp.

qint64 Response::contentLength ( ) const

Short for headers().contentLength();

Definition at line 178 of file response.cpp.

QString Response::contentType ( ) const

Short for headers().contentType();

Definition at line 194 of file response.cpp.

Referenced by Cutelyst::RenderView::doExecute().

QString Response::contentTypeCharset ( ) const

Short for headers().contentTypeCharset();

Definition at line 200 of file response.cpp.

QVariant Response::cookie ( const QByteArray &  name) const

Returns the first QNetworkCookie matching the name or a null QVariant if not found

Definition at line 206 of file response.cpp.

Referenced by setCookie(), and setCookies().

QList< QNetworkCookie > Response::cookies ( ) const

Returns a list of all cookies set

Definition at line 212 of file response.cpp.

Referenced by Cutelyst::EngineRequest::finalizeCookies(), and Cutelyst::Engine::finalizeCookies().

bool Response::hasBody ( ) const

Returns true if a body device has been defined as QByteArray or QIODevice or write() was called and it's on chunked mode

Definition at line 96 of file response.cpp.

Referenced by Cutelyst::RenderView::doExecute(), and Cutelyst::View::modifiers().

QString Response::header ( const QString &  field) const

Shortcut headers().header()

Definition at line 281 of file response.cpp.

Headers & Response::headers ( )
bool Response::isSequential ( ) const
overridevirtual

Writting to user-agent is always sequential

Definition at line 303 of file response.cpp.

QUrl Response::location ( ) const

Returns the HTTP location set by the redirect

Definition at line 275 of file response.cpp.

Referenced by redirect().

qint64 Response::readData ( char *  data,
qint64  maxlen 
)
overrideprotectedvirtual

Reimplemented from QIODevice::readData().

Definition at line 44 of file response.cpp.

void Response::redirect ( const QUrl &  url,
quint16  status = Found 
)

Causes the response to redirect to the specified URL. The default status is 302. This is a convenience method that sets the Location header to the redirect destination, and then sets the response status. You will want to return false or c->detach() to interrupt the normal processing flow if you want the redirect to occur straight away.

Note
do not give a relative URL as $url, i.e: one that is not fully qualified ("http://...", etc.) or that starts with a slash "/path/here". While it may work, it is not guaranteed to do the right thing and is not a standard behaviour. You may opt to use uriFor() or uriForAction() instead.

Definition at line 238 of file response.cpp.

References location(), setBody(), and status().

Referenced by redirect().

void Response::redirect ( const QString &  url,
quint16  status = Found 
)

Causes the response to redirect to the specified URL. The default status is 302. This is a convenience method that sets the Location header to the redirect destination, and then sets the response status. You will want to return false or c->detach() to interrupt the normal processing flow if you want the redirect to occur straight away.

Note
do not give a relative URL as $url, i.e: one that is not fully qualified ("http://...", etc.) or that starts with a slash "/path/here". While it may work, it is not guaranteed to do the right thing and is not a standard behaviour. You may opt to use uriFor() or uriForAction() instead.

Definition at line 270 of file response.cpp.

References redirect().

int Response::removeCookies ( const QByteArray &  name)

Removes all cookies that matches name, returning the number of cookies removed

Definition at line 232 of file response.cpp.

void Response::setBody ( QIODevice *  body)

Sets an IO device as the response body, the open mode must be at least QIODevice::ReadOnly. This function takes ownership of your device deleting after the request has completed

Definition at line 119 of file response.cpp.

References body().

Referenced by Cutelyst::CredentialHttp::authenticate(), Cutelyst::ActionREST::doExecute(), Cutelyst::EngineRequest::finalizeError(), Cutelyst::Engine::finalizeError(), Cutelyst::View::modifiers(), redirect(), Cutelyst::ClearSilver::render(), Cutelyst::GrantleeView::render(), Cutelyst::StaticSimple::setup(), and Cutelyst::StaticCompressed::setup().

void Response::setBody ( const QByteArray &  body)

Sets a QByteArray as the response body, content length will be automatically set to it's size.

Definition at line 132 of file response.cpp.

void Cutelyst::Response::setBody ( const QString &  body)
inline

Sets a QString as the response body, the output will be UTF-8 and content length will be automatically set to it's size.

Definition at line 359 of file response.h.

void Cutelyst::Response::setContentEncoding ( const QString &  encoding)

Short for headers().setContentEncoding(encoding);

Definition at line 168 of file response.cpp.

void Response::setContentLength ( qint64  length)

Short for headers().setContentLength(length);

Definition at line 184 of file response.cpp.

void Cutelyst::Response::setContentType ( const QString &  type)
inline
void Response::setCookie ( const QNetworkCookie &  cookie)

Defines a QNetworkCookie to be sent to the user-agent, if a previous cookie->name() was set it will be replaced

Definition at line 218 of file response.cpp.

References cookie().

Referenced by Cutelyst::Session::isValid().

void Response::setCookies ( const QList< QNetworkCookie > &  cookies)

Defines a list of QNetworkCookie to be sent to the user-agent, all previous matches to cookie->name() will be preserved.

Definition at line 224 of file response.cpp.

References cookie().

void Response::setHeader ( const QString &  field,
const QString &  value 
)

Shortcut headers().setHeader()

Definition at line 287 of file response.cpp.

Referenced by Cutelyst::ActionREST::doExecute(), and Cutelyst::ViewJson::render().

void Response::setJsonBody ( const QJsonDocument &  documment)

Sets a QJsonDocument as the response body, using toJson(QJsonDocument::Compact) output and setting content-type to application/json.

Definition at line 138 of file response.cpp.

References body().

void Response::setJsonBody ( const QJsonObject &  object)

Sets a QJsonObject on a QJsonDocument as the response body, using toJson(QJsonDocument::Compact) output and setting content-type to application/json.

Definition at line 146 of file response.cpp.

References body().

void Response::setJsonBody ( const QJsonArray &  array)

Sets a QJsonArray on a QJsonDocument as the response body, using toJson(QJsonDocument::Compact) output and setting content-type to application/json.

Definition at line 154 of file response.cpp.

References body().

void Response::setStatus ( quint16  status)
qint64 Response::size ( ) const
overridevirtual

Reimplemented from QIODevice::readData().

Definition at line 308 of file response.cpp.

Referenced by Cutelyst::EngineRequest::finalizeHeaders().

quint16 Response::status ( ) const

The current response code status

Definition at line 84 of file response.cpp.

Referenced by Cutelyst::RenderView::doExecute(), Cutelyst::EngineRequest::finalizeHeaders(), redirect(), and setStatus().

bool Response::webSocketHandshake ( const QString &  key = QString(),
const QString &  origin = QString(),
const QString &  protocol = QString() 
)

Returns true in case of success, false otherwise, which can be due missing support on the engine or missing the appropriate headers.

Definition at line 320 of file response.cpp.

Referenced by Cutelyst::Engine::finalize().

qint64 Response::writeData ( const char *  data,
qint64  len 
)
overrideprotectedvirtual

Writes data to the response body, this will flush all response headers first and will enter in chunked mode if Transfer-Encoding header is set to chunked or if no content length is set and status is not 1xx or 204 (NoContent) or 304 (NotModified)

Definition at line 51 of file response.cpp.