pion-net  4.0.9
HTTPResponse.hpp
1 // ------------------------------------------------------------------
2 // pion-net: a C++ framework for building lightweight HTTP interfaces
3 // ------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
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_HTTPRESPONSE_HEADER__
11 #define __PION_HTTPRESPONSE_HEADER__
12 
13 #include <boost/shared_ptr.hpp>
14 #include <boost/lexical_cast.hpp>
15 #include <pion/PionConfig.hpp>
16 #include <pion/net/HTTPMessage.hpp>
17 #include <pion/net/HTTPRequest.hpp>
18 
19 
20 namespace pion { // begin namespace pion
21 namespace net { // begin namespace net (Pion Network Library)
22 
23 
28  : public HTTPMessage
29 {
30 public:
31 
37  HTTPResponse(const HTTPRequest& http_request)
38  : m_status_code(RESPONSE_CODE_OK),
39  m_status_message(RESPONSE_MESSAGE_OK)
40  {
41  updateRequestInfo(http_request);
42  }
43 
49  HTTPResponse(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  HTTPResponse(const HTTPResponse& http_response)
56  : HTTPMessage(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 
65  : m_status_code(RESPONSE_CODE_OK),
66  m_status_message(RESPONSE_MESSAGE_OK)
67  {}
68 
70  virtual ~HTTPResponse() {}
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 isContentLengthImplied(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 updateRequestInfo(const HTTPRequest& http_request) {
96  m_request_method = http_request.getMethod();
97  if (http_request.getVersionMajor() == 1 && http_request.getVersionMinor() >= 1)
98  setChunksSupported(true);
99  }
100 
102  inline void setStatusCode(unsigned int n) {
103  m_status_code = n;
104  clearFirstLine();
105  }
106 
108  inline void setStatusMessage(const std::string& msg) {
109  m_status_message = msg;
110  clearFirstLine();
111  }
112 
114  inline unsigned int getStatusCode(void) const { return m_status_code; }
115 
117  inline const std::string& getStatusMessage(void) const { return m_status_message; }
118 
119 
127  inline void setCookie(const std::string& name, const std::string& value) {
128  std::string set_cookie_header(make_set_cookie_header(name, value, "/"));
129  addHeader(HEADER_SET_COOKIE, set_cookie_header);
130  }
131 
140  inline void setCookie(const std::string& name, const std::string& value,
141  const std::string& path)
142  {
143  std::string set_cookie_header(make_set_cookie_header(name, value, path));
144  addHeader(HEADER_SET_COOKIE, set_cookie_header);
145  }
146 
155  inline void setCookie(const std::string& name, const std::string& value,
156  const std::string& path, const unsigned long max_age)
157  {
158  std::string set_cookie_header(make_set_cookie_header(name, value, path, true, max_age));
159  addHeader(HEADER_SET_COOKIE, set_cookie_header);
160  }
161 
169  inline void setCookie(const std::string& name, const std::string& value,
170  const unsigned long max_age)
171  {
172  std::string set_cookie_header(make_set_cookie_header(name, value, "/", true, max_age));
173  addHeader(HEADER_SET_COOKIE, set_cookie_header);
174  }
175 
177  inline void deleteCookie(const std::string& name) {
178  std::string set_cookie_header(make_set_cookie_header(name, "", "/", true, 0));
179  addHeader(HEADER_SET_COOKIE, set_cookie_header);
180  }
181 
183  inline void deleteCookie(const std::string& name, const std::string& path) {
184  std::string set_cookie_header(make_set_cookie_header(name, "", path, true, 0));
185  addHeader(HEADER_SET_COOKIE, set_cookie_header);
186  }
187 
189  inline void setLastModified(const unsigned long t) {
190  changeHeader(HEADER_LAST_MODIFIED, get_date_string(t));
191  }
192 
193 
194 protected:
195 
197  virtual void updateFirstLine(void) const {
198  // start out with the HTTP version
200  m_first_line += ' ';
201  // append the response status code
202  m_first_line += boost::lexical_cast<std::string>(m_status_code);
203  m_first_line += ' ';
204  // append the response status message
205  m_first_line += m_status_message;
206  }
207 
208 
209 private:
210 
212  unsigned int m_status_code;
213 
215  std::string m_status_message;
216 
218  std::string m_request_method;
219 };
220 
221 
223 typedef boost::shared_ptr<HTTPResponse> HTTPResponsePtr;
224 
225 
226 } // end namespace net
227 } // end namespace pion
228 
229 #endif
void setStatusCode(unsigned int n)
sets the HTTP response status code
void addHeader(const std::string &key, const std::string &value)
adds a value for the HTTP header named key
void setLastModified(const unsigned long t)
sets the time that the response was last modified (Last-Modified)
virtual void clear(void)
clears all message data
static std::string get_date_string(const time_t t)
converts time_t format into an HTTP-date string
Definition: HTTPTypes.cpp:95
HTTPResponse(const HTTPRequest &http_request)
virtual void updateFirstLine(void) const
updates the string containing the first line for the HTTP message
void changeHeader(const std::string &key, const std::string &value)
changes the value for the HTTP header named key
void setCookie(const std::string &name, const std::string &value, const unsigned long max_age)
boost::uint16_t getVersionMajor(void) const
returns the major HTTP version number
virtual void clear(void)
clears all response data
virtual ~HTTPResponse()
virtual destructor
void setStatusMessage(const std::string &msg)
sets the HTTP response status message
virtual bool isContentLengthImplied(void) const
the content length may be implied for certain types of responses
void setCookie(const std::string &name, const std::string &value, const std::string &path)
void setCookie(const std::string &name, const std::string &value, const std::string &path, const unsigned long max_age)
const std::string & getMethod(void) const
returns the request method (i.e. GET, POST, PUT)
Definition: HTTPRequest.hpp:59
HTTPResponse(const HTTPResponse &http_response)
copy constructor
boost::uint16_t getVersionMinor(void) const
returns the minor HTTP version number
void setCookie(const std::string &name, const std::string &value)
HTTPResponse(const std::string &request_method)
const std::string & getStatusMessage(void) const
returns the HTTP response status message
void deleteCookie(const std::string &name, const std::string &path)
deletes cookie called name by adding a Set-Cookie header (cookie has a path)
std::string getVersionString(void) const
returns a string representation of the HTTP version (i.e. &quot;HTTP/1.1&quot;)
void deleteCookie(const std::string &name)
deletes cookie called name by adding a Set-Cookie header (cookie has no path)
unsigned int getStatusCode(void) const
returns the HTTP response status code
void setChunksSupported(bool b)
set to true if chunked transfer encodings are supported
void clearFirstLine(void) const
void updateRequestInfo(const HTTPRequest &http_request)
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: HTTPTypes.cpp:124