Cutelyst  2.5.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 : 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();
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 
169  void setJsonObjectBody(const QJsonObject &object);
170 
176  void setJsonArrayBody(const QJsonArray &array);
177 
181  QString contentEncoding() const;
182 
186  void setContentEncoding(const QString &encoding);
187 
191  qint64 contentLength() const;
192 
196  void setContentLength(qint64 length);
197 
201  QString contentType() const;
202 
206  void setContentType(const QString &type)
207  { headers().setContentType(type); }
208 
212  QString contentTypeCharset() const;
213 
218  QVariant cookie(const QByteArray &name) const;
219 
223  QList<QNetworkCookie> cookies() const;
224 
229  void setCookie(const QNetworkCookie &cookie);
230 
235  void setCookies(const QList<QNetworkCookie> &cookies);
236 
241  int removeCookies(const QByteArray &name);
242 
255  void redirect(const QUrl &url, quint16 status = Found);
256 
269  void redirect(const QString &url, quint16 status = Found);
270 
274  QUrl location() const;
275 
279  QString header(const QString &field) const;
280 
284  void setHeader(const QString &field, const QString &value);
285 
289  Headers &headers();
290 
294  virtual bool isSequential() const override;
295 
299  virtual qint64 size() const override;
300 
301 
307  bool webSocketHandshake(const QString &key = QString(), const QString &origin = QString(), const QString &protocol = QString());
308 
312  bool webSocketTextMessage(const QString &message);
313 
317  bool webSocketBinaryMessage(const QByteArray &message);
318 
323  bool webSocketPing(const QByteArray &payload = QByteArray());
324 
328  bool webSocketClose(quint16 code = Response::CloseCodeNormal, const QString &reason = QString());
329 
330 protected:
334  explicit Response(const Headers &defaultHeaders, EngineRequest *conn = nullptr);
335 
343  virtual qint64 writeData(const char *data, qint64 len) override;
344 
348  virtual qint64 readData(char *data, qint64 maxlen) override;
349 
350  ResponsePrivate *d_ptr;
351  friend class Application;
352  friend class Engine;
353  friend class EngineConnection;
354  friend class Context;
355  friend class ContextPrivate;
356 };
357 
358 inline void Response::setBody(const QString &_body) {
359  setBody(_body.toUtf8());
360 }
361 
362 }
363 
364 #endif // CUTELYST_RESPONSE_H
void setContentType(const QString &type)
Definition: response.h:206
The Cutelyst Context.
Definition: context.h:50
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
The Cutelyst Application.
Definition: application.h:55
void setBody(QIODevice *body)
Definition: response.cpp:114
The Cutelyst Engine.
Definition: engine.h:33