cutelyst 5.0.0
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-2023 Daniel Nicoletti <dantti12@gmail.com>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#pragma once
6
7#include <Cutelyst/cutelyst_export.h>
8#include <Cutelyst/headers.h>
9
10#include <QtCore/QIODevice>
11
12class QNetworkCookie;
13
14namespace Cutelyst {
15
16class Context;
17class Engine;
18class EngineRequest;
19class ResponsePrivate;
28class CUTELYST_EXPORT Response final : public QIODevice
29{
30 Q_OBJECT
31 Q_DECLARE_PRIVATE(Response)
32public:
35 Continue = 100,
36 SwitchingProtocols = 101,
37 OK = 200,
38 Created = 201,
39 Accepted = 202,
40 NonAuthoritativeInformation = 203,
41 NoContent = 204,
42 ResetContent = 205,
43 PartialContent = 206,
44 MultiStatus = 207,
45 MultipleChoices = 300,
46 MovedPermanently = 301,
47 Found = 302,
48 SeeOther = 303, // Since HTTP/1.1
49 NotModified = 304,
50 UseProxy = 305, // Since HTTP/1.1
51 TemporaryRedirect = 307, // Since HTTP/1.1
52 PermanentRedirect = 308, // Since HTTP/1.1
53 BadRequest = 400,
54 Unauthorized = 401,
55 PaymentRequired = 402,
56 Forbidden = 403,
57 NotFound = 404,
58 MethodNotAllowed = 405,
59 NotAcceptable = 406,
60 ProxyAuthenticationRequired = 407,
61 RequestTimeout = 408,
62 Conflict = 409,
63 Gone = 410,
64 LengthRequired = 411,
65 PreconditionFailed = 412,
66 RequestEntityTooLarge = 413,
67 RequestURITooLong = 414,
68 UnsupportedMediaType = 415,
69 RequestedRangeNotSatisfiable = 416,
70 ExpectationFailed = 417,
71 InternalServerError = 500,
72 NotImplemented = 501,
73 BadGateway = 502,
74 ServiceUnavailable = 503,
75 GatewayTimeout = 504,
76 HTTPVersionNotSupported = 505,
77 BandwidthLimitExceeded = 509
78 };
79 Q_ENUM(HttpStatus)
80
81
82 enum CloseCode {
83 CloseCodeNormal = 1000,
84 CloseCodeGoingAway = 1001,
85 CloseCodeProtocolError = 1002,
86 CloseCodeDatatypeNotSupported = 1003,
87 CloseCodeReserved1004 = 1004,
88 CloseCodeMissingStatusCode = 1005,
89 CloseCodeAbnormalDisconnection = 1006,
90 CloseCodeWrongDatatype = 1007,
91 CloseCodePolicyViolated = 1008,
92 CloseCodeTooMuchData = 1009,
93 CloseCodeMissingExtension = 1010,
94 CloseCodeBadOperation = 1011,
95 CloseCodeTlsHandshakeFailed = 1015
96 };
97 Q_ENUM(CloseCode)
98
99
102 virtual ~Response() override;
103
107 quint16 status() const noexcept;
108
112 void setStatus(quint16 status) noexcept;
113
119 bool hasBody() const noexcept;
120
127 [[nodiscard]] QByteArray &body();
128
132 QIODevice *bodyDevice() const noexcept;
133
140 void setBody(QIODevice *body);
141
146 void setBody(const QByteArray &body);
147
152 inline void setBody(const QString &body);
153
158 inline void setBody(QStringView body);
159
164 void setCborBody(const QByteArray &cbor);
165
170 void setCborValueBody(const QCborValue &value);
171
176 inline void setJsonBody(QStringView json);
177
182 void setJsonBody(const QByteArray &json);
183
189 void setJsonObjectBody(const QJsonObject &obj);
190
196 void setJsonArrayBody(const QJsonArray &array);
197
203 QByteArray contentEncoding() const noexcept;
204
210 void setContentEncoding(const QByteArray &encoding);
211
216 qint64 contentLength() const;
217
223 QByteArray contentType() const;
224
230 void setContentType(const QByteArray &type) { headers().setContentType(type); }
231
236 QByteArray contentTypeCharset() const;
237
242 QVariant cookie(const QByteArray &name) const;
243
247 QList<QNetworkCookie> cookies() const;
248
253 void setCookie(const QNetworkCookie &cookie);
254
259 void setCookies(const QList<QNetworkCookie> &cookies);
260
265 int removeCookies(const QByteArray &name);
266
279 void redirect(const QUrl &url, quint16 status = Found);
280
293 void redirect(const QString &url, quint16 status = Found);
294
312 void redirectSafe(const QUrl &url, const QUrl &fallback);
313
317 QUrl location() const noexcept;
318
323 QByteArray header(const QByteArray &field) const noexcept;
324
329 void setHeader(const QByteArray &key, const QByteArray &value);
330
334 Headers &headers() noexcept;
335
339 bool isFinalizedHeaders() const noexcept;
340
344 bool isSequential() const noexcept override;
345
349 qint64 size() const noexcept override;
350
361 bool webSocketHandshake(const QByteArray &key = {},
362 const QByteArray &origin = {},
363 const QByteArray &protocol = {});
364
368 bool webSocketTextMessage(const QString &message);
369
373 bool webSocketBinaryMessage(const QByteArray &message);
374
383 bool webSocketPing(const QByteArray &payload = {});
384
392 bool webSocketClose(quint16 code = Response::CloseCodeNormal, const QString &reason = {});
393
394protected:
398 explicit Response(const Headers &defaultHeaders, EngineRequest *conn = nullptr);
399
407 virtual qint64 writeData(const char *data, qint64 len) override;
408
412 virtual qint64 readData(char *data, qint64 maxlen) override;
413
414 ResponsePrivate *d_ptr;
415 friend class Application;
416 friend class Engine;
417 friend class EngineConnection;
418 friend class Context;
419 friend class ContextPrivate;
420};
421
422inline void Response::setBody(const QString &_body)
423{
424 setBody(_body.toUtf8());
425}
426
428{
429 setBody(_body.toUtf8());
430}
431
433{
434 setJsonBody(_body.toUtf8());
435}
436
437} // namespace Cutelyst
The Cutelyst application.
Definition application.h:66
The Cutelyst Context.
Definition context.h:42
The Cutelyst Engine.
Definition engine.h:20
Container for HTTP headers.
Definition headers.h:24
A Cutelyst response.
Definition response.h:29
bool webSocketTextMessage(const QString &message)
bool webSocketPing(const QByteArray &payload={})
void setBody(QIODevice *body)
Definition response.cpp:105
void redirectSafe(const QUrl &url, const QUrl &fallback)
bool webSocketBinaryMessage(const QByteArray &message)
void redirect(const QString &url, quint16 status=Found)
void setJsonBody(QStringView json)
Definition response.h:432
bool webSocketClose(quint16 code=Response::CloseCodeNormal, const QString &reason={})
QUrl location() const noexcept
The Cutelyst namespace holds all public Cutelyst API.
QByteArray toUtf8() const const
QByteArray toUtf8() const const