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 #if (QT_VERSION < QT_VERSION_CHECK(6, 1, 0))
18 class Cookie;
19 #endif
20 class Context;
21 class Engine;
22 class EngineRequest;
23 class ResponsePrivate;
24 class CUTELYST_LIBRARY Response final : public QIODevice
25 {
26  Q_OBJECT
27  Q_DECLARE_PRIVATE(Response)
28 public:
30  enum HttpStatus {
31  Continue = 100,
32  SwitchingProtocols = 101,
33  OK = 200,
34  Created = 201,
35  Accepted = 202,
36  NonAuthoritativeInformation = 203,
37  NoContent = 204,
38  ResetContent = 205,
39  PartialContent = 206,
40  MultiStatus = 207,
41  MultipleChoices = 300,
42  MovedPermanently = 301,
43  Found = 302,
44  SeeOther = 303, // Since HTTP/1.1
45  NotModified = 304,
46  UseProxy = 305, // Since HTTP/1.1
47  TemporaryRedirect = 307, // Since HTTP/1.1
48  PermanentRedirect = 308, // Since HTTP/1.1
49  BadRequest = 400,
50  Unauthorized = 401,
51  PaymentRequired = 402,
52  Forbidden = 403,
53  NotFound = 404,
54  MethodNotAllowed = 405,
55  NotAcceptable = 406,
56  ProxyAuthenticationRequired = 407,
57  RequestTimeout = 408,
58  Conflict = 409,
59  Gone = 410,
60  LengthRequired = 411,
61  PreconditionFailed = 412,
62  RequestEntityTooLarge = 413,
63  RequestURITooLong = 414,
64  UnsupportedMediaType = 415,
65  RequestedRangeNotSatisfiable = 416,
66  ExpectationFailed = 417,
67  InternalServerError = 500,
68  NotImplemented = 501,
69  BadGateway = 502,
70  ServiceUnavailable = 503,
71  GatewayTimeout = 504,
72  HTTPVersionNotSupported = 505,
73  BandwidthLimitExceeded = 509
74  };
75  Q_ENUM(HttpStatus)
76 
77 
78  enum CloseCode {
79  CloseCodeNormal = 1000,
80  CloseCodeGoingAway = 1001,
81  CloseCodeProtocolError = 1002,
82  CloseCodeDatatypeNotSupported = 1003,
83  CloseCodeReserved1004 = 1004,
84  CloseCodeMissingStatusCode = 1005,
85  CloseCodeAbnormalDisconnection = 1006,
86  CloseCodeWrongDatatype = 1007,
87  CloseCodePolicyViolated = 1008,
88  CloseCodeTooMuchData = 1009,
89  CloseCodeMissingExtension = 1010,
90  CloseCodeBadOperation = 1011,
91  CloseCodeTlsHandshakeFailed = 1015
92  };
93  Q_ENUM(CloseCode)
94 
95  virtual ~Response() override;
96 
100  quint16 status() const noexcept;
101 
105  void setStatus(quint16 status) noexcept;
106 
112  bool hasBody() const noexcept;
113 
120  Q_REQUIRED_RESULT QByteArray &body();
121 
125  QIODevice *bodyDevice() const;
126 
133  void setBody(QIODevice *body);
134 
139  void setBody(const QByteArray &body);
140 
145  inline void setBody(const QString &body);
146 
151  inline void setBody(QStringView body);
152 
158  void setJsonBody(const QJsonDocument &documment);
159 
164  void setJsonBody(const QString &json);
165 
170  inline void setJsonBody(QStringView json);
171 
176  void setJsonBody(const QByteArray &json);
177 
183  void setJsonObjectBody(const QJsonObject &object);
184 
190  void setJsonArrayBody(const QJsonArray &array);
191 
195  QString contentEncoding() const;
196 
200  void setContentEncoding(const QString &encoding);
201 
205  qint64 contentLength() const;
206 
210  void setContentLength(qint64 length);
211 
215  QString contentType() const;
216 
220  void setContentType(const QString &type)
221  { headers().setContentType(type); }
222 
226  QString contentTypeCharset() const;
227 
232  QVariant cookie(const QByteArray &name) const;
233 
234 #if (QT_VERSION < QT_VERSION_CHECK(6, 1, 0))
241  QVariant cuteCookie(const QByteArray &name) const;
242 #endif
243 
247  QList<QNetworkCookie> cookies() const;
248 
249 #if (QT_VERSION < QT_VERSION_CHECK(6, 1, 0))
255  QList<Cookie> cuteCookies() const;
256 #endif
257 
262  void setCookie(const QNetworkCookie &cookie);
263 
264 #if (QT_VERSION < QT_VERSION_CHECK(6, 1, 0))
271  void setCuteCookie(const Cookie &cookie);
272 #endif
273 
278  void setCookies(const QList<QNetworkCookie> &cookies);
279 
280 #if (QT_VERSION < QT_VERSION_CHECK(6, 1, 0))
287  void setCuteCookies(const QList<Cookie> &cookies);
288 #endif
289 
294  int removeCookies(const QByteArray &name);
295 
296 #if (QT_VERSION < QT_VERSION_CHECK(6, 1, 0))
303  int removeCuteCookies(const QByteArray &name);
304 #endif
305 
318  void redirect(const QUrl &url, quint16 status = Found);
319 
332  void redirect(const QString &url, quint16 status = Found);
333 
351  void redirectSafe(const QUrl &url, const QUrl &fallback);
352 
356  QUrl location() const noexcept;
357 
361  QString header(const QString &field) const;
362 
366  void setHeader(const QString &field, const QString &value);
367 
371  Headers &headers() noexcept;
372 
376  bool isFinalizedHeaders() const noexcept;
377 
381  virtual bool isSequential() const noexcept override;
382 
386  virtual qint64 size() const noexcept override;
387 
398  bool webSocketHandshake(const QString &key = {}, const QString &origin = {}, const QString &protocol = {});
399 
403  bool webSocketTextMessage(const QString &message);
404 
408  bool webSocketBinaryMessage(const QByteArray &message);
409 
418  bool webSocketPing(const QByteArray &payload = {});
419 
427  bool webSocketClose(quint16 code = Response::CloseCodeNormal, const QString &reason = {});
428 
429 protected:
433  explicit Response(const Headers &defaultHeaders, EngineRequest *conn = nullptr);
434 
442  virtual qint64 writeData(const char *data, qint64 len) override;
443 
447  virtual qint64 readData(char *data, qint64 maxlen) override;
448 
449  ResponsePrivate *d_ptr;
450  friend class Application;
451  friend class Engine;
452  friend class EngineConnection;
453  friend class Context;
454  friend class ContextPrivate;
455 };
456 
457 inline void Response::setBody(const QString &_body) {
458  setBody(_body.toUtf8());
459 }
460 
461 inline void Response::setBody(QStringView _body) {
462  setBody(_body.toUtf8());
463 }
464 
465 inline void Response::setJsonBody(QStringView _body) {
466  setJsonBody(_body.toUtf8());
467 }
468 
469 }
470 
471 #endif // CUTELYST_RESPONSE_H
The Cutelyst Application.
Definition: application.h:43
The Cutelyst Context.
Definition: context.h:39
The Cutelyst Cookie.
Definition: cookie.h:29
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
QByteArray toUtf8() const const
QByteArray toUtf8() const const