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