Cutelyst  2.13.0
response.h
1 /*
2  * Copyright (C) 2013-2018 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #ifndef CUTELYST_RESPONSE_H
19 #define CUTELYST_RESPONSE_H
20 
21 #include <QtCore/QIODevice>
22 
23 #include <Cutelyst/cutelyst_global.h>
24 #include <Cutelyst/headers.h>
25 
26 class QNetworkCookie;
27 
28 namespace Cutelyst {
29 
30 class Context;
31 class Engine;
32 class EngineRequest;
33 class ResponsePrivate;
34 class CUTELYST_LIBRARY Response final : public QIODevice
35 {
36  Q_OBJECT
37  Q_DECLARE_PRIVATE(Response)
38 public:
40  enum HttpStatus {
41  Continue = 100,
42  SwitchingProtocols = 101,
43  OK = 200,
44  Created = 201,
45  Accepted = 202,
46  NonAuthoritativeInformation = 203,
47  NoContent = 204,
48  ResetContent = 205,
49  PartialContent = 206,
50  MultiStatus = 207,
51  MultipleChoices = 300,
52  MovedPermanently = 301,
53  Found = 302,
54  SeeOther = 303, // Since HTTP/1.1
55  NotModified = 304,
56  UseProxy = 305, // Since HTTP/1.1
57  TemporaryRedirect = 307, // Since HTTP/1.1
58  PermanentRedirect = 308, // Since HTTP/1.1
59  BadRequest = 400,
60  Unauthorized = 401,
61  PaymentRequired = 402,
62  Forbidden = 403,
63  NotFound = 404,
64  MethodNotAllowed = 405,
65  NotAcceptable = 406,
66  ProxyAuthenticationRequired = 407,
67  RequestTimeout = 408,
68  Conflict = 409,
69  Gone = 410,
70  LengthRequired = 411,
71  PreconditionFailed = 412,
72  RequestEntityTooLarge = 413,
73  RequestURITooLong = 414,
74  UnsupportedMediaType = 415,
75  RequestedRangeNotSatisfiable = 416,
76  ExpectationFailed = 417,
77  InternalServerError = 500,
78  NotImplemented = 501,
79  BadGateway = 502,
80  ServiceUnavailable = 503,
81  GatewayTimeout = 504,
82  HTTPVersionNotSupported = 505,
83  BandwidthLimitExceeded = 509
84  };
85  Q_ENUM(HttpStatus)
86 
87 
88  enum CloseCode {
89  CloseCodeNormal = 1000,
90  CloseCodeGoingAway = 1001,
91  CloseCodeProtocolError = 1002,
92  CloseCodeDatatypeNotSupported = 1003,
93  CloseCodeReserved1004 = 1004,
94  CloseCodeMissingStatusCode = 1005,
95  CloseCodeAbnormalDisconnection = 1006,
96  CloseCodeWrongDatatype = 1007,
97  CloseCodePolicyViolated = 1008,
98  CloseCodeTooMuchData = 1009,
99  CloseCodeMissingExtension = 1010,
100  CloseCodeBadOperation = 1011,
101  CloseCodeTlsHandshakeFailed = 1015
102  };
103  Q_ENUM(CloseCode)
104 
105  virtual ~Response() override;
106 
110  quint16 status() const;
111 
115  void setStatus(quint16 status);
116 
122  bool hasBody() const;
123 
130  Q_REQUIRED_RESULT QByteArray &body();
131 
135  QIODevice *bodyDevice() const;
136 
143  void setBody(QIODevice *body);
144 
149  void setBody(const QByteArray &body);
150 
155  inline void setBody(const QString &body);
156 
162  void setJsonBody(const QJsonDocument &documment);
163 
168  void setJsonBody(const QString &json);
169 
174  void setJsonBody(const QByteArray &json);
175 
181  void setJsonObjectBody(const QJsonObject &object);
182 
188  void setJsonArrayBody(const QJsonArray &array);
189 
193  QString contentEncoding() const;
194 
198  void setContentEncoding(const QString &encoding);
199 
203  qint64 contentLength() const;
204 
208  void setContentLength(qint64 length);
209 
213  QString contentType() const;
214 
218  void setContentType(const QString &type)
219  { headers().setContentType(type); }
220 
224  QString contentTypeCharset() const;
225 
230  QVariant cookie(const QByteArray &name) const;
231 
235  QList<QNetworkCookie> cookies() const;
236 
241  void setCookie(const QNetworkCookie &cookie);
242 
247  void setCookies(const QList<QNetworkCookie> &cookies);
248 
253  int removeCookies(const QByteArray &name);
254 
267  void redirect(const QUrl &url, quint16 status = Found);
268 
281  void redirect(const QString &url, quint16 status = Found);
282 
300  void redirectSafe(const QUrl &url, const QUrl &fallback);
301 
305  QUrl location() const;
306 
310  QString header(const QString &field) const;
311 
315  void setHeader(const QString &field, const QString &value);
316 
320  Headers &headers();
321 
325  bool isFinalizedHeaders() const;
326 
330  virtual bool isSequential() const override;
331 
335  virtual qint64 size() const override;
336 
347  bool webSocketHandshake(const QString &key = QString(), const QString &origin = QString(), const QString &protocol = QString());
348 
352  bool webSocketTextMessage(const QString &message);
353 
357  bool webSocketBinaryMessage(const QByteArray &message);
358 
367  bool webSocketPing(const QByteArray &payload = QByteArray());
368 
376  bool webSocketClose(quint16 code = Response::CloseCodeNormal, const QString &reason = QString());
377 
378 protected:
382  explicit Response(const Headers &defaultHeaders, EngineRequest *conn = nullptr);
383 
391  virtual qint64 writeData(const char *data, qint64 len) override;
392 
396  virtual qint64 readData(char *data, qint64 maxlen) override;
397 
398  ResponsePrivate *d_ptr;
399  friend class Application;
400  friend class Engine;
401  friend class EngineConnection;
402  friend class Context;
403  friend class ContextPrivate;
404 };
405 
406 inline void Response::setBody(const QString &_body) {
407  setBody(_body.toUtf8());
408 }
409 
410 }
411 
412 #endif // CUTELYST_RESPONSE_H
Cutelyst::Application
The Cutelyst Application.
Definition: application.h:55
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:50
Cutelyst::Response::setContentType
void setContentType(const QString &type)
Definition: response.h:218
Cutelyst::EngineRequest
Definition: enginerequest.h:31
Cutelyst::Response::setBody
void setBody(QIODevice *body)
Definition: response.cpp:114
Cutelyst::Headers
Definition: headers.h:29
Cutelyst::Response::HttpStatus
HttpStatus
Definition: response.h:40
Cutelyst::Response::CloseCode
CloseCode
Definition: response.h:88
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::Engine
The Cutelyst Engine.
Definition: engine.h:33
Cutelyst::Response
Definition: response.h:34