Cutelyst  3.5.0
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 
149  void setJsonBody(const QJsonDocument &documment);
150 
155  void setJsonBody(const QString &json);
156 
161  void setJsonBody(const QByteArray &json);
162 
168  void setJsonObjectBody(const QJsonObject &object);
169 
175  void setJsonArrayBody(const QJsonArray &array);
176 
180  QString contentEncoding() const;
181 
185  void setContentEncoding(const QString &encoding);
186 
190  qint64 contentLength() const;
191 
195  void setContentLength(qint64 length);
196 
200  QString contentType() const;
201 
205  void setContentType(const QString &type)
206  { headers().setContentType(type); }
207 
211  QString contentTypeCharset() const;
212 
217  QVariant cookie(const QByteArray &name) const;
218 
222  QList<QNetworkCookie> cookies() const;
223 
228  void setCookie(const QNetworkCookie &cookie);
229 
234  void setCookies(const QList<QNetworkCookie> &cookies);
235 
240  int removeCookies(const QByteArray &name);
241 
254  void redirect(const QUrl &url, quint16 status = Found);
255 
268  void redirect(const QString &url, quint16 status = Found);
269 
287  void redirectSafe(const QUrl &url, const QUrl &fallback);
288 
292  QUrl location() const noexcept;
293 
297  QString header(const QString &field) const;
298 
302  void setHeader(const QString &field, const QString &value);
303 
307  Headers &headers() noexcept;
308 
312  bool isFinalizedHeaders() const noexcept;
313 
317  virtual bool isSequential() const noexcept override;
318 
322  virtual qint64 size() const noexcept override;
323 
334  bool webSocketHandshake(const QString &key = {}, const QString &origin = {}, const QString &protocol = {});
335 
339  bool webSocketTextMessage(const QString &message);
340 
344  bool webSocketBinaryMessage(const QByteArray &message);
345 
354  bool webSocketPing(const QByteArray &payload = {});
355 
363  bool webSocketClose(quint16 code = Response::CloseCodeNormal, const QString &reason = {});
364 
365 protected:
369  explicit Response(const Headers &defaultHeaders, EngineRequest *conn = nullptr);
370 
378  virtual qint64 writeData(const char *data, qint64 len) override;
379 
383  virtual qint64 readData(char *data, qint64 maxlen) override;
384 
385  ResponsePrivate *d_ptr;
386  friend class Application;
387  friend class Engine;
388  friend class EngineConnection;
389  friend class Context;
390  friend class ContextPrivate;
391 };
392 
393 inline void Response::setBody(const QString &_body) {
394  setBody(_body.toUtf8());
395 }
396 
397 }
398 
399 #endif // CUTELYST_RESPONSE_H
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
void setBody(QIODevice *body)
Definition: response.cpp:101
QByteArray toUtf8() const const