CppCMS
http_response.h
1 //
3 // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
4 //
5 // See accompanying file COPYING.TXT file for licensing details.
6 //
8 #ifndef CPPCMS_HTTP_RESPONSE_H
9 #define CPPCMS_HTTP_RESPONSE_H
10 
11 #include <cppcms/defs.h>
12 #include <booster/noncopyable.h>
13 #include <booster/hold_ptr.h>
14 
15 #include <string>
16 #include <iostream>
17 #include <cppcms/cstdint.h>
18 
19 namespace cppcms {
20 class cache_interface;
21 namespace impl { namespace cgi { class connection; }}
22 namespace http {
23 
24  class context;
25  class cookie;
26 
31  class CPPCMS_API response : public booster::noncopyable {
32  public:
36  typedef enum {
37  continue_transfer = 100,
38  switching_protocol = 101,
39  ok = 200,
40  created = 201,
41  accepted = 202,
42  non_authoritative_information = 203,
43  no_content = 204,
44  reset_content = 205,
45  partial_content = 206,
46  multiple_choices = 300,
47  moved_permanently = 301,
48  found = 302,
49  see_other = 303,
50  not_modified = 304,
51  use_proxy = 305,
52  temporary_redirect = 307,
53  bad_request = 400,
54  unauthorized = 401,
55  payment_required = 402,
56  forbidden = 403,
57  not_found = 404,
58  method_not_allowed = 405,
59  not_acceptable = 406,
60  proxy_authentication_required = 407,
61  request_time_out = 408,
62  conflict = 409,
63  gone = 410,
64  precondition_failed = 412,
65  request_entity_too_large = 413,
66  request_uri_too_large = 414,
67  unsupported_media_type = 415,
68  requested_range_not_satisfiable = 416,
69  expectation_failed = 417,
70  internal_server_error = 500,
71  not_implemented = 501,
72  bad_gateway = 502,
73  service_unavailable = 503,
74  gateway_timeout = 504,
75  http_version_not_supported = 505
76  } status_type;
77 
83  typedef enum {
86  raw,
87  asynchronous,
91  asynchronous_raw
93 
94  } io_mode_type;
95 
96 
97  // Standard HTTP Response Headers RFC 2616
98 
102  void accept_ranges(std::string const &);
106  void age(unsigned seconds);
110  void allow(std::string const &);
114  void cache_control(std::string const &);
120  void content_encoding(std::string const &);
124  void content_language(std::string const &);
128  void content_length(unsigned long long len);
132  void content_location(std::string const &);
136  void content_md5(std::string const &);
140  void content_range(std::string const &);
144  void content_type(std::string const &);
148  void date(time_t);
152  void etag(std::string const &);
156  void expires(time_t t);
160  void last_modified(time_t t);
164  void location(std::string const &);
168  void pragma(std::string const &);
172  void proxy_authenticate(std::string const &);
176  void retry_after(std::string const &);
180  void retry_after(unsigned);
184  void status(int code);
188  void status(int code,std::string const &message);
192  void trailer(std::string const &);
196  void transfer_encoding(std::string const &);
200  void vary(std::string const &);
204  void via(std::string const &);
208  void warning(std::string const &);
212  void www_authenticate(std::string const &);
213 
214 
218  void set_header(std::string const &name,std::string const &value);
222  std::string get_header(std::string const &name);
226  void erase_header(std::string const &h);
227 
232  void set_content_header(std::string const &content_type);
233 
237  void set_html_header();
241  void set_xhtml_header();
245  void set_plain_text_header();
249  void set_redirect_header(std::string const &location,int status = found);
253  void set_cookie(cookie const &);
254 
260  void make_error_response(int stat,std::string const &msg = std::string());
261 
265  io_mode_type io_mode();
271  void io_mode(io_mode_type);
272 
282  std::ostream &out();
283 
287  static std::string make_http_time(time_t);
291  static char const *status_to_string(int status);
292 
297  bool some_output_was_written();
302  void finalize();
303 
305  response(context &);
306  ~response();
308  private:
309  friend class impl::cgi::connection;
310  friend class ::cppcms::cache_interface;
311 
312  void copy_to_cache();
313  std::string copied_data();
314  bool need_gzip();
315 
316  std::pair<char const *,size_t> output();
317 
318  void write_http_headers(std::ostream &);
319  std::string get_async_chunk();
320 
321  struct _data;
323 
324  context &context_;
325  std::ostream *stream_;
326  io_mode_type io_mode_;
327 
328  uint32_t disable_compression_ : 1;
329  uint32_t ostream_requested_ : 1;
330  uint32_t copy_to_cache_ : 1;
331  uint32_t finalized_ : 1;
332  uint32_t reserved_ : 28;
333  };
334 
335 } /* http */
336 } /* cppcms */
337 
338 
339 #endif
std::ios_base & date(std::ios_base &ios)
Definition: formatting.h:319
Class that represents parsed Content-Type header, this is immutable class. Once it is created its val...
Definition: http_content_type.h:23
Synchronous IO. Write the request, it is buffered and possible compressed using gzip.
Definition: http_response.h:84
context is a central class that holds all specific connection related information. It encapsulates CGI request and response, cache, session and locale information
Definition: http_context.h:45
this class represents all HTTP/CGI response related API, generation of output content and HTTP header...
Definition: http_response.h:31
basic_message< char > message
Definition: message.h:494
Same as normal but disable gzip compression.
Definition: http_response.h:85
Class that represents single HTTP Cookie Generally used in context of http::request and http::respons...
Definition: http_cookie.h:27
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15