cutelyst  3.7.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-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 <QtCore/QIODevice>
9 
10 #include <Cutelyst/cutelyst_global.h>
11 #include <Cutelyst/headers.h>
12 
13 class QNetworkCookie;
14 
15 namespace Cutelyst {
16 
17 class Context;
18 class Engine;
19 class EngineRequest;
20 class ResponsePrivate;
21 class CUTELYST_LIBRARY Response final : public QIODevice
22 {
23  Q_OBJECT
24  Q_DECLARE_PRIVATE(Response)
25 public:
27  enum HttpStatus {
28  Continue = 100,
29  SwitchingProtocols = 101,
30  OK = 200,
31  Created = 201,
32  Accepted = 202,
33  NonAuthoritativeInformation = 203,
34  NoContent = 204,
35  ResetContent = 205,
36  PartialContent = 206,
37  MultiStatus = 207,
38  MultipleChoices = 300,
39  MovedPermanently = 301,
40  Found = 302,
41  SeeOther = 303, // Since HTTP/1.1
42  NotModified = 304,
43  UseProxy = 305, // Since HTTP/1.1
44  TemporaryRedirect = 307, // Since HTTP/1.1
45  PermanentRedirect = 308, // Since HTTP/1.1
46  BadRequest = 400,
47  Unauthorized = 401,
48  PaymentRequired = 402,
49  Forbidden = 403,
50  NotFound = 404,
51  MethodNotAllowed = 405,
52  NotAcceptable = 406,
53  ProxyAuthenticationRequired = 407,
54  RequestTimeout = 408,
55  Conflict = 409,
56  Gone = 410,
57  LengthRequired = 411,
58  PreconditionFailed = 412,
59  RequestEntityTooLarge = 413,
60  RequestURITooLong = 414,
61  UnsupportedMediaType = 415,
62  RequestedRangeNotSatisfiable = 416,
63  ExpectationFailed = 417,
64  InternalServerError = 500,
65  NotImplemented = 501,
66  BadGateway = 502,
67  ServiceUnavailable = 503,
68  GatewayTimeout = 504,
69  HTTPVersionNotSupported = 505,
70  BandwidthLimitExceeded = 509
71  };
72  Q_ENUM(HttpStatus)
73 
74 
75  enum CloseCode {
76  CloseCodeNormal = 1000,
77  CloseCodeGoingAway = 1001,
78  CloseCodeProtocolError = 1002,
79  CloseCodeDatatypeNotSupported = 1003,
80  CloseCodeReserved1004 = 1004,
81  CloseCodeMissingStatusCode = 1005,
82  CloseCodeAbnormalDisconnection = 1006,
83  CloseCodeWrongDatatype = 1007,
84  CloseCodePolicyViolated = 1008,
85  CloseCodeTooMuchData = 1009,
86  CloseCodeMissingExtension = 1010,
87  CloseCodeBadOperation = 1011,
88  CloseCodeTlsHandshakeFailed = 1015
89  };
90  Q_ENUM(CloseCode)
91 
92  virtual ~Response() override;
93 
97  quint16 status() const noexcept;
98 
102  void setStatus(quint16 status) noexcept;
103 
109  bool hasBody() const noexcept;
110 
117  Q_REQUIRED_RESULT QByteArray &body();
118 
122  QIODevice *bodyDevice() const;
123 
130  void setBody(QIODevice *body);
131 
136  void setBody(const QByteArray &body);
137 
142  inline void setBody(const QString &body);
143 
148  inline void setBody(QStringView body);
149 
155  void setJsonBody(const QJsonDocument &documment);
156 
161  void setJsonBody(const QString &json);
162 
167  inline void setJsonBody(QStringView json);
168 
173  void setJsonBody(const QByteArray &json);
174 
180  void setJsonObjectBody(const QJsonObject &object);
181 
187  void setJsonArrayBody(const QJsonArray &array);
188 
192  QString contentEncoding() const;
193 
197  void setContentEncoding(const QString &encoding);
198 
202  qint64 contentLength() const;
203 
207  void setContentLength(qint64 length);
208 
212  QString contentType() const;
213 
217  void setContentType(const QString &type)
218  { headers().setContentType(type); }
219 
223  QString contentTypeCharset() const;
224 
229  QVariant cookie(const QByteArray &name) const;
230 
234  QList<QNetworkCookie> cookies() const;
235 
240  void setCookie(const QNetworkCookie &cookie);
241 
246  void setCookies(const QList<QNetworkCookie> &cookies);
247 
252  int removeCookies(const QByteArray &name);
253 
266  void redirect(const QUrl &url, quint16 status = Found);
267 
280  void redirect(const QString &url, quint16 status = Found);
281 
299  void redirectSafe(const QUrl &url, const QUrl &fallback);
300 
304  QUrl location() const noexcept;
305 
309  QString header(const QString &field) const;
310 
314  void setHeader(const QString &field, const QString &value);
315 
319  Headers &headers() noexcept;
320 
324  bool isFinalizedHeaders() const noexcept;
325 
329  virtual bool isSequential() const noexcept override;
330 
334  virtual qint64 size() const noexcept override;
335 
346  bool webSocketHandshake(const QString &key = {}, const QString &origin = {}, const QString &protocol = {});
347 
351  bool webSocketTextMessage(const QString &message);
352 
356  bool webSocketBinaryMessage(const QByteArray &message);
357 
366  bool webSocketPing(const QByteArray &payload = {});
367 
375  bool webSocketClose(quint16 code = Response::CloseCodeNormal, const QString &reason = {});
376 
377 protected:
381  explicit Response(const Headers &defaultHeaders, EngineRequest *conn = nullptr);
382 
390  virtual qint64 writeData(const char *data, qint64 len) override;
391 
395  virtual qint64 readData(char *data, qint64 maxlen) override;
396 
397  ResponsePrivate *d_ptr;
398  friend class Application;
399  friend class Engine;
400  friend class EngineConnection;
401  friend class Context;
402  friend class ContextPrivate;
403 };
404 
405 inline void Response::setBody(const QString &_body) {
406  setBody(_body.toUtf8());
407 }
408 
409 inline void Response::setBody(QStringView _body) {
410  setBody(_body.toUtf8());
411 }
412 
413 inline void Response::setJsonBody(QStringView _body) {
414  setJsonBody(_body.toUtf8());
415 }
416 
417 }
418 
419 #endif // CUTELYST_RESPONSE_H
The Cutelyst Application.
Definition: application.h:43
The Cutelyst Context.
Definition: context.h:39
The Cutelyst Engine.
Definition: engine.h:21
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:101
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(const QJsonDocument &documment)
Definition: response.cpp:121
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