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 #include <booster/system_error.h>
15 
16 #include <string>
17 #include <iostream>
18 #include <cppcms/cstdint.h>
19 
20 
21 namespace cppcms {
22 class cache_interface;
23 namespace impl { namespace cgi { class connection; }}
24 namespace http {
25 
26  class context;
27  class cookie;
28 
33  class CPPCMS_API response : public booster::noncopyable {
34  public:
38  typedef enum {
39  continue_transfer = 100,
40  switching_protocol = 101,
41  ok = 200,
42  created = 201,
43  accepted = 202,
44  non_authoritative_information = 203,
45  no_content = 204,
46  reset_content = 205,
47  partial_content = 206,
48  multiple_choices = 300,
49  moved_permanently = 301,
50  found = 302,
51  see_other = 303,
52  not_modified = 304,
53  use_proxy = 305,
54  temporary_redirect = 307,
55  bad_request = 400,
56  unauthorized = 401,
57  payment_required = 402,
58  forbidden = 403,
59  not_found = 404,
60  method_not_allowed = 405,
61  not_acceptable = 406,
62  proxy_authentication_required = 407,
63  request_time_out = 408,
64  conflict = 409,
65  gone = 410,
66  precondition_failed = 412,
67  request_entity_too_large = 413,
68  request_uri_too_large = 414,
69  unsupported_media_type = 415,
70  requested_range_not_satisfiable = 416,
71  expectation_failed = 417,
72  internal_server_error = 500,
73  not_implemented = 501,
74  bad_gateway = 502,
75  service_unavailable = 503,
76  gateway_timeout = 504,
77  http_version_not_supported = 505
78  } status_type;
79 
85  typedef enum {
88  raw,
89  asynchronous,
93  asynchronous_raw
95 
96  } io_mode_type;
97 
98 
99  // Standard HTTP Response Headers RFC 2616
100 
104  void accept_ranges(std::string const &);
108  void age(unsigned seconds);
112  void allow(std::string const &);
116  void cache_control(std::string const &);
122  void content_encoding(std::string const &);
126  void content_language(std::string const &);
130  void content_length(unsigned long long len);
134  void content_location(std::string const &);
138  void content_md5(std::string const &);
142  void content_range(std::string const &);
146  void content_type(std::string const &);
150  void date(time_t);
154  void etag(std::string const &);
158  void expires(time_t t);
162  void last_modified(time_t t);
166  void location(std::string const &);
170  void pragma(std::string const &);
174  void proxy_authenticate(std::string const &);
178  void retry_after(std::string const &);
182  void retry_after(unsigned);
186  void status(int code);
190  void status(int code,std::string const &message);
194  void trailer(std::string const &);
198  void vary(std::string const &);
202  void via(std::string const &);
206  void warning(std::string const &);
210  void www_authenticate(std::string const &);
211 
212 
216  void set_header(std::string const &name,std::string const &value);
220  std::string get_header(std::string const &name);
224  void erase_header(std::string const &h);
225 
231  void add_header(std::string const &name,std::string const &value);
232 
237  void set_content_header(std::string const &content_type);
238 
242  void set_html_header();
246  void set_xhtml_header();
250  void set_plain_text_header();
254  void set_redirect_header(std::string const &location,int status = found);
258  void set_cookie(cookie const &);
259 
265  void make_error_response(int stat,std::string const &msg = std::string());
272  static void make_error_response_html_body(int stat,std::ostream &out,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 cppcms::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();
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:86
status_type
Definition: http_response.h:38
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
io_mode_type
Definition: http_response.h:85
this class represents all HTTP/CGI response related API, generation of output content and HTTP header...
Definition: http_response.h:33
This class is the major gateway of the application to CppCMS caching abilities. Any access too cache ...
Definition: cache_interface.h:139
basic_message< char > message
Definition: message.h:494
Same as normal but disable gzip compression.
Definition: http_response.h:87
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