tufao  0.8.1
An asynchronous web framework for C++ built on top of Qt
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
httpserverresponse.h
1 /*
2  Copyright (c) 2012 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 
57 class TUFAO_EXPORT HttpServerResponse : public QObject
58 {
59  Q_OBJECT
60 public:
71  {
72  // 1xx Informational
73  CONTINUE = 100,
74  SWITCHING_PROTOCOLS = 101,
75  PROCESSING = 102,
76  CHECKPOINT = 103,
77  // 2xx Successful
78  OK = 200,
79  CREATED = 201,
80  ACCEPTED = 202,
81  NON_AUTHORITATIVE_INFORMATION = 203,
82  NO_CONTENT = 204,
83  RESET_CONTENT = 205,
84  PARTIAL_CONTENT = 206,
85  MULTI_STATUS = 207,
86  ALREADY_REPORTED = 208,
87  IM_USED = 226,
88  // 3xx Redirection
89  MULTIPLE_CHOICES = 300,
90  MOVED_PERMANENTLY = 301,
91  FOUND = 302,
92  SEE_OTHER = 303,
93  NOT_MODIFIED = 304,
94  USE_PROXY = 305,
95  SWITCH_PROXY = 306,
96  TEMPORARY_REDIRECT = 307,
97  RESUME_INCOMPLETE = 308,
98  // 4xx Client Error
99  BAD_REQUEST = 400,
100  UNAUTHORIZED = 401,
101  PAYMENT_REQUIRED = 402,
102  FORBIDDEN = 403,
103  NOT_FOUND = 404,
104  METHOD_NOT_ALLOWED = 405,
105  NOT_ACCEPTABLE = 406,
106  PROXY_AUTHENTICATION_REQUIRED = 407,
107  REQUEST_TIMEOUT = 408,
108  CONFLICT = 409,
109  GONE = 410,
110  LENGTH_REQUIRED = 411,
111  PRECONDITION_FAILED = 412,
112  REQUEST_ENTITY_TOO_LARGE = 413,
113  REQUEST_URI_TOO_LONG = 414,
114  UNSUPPORTED_MEDIA_TYPE = 415,
115  REQUESTED_RANGE_NOT_SATISFIABLE = 416,
116  EXPECTATION_FAILED = 417,
117  I_AM_A_TEAPOT = 418,
118  UNPROCESSABLE_ENTITY = 422,
119  LOCKED = 423,
120  FAILED_DEPENDENCY = 424,
121  UNORDERED_COLLECTION = 425,
122  UPGRADE_REQUIRED = 426,
123  PRECONDITION_REQUIRED = 428,
124  TOO_MANY_REQUESTS = 429,
125  REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
126  NO_RESPONSE = 444,
127  RETRY_WITH = 449,
128  CLIENT_CLOSED_REQUEST = 499,
129  // 5xx Internal Server Error
130  INTERNAL_SERVER_ERROR = 500,
131  NOT_IMPLEMENTED = 501,
132  BAD_GATEWAY = 502,
133  SERVICE_UNAVAILABLE = 503,
134  GATEWAY_TIMEOUT = 504,
135  HTTP_VERSION_NOT_SUPPORTED = 505,
136  VARIANT_ALSO_NEGOTIATES = 506,
137  INSUFFICIENT_STORAGE = 507,
138  LOOP_DETECTED = 508,
139  BANDWIDTH_LIMIT_EXCEEDED = 509,
140  NOT_EXTENDED = 510
141  };
142 
146  enum Option
147  {
151  HTTP_1_0 = 1,
155  HTTP_1_1 = 1 << 1,
162  KEEP_ALIVE = 1 << 2
163  };
164  Q_DECLARE_FLAGS(Options, Option)
165 
166 
176  explicit HttpServerResponse(QIODevice *device,
177  Options options,
178  QObject *parent = 0);
179 
184 
188  Options options() const;
189 
196  const Headers &headers() const;
197 
209  Headers &headers();
210 
220  HttpServerResponse &operator <<(const QByteArray &chunk);
221 
232  bool flush();
233 
234 signals:
241  void finished();
242 
243 public slots:
263  bool writeContinue();
264 
275  bool writeHead(int statusCode, const QByteArray &reasonPhrase,
276  const Headers &headers);
277 
284  bool writeHead(int statusCode, const QByteArray &reasonPhrase);
285 
292  bool writeHead(int statusCode, const Headers &headers);
293 
300  bool writeHead(int statusCode);
301 
324  bool write(const QByteArray &chunk);
325 
351  bool addTrailers(const Headers &headers);
352 
378  bool addTrailer(const QByteArray &headerName,
379  const QByteArray &headerValue);
380 
390  bool end(const QByteArray &chunk = QByteArray());
391 
392 private:
393  struct Priv;
394  Priv *priv;
395 };
396 
397 inline
398 HttpServerResponse &HttpServerResponse::operator <<(const QByteArray &chunk)
399 {
400  write(chunk);
401  return *this;
402 }
403 
404 } // namespace Tufao
405 
406 #endif // TUFAO_HTTPSERVERRESPONSE_H
The Tufao::HttpServerResponse is used to respond to a Tufao::HttpServerRequest.
Definition: httpserverresponse.h:57
Option
This enum represents some aspects of a HTTP response.
Definition: httpserverresponse.h:146
StatusCode
The values in this enum represents a HTTP status code.
Definition: httpserverresponse.h:70
This class provides a representation of HTTP headers.
Definition: headers.h:42