cutelyst 4.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_global.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;
20class CUTELYST_LIBRARY Response final : public QIODevice
21{
22 Q_OBJECT
23 Q_DECLARE_PRIVATE(Response)
24public:
27 Continue = 100,
28 SwitchingProtocols = 101,
29 OK = 200,
30 Created = 201,
31 Accepted = 202,
32 NonAuthoritativeInformation = 203,
33 NoContent = 204,
34 ResetContent = 205,
35 PartialContent = 206,
36 MultiStatus = 207,
37 MultipleChoices = 300,
38 MovedPermanently = 301,
39 Found = 302,
40 SeeOther = 303, // Since HTTP/1.1
41 NotModified = 304,
42 UseProxy = 305, // Since HTTP/1.1
43 TemporaryRedirect = 307, // Since HTTP/1.1
44 PermanentRedirect = 308, // Since HTTP/1.1
45 BadRequest = 400,
46 Unauthorized = 401,
47 PaymentRequired = 402,
48 Forbidden = 403,
49 NotFound = 404,
50 MethodNotAllowed = 405,
51 NotAcceptable = 406,
52 ProxyAuthenticationRequired = 407,
53 RequestTimeout = 408,
54 Conflict = 409,
55 Gone = 410,
56 LengthRequired = 411,
57 PreconditionFailed = 412,
58 RequestEntityTooLarge = 413,
59 RequestURITooLong = 414,
60 UnsupportedMediaType = 415,
61 RequestedRangeNotSatisfiable = 416,
62 ExpectationFailed = 417,
63 InternalServerError = 500,
64 NotImplemented = 501,
65 BadGateway = 502,
66 ServiceUnavailable = 503,
67 GatewayTimeout = 504,
68 HTTPVersionNotSupported = 505,
69 BandwidthLimitExceeded = 509
70 };
71 Q_ENUM(HttpStatus)
72
73
74 enum CloseCode {
75 CloseCodeNormal = 1000,
76 CloseCodeGoingAway = 1001,
77 CloseCodeProtocolError = 1002,
78 CloseCodeDatatypeNotSupported = 1003,
79 CloseCodeReserved1004 = 1004,
80 CloseCodeMissingStatusCode = 1005,
81 CloseCodeAbnormalDisconnection = 1006,
82 CloseCodeWrongDatatype = 1007,
83 CloseCodePolicyViolated = 1008,
84 CloseCodeTooMuchData = 1009,
85 CloseCodeMissingExtension = 1010,
86 CloseCodeBadOperation = 1011,
87 CloseCodeTlsHandshakeFailed = 1015
88 };
89 Q_ENUM(CloseCode)
90
91 virtual ~Response() override;
92
96 quint16 status() const noexcept;
97
101 void setStatus(quint16 status) noexcept;
102
108 bool hasBody() const noexcept;
109
116 [[nodiscard]] QByteArray &body();
117
121 QIODevice *bodyDevice() const noexcept;
122
129 void setBody(QIODevice *body);
130
135 void setBody(const QByteArray &body);
136
141 inline void setBody(const QString &body);
142
147 inline void setBody(QStringView body);
148
153 inline void setJsonBody(QStringView json);
154
159 void setJsonBody(const QByteArray &json);
160
166 void setJsonObjectBody(const QJsonObject &obj);
167
173 void setJsonArrayBody(const QJsonArray &array);
174
178 QByteArray contentEncoding() const noexcept;
179
183 void setContentEncoding(const QByteArray &encoding);
184
188 qint64 contentLength() const;
189
193 void setContentLength(qint64 length);
194
198 QByteArray contentType() const;
199
203 void setContentType(const QByteArray &type) { headers().setContentType(type); }
204
208 QByteArray contentTypeCharset() const;
209
214 QVariant cookie(const QByteArray &name) const;
215
219 QList<QNetworkCookie> cookies() const;
220
225 void setCookie(const QNetworkCookie &cookie);
226
231 void setCookies(const QList<QNetworkCookie> &cookies);
232
237 int removeCookies(const QByteArray &name);
238
251 void redirect(const QUrl &url, quint16 status = Found);
252
265 void redirect(const QString &url, quint16 status = Found);
266
284 void redirectSafe(const QUrl &url, const QUrl &fallback);
285
289 QUrl location() const noexcept;
290
294 QByteArray header(const QByteArray &field) const noexcept;
295
299 void setHeader(const QByteArray &key, const QByteArray &value);
300
304 Headers &headers() noexcept;
305
309 bool isFinalizedHeaders() const noexcept;
310
314 bool isSequential() const noexcept override;
315
319 qint64 size() const noexcept override;
320
331 bool webSocketHandshake(const QByteArray &key = {},
332 const QByteArray &origin = {},
333 const QByteArray &protocol = {});
334
338 bool webSocketTextMessage(const QString &message);
339
343 bool webSocketBinaryMessage(const QByteArray &message);
344
353 bool webSocketPing(const QByteArray &payload = {});
354
362 bool webSocketClose(quint16 code = Response::CloseCodeNormal, const QString &reason = {});
363
364protected:
368 explicit Response(const Headers &defaultHeaders, EngineRequest *conn = nullptr);
369
377 virtual qint64 writeData(const char *data, qint64 len) override;
378
382 virtual qint64 readData(char *data, qint64 maxlen) override;
383
384 ResponsePrivate *d_ptr;
385 friend class Application;
386 friend class Engine;
387 friend class EngineConnection;
388 friend class Context;
389 friend class ContextPrivate;
390};
391
392inline void Response::setBody(const QString &_body)
393{
394 setBody(_body.toUtf8());
395}
396
397inline void Response::setBody(QStringView _body)
398{
399 setBody(_body.toUtf8());
400}
401
402inline void Response::setJsonBody(QStringView _body)
403{
404 setJsonBody(_body.toUtf8());
405}
406
407} // namespace Cutelyst
The Cutelyst Application.
Definition: application.h:43
The Cutelyst Context.
Definition: context.h:38
The Cutelyst Engine.
Definition: engine.h:20
bool webSocketTextMessage(const QString &message)
Sends a WebSocket text message.
bool webSocketPing(const QByteArray &payload={})
Sends a WebSocket ping with an optional payload limited to 125 bytes, which will be truncated if larg...
void setBody(QIODevice *body)
Definition: response.cpp:102
void redirectSafe(const QUrl &url, const QUrl &fallback)
bool webSocketBinaryMessage(const QByteArray &message)
Sends a WebSocket binary message.
void redirect(const QString &url, quint16 status=Found)
void setJsonBody(QStringView json)
Definition: response.h:402
bool webSocketClose(quint16 code=Response::CloseCodeNormal, const QString &reason={})
Sends a WebSocket close frame, with both optional close code and a string reason.
QUrl location() const noexcept
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8