Cutelyst  1.5.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:
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  virtual ~Response();
87 
91  quint16 status() const;
92 
96  void setStatus(quint16 status);
97 
103  bool hasBody() const;
104 
111  QByteArray &body() Q_REQUIRED_RESULT;
112 
116  QIODevice *bodyDevice() const;
117 
124  void setBody(QIODevice *body);
125 
130  void setBody(const QByteArray &body);
131 
136  inline void setBody(const QString &body);
137 
143  void setJsonBody(const QJsonDocument &documment);
144 
148  QString contentEncoding() const;
149 
153  void setContentEncoding(const QString &encoding);
154 
158  qint64 contentLength() const;
159 
163  void setContentLength(qint64 length);
164 
168  QString contentType() const;
169 
173  void setContentType(const QString &type)
174  { headers().setContentType(type); }
175 
179  QString contentTypeCharset() const;
180 
185  QVariant cookie(const QByteArray &name) const;
186 
190  QList<QNetworkCookie> cookies() const;
191 
196  void setCookie(const QNetworkCookie &cookie);
197 
202  void setCookies(const QList<QNetworkCookie> &cookies);
203 
208  int removeCookies(const QByteArray &name);
209 
222  void redirect(const QUrl &url, quint16 status = Found);
223 
236  void redirect(const QString &url, quint16 status = Found);
237 
241  QUrl location() const;
242 
246  QString header(const QString &field) const;
247 
251  void setHeader(const QString &field, const QString &value);
252 
256  Headers &headers();
257 
261  bool isSequential() const override;
262 
263  qint64 size() const override;
264 
265 protected:
266  explicit Response(Context *c, Engine *engine, const Headers &defaultHeaders);
267 
275  virtual qint64 writeData(const char *data, qint64 len) override;
276  virtual qint64 readData(char *data, qint64 maxlen) override;
277 
278  ResponsePrivate *d_ptr;
279  friend class Application;
280  friend class Engine;
281  friend class ContextPrivate;
282 };
283 
284 inline void Response::setBody(const QString &_body) {
285  setBody(_body.toUtf8());
286 }
287 
288 }
289 
290 #endif // CUTELYST_RESPONSE_H
void setContentType(const QString &type)
Definition: response.h:173
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:55