cutelyst  5.0.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-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 
12 class QNetworkCookie;
13 
14 namespace Cutelyst {
15 
16 class Context;
17 class Engine;
18 class EngineRequest;
19 class ResponsePrivate;
28 class CUTELYST_EXPORT Response final : public QIODevice
29 {
30  Q_OBJECT
31  Q_DECLARE_PRIVATE(Response)
32 public:
34  enum HttpStatus {
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 
340  bool isFinalized() const noexcept;
341 
345  bool isFinalizedHeaders() const noexcept;
346 
350  bool isSequential() const noexcept override;
351 
355  qint64 size() const noexcept override;
356 
367  bool webSocketHandshake(const QByteArray &key = {},
368  const QByteArray &origin = {},
369  const QByteArray &protocol = {});
370 
374  bool webSocketTextMessage(const QString &message);
375 
379  bool webSocketBinaryMessage(const QByteArray &message);
380 
389  bool webSocketPing(const QByteArray &payload = {});
390 
398  bool webSocketClose(quint16 code = Response::CloseCodeNormal, const QString &reason = {});
399 
400 protected:
404  explicit Response(const Headers &defaultHeaders, EngineRequest *conn = nullptr);
405 
413  virtual qint64 writeData(const char *data, qint64 len) override;
414 
418  virtual qint64 readData(char *data, qint64 maxlen) override;
419 
420  ResponsePrivate *d_ptr;
421  friend class Application;
422  friend class Engine;
423  friend class EngineConnection;
424  friend class Context;
425  friend class ContextPrivate;
426 };
427 
428 inline void Response::setBody(const QString &_body)
429 {
430  setBody(_body.toUtf8());
431 }
432 
433 inline void Response::setBody(QStringView _body)
434 {
435  setBody(_body.toUtf8());
436 }
437 
439 {
440  setJsonBody(_body.toUtf8());
441 }
442 
443 } // namespace Cutelyst
void setJsonBody(QStringView json)
Definition: response.h:438
Container for HTTP headers.
Definition: headers.h:23
A Cutelyst response.
Definition: response.h:28
The Cutelyst namespace holds all public Cutelyst API.
QByteArray toUtf8() const const
void setBody(QIODevice *body)
Definition: response.cpp:105
QByteArray toUtf8() const const