cutelyst
4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
|
A Cutelyst response. More...
#include <Cutelyst/Response>
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 , MultiStatus , 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 Types inherited from QIODeviceBase | |
enum | OpenModeFlag |
Public Member Functions | |
virtual | ~Response () override |
QByteArray & | body () |
QIODevice * | bodyDevice () const noexcept |
QByteArray | contentEncoding () const noexcept |
qint64 | contentLength () const |
QByteArray | contentType () const |
QByteArray | contentTypeCharset () const |
QVariant | cookie (const QByteArray &name) const |
QList< QNetworkCookie > | cookies () const |
bool | hasBody () const noexcept |
QByteArray | header (const QByteArray &field) const noexcept |
Headers & | headers () noexcept |
bool | isFinalizedHeaders () const noexcept |
bool | isSequential () const noexcept override |
QUrl | location () const noexcept |
void | redirect (const QString &url, quint16 status=Found) |
void | redirect (const QUrl &url, quint16 status=Found) |
void | redirectSafe (const QUrl &url, const QUrl &fallback) |
int | removeCookies (const QByteArray &name) |
void | setBody (const QByteArray &body) |
void | setBody (const QString &body) |
void | setBody (QIODevice *body) |
void | setBody (QStringView body) |
void | setCborBody (const QByteArray &cbor) |
void | setCborValueBody (const QCborValue &value) |
void | setContentEncoding (const QByteArray &encoding) |
void | setContentLength (qint64 length) |
void | setContentType (const QByteArray &type) |
void | setCookie (const QNetworkCookie &cookie) |
void | setCookies (const QList< QNetworkCookie > &cookies) |
void | setHeader (const QByteArray &key, const QByteArray &value) |
void | setJsonArrayBody (const QJsonArray &array) |
void | setJsonBody (const QByteArray &json) |
void | setJsonBody (QStringView json) |
void | setJsonObjectBody (const QJsonObject &obj) |
void | setStatus (quint16 status) noexcept |
qint64 | size () const noexcept override |
quint16 | status () const noexcept |
bool | webSocketBinaryMessage (const QByteArray &message) |
bool | webSocketClose (quint16 code=Response::CloseCodeNormal, const QString &reason={}) |
bool | webSocketHandshake (const QByteArray &key={}, const QByteArray &origin={}, const QByteArray &protocol={}) |
bool | webSocketPing (const QByteArray &payload={}) |
bool | webSocketTextMessage (const QString &message) |
Public Member Functions inherited from QIODevice | |
QIODevice (QObject *parent) | |
void | aboutToClose () |
virtual bool | atEnd () const const |
virtual qint64 | bytesAvailable () const const |
virtual qint64 | bytesToWrite () const const |
void | bytesWritten (qint64 bytes) |
virtual bool | canReadLine () const const |
void | channelBytesWritten (int channel, qint64 bytes) |
void | channelReadyRead (int channel) |
virtual void | close () |
void | commitTransaction () |
int | currentReadChannel () const const |
int | currentWriteChannel () const const |
QString | errorString () const const |
bool | getChar (char *c) |
bool | isOpen () const const |
bool | isReadable () const const |
bool | isTextModeEnabled () const const |
bool | isTransactionStarted () const const |
bool | isWritable () const const |
virtual bool | open (QIODeviceBase::OpenMode mode) |
QIODeviceBase::OpenMode | openMode () const const |
qint64 | peek (char *data, qint64 maxSize) |
QByteArray | peek (qint64 maxSize) |
virtual qint64 | pos () const const |
bool | putChar (char c) |
qint64 | read (char *data, qint64 maxSize) |
QByteArray | read (qint64 maxSize) |
QByteArray | readAll () |
int | readChannelCount () const const |
void | readChannelFinished () |
qint64 | readLine (char *data, qint64 maxSize) |
QByteArray | readLine (qint64 maxSize) |
void | readyRead () |
virtual bool | reset () |
void | rollbackTransaction () |
virtual bool | seek (qint64 pos) |
void | setCurrentReadChannel (int channel) |
void | setCurrentWriteChannel (int channel) |
void | setTextModeEnabled (bool enabled) |
qint64 | skip (qint64 maxSize) |
void | startTransaction () |
void | ungetChar (char c) |
virtual bool | waitForBytesWritten (int msecs) |
virtual bool | waitForReadyRead (int msecs) |
qint64 | write (const char *data) |
qint64 | write (const char *data, qint64 maxSize) |
qint64 | write (const QByteArray &data) |
int | writeChannelCount () const const |
Public Member Functions inherited from QObject | |
QObject (QObject *parent) | |
QBindable< QString > | bindableObjectName () |
bool | blockSignals (bool block) |
const QObjectList & | children () const const |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const |
void | deleteLater () |
void | destroyed (QObject *obj) |
bool | disconnect (const char *signal, const QObject *receiver, const char *method) const const |
bool | disconnect (const QObject *receiver, const char *method) const const |
void | dumpObjectInfo () const const |
void | dumpObjectTree () const const |
QList< QByteArray > | dynamicPropertyNames () const const |
virtual bool | event (QEvent *e) |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
T | findChild (const QString &name, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QString &name, Qt::FindChildOptions options) const const |
QList< T > | findChildren (Qt::FindChildOptions options) const const |
bool | inherits (const char *className) const const |
void | installEventFilter (QObject *filterObj) |
bool | isQuickItemType () const const |
bool | isWidgetType () const const |
bool | isWindowType () const const |
void | killTimer (int id) |
virtual const QMetaObject * | metaObject () const const |
void | moveToThread (QThread *targetThread) |
QString | objectName () const const |
void | objectNameChanged (const QString &objectName) |
QObject * | parent () const const |
QVariant | property (const char *name) const const |
Q_CLASSINFO (Name, Value) | |
Q_EMIT Q_EMIT | |
Q_ENUM (...) | |
Q_ENUM_NS (...) | |
Q_ENUMS (...) | |
Q_FLAG (...) | |
Q_FLAG_NS (...) | |
Q_FLAGS (...) | |
Q_GADGET Q_GADGET | |
Q_GADGET_EXPORT (EXPORT_MACRO) | |
Q_INTERFACES (...) | |
Q_INVOKABLE Q_INVOKABLE | |
Q_MOC_INCLUDE Q_MOC_INCLUDE | |
Q_NAMESPACE Q_NAMESPACE | |
Q_NAMESPACE_EXPORT (EXPORT_MACRO) | |
Q_OBJECT Q_OBJECT | |
Q_PROPERTY (...) | |
Q_REVISION Q_REVISION | |
Q_SET_OBJECT_NAME (Object) | |
Q_SIGNAL Q_SIGNAL | |
Q_SIGNALS Q_SIGNALS | |
Q_SLOT Q_SLOT | |
Q_SLOTS Q_SLOTS | |
T | qobject_cast (const QObject *object) |
T | qobject_cast (QObject *object) |
QT_NO_NARROWING_CONVERSIONS_IN_CONNECT QT_NO_NARROWING_CONVERSIONS_IN_CONNECT | |
void | removeEventFilter (QObject *obj) |
void | setObjectName (const QString &name) |
void | setObjectName (QAnyStringView name) |
void | setParent (QObject *parent) |
bool | setProperty (const char *name, const QVariant &value) |
bool | setProperty (const char *name, QVariant &&value) |
bool | signalsBlocked () const const |
int | startTimer (int interval, Qt::TimerType timerType) |
int | startTimer (std::chrono::milliseconds interval, Qt::TimerType timerType) |
QThread * | thread () const const |
Protected Member Functions | |
Response (const Headers &defaultHeaders, EngineRequest *conn=nullptr) | |
virtual qint64 | readData (char *data, qint64 maxlen) override |
virtual qint64 | writeData (const char *data, qint64 len) override |
Protected Member Functions inherited from QIODevice | |
virtual qint64 | readLineData (char *data, qint64 maxSize) |
void | setErrorString (const QString &str) |
void | setOpenMode (QIODeviceBase::OpenMode openMode) |
virtual qint64 | skipData (qint64 maxSize) |
Protected Member Functions inherited from QObject | |
virtual void | childEvent (QChildEvent *event) |
virtual void | connectNotify (const QMetaMethod &signal) |
virtual void | customEvent (QEvent *event) |
virtual void | disconnectNotify (const QMetaMethod &signal) |
bool | isSignalConnected (const QMetaMethod &signal) const const |
int | receivers (const char *signal) const const |
QObject * | sender () const const |
int | senderSignalIndex () const const |
virtual void | timerEvent (QTimerEvent *event) |
Friends | |
class | Application |
class | Context |
class | ContextPrivate |
class | Engine |
class | EngineConnection |
Additional Inherited Members | |
Static Public Member Functions inherited from QObject | |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, Functor functor) |
bool | disconnect (const QMetaObject::Connection &connection) |
bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method) |
bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method) |
bool | disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method) |
QString | tr (const char *sourceText, const char *disambiguation, int n) |
Public Attributes inherited from QObject | |
typedef | QObjectList |
Public Attributes inherited from QIODeviceBase | |
Append | |
ExistingOnly | |
NewOnly | |
NotOpen | |
typedef | OpenMode |
ReadOnly | |
ReadWrite | |
Text | |
Truncate | |
Unbuffered | |
WriteOnly | |
Properties inherited from QObject | |
objectName | |
A Cutelyst response contains the data created by your application that should be send back to the requesting client.
Definition at line 28 of file response.h.
This enum type specifies the status response to be sent to the client
Definition at line 82 of file response.h.
This enum type specifies the status response to be sent to the client
Definition at line 34 of file response.h.
|
overridevirtual |
Destroys the Response object.
Definition at line 60 of file response.cpp.
|
explicitprotected |
Constructs a Response object, for engine request conn with defaultHeaders.
Definition at line 19 of file response.cpp.
References QIODevice::open(), and QIODeviceBase::WriteOnly.
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 85 of file response.cpp.
Referenced by Cutelyst::EngineRequest::finalizeBody(), and setBody().
|
noexcept |
Returns the body IO device (if any) of this response.
Definition at line 97 of file response.cpp.
Referenced by Cutelyst::EngineRequest::finalizeBody().
|
noexcept |
Short for headers().contentEncoding();
Definition at line 154 of file response.cpp.
qint64 Response::contentLength | ( | ) | const |
Short for headers().contentLength();
Definition at line 171 of file response.cpp.
QByteArray Response::contentType | ( | ) | const |
Short for headers().contentType();
Definition at line 188 of file response.cpp.
Referenced by Cutelyst::RenderView::doExecute().
QByteArray Response::contentTypeCharset | ( | ) | const |
Short for headers().contentTypeCharset();
Definition at line 194 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 200 of file response.cpp.
References QVariant::fromValue().
Referenced by setCookie(), and setCookies().
QList< QNetworkCookie > Response::cookies | ( | ) | const |
Returns a list of all cookies set.
Definition at line 206 of file response.cpp.
Referenced by Cutelyst::EngineRequest::finalizeCookies(), and setCookies().
|
noexcept |
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 78 of file response.cpp.
Referenced by Cutelyst::RenderView::doExecute().
|
noexcept |
|
noexcept |
Returns a reference to the response headers class.
Referenced by Cutelyst::EngineRequest::finalizeCookies(), and Cutelyst::EngineRequest::finalizeHeaders().
|
noexcept |
Returns true
if Headers are finalized (sent to the client).
|
overridevirtualnoexcept |
Writing to user-agent is always sequential.
Reimplemented from QIODevice.
|
noexcept |
Returns the HTTP location set by the redirect
Referenced by redirect().
|
overrideprotectedvirtual |
Reimplemented from QIODevice::readData().
Implements QIODevice.
Definition at line 25 of file response.cpp.
void Cutelyst::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.
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.
Definition at line 232 of file response.cpp.
References QUrl::FullyEncoded, QUrl::isValid(), location(), setBody(), status(), and QUrl::toEncoded().
Open Redirect Vulnerability is when you get an user provided URL and redirect to it without checking if it's safe.
This can be used on login forms that receive some "redir" parameter that once logged in allows the user to go straight to that page instead of some home page.
It's then possible to receive a link like http://example.com/login?redir=http://exemple.com/login notice how both domain names are similar for malicious porpuses, once logged in it redirects to a similar login page that will pretent the auth didn't work, user might then type their credentials on that page.
This method validades that the url scheme, domain name and port are the same of the request to your server if it isn't it will send the user to fallback
url that you know it's safe. If you need to redirect the user to some other domain/port validate the URL manually an use the regular
int Response::removeCookies | ( | const QByteArray & | name | ) |
Removes all cookies that matches name, returning the number of cookies removed
Definition at line 226 of file response.cpp.
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 119 of file response.cpp.
References 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 430 of file response.h.
References setBody(), and QString::toUtf8().
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 103 of file response.cpp.
References body().
Referenced by Cutelyst::EngineRequest::finalizeError(), Cutelyst::UA::forwardAsync(), Cutelyst::UA::forwardRequestResponse(), redirect(), Cutelyst::CuteleeView::render(), and setBody().
|
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 435 of file response.h.
References setBody(), and QStringView::toUtf8().
void Response::setCborBody | ( | const QByteArray & | cbor | ) |
Sets a CBOR data as the response body, this method is provided for convenience as it sets the content-type to application/cbor.
Definition at line 125 of file response.cpp.
Referenced by setCborValueBody().
void Response::setCborValueBody | ( | const QCborValue & | value | ) |
Sets a CBOR value without transformation as the response body, this method is provided for convenience as it sets the content-type to application/json.
Definition at line 132 of file response.cpp.
References setCborBody().
void Cutelyst::Response::setContentEncoding | ( | const QByteArray & | encoding | ) |
Short for headers().setContentEncoding(encoding);
Definition at line 160 of file response.cpp.
void Response::setContentLength | ( | qint64 | length | ) |
Short for headers().setContentLength(length);
Definition at line 177 of file response.cpp.
|
inline |
Short for headers().setContentType(type);
Definition at line 238 of file response.h.
Referenced by Cutelyst::RenderView::doExecute(), Cutelyst::EngineRequest::finalizeError(), and Cutelyst::ViewJson::render().
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 212 of file response.cpp.
References cookie().
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 218 of file response.cpp.
void Cutelyst::Response::setHeader | ( | const QByteArray & | key, |
const QByteArray & | value | ||
) |
Shortcut headers().setHeader()
Referenced by Cutelyst::ViewJson::render().
void Response::setJsonArrayBody | ( | const QJsonArray & | array | ) |
Sets a QJsonArray array on a QJsonDocument as the response body, using toJson(QJsonDocument::Compact) output and setting content-type to application/json.
Definition at line 149 of file response.cpp.
References QJsonDocument::Compact, and setJsonBody().
void Response::setJsonBody | ( | const QByteArray & | json | ) |
Sets a JSON string as the response body, this method is provided for convenience as it sets the content-type to application/json.
Definition at line 137 of file response.cpp.
|
inline |
Sets a JSON string as the response body, this method is provided for convenience as it sets the content-type to application/json.
Definition at line 440 of file response.h.
References QStringView::toUtf8().
Referenced by setJsonArrayBody(), and setJsonObjectBody().
void Response::setJsonObjectBody | ( | const QJsonObject & | obj | ) |
Sets a QJsonObject obj on a QJsonDocument as the response body, using toJson(QJsonDocument::Compact) output and setting content-type to application/json.
Definition at line 144 of file response.cpp.
References QJsonDocument::Compact, and setJsonBody().
|
noexcept |
Sets the response code status.
Definition at line 72 of file response.cpp.
Referenced by Cutelyst::RenderView::doExecute(), and Cutelyst::EngineRequest::finalizeError().
|
overridevirtualnoexcept |
Reimplemented from QIODevice::readData().
Reimplemented from QIODevice.
Referenced by Cutelyst::EngineRequest::finalizeHeaders().
|
noexcept |
The current response code status.
Definition at line 66 of file response.cpp.
Referenced by Cutelyst::RenderView::doExecute(), Cutelyst::EngineRequest::finalizeHeaders(), and redirect().
bool Cutelyst::Response::webSocketBinaryMessage | ( | const QByteArray & | message | ) |
Sends a WebSocket binary message.
bool Cutelyst::Response::webSocketClose | ( | quint16 | code = Response::CloseCodeNormal , |
const QString & | reason = {} |
||
) |
Sends a WebSocket close frame, with both optional close code and a string reason.
bool Cutelyst::Response::webSocketHandshake | ( | const QByteArray & | key = {} , |
const QByteArray & | origin = {} , |
||
const QByteArray & | protocol = {} |
||
) |
Sends the websocket handshake, if no parameters are defined it will use header data. Returns true in case of success, false otherwise, which can be due missing support on the engine or missing the appropriate headers.
bool Cutelyst::Response::webSocketPing | ( | const QByteArray & | payload = {} | ) |
Sends a WebSocket ping with an optional payload limited to 125 bytes, which will be truncated if larger.
bool Cutelyst::Response::webSocketTextMessage | ( | const QString & | message | ) |
Sends a WebSocket text message.
|
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)
Implements QIODevice.
Definition at line 32 of file response.cpp.