- Main Page
- Classes
- Files

- File List

# include/pion/http/response.hpp

    00001 // ---------------------------------------------------------------------
    00002 // pion:  a Boost C++ framework for building lightweight HTTP interfaces
    00003 // ---------------------------------------------------------------------
    00004 // Copyright (C) 2007-2012 Cloudmeter, Inc.  (http://www.cloudmeter.com)
    00005 //
    00006 // Distributed under the Boost Software License, Version 1.0.
    00007 // See http://www.boost.org/LICENSE_1_0.txt
    00008 //
    00009
    00010 #ifndef __PION_HTTP_RESPONSE_HEADER__
    00011 #define __PION_HTTP_RESPONSE_HEADER__
    00012
    00013 #include <boost/shared_ptr.hpp>
    00014 #include <boost/lexical_cast.hpp>
    00015 #include <pion/config.hpp>
    00016 #include <pion/http/message.hpp>
    00017 #include <pion/http/request.hpp>
    00018
    00019
    00020 namespace pion {    // begin namespace pion
    00021 namespace http {    // begin namespace http
    00022
    00023
    00027 class response
    00028     : public http::message
    00029 {
    00030 public:
    00031
    00037     response(const http::request& http_request_ptr)
    00038         : m_status_code(RESPONSE_CODE_OK),
    00039         m_status_message(RESPONSE_MESSAGE_OK)
    00040     {
    00041         update_request_info(http_request_ptr);
    00042     }
    00043
    00049     response(const std::string& request_method)
    00050         : m_status_code(RESPONSE_CODE_OK), m_status_message(RESPONSE_MESSAGE_OK),
    00051         m_request_method(request_method)
    00052     {}
    00053
    00055     response(const response& http_response)
    00056         : message(http_response),
    00057         m_status_code(http_response.m_status_code),
    00058         m_status_message(http_response.m_status_message),
    00059         m_request_method(http_response.m_request_method)
    00060     {}
    00061
    00064     response(void)
    00065         : m_status_code(RESPONSE_CODE_OK),
    00066         m_status_message(RESPONSE_MESSAGE_OK)
    00067     {}
    00068
    00070     virtual ~response() {}
    00071
    00073     virtual void clear(void) {
    00074         http::message::clear();
    00075         m_status_code = RESPONSE_CODE_OK;
    00076         m_status_message = RESPONSE_MESSAGE_OK;
    00077         m_request_method.clear();
    00078     }
    00079
    00081     virtual bool is_content_length_implied(void) const {
    00082         return (m_request_method == REQUEST_METHOD_HEAD             // HEAD responses have no content
    00083                 || (m_status_code >= 100 && m_status_code <= 199)       // 1xx responses have no content
    00084                 || m_status_code == 204 || m_status_code == 205     // no content & reset content responses
    00085                 || m_status_code == 304                             // not modified responses have no content
    00086                 );
    00087     }
    00088
    00095     inline void update_request_info(const http::request& http_request) {
    00096         m_request_method = http_request.get_method();
    00097         if (http_request.get_version_major() == 1 && http_request.get_version_minor() >= 1)
    00098             set_chunks_supported(true);
    00099     }
    00100
    00102     inline void set_status_code(unsigned int n) {
    00103         m_status_code = n;
    00104         clear_first_line();
    00105     }
    00106
    00108     inline void set_status_message(const std::string& msg) {
    00109         m_status_message = msg;
    00110         clear_first_line();
    00111     }
    00112
    00114     inline unsigned int get_status_code(void) const { return m_status_code; }
    00115
    00117     inline const std::string& get_status_message(void) const { return m_status_message; }
    00118
    00119
    00127     inline void set_cookie(const std::string& name, const std::string& value) {
    00128         std::string set_cookie_header(make_set_cookie_header(name, value, "/"));
    00129         add_header(HEADER_SET_COOKIE, set_cookie_header);
    00130     }
    00131
    00140     inline void set_cookie(const std::string& name, const std::string& value,
    00141                           const std::string& path)
    00142     {
    00143         std::string set_cookie_header(make_set_cookie_header(name, value, path));
    00144         add_header(HEADER_SET_COOKIE, set_cookie_header);
    00145     }
    00146
    00155     inline void set_cookie(const std::string& name, const std::string& value,
    00156                           const std::string& path, const unsigned long max_age)
    00157     {
    00158         std::string set_cookie_header(make_set_cookie_header(name, value, path, true, max_age));
    00159         add_header(HEADER_SET_COOKIE, set_cookie_header);
    00160     }
    00161
    00169     inline void set_cookie(const std::string& name, const std::string& value,
    00170                           const unsigned long max_age)
    00171     {
    00172         std::string set_cookie_header(make_set_cookie_header(name, value, "/", true, max_age));
    00173         add_header(HEADER_SET_COOKIE, set_cookie_header);
    00174     }
    00175
    00177     inline void delete_cookie(const std::string& name) {
    00178         std::string set_cookie_header(make_set_cookie_header(name, "", "/", true, 0));
    00179         add_header(HEADER_SET_COOKIE, set_cookie_header);
    00180     }
    00181
    00183     inline void delete_cookie(const std::string& name, const std::string& path) {
    00184         std::string set_cookie_header(make_set_cookie_header(name, "", path, true, 0));
    00185         add_header(HEADER_SET_COOKIE, set_cookie_header);
    00186     }
    00187
    00189     inline void set_last_modified(const unsigned long t) {
    00190         change_header(HEADER_LAST_MODIFIED, get_date_string(t));
    00191     }
    00192
    00193
    00194 protected:
    00195
    00197     virtual void update_first_line(void) const {
    00198         // start out with the HTTP version
    00199         m_first_line = get_version_string();
    00200         m_first_line += ' ';
    00201         // append the response status code
    00202         m_first_line +=  boost::lexical_cast<std::string>(m_status_code);
    00203         m_first_line += ' ';
    00204         // append the response status message
    00205         m_first_line += m_status_message;
    00206     }
    00207
    00209     virtual void append_cookie_headers(void) {
    00210         for (ihash_multimap::const_iterator i = get_cookies().begin(); i != get_cookies().end(); ++i) {
    00211             set_cookie(i->first, i->second);
    00212         }
    00213     }
    00214
    00215
    00216 private:
    00217
    00219     unsigned int            m_status_code;
    00220
    00222     std::string             m_status_message;
    00223
    00225     std::string             m_request_method;
    00226 };
    00227
    00228
    00230 typedef boost::shared_ptr<response>     response_ptr;
    00231
    00232
    00233 }   // end namespace http
    00234 }   // end namespace pion
    00235
    00236 #endif