cutelyst  4.4.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 
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 
217  qint64 contentLength() const;
218 
224  void setContentLength(qint64 length);
225 
231  QByteArray contentType() const;
232 
238  void setContentType(const QByteArray &type) { headers().setContentType(type); }
239 
244  QByteArray contentTypeCharset() const;
245 
250  QVariant cookie(const QByteArray &name) const;
251 
255  QList<QNetworkCookie> cookies() const;
256 
261  void setCookie(const QNetworkCookie &cookie);
262 
267  void setCookies(const QList<QNetworkCookie> &cookies);
268 
273  int removeCookies(const QByteArray &name);
274 
287  void redirect(const QUrl &url, quint16 status = Found);
288 
301  void redirect(const QString &url, quint16 status = Found);
302 
320  void redirectSafe(const QUrl &url, const QUrl &fallback);
321 
325  QUrl location() const noexcept;
326 
331  QByteArray header(const QByteArray &field) const noexcept;
332 
337  void setHeader(const QByteArray &key, const QByteArray &value);
338 
342  Headers &headers() noexcept;
343 
347  bool isFinalizedHeaders() const noexcept;
348 
352  bool isSequential() const noexcept override;
353 
357  qint64 size() const noexcept override;
358 
369  bool webSocketHandshake(const QByteArray &key = {},
370  const QByteArray &origin = {},
371  const QByteArray &protocol = {});
372 
376  bool webSocketTextMessage(const QString &message);
377 
381  bool webSocketBinaryMessage(const QByteArray &message);
382 
391  bool webSocketPing(const QByteArray &payload = {});
392 
400  bool webSocketClose(quint16 code = Response::CloseCodeNormal, const QString &reason = {});
401 
402 protected:
406  explicit Response(const Headers &defaultHeaders, EngineRequest *conn = nullptr);
407 
415  virtual qint64 writeData(const char *data, qint64 len) override;
416 
420  virtual qint64 readData(char *data, qint64 maxlen) override;
421 
422  ResponsePrivate *d_ptr;
423  friend class Application;
424  friend class Engine;
425  friend class EngineConnection;
426  friend class Context;
427  friend class ContextPrivate;
428 };
429 
430 inline void Response::setBody(const QString &_body)
431 {
432  setBody(_body.toUtf8());
433 }
434 
435 inline void Response::setBody(QStringView _body)
436 {
437  setBody(_body.toUtf8());
438 }
439 
441 {
442  setJsonBody(_body.toUtf8());
443 }
444 
445 } // 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:103
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:440
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