Cutelyst  2.1.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  MultipleChoices = 300,
51  MovedPermanently = 301,
52  Found = 302,
53  SeeOther = 303, // Since HTTP/1.1
54  NotModified = 304,
55  UseProxy = 305, // Since HTTP/1.1
56  TemporaryRedirect = 307, // Since HTTP/1.1
57  PermanentRedirect = 308, // Since HTTP/1.1
58  BadRequest = 400,
59  Unauthorized = 401,
60  PaymentRequired = 402,
61  Forbidden = 403,
62  NotFound = 404,
63  MethodNotAllowed = 405,
64  NotAcceptable = 406,
65  ProxyAuthenticationRequired = 407,
66  RequestTimeout = 408,
67  Conflict = 409,
68  Gone = 410,
69  LengthRequired = 411,
70  PreconditionFailed = 412,
71  RequestEntityTooLarge = 413,
72  RequestURITooLong = 414,
73  UnsupportedMediaType = 415,
74  RequestedRangeNotSatisfiable = 416,
75  ExpectationFailed = 417,
76  InternalServerError = 500,
77  NotImplemented = 501,
78  BadGateway = 502,
79  ServiceUnavailable = 503,
80  GatewayTimeout = 504,
81  HTTPVersionNotSupported = 505,
82  BandwidthLimitExceeded = 509
83  };
84  Q_ENUM(HttpStatus)
85 
86 
87  enum CloseCode {
88  CloseCodeNormal = 1000,
89  CloseCodeGoingAway = 1001,
90  CloseCodeProtocolError = 1002,
91  CloseCodeDatatypeNotSupported = 1003,
92  CloseCodeReserved1004 = 1004,
93  CloseCodeMissingStatusCode = 1005,
94  CloseCodeAbnormalDisconnection = 1006,
95  CloseCodeWrongDatatype = 1007,
96  CloseCodePolicyViolated = 1008,
97  CloseCodeTooMuchData = 1009,
98  CloseCodeMissingExtension = 1010,
99  CloseCodeBadOperation = 1011,
100  CloseCodeTlsHandshakeFailed = 1015
101  };
102  Q_ENUM(CloseCode)
103 
104  virtual ~Response();
105 
109  quint16 status() const;
110 
114  void setStatus(quint16 status);
115 
121  bool hasBody() const;
122 
129  Q_REQUIRED_RESULT QByteArray &body();
130 
134  QIODevice *bodyDevice() const;
135 
142  void setBody(QIODevice *body);
143 
148  void setBody(const QByteArray &body);
149 
154  inline void setBody(const QString &body);
155 
161  void setJsonBody(const QJsonDocument &documment);
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 
273  QUrl location() const;
274 
278  QString header(const QString &field) const;
279 
283  void setHeader(const QString &field, const QString &value);
284 
288  Headers &headers();
289 
293  virtual bool isSequential() const override;
294 
298  virtual qint64 size() const override;
299 
300 
306  bool webSocketHandshake(const QString &key = QString(), const QString &origin = QString(), const QString &protocol = QString());
307 
311  bool webSocketTextMessage(const QString &message);
312 
316  bool webSocketBinaryMessage(const QByteArray &message);
317 
322  bool webSocketPing(const QByteArray &payload = QByteArray());
323 
327  bool webSocketClose(quint16 code = Response::CloseCodeNormal, const QString &reason = QString());
328 
329 protected:
333  explicit Response(const Headers &defaultHeaders, EngineRequest *conn = nullptr);
334 
342  virtual qint64 writeData(const char *data, qint64 len) override;
343 
347  virtual qint64 readData(char *data, qint64 maxlen) override;
348 
349  ResponsePrivate *d_ptr;
350  friend class Application;
351  friend class Engine;
352  friend class EngineConnection;
353  friend class Context;
354  friend class ContextPrivate;
355 };
356 
357 inline void Response::setBody(const QString &_body) {
358  setBody(_body.toUtf8());
359 }
360 
361 }
362 
363 #endif // CUTELYST_RESPONSE_H
void setContentType(const QString &type)
Definition: response.h:205
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