cutelyst 3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
response.h
1/*
2 * SPDX-FileCopyrightText: (C) 2013-2022 Daniel Nicoletti <dantti12@gmail.com>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#ifndef CUTELYST_RESPONSE_H
6#define CUTELYST_RESPONSE_H
7
8#include <Cutelyst/cutelyst_global.h>
9#include <Cutelyst/headers.h>
10
11#include <QtCore/QIODevice>
12
13class QNetworkCookie;
14
15namespace Cutelyst {
16
17#if (QT_VERSION < QT_VERSION_CHECK(6, 1, 0))
18class Cookie;
19#endif
20class Context;
21class Engine;
22class EngineRequest;
23class ResponsePrivate;
24class CUTELYST_LIBRARY Response final : public QIODevice
25{
27 Q_DECLARE_PRIVATE(Response)
28public:
31 Continue = 100,
32 SwitchingProtocols = 101,
33 OK = 200,
34 Created = 201,
35 Accepted = 202,
36 NonAuthoritativeInformation = 203,
37 NoContent = 204,
38 ResetContent = 205,
39 PartialContent = 206,
40 MultiStatus = 207,
41 MultipleChoices = 300,
42 MovedPermanently = 301,
43 Found = 302,
44 SeeOther = 303, // Since HTTP/1.1
45 NotModified = 304,
46 UseProxy = 305, // Since HTTP/1.1
47 TemporaryRedirect = 307, // Since HTTP/1.1
48 PermanentRedirect = 308, // Since HTTP/1.1
49 BadRequest = 400,
50 Unauthorized = 401,
51 PaymentRequired = 402,
52 Forbidden = 403,
53 NotFound = 404,
54 MethodNotAllowed = 405,
55 NotAcceptable = 406,
56 ProxyAuthenticationRequired = 407,
57 RequestTimeout = 408,
58 Conflict = 409,
59 Gone = 410,
60 LengthRequired = 411,
61 PreconditionFailed = 412,
62 RequestEntityTooLarge = 413,
63 RequestURITooLong = 414,
64 UnsupportedMediaType = 415,
65 RequestedRangeNotSatisfiable = 416,
66 ExpectationFailed = 417,
67 InternalServerError = 500,
68 NotImplemented = 501,
69 BadGateway = 502,
70 ServiceUnavailable = 503,
71 GatewayTimeout = 504,
72 HTTPVersionNotSupported = 505,
73 BandwidthLimitExceeded = 509
74 };
75 Q_ENUM(HttpStatus)
76
77
78 enum CloseCode {
79 CloseCodeNormal = 1000,
80 CloseCodeGoingAway = 1001,
81 CloseCodeProtocolError = 1002,
82 CloseCodeDatatypeNotSupported = 1003,
83 CloseCodeReserved1004 = 1004,
84 CloseCodeMissingStatusCode = 1005,
85 CloseCodeAbnormalDisconnection = 1006,
86 CloseCodeWrongDatatype = 1007,
87 CloseCodePolicyViolated = 1008,
88 CloseCodeTooMuchData = 1009,
89 CloseCodeMissingExtension = 1010,
90 CloseCodeBadOperation = 1011,
91 CloseCodeTlsHandshakeFailed = 1015
92 };
93 Q_ENUM(CloseCode)
94
95 virtual ~Response() override;
96
100 quint16 status() const noexcept;
101
105 void setStatus(quint16 status) noexcept;
106
112 bool hasBody() const noexcept;
113
120 Q_REQUIRED_RESULT QByteArray &body();
121
125 QIODevice *bodyDevice() const;
126
133 void setBody(QIODevice *body);
134
139 void setBody(const QByteArray &body);
140
145 inline void setBody(const QString &body);
146
151 inline void setBody(QStringView body);
152
158 void setJsonBody(const QJsonDocument &documment);
159
164 void setJsonBody(const QString &json);
165
170 inline void setJsonBody(QStringView json);
171
176 void setJsonBody(const QByteArray &json);
177
183 void setJsonObjectBody(const QJsonObject &object);
184
190 void setJsonArrayBody(const QJsonArray &array);
191
195 QString contentEncoding() const;
196
200 void setContentEncoding(const QString &encoding);
201
205 qint64 contentLength() const;
206
210 void setContentLength(qint64 length);
211
215 QString contentType() const;
216
220 void setContentType(const QString &type) { headers().setContentType(type); }
221
225 QString contentTypeCharset() const;
226
231 QVariant cookie(const QByteArray &name) const;
232
233#if (QT_VERSION < QT_VERSION_CHECK(6, 1, 0))
240 QVariant cuteCookie(const QByteArray &name) const;
241#endif
242
246 QList<QNetworkCookie> cookies() const;
247
248#if (QT_VERSION < QT_VERSION_CHECK(6, 1, 0))
254 QList<Cookie> cuteCookies() const;
255#endif
256
261 void setCookie(const QNetworkCookie &cookie);
262
263#if (QT_VERSION < QT_VERSION_CHECK(6, 1, 0))
270 void setCuteCookie(const Cookie &cookie);
271#endif
272
277 void setCookies(const QList<QNetworkCookie> &cookies);
278
279#if (QT_VERSION < QT_VERSION_CHECK(6, 1, 0))
286 void setCuteCookies(const QList<Cookie> &cookies);
287#endif
288
293 int removeCookies(const QByteArray &name);
294
295#if (QT_VERSION < QT_VERSION_CHECK(6, 1, 0))
302 int removeCuteCookies(const QByteArray &name);
303#endif
304
317 void redirect(const QUrl &url, quint16 status = Found);
318
331 void redirect(const QString &url, quint16 status = Found);
332
350 void redirectSafe(const QUrl &url, const QUrl &fallback);
351
355 QUrl location() const noexcept;
356
360 QString header(const QString &field) const;
361
365 void setHeader(const QString &field, const QString &value);
366
370 Headers &headers() noexcept;
371
375 bool isFinalizedHeaders() const noexcept;
376
380 virtual bool isSequential() const noexcept override;
381
385 virtual qint64 size() const noexcept override;
386
397 bool webSocketHandshake(const QString &key = {},
398 const QString &origin = {},
399 const QString &protocol = {});
400
404 bool webSocketTextMessage(const QString &message);
405
409 bool webSocketBinaryMessage(const QByteArray &message);
410
419 bool webSocketPing(const QByteArray &payload = {});
420
428 bool webSocketClose(quint16 code = Response::CloseCodeNormal, const QString &reason = {});
429
430protected:
434 explicit Response(const Headers &defaultHeaders, EngineRequest *conn = nullptr);
435
443 virtual qint64 writeData(const char *data, qint64 len) override;
444
448 virtual qint64 readData(char *data, qint64 maxlen) override;
449
450 ResponsePrivate *d_ptr;
451 friend class Application;
452 friend class Engine;
453 friend class EngineConnection;
454 friend class Context;
455 friend class ContextPrivate;
456};
457
458inline void Response::setBody(const QString &_body)
459{
460 setBody(_body.toUtf8());
461}
462
464{
465 setBody(_body.toUtf8());
466}
467
469{
470 setJsonBody(_body.toUtf8());
471}
472
473} // namespace Cutelyst
474
475#endif // CUTELYST_RESPONSE_H
The Cutelyst Context.
Definition context.h:39
The Cutelyst Cookie.
Definition cookie.h:29
The Cutelyst Engine.
Definition engine.h:21
Headers & headers() noexcept
Definition response.cpp:339
Response(const Headers &defaultHeaders, EngineRequest *conn=nullptr)
Definition response.cpp:17
void setBody(QIODevice *body)
Definition response.cpp:100
void setJsonBody(const QJsonDocument &documment)
Definition response.cpp:120
void setContentType(const QString &type)
Definition response.h:220
The Cutelyst namespace holds all public Cutelyst API.
Definition Mainpage.dox:8
Q_OBJECTQ_OBJECT
QByteArray toUtf8() const const
QByteArray toUtf8() const const