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 booster{
20  namespace system {
21  class error_code;
22  }
23 }
24 namespace cppcms {
25 class cache_interface;
26 namespace impl { namespace cgi { class connection; }}
27 namespace http {
28 
29  class context;
30  class cookie;
31 
36  class CPPCMS_API response : public booster::noncopyable {
37  public:
41  typedef enum {
42  continue_transfer = 100,
43  switching_protocol = 101,
44  ok = 200,
45  created = 201,
46  accepted = 202,
47  non_authoritative_information = 203,
48  no_content = 204,
49  reset_content = 205,
50  partial_content = 206,
51  multiple_choices = 300,
52  moved_permanently = 301,
53  found = 302,
54  see_other = 303,
55  not_modified = 304,
56  use_proxy = 305,
57  temporary_redirect = 307,
58  bad_request = 400,
59  unauthorized = 401,
60  payment_required = 402,
61  forbidden = 403,
62  not_found = 404,
63  method_not_allowed = 405,
64  not_acceptable = 406,
65  proxy_authentication_required = 407,
66  request_time_out = 408,
67  conflict = 409,
68  gone = 410,
69  precondition_failed = 412,
70  request_entity_too_large = 413,
71  request_uri_too_large = 414,
72  unsupported_media_type = 415,
73  requested_range_not_satisfiable = 416,
74  expectation_failed = 417,
75  internal_server_error = 500,
76  not_implemented = 501,
77  bad_gateway = 502,
78  service_unavailable = 503,
79  gateway_timeout = 504,
80  http_version_not_supported = 505
81  } status_type;
82 
88  typedef enum {
91  raw,
92  asynchronous,
96  asynchronous_raw
98 
99  } io_mode_type;
100 
101 
102  // Standard HTTP Response Headers RFC 2616
103 
107  void accept_ranges(std::string const &);
111  void age(unsigned seconds);
115  void allow(std::string const &);
119  void cache_control(std::string const &);
125  void content_encoding(std::string const &);
129  void content_language(std::string const &);
133  void content_length(unsigned long long len);
137  void content_location(std::string const &);
141  void content_md5(std::string const &);
145  void content_range(std::string const &);
149  void content_type(std::string const &);
153  void date(time_t);
157  void etag(std::string const &);
161  void expires(time_t t);
165  void last_modified(time_t t);
169  void location(std::string const &);
173  void pragma(std::string const &);
177  void proxy_authenticate(std::string const &);
181  void retry_after(std::string const &);
185  void retry_after(unsigned);
189  void status(int code);
193  void status(int code,std::string const &message);
197  void trailer(std::string const &);
201  void transfer_encoding(std::string const &);
205  void vary(std::string const &);
209  void via(std::string const &);
213  void warning(std::string const &);
217  void www_authenticate(std::string const &);
218 
219 
223  void set_header(std::string const &name,std::string const &value);
227  std::string get_header(std::string const &name);
231  void erase_header(std::string const &h);
232 
238  void add_header(std::string const &name,std::string const &value);
239 
244  void set_content_header(std::string const &content_type);
245 
249  void set_html_header();
253  void set_xhtml_header();
257  void set_plain_text_header();
261  void set_redirect_header(std::string const &location,int status = found);
265  void set_cookie(cookie const &);
266 
272  void make_error_response(int stat,std::string const &msg = std::string());
273 
277  io_mode_type io_mode();
283  void io_mode(io_mode_type);
284 
294  std::ostream &out();
295 
299  static std::string make_http_time(time_t);
303  static char const *status_to_string(int status);
304 
309  bool some_output_was_written();
314  void finalize();
315 
329  void setbuf(int buffer_size);
342  void full_asynchronous_buffering(bool enable);
343 
348  bool full_asynchronous_buffering();
349 
358  bool pending_blocked_output();
359 
360 
362  response(context &);
363  ~response();
365  private:
366  friend class impl::cgi::connection;
367  friend class ::cppcms::cache_interface;
368 
369  void copy_to_cache();
370  std::string copied_data();
371  bool need_gzip();
372 
373  std::pair<char const *,size_t> output();
374 
375  void write_http_headers(std::ostream &);
376 
377  int flush_async_chunk(booster::system::error_code &e);
378 
379  struct _data;
381 
382  context &context_;
383  std::ostream *stream_;
384  io_mode_type io_mode_;
385 
386  uint32_t disable_compression_ : 1;
387  uint32_t ostream_requested_ : 1;
388  uint32_t copy_to_cache_ : 1;
389  uint32_t finalized_ : 1;
390  uint32_t reserved_ : 28;
391  };
392 
393 } /* http */
394 } /* cppcms */
395 
396 
397 #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:89
status_type
Definition: http_response.h:41
This is the namespace where all CppCMS functionality is placed.
Definition: application.h:19
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:47
The lightweight object that carries a error code information and its category.
Definition: system_error.h:83
io_mode_type
Definition: http_response.h:88
this class represents all HTTP/CGI response related API, generation of output content and HTTP header...
Definition: http_response.h:36
basic_message< char > message
Definition: message.h:494
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23
Same as normal but disable gzip compression.
Definition: http_response.h:90
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