Cutelyst  1.7.0
response.h
1 /*
2  * Copyright (C) 2013-2016 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 Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifndef CUTELYST_RESPONSE_H
21 #define CUTELYST_RESPONSE_H
22 
23 #include <QtCore/QIODevice>
24 
25 #include <Cutelyst/cutelyst_global.h>
26 #include <Cutelyst/headers.h>
27 
28 class QNetworkCookie;
29 
30 namespace Cutelyst {
31 
32 class Context;
33 class Engine;
34 class ResponsePrivate;
35 class CUTELYST_LIBRARY Response : public QIODevice
36 {
37  Q_OBJECT
38  Q_DECLARE_PRIVATE(Response)
39 public:
41  enum HttpStatus {
42  Continue = 100,
43  SwitchingProtocols = 101,
44  OK = 200,
45  Created = 201,
46  Accepted = 202,
47  NonAuthoritativeInformation = 203,
48  NoContent = 204,
49  ResetContent = 205,
50  PartialContent = 206,
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  QByteArray &body() Q_REQUIRED_RESULT;
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 
167  QString contentEncoding() const;
168 
172  void setContentEncoding(const QString &encoding);
173 
177  qint64 contentLength() const;
178 
182  void setContentLength(qint64 length);
183 
187  QString contentType() const;
188 
192  void setContentType(const QString &type)
193  { headers().setContentType(type); }
194 
198  QString contentTypeCharset() const;
199 
204  QVariant cookie(const QByteArray &name) const;
205 
209  QList<QNetworkCookie> cookies() const;
210 
215  void setCookie(const QNetworkCookie &cookie);
216 
221  void setCookies(const QList<QNetworkCookie> &cookies);
222 
227  int removeCookies(const QByteArray &name);
228 
241  void redirect(const QUrl &url, quint16 status = Found);
242 
255  void redirect(const QString &url, quint16 status = Found);
256 
260  QUrl location() const;
261 
265  QString header(const QString &field) const;
266 
270  void setHeader(const QString &field, const QString &value);
271 
275  Headers &headers();
276 
280  virtual bool isSequential() const override;
281 
285  virtual qint64 size() const override;
286 
287 
293  bool webSocketHandshake(const QString &key = QString(), const QString &origin = QString(), const QString &protocol = QString());
294 
298  bool webSocketTextMessage(const QString &message);
299 
303  bool webSocketBinaryMessage(const QByteArray &message);
304 
309  bool webSocketPing(const QByteArray &payload = QByteArray());
310 
314  bool webSocketClose(quint16 code = Response::CloseCodeNormal, const QString &reason = QString());
315 
316 protected:
320  explicit Response(Context *c, Engine *engine, const Headers &defaultHeaders);
321 
329  virtual qint64 writeData(const char *data, qint64 len) override;
330 
334  virtual qint64 readData(char *data, qint64 maxlen) override;
335 
336  ResponsePrivate *d_ptr;
337  friend class Application;
338  friend class Engine;
339  friend class ContextPrivate;
340 };
341 
342 inline void Response::setBody(const QString &_body) {
343  setBody(_body.toUtf8());
344 }
345 
346 }
347 
348 #endif // CUTELYST_RESPONSE_H
void setContentType(const QString &type)
Definition: response.h:192
The Cutelyst Context.
Definition: context.h:52
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
void setBody(QIODevice *body)
The Cutelyst Application.
Definition: application.h:56