00001
00002
00003
00004
00005
00006
00007
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 {
00020 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();
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
00147 inline void set_user(user_ptr user) { m_user_record = user; }
00148
00150 inline user_ptr get_user() const { return m_user_record; }
00151
00152
00153 protected:
00154
00156 virtual void update_first_line(void) const {
00157
00158 m_first_line = m_method;
00159 m_first_line += ' ';
00160
00161 m_first_line += m_resource;
00162 if (! m_query_string.empty()) {
00163
00164 m_first_line += '?';
00165 m_first_line += m_query_string;
00166 }
00167 m_first_line += ' ';
00168
00169 m_first_line += get_version_string();
00170 }
00171
00173 virtual void append_cookie_headers(void) {
00174 for (ihash_multimap::const_iterator i = get_cookies().begin(); i != get_cookies().end(); ++i) {
00175 std::string cookie_header;
00176 cookie_header = i->first;
00177 cookie_header += COOKIE_NAME_VALUE_DELIMITER;
00178 cookie_header += i->second;
00179 add_header(HEADER_COOKIE, cookie_header);
00180 }
00181 }
00182
00183
00184 private:
00185
00187 std::string m_method;
00188
00190 std::string m_resource;
00191
00193 std::string m_original_resource;
00194
00196 std::string m_query_string;
00197
00199 ihash_multimap m_query_params;
00200
00202 user_ptr m_user_record;
00203 };
00204
00205
00207 typedef boost::shared_ptr<request> request_ptr;
00208
00209
00210 }
00211 }
00212
00213 #endif