pion  5.0.6
response.hpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_HTTP_RESPONSE_HEADER__
11 #define __PION_HTTP_RESPONSE_HEADER__
12 
13 #include <boost/shared_ptr.hpp>
14 #include <boost/lexical_cast.hpp>
15 #include <pion/config.hpp>
16 #include <pion/http/message.hpp>
17 #include <pion/http/request.hpp>
18 
19 
20 namespace pion { // begin namespace pion
21 namespace http { // begin namespace http
22 
23 
27 class response
28  : public http::message
29 {
30 public:
31 
37  response(const http::request& http_request_ptr)
38  : m_status_code(RESPONSE_CODE_OK),
39  m_status_message(RESPONSE_MESSAGE_OK)
40  {
41  update_request_info(http_request_ptr);
42  }
43 
49  response(const std::string& request_method)
50  : m_status_code(RESPONSE_CODE_OK), m_status_message(RESPONSE_MESSAGE_OK),
51  m_request_method(request_method)
52  {}
53 
55  response(const response& http_response)
56  : message(http_response),
57  m_status_code(http_response.m_status_code),
58  m_status_message(http_response.m_status_message),
59  m_request_method(http_response.m_request_method)
60  {}
61 
64  response(void)
65  : m_status_code(RESPONSE_CODE_OK),
66  m_status_message(RESPONSE_MESSAGE_OK)
67  {}
68 
70  virtual ~response() {}
71 
73  virtual void clear(void) {
75  m_status_code = RESPONSE_CODE_OK;
76  m_status_message = RESPONSE_MESSAGE_OK;
77  m_request_method.clear();
78  }
79 
81  virtual bool is_content_length_implied(void) const {
82  return (m_request_method == REQUEST_METHOD_HEAD // HEAD responses have no content
83  || (m_status_code >= 100 && m_status_code <= 199) // 1xx responses have no content
84  || m_status_code == 204 || m_status_code == 205 // no content & reset content responses
85  || m_status_code == 304 // not modified responses have no content
86  );
87  }
88 
95  inline void update_request_info(const http::request& http_request) {
96  m_request_method = http_request.get_method();
97  if (http_request.get_version_major() == 1 && http_request.get_version_minor() >= 1) {
99  } else if (http_request.get_version_major() == 0) {
100  // the request is likely HTTP 0.9 "simple-request", so expect the response to contain no header and no version info
101  set_status_code(0U);
102  set_status_message("");
105  }
106  }
107 
109  inline void set_status_code(unsigned int n) {
110  m_status_code = n;
112  }
113 
115  inline void set_status_message(const std::string& msg) {
116  m_status_message = msg;
118  }
119 
121  inline unsigned int get_status_code(void) const { return m_status_code; }
122 
124  inline const std::string& get_status_message(void) const { return m_status_message; }
125 
126 
134  inline void set_cookie(const std::string& name, const std::string& value) {
135  std::string set_cookie_header(make_set_cookie_header(name, value, "/"));
136  add_header(HEADER_SET_COOKIE, set_cookie_header);
137  }
138 
147  inline void set_cookie(const std::string& name, const std::string& value,
148  const std::string& path)
149  {
150  std::string set_cookie_header(make_set_cookie_header(name, value, path));
151  add_header(HEADER_SET_COOKIE, set_cookie_header);
152  }
153 
162  inline void set_cookie(const std::string& name, const std::string& value,
163  const std::string& path, const unsigned long max_age)
164  {
165  std::string set_cookie_header(make_set_cookie_header(name, value, path, true, max_age));
166  add_header(HEADER_SET_COOKIE, set_cookie_header);
167  }
168 
176  inline void set_cookie(const std::string& name, const std::string& value,
177  const unsigned long max_age)
178  {
179  std::string set_cookie_header(make_set_cookie_header(name, value, "/", true, max_age));
180  add_header(HEADER_SET_COOKIE, set_cookie_header);
181  }
182 
184  inline void delete_cookie(const std::string& name) {
185  std::string set_cookie_header(make_set_cookie_header(name, "", "/", true, 0));
186  add_header(HEADER_SET_COOKIE, set_cookie_header);
187  }
188 
190  inline void delete_cookie(const std::string& name, const std::string& path) {
191  std::string set_cookie_header(make_set_cookie_header(name, "", path, true, 0));
192  add_header(HEADER_SET_COOKIE, set_cookie_header);
193  }
194 
196  inline void set_last_modified(const unsigned long t) {
197  change_header(HEADER_LAST_MODIFIED, get_date_string(t));
198  }
199 
200 
201 protected:
202 
204  virtual void update_first_line(void) const {
205  // start out with the HTTP version
207  m_first_line += ' ';
208  // append the response status code
209  m_first_line += boost::lexical_cast<std::string>(m_status_code);
210  m_first_line += ' ';
211  // append the response status message
212  m_first_line += m_status_message;
213  }
214 
216  virtual void append_cookie_headers(void) {
217  for (ihash_multimap::const_iterator i = get_cookies().begin(); i != get_cookies().end(); ++i) {
218  set_cookie(i->first, i->second);
219  }
220  }
221 
222 
223 private:
224 
226  unsigned int m_status_code;
227 
229  std::string m_status_message;
230 
232  std::string m_request_method;
233 };
234 
235 
237 typedef boost::shared_ptr<response> response_ptr;
238 
239 
240 } // end namespace http
241 } // end namespace pion
242 
243 #endif
void set_cookie(const std::string &name, const std::string &value, const std::string &path)
Definition: response.hpp:147
const std::string & get_status_message(void) const
returns the HTTP response status message
Definition: response.hpp:124
virtual void clear(void)
clears all response data
Definition: response.hpp:73
unsigned int get_status_code(void) const
returns the HTTP response status code
Definition: response.hpp:121
void delete_cookie(const std::string &name, const std::string &path)
deletes cookie called name by adding a Set-Cookie header (cookie has a path)
Definition: response.hpp:190
void set_cookie(const std::string &name, const std::string &value, const std::string &path, const unsigned long max_age)
Definition: response.hpp:162
void add_header(const std::string &key, const std::string &value)
adds a value for the HTTP header named key
Definition: message.hpp:363
virtual void clear(void)
clears all message data
Definition: message.hpp:146
void set_status_code(unsigned int n)
sets the HTTP response status code
Definition: response.hpp:109
void set_version_minor(const boost::uint16_t n)
sets the minor HTTP version number
Definition: message.hpp:296
void set_version_major(const boost::uint16_t n)
sets the major HTTP version number
Definition: message.hpp:290
boost::uint16_t get_version_major(void) const
returns the major HTTP version number
Definition: message.hpp:177
void clear_first_line(void) const
Definition: message.hpp:668
void change_header(const std::string &key, const std::string &value)
changes the value for the HTTP header named key
Definition: message.hpp:368
void set_chunks_supported(bool b)
set to true if chunked transfer encodings are supported
Definition: message.hpp:284
static std::string get_date_string(const time_t t)
converts time_t format into an HTTP-date string
Definition: http_types.cpp:98
std::string get_version_string(void) const
returns a string representation of the HTTP version (i.e. "HTTP/1.1")
Definition: message.hpp:183
std::string m_first_line
Definition: message.hpp:678
virtual ~response()
virtual destructor
Definition: response.hpp:70
void delete_cookie(const std::string &name)
deletes cookie called name by adding a Set-Cookie header (cookie has no path)
Definition: response.hpp:184
boost::uint16_t get_version_minor(void) const
returns the minor HTTP version number
Definition: message.hpp:180
response(const std::string &request_method)
Definition: response.hpp:49
void set_status_message(const std::string &msg)
sets the HTTP response status message
Definition: response.hpp:115
response(const http::request &http_request_ptr)
Definition: response.hpp:37
const std::string & get_method(void) const
returns the request method (i.e. GET, POST, PUT)
Definition: request.hpp:60
void set_cookie(const std::string &name, const std::string &value, const unsigned long max_age)
Definition: response.hpp:176
virtual void append_cookie_headers(void)
appends HTTP headers for any cookies defined by the http::message
Definition: response.hpp:216
response(const response &http_response)
copy constructor
Definition: response.hpp:55
void update_request_info(const http::request &http_request)
Definition: response.hpp:95
virtual bool is_content_length_implied(void) const
the content length may be implied for certain types of responses
Definition: response.hpp:81
ihash_multimap & get_cookies(void)
returns the cookie parameters
Definition: message.hpp:234
static std::string make_set_cookie_header(const std::string &name, const std::string &value, const std::string &path, const bool has_max_age=false, const unsigned long max_age=0)
Definition: http_types.cpp:127
void set_last_modified(const unsigned long t)
sets the time that the response was last modified (Last-Modified)
Definition: response.hpp:196
virtual void update_first_line(void) const
updates the string containing the first line for the HTTP message
Definition: response.hpp:204
void set_cookie(const std::string &name, const std::string &value)
Definition: response.hpp:134