pion  5.0.6
request.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_REQUEST_HEADER__
11 #define __PION_HTTP_REQUEST_HEADER__
12 
13 #include <boost/shared_ptr.hpp>
14 #include <pion/config.hpp>
15 #include <pion/http/message.hpp>
16 #include <pion/user.hpp>
17 
18 
19 namespace pion { // begin namespace pion
20 namespace http { // begin namespace http
21 
22 
26 class request
27  : public http::message
28 {
29 public:
30 
36  request(const std::string& resource)
37  : m_method(REQUEST_METHOD_GET), m_resource(resource) {}
38 
40  request(void) : m_method(REQUEST_METHOD_GET) {}
41 
43  virtual ~request() {}
44 
46  virtual void clear(void) {
48  m_method.erase();
49  m_resource.erase();
50  m_original_resource.erase();
51  m_query_string.erase();
52  m_query_params.clear();
53  m_user_record.reset();
54  }
55 
57  virtual bool is_content_length_implied(void) const { return false; }
58 
60  inline const std::string& get_method(void) const { return m_method; }
61 
63  inline const std::string& get_resource(void) const { return m_resource; }
64 
66  inline const std::string& get_original_resource(void) const { return m_original_resource; }
67 
69  inline const std::string& get_query_string(void) const { return m_query_string; }
70 
72  inline const std::string& get_query(const std::string& key) const {
73  return get_value(m_query_params, key);
74  }
75 
77  inline ihash_multimap& get_queries(void) {
78  return m_query_params;
79  }
80 
82  inline bool has_query(const std::string& key) const {
83  return(m_query_params.find(key) != m_query_params.end());
84  }
85 
87  inline void set_method(const std::string& str) {
88  m_method = str;
90  }
91 
93  inline void set_resource(const std::string& str) {
94  m_resource = m_original_resource = str;
96  }
97 
99  inline void change_resource(const std::string& str) { m_resource = str; }
100 
102  inline void set_query_string(const std::string& str) {
103  m_query_string = str;
105  }
106 
108  inline void add_query(const std::string& key, const std::string& value) {
109  m_query_params.insert(std::make_pair(key, value));
110  }
111 
113  inline void change_query(const std::string& key, const std::string& value) {
114  change_value(m_query_params, key, value);
115  }
116 
118  inline void delete_query(const std::string& key) {
119  delete_value(m_query_params, key);
120  }
121 
124  set_query_string(make_query_string(m_query_params));
125  }
126 
129  std::string post_content(make_query_string(m_query_params));
130  set_content_length(post_content.size());
131  char *ptr = create_content_buffer(); // null-terminates buffer
132  if (! post_content.empty())
133  memcpy(ptr, post_content.c_str(), post_content.size());
134  set_method(REQUEST_METHOD_POST);
135  set_content_type(CONTENT_TYPE_URLENCODED);
136  }
137 
139  inline void set_content(const std::string &value) {
140  set_content_length(value.size());
141  char *ptr = create_content_buffer();
142  if (! value.empty())
143  memcpy(ptr, value.c_str(), value.size());
144  }
145 
148  inline void set_content(const char* value, size_t size) {
149  if ( NULL == value || 0 == size )
150  return;
151  set_content_length(size);
152  char *ptr = create_content_buffer();
153  memcpy(ptr, value, size);
154  }
155 
157  inline void set_user(user_ptr user) { m_user_record = user; }
158 
160  inline user_ptr get_user() const { return m_user_record; }
161 
162 
163 protected:
164 
166  virtual void update_first_line(void) const {
167  // start out with the request method
168  m_first_line = m_method;
169  m_first_line += ' ';
170  // append the resource requested
171  m_first_line += m_resource;
172  if (! m_query_string.empty()) {
173  // append query string if not empty
174  m_first_line += '?';
175  m_first_line += m_query_string;
176  }
177  m_first_line += ' ';
178  // append HTTP version
180  }
181 
183  virtual void append_cookie_headers(void) {
184  for (ihash_multimap::const_iterator i = get_cookies().begin(); i != get_cookies().end(); ++i) {
185  std::string cookie_header;
186  cookie_header = i->first;
187  cookie_header += COOKIE_NAME_VALUE_DELIMITER;
188  cookie_header += i->second;
189  add_header(HEADER_COOKIE, cookie_header);
190  }
191  }
192 
193 
194 private:
195 
197  std::string m_method;
198 
200  std::string m_resource;
201 
203  std::string m_original_resource;
204 
206  std::string m_query_string;
207 
209  ihash_multimap m_query_params;
210 
212  user_ptr m_user_record;
213 };
214 
215 
217 typedef boost::shared_ptr<request> request_ptr;
218 
219 
220 } // end namespace http
221 } // end namespace pion
222 
223 #endif
void delete_query(const std::string &key)
removes all values for a query key
Definition: request.hpp:118
virtual ~request()
virtual destructor
Definition: request.hpp:43
void set_content(const std::string &value)
add content (for POST) from string
Definition: request.hpp:139
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
virtual void clear(void)
clears all request data
Definition: request.hpp:46
void change_resource(const std::string &str)
changes the resource or uri-stem to be delivered (called as the result of a redirect) ...
Definition: request.hpp:99
virtual bool is_content_length_implied(void) const
the content length of the message can never be implied for requests
Definition: request.hpp:57
void clear_first_line(void) const
Definition: message.hpp:668
const std::string & get_original_resource(void) const
returns the resource uri-stem originally requested
Definition: request.hpp:66
static const std::string & get_value(const DictionaryType &dict, const std::string &key)
Definition: message.hpp:609
const std::string & get_resource(void) const
returns the resource uri-stem to be delivered (possibly the result of a redirect) ...
Definition: request.hpp:63
void set_content_length(size_t n)
sets the length of the payload content (in bytes)
Definition: message.hpp:302
void set_content_type(const std::string &type)
sets the content type for the message payload
Definition: message.hpp:358
static void change_value(DictionaryType &dict, const std::string &key, const std::string &value)
Definition: message.hpp:626
request(void)
constructs a new request object (default constructor)
Definition: request.hpp:40
std::string get_version_string(void) const
returns a string representation of the HTTP version (i.e. "HTTP/1.1")
Definition: message.hpp:183
void set_method(const std::string &str)
sets the HTTP request method (i.e. GET, POST, PUT)
Definition: request.hpp:87
void set_user(user_ptr user)
sets the user record for HTTP request after authentication
Definition: request.hpp:157
void set_content(const char *value, size_t size)
Definition: request.hpp:148
std::string m_first_line
Definition: message.hpp:678
void change_query(const std::string &key, const std::string &value)
changes the value of a query key
Definition: request.hpp:113
void add_query(const std::string &key, const std::string &value)
adds a value for the query key
Definition: request.hpp:108
const std::string & get_method(void) const
returns the request method (i.e. GET, POST, PUT)
Definition: request.hpp:60
virtual void append_cookie_headers(void)
appends HTTP headers for any cookies defined by the http::message
Definition: request.hpp:183
bool has_query(const std::string &key) const
returns true if at least one value for the query key is defined
Definition: request.hpp:82
const std::string & get_query(const std::string &key) const
returns a value for the query key if any are defined; otherwise, an empty string
Definition: request.hpp:72
char * create_content_buffer(void)
Definition: message.hpp:338
static void delete_value(DictionaryType &dict, const std::string &key)
Definition: message.hpp:657
void use_query_params_for_query_string(void)
use the query parameters to build a query string for the request
Definition: request.hpp:123
virtual void update_first_line(void) const
updates the string containing the first line for the HTTP message
Definition: request.hpp:166
void set_query_string(const std::string &str)
sets the uri-query or query string requested
Definition: request.hpp:102
ihash_multimap & get_queries(void)
returns the query parameters
Definition: request.hpp:77
request(const std::string &resource)
Definition: request.hpp:36
ihash_multimap & get_cookies(void)
returns the cookie parameters
Definition: message.hpp:234
const std::string & get_query_string(void) const
returns the uri-query or query string requested
Definition: request.hpp:69
user_ptr get_user() const
get the user record for HTTP request after authentication
Definition: request.hpp:160
void use_query_params_for_post_content(void)
use the query parameters to build POST content for the request
Definition: request.hpp:128
static std::string make_query_string(const ihash_multimap &query_params)
builds an HTTP query string from a collection of query parameters
Definition: http_types.cpp:114
void set_resource(const std::string &str)
sets the resource or uri-stem originally requested
Definition: request.hpp:93