pion  5.0.6
include/pion/http/request.hpp
00001 // ---------------------------------------------------------------------
00002 // pion:  a Boost C++ framework for building lightweight HTTP interfaces
00003 // ---------------------------------------------------------------------
00004 // Copyright (C) 2007-2014 Splunk Inc.  (https://github.com/splunk/pion)
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_REQUEST_HEADER__
00011 #define __PION_HTTP_REQUEST_HEADER__
00012 
00013 #include <boost/shared_ptr.hpp>
00014 #include <pion/config.hpp>
00015 #include <pion/http/message.hpp>
00016 #include <pion/user.hpp>
00017 
00018 
00019 namespace pion {    // begin namespace pion
00020 namespace http {    // begin namespace http
00021 
00022 
00026 class request
00027     : public http::message
00028 {
00029 public:
00030 
00036     request(const std::string& resource)
00037         : m_method(REQUEST_METHOD_GET), m_resource(resource) {}
00038     
00040     request(void) : m_method(REQUEST_METHOD_GET) {}
00041     
00043     virtual ~request() {}
00044 
00046     virtual void clear(void) {
00047         http::message::clear();
00048         m_method.erase();
00049         m_resource.erase();
00050         m_original_resource.erase();
00051         m_query_string.erase();
00052         m_query_params.clear();
00053         m_user_record.reset();
00054     }
00055 
00057     virtual bool is_content_length_implied(void) const { return false; }
00058 
00060     inline const std::string& get_method(void) const { return m_method; }
00061     
00063     inline const std::string& get_resource(void) const { return m_resource; }
00064 
00066     inline const std::string& get_original_resource(void) const { return m_original_resource; }
00067 
00069     inline const std::string& get_query_string(void) const { return m_query_string; }
00070     
00072     inline const std::string& get_query(const std::string& key) const {
00073         return get_value(m_query_params, key);
00074     }
00075 
00077     inline ihash_multimap& get_queries(void) {
00078         return m_query_params;
00079     }
00080     
00082     inline bool has_query(const std::string& key) const {
00083         return(m_query_params.find(key) != m_query_params.end());
00084     }
00085         
00087     inline void set_method(const std::string& str) { 
00088         m_method = str;
00089         clear_first_line();
00090     }
00091     
00093     inline void set_resource(const std::string& str) {
00094         m_resource = m_original_resource = str;
00095         clear_first_line();
00096     }
00097 
00099     inline void change_resource(const std::string& str) { m_resource = str; }
00100 
00102     inline void set_query_string(const std::string& str) {
00103         m_query_string = str;
00104         clear_first_line();
00105     }
00106     
00108     inline void add_query(const std::string& key, const std::string& value) {
00109         m_query_params.insert(std::make_pair(key, value));
00110     }
00111     
00113     inline void change_query(const std::string& key, const std::string& value) {
00114         change_value(m_query_params, key, value);
00115     }
00116     
00118     inline void delete_query(const std::string& key) {
00119         delete_value(m_query_params, key);
00120     }
00121     
00123     inline void use_query_params_for_query_string(void) {
00124         set_query_string(make_query_string(m_query_params));
00125     }
00126 
00128     inline void use_query_params_for_post_content(void) {
00129         std::string post_content(make_query_string(m_query_params));
00130         set_content_length(post_content.size());
00131         char *ptr = create_content_buffer();  // null-terminates buffer
00132         if (! post_content.empty())
00133             memcpy(ptr, post_content.c_str(), post_content.size());
00134         set_method(REQUEST_METHOD_POST);
00135         set_content_type(CONTENT_TYPE_URLENCODED);
00136     }
00137 
00139     inline void set_content(const std::string &value) {
00140         set_content_length(value.size());
00141         char *ptr = create_content_buffer();
00142         if (! value.empty())
00143             memcpy(ptr, value.c_str(), value.size());
00144     }
00145     
00148     inline void set_content(const char* value, size_t size) {
00149         if ( NULL == value || 0 == size )
00150             return;
00151         set_content_length(size);
00152         char *ptr = create_content_buffer();
00153         memcpy(ptr, value, size);
00154     }
00155     
00157     inline void set_user(user_ptr user) { m_user_record = user; }
00158     
00160     inline user_ptr get_user() const { return m_user_record; }
00161 
00162 
00163 protected:
00164 
00166     virtual void update_first_line(void) const {
00167         // start out with the request method
00168         m_first_line = m_method;
00169         m_first_line += ' ';
00170         // append the resource requested
00171         m_first_line += m_resource;
00172         if (! m_query_string.empty()) {
00173             // append query string if not empty
00174             m_first_line += '?';
00175             m_first_line += m_query_string;
00176         }
00177         m_first_line += ' ';
00178         // append HTTP version
00179         m_first_line += get_version_string();
00180     }
00181     
00183     virtual void append_cookie_headers(void) {
00184         for (ihash_multimap::const_iterator i = get_cookies().begin(); i != get_cookies().end(); ++i) {
00185             std::string cookie_header;
00186             cookie_header = i->first;
00187             cookie_header += COOKIE_NAME_VALUE_DELIMITER;
00188             cookie_header += i->second;
00189             add_header(HEADER_COOKIE, cookie_header);
00190         }
00191     }
00192 
00193     
00194 private:
00195 
00197     std::string                     m_method;
00198 
00200     std::string                     m_resource;
00201 
00203     std::string                     m_original_resource;
00204 
00206     std::string                     m_query_string;
00207     
00209     ihash_multimap                  m_query_params;
00210 
00212     user_ptr                        m_user_record;
00213 };
00214 
00215 
00217 typedef boost::shared_ptr<request>      request_ptr;
00218 
00219 
00220 }   // end namespace http
00221 }   // end namespace pion
00222 
00223 #endif