pion-net  4.0.9
HTTPRequest.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_HTTPREQUEST_HEADER__
11 #define __PION_HTTPREQUEST_HEADER__
12 
13 #include <boost/shared_ptr.hpp>
14 #include <pion/PionConfig.hpp>
15 #include <pion/net/HTTPMessage.hpp>
16 #include <pion/net/PionUser.hpp>
17 
18 namespace pion { // begin namespace pion
19 namespace net { // begin namespace net (Pion Network Library)
20 
21 
26  : public HTTPMessage
27 {
28 public:
29 
35  HTTPRequest(const std::string& resource)
36  : m_method(REQUEST_METHOD_GET), m_resource(resource) {}
37 
39  HTTPRequest(void) : m_method(REQUEST_METHOD_GET) {}
40 
42  virtual ~HTTPRequest() {}
43 
45  virtual void clear(void) {
47  m_method.erase();
48  m_resource.erase();
49  m_original_resource.erase();
50  m_query_string.erase();
51  m_query_params.clear();
52  m_user_record.reset();
53  }
54 
56  virtual bool isContentLengthImplied(void) const { return false; }
57 
59  inline const std::string& getMethod(void) const { return m_method; }
60 
62  inline const std::string& getResource(void) const { return m_resource; }
63 
65  inline const std::string& getOriginalResource(void) const { return m_original_resource; }
66 
68  inline const std::string& getQueryString(void) const { return m_query_string; }
69 
71  inline const std::string& getQuery(const std::string& key) const {
72  return getValue(m_query_params, key);
73  }
74 
76  inline QueryParams& getQueryParams(void) {
77  return m_query_params;
78  }
79 
81  inline bool hasQuery(const std::string& key) const {
82  return(m_query_params.find(key) != m_query_params.end());
83  }
84 
86  inline void setMethod(const std::string& str) {
87  m_method = str;
89  }
90 
92  inline void setResource(const std::string& str) {
93  m_resource = m_original_resource = str;
95  }
96 
98  inline void changeResource(const std::string& str) { m_resource = str; }
99 
101  inline void setQueryString(const std::string& str) {
102  m_query_string = str;
103  clearFirstLine();
104  }
105 
107  inline void addQuery(const std::string& key, const std::string& value) {
108  m_query_params.insert(std::make_pair(key, value));
109  }
110 
112  inline void changeQuery(const std::string& key, const std::string& value) {
113  changeValue(m_query_params, key, value);
114  }
115 
117  inline void deleteQuery(const std::string& key) {
118  deleteValue(m_query_params, key);
119  }
120 
122  inline void useQueryParamsForQueryString(void) {
123  setQueryString(make_query_string(m_query_params));
124  }
125 
127  inline void useQueryParamsForPostContent(void) {
128  std::string post_content(make_query_string(m_query_params));
129  setContentLength(post_content.size());
130  char *ptr = createContentBuffer(); // null-terminates buffer
131  if (! post_content.empty())
132  memcpy(ptr, post_content.c_str(), post_content.size());
133  setMethod(REQUEST_METHOD_POST);
134  setContentType(CONTENT_TYPE_URLENCODED);
135  }
136 
138  inline void setContent(const std::string &value) {
139  setContentLength(value.size());
140  char *ptr = createContentBuffer();
141  if (! value.empty())
142  memcpy(ptr, value.c_str(), value.size());
143  }
144 
146  inline void setUser(PionUserPtr user) { m_user_record = user; }
147 
149  inline PionUserPtr getUser() const { return m_user_record; }
150 
151 
152 protected:
153 
155  virtual void updateFirstLine(void) const {
156  // start out with the request method
157  m_first_line = m_method;
158  m_first_line += ' ';
159  // append the resource requested
160  m_first_line += m_resource;
161  if (! m_query_string.empty()) {
162  // append query string if not empty
163  m_first_line += '?';
164  m_first_line += m_query_string;
165  }
166  m_first_line += ' ';
167  // append HTTP version
169  }
170 
171 
172 private:
173 
175  std::string m_method;
176 
178  std::string m_resource;
179 
181  std::string m_original_resource;
182 
184  std::string m_query_string;
185 
187  QueryParams m_query_params;
188 
190  PionUserPtr m_user_record;
191 };
192 
193 
195 typedef boost::shared_ptr<HTTPRequest> HTTPRequestPtr;
196 
197 
198 } // end namespace net
199 } // end namespace pion
200 
201 #endif
void changeQuery(const std::string &key, const std::string &value)
changes the value of a query key
QueryParams & getQueryParams(void)
returns the query parameters
Definition: HTTPRequest.hpp:76
void addQuery(const std::string &key, const std::string &value)
adds a value for the query key
virtual void clear(void)
clears all message data
static void changeValue(DictionaryType &dict, const std::string &key, const std::string &value)
static void deleteValue(DictionaryType &dict, const std::string &key)
const std::string & getResource(void) const
returns the resource uri-stem to be delivered (possibly the result of a redirect) ...
Definition: HTTPRequest.hpp:62
void useQueryParamsForQueryString(void)
use the query parameters to build a query string for the request
HTTPRequest(void)
constructs a new HTTPRequest object (default constructor)
Definition: HTTPRequest.hpp:39
static std::string make_query_string(const QueryParams &query_params)
builds an HTTP query string from a collection of query parameters
Definition: HTTPTypes.cpp:111
void changeResource(const std::string &str)
changes the resource or uri-stem to be delivered (called as the result of a redirect) ...
Definition: HTTPRequest.hpp:98
bool hasQuery(const std::string &key) const
returns true if at least one value for the query key is defined
Definition: HTTPRequest.hpp:81
virtual void clear(void)
clears all request data
Definition: HTTPRequest.hpp:45
void setResource(const std::string &str)
sets the resource or uri-stem originally requested
Definition: HTTPRequest.hpp:92
const std::string & getQuery(const std::string &key) const
returns a value for the query key if any are defined; otherwise, an empty string
Definition: HTTPRequest.hpp:71
void useQueryParamsForPostContent(void)
use the query parameters to build POST content for the request
StringDictionary QueryParams
data type for HTTP query parameters
Definition: HTTPTypes.hpp:106
virtual void updateFirstLine(void) const
updates the string containing the first line for the HTTP message
char * createContentBuffer(void)
const std::string & getOriginalResource(void) const
returns the resource uri-stem originally requested
Definition: HTTPRequest.hpp:65
void setContentLength(const std::size_t n)
sets the length of the payload content (in bytes)
HTTPRequest(const std::string &resource)
Definition: HTTPRequest.hpp:35
void setUser(PionUserPtr user)
sets the user record for HTTP request after authentication
const std::string & getMethod(void) const
returns the request method (i.e. GET, POST, PUT)
Definition: HTTPRequest.hpp:59
void deleteQuery(const std::string &key)
removes all values for a query key
virtual bool isContentLengthImplied(void) const
the content length of the message can never be implied for requests
Definition: HTTPRequest.hpp:56
the following enables use of the lock-free cache
const std::string & getQueryString(void) const
returns the uri-query or query string requested
Definition: HTTPRequest.hpp:68
static const std::string & getValue(const DictionaryType &dict, const std::string &key)
void setContentType(const std::string &type)
sets the content type for the message payload
void setContent(const std::string &value)
add content (for POST) from string
std::string getVersionString(void) const
returns a string representation of the HTTP version (i.e. "HTTP/1.1")
PionUserPtr getUser() const
get the user record for HTTP request after authentication
virtual ~HTTPRequest()
virtual destructor
Definition: HTTPRequest.hpp:42
void clearFirstLine(void) const
void setQueryString(const std::string &str)
sets the uri-query or query string requested
void setMethod(const std::string &str)
sets the HTTP request method (i.e. GET, POST, PUT)
Definition: HTTPRequest.hpp:86