tufao  1.3.0
An asynchronous web framework for C++ built on top of Qt
httpserverresponse.h
1 /*
2  Copyright (c) 2012, 2013 Vinícius dos Santos Oliveira
3 
4  Permission is hereby granted, free of charge, to any person obtaining a copy
5  of this software and associated documentation files (the "Software"), to deal
6  in the Software without restriction, including without limitation the rights
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  copies of the Software, and to permit persons to whom the Software is
9  furnished to do so, subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in all
12  copies or substantial portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  SOFTWARE.
21  */
22 
23 #ifndef TUFAO_HTTPSERVERRESPONSE_H
24 #define TUFAO_HTTPSERVERRESPONSE_H
25 
26 #include <QtCore/QObject>
27 #include "tufao_global.h"
28 
29 class QIODevice;
30 
31 namespace Tufao {
32 
33 struct Headers;
34 
48 {
49  // 1xx Informational
50  CONTINUE = 100,
51  SWITCHING_PROTOCOLS = 101,
52  PROCESSING = 102,
53  CHECKPOINT = 103,
54  // 2xx Successful
55  OK = 200,
56  CREATED = 201,
57  ACCEPTED = 202,
58  NON_AUTHORITATIVE_INFORMATION = 203,
59  NO_CONTENT = 204,
60  RESET_CONTENT = 205,
61  PARTIAL_CONTENT = 206,
62  MULTI_STATUS = 207,
63  ALREADY_REPORTED = 208,
64  IM_USED = 226,
65  // 3xx Redirection
66  MULTIPLE_CHOICES = 300,
67  MOVED_PERMANENTLY = 301,
68  FOUND = 302,
69  SEE_OTHER = 303,
70  NOT_MODIFIED = 304,
71  USE_PROXY = 305,
72  SWITCH_PROXY = 306,
73  TEMPORARY_REDIRECT = 307,
74  RESUME_INCOMPLETE = 308,
75  // 4xx Client Error
76  BAD_REQUEST = 400,
77  UNAUTHORIZED = 401,
78  PAYMENT_REQUIRED = 402,
79  FORBIDDEN = 403,
80  NOT_FOUND = 404,
81  METHOD_NOT_ALLOWED = 405,
82  NOT_ACCEPTABLE = 406,
83  PROXY_AUTHENTICATION_REQUIRED = 407,
84  REQUEST_TIMEOUT = 408,
85  CONFLICT = 409,
86  GONE = 410,
87  LENGTH_REQUIRED = 411,
88  PRECONDITION_FAILED = 412,
89  REQUEST_ENTITY_TOO_LARGE = 413,
90  REQUEST_URI_TOO_LONG = 414,
91  UNSUPPORTED_MEDIA_TYPE = 415,
92  REQUESTED_RANGE_NOT_SATISFIABLE = 416,
93  EXPECTATION_FAILED = 417,
94  I_AM_A_TEAPOT = 418,
95  UNPROCESSABLE_ENTITY = 422,
96  LOCKED = 423,
97  FAILED_DEPENDENCY = 424,
98  UNORDERED_COLLECTION = 425,
99  UPGRADE_REQUIRED = 426,
100  PRECONDITION_REQUIRED = 428,
101  TOO_MANY_REQUESTS = 429,
102  REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
103  NO_RESPONSE = 444,
104  RETRY_WITH = 449,
105  CLIENT_CLOSED_REQUEST = 499,
106  // 5xx Internal Server Error
107  INTERNAL_SERVER_ERROR = 500,
108  NOT_IMPLEMENTED = 501,
109  BAD_GATEWAY = 502,
110  SERVICE_UNAVAILABLE = 503,
111  GATEWAY_TIMEOUT = 504,
112  HTTP_VERSION_NOT_SUPPORTED = 505,
113  VARIANT_ALSO_NEGOTIATES = 506,
114  INSUFFICIENT_STORAGE = 507,
115  LOOP_DETECTED = 508,
116  BANDWIDTH_LIMIT_EXCEEDED = 509,
117  NOT_EXTENDED = 510
118 };
119 
142 class TUFAO_EXPORT HttpServerResponse : public QObject
143 {
144  Q_OBJECT
145 public:
149  enum Option
150  {
154  HTTP_1_0 = 1,
158  HTTP_1_1 = 1 << 1,
165  KEEP_ALIVE = 1 << 2
166  };
167  Q_DECLARE_FLAGS(Options, Option)
168 
169 
186  explicit HttpServerResponse(QIODevice &device,
187  Options options = Options(),
188  QObject *parent = 0);
189 
194 
198  Options options() const;
199 
215  bool setOptions(Options options);
216 
223  const Headers &headers() const;
224 
236  Headers &headers();
237 
248  bool flush();
249 
250 signals:
257  void finished();
258 
259 public slots:
279  bool writeContinue();
280 
291  bool writeHead(int statusCode, const QByteArray &reasonPhrase,
292  const Headers &headers);
293 
300  bool writeHead(int statusCode, const QByteArray &reasonPhrase);
301 
308  bool writeHead(HttpResponseStatus statusCode,
309  const QByteArray &reasonPhrase, const Headers &headers);
310 
317  bool writeHead(HttpResponseStatus statusCode,
318  const QByteArray &reasonPhrase);
319 
326  bool writeHead(HttpResponseStatus statusCode, const Headers &headers);
327 
334  bool writeHead(HttpResponseStatus statusCode);
335 
361  bool write(const QByteArray &chunk);
362 
388  bool addTrailers(const Headers &headers);
389 
415  bool addTrailer(const QByteArray &headerName,
416  const QByteArray &headerValue);
417 
427  bool end(const QByteArray &chunk = QByteArray());
428 
429 private:
430  struct Priv;
431  Priv *priv;
432 };
433 
444 inline HttpServerResponse &operator <<(HttpServerResponse &response,
445  const QByteArray &chunk)
446 {
447  response.write(chunk);
448  return response;
449 }
450 
451 } // namespace Tufao
452 
453 #endif // TUFAO_HTTPSERVERRESPONSE_H
The Tufao::HttpServerResponse is used to respond to a Tufao::HttpServerRequest.
Definition: httpserverresponse.h:142
Option
This enum represents some aspects of a HTTP response.
Definition: httpserverresponse.h:149
HttpResponseStatus
The values in this enum represents a HTTP status code.
Definition: httpserverresponse.h:47
This is the namespace where all Tufão facilities are grouped.
Definition: abstracthttpserverrequesthandler.h:30
This class provides a representation of HTTP headers.
Definition: headers.h:42
bool write(const QByteArray &chunk)
This sends a chunk of the response body.