net/include/pion/net/HTTPRequest.hpp

00001 // ------------------------------------------------------------------
00002 // pion-net: a C++ framework for building lightweight HTTP interfaces
00003 // ------------------------------------------------------------------
00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
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_HTTPREQUEST_HEADER__
00011 #define __PION_HTTPREQUEST_HEADER__
00012 
00013 #include <boost/shared_ptr.hpp>
00014 #include <pion/PionConfig.hpp>
00015 #include <pion/net/HTTPMessage.hpp>
00016 #include <pion/net/PionUser.hpp>
00017 
00018 namespace pion {    // begin namespace pion
00019 namespace net {     // begin namespace net (Pion Network Library)
00020 
00021 
00025 class HTTPRequest
00026     : public HTTPMessage
00027 {
00028 public:
00029 
00035     HTTPRequest(const std::string& resource)
00036         : m_method(REQUEST_METHOD_GET), m_resource(resource) {}
00037     
00039     HTTPRequest(void) : m_method(REQUEST_METHOD_GET) {}
00040     
00042     virtual ~HTTPRequest() {}
00043 
00045     virtual void clear(void) {
00046         HTTPMessage::clear();
00047         m_method.erase();
00048         m_resource.erase();
00049         m_original_resource.erase();
00050         m_query_string.erase();
00051         m_query_params.clear();
00052         m_cookie_params.clear();
00053         m_user_record.reset();
00054         m_charset.clear();
00055     }
00056 
00058     virtual bool isContentLengthImplied(void) const { return false; }
00059 
00061     inline const std::string& getMethod(void) const { return m_method; }
00062     
00064     inline const std::string& getResource(void) const { return m_resource; }
00065 
00067     inline const std::string& getOriginalResource(void) const { return m_original_resource; }
00068 
00070     inline const std::string& getQueryString(void) const { return m_query_string; }
00071     
00073     inline const std::string& getQuery(const std::string& key) const {
00074         return getValue(m_query_params, key);
00075     }
00076 
00079     inline const std::string& getCookie(const std::string& key) const {
00080         return getValue(m_cookie_params, key);
00081     }
00082     
00084     inline QueryParams& getQueryParams(void) {
00085         return m_query_params;
00086     }
00087     
00089     inline CookieParams& getCookieParams(void) {
00090         return m_cookie_params;
00091     }
00092 
00094     inline bool hasQuery(const std::string& key) const {
00095         return(m_query_params.find(key) != m_query_params.end());
00096     }
00097     
00100     inline bool hasCookie(const std::string& key) const {
00101         return(m_cookie_params.find(key) != m_cookie_params.end());
00102     }
00103     
00104     
00106     inline void setMethod(const std::string& str) { 
00107         m_method = str;
00108         clearFirstLine();
00109     }
00110     
00112     inline void setResource(const std::string& str) {
00113         m_resource = m_original_resource = str;
00114         clearFirstLine();
00115     }
00116 
00118     inline void changeResource(const std::string& str) { m_resource = str; }
00119 
00121     inline void setQueryString(const std::string& str) {
00122         m_query_string = str;
00123         clearFirstLine();
00124     }
00125     
00127     inline void addQuery(const std::string& key, const std::string& value) {
00128         m_query_params.insert(std::make_pair(key, value));
00129     }
00130     
00132     inline void changeQuery(const std::string& key, const std::string& value) {
00133         changeValue(m_query_params, key, value);
00134     }
00135     
00137     inline void deleteQuery(const std::string& key) {
00138         deleteValue(m_query_params, key);
00139     }
00140     
00142     inline void useQueryParamsForQueryString(void) {
00143         setQueryString(make_query_string(m_query_params));
00144     }
00145 
00147     inline void useQueryParamsForPostContent(void) {
00148         std::string post_content(make_query_string(m_query_params));
00149         setContentLength(post_content.size());
00150         char *ptr = createContentBuffer();  // null-terminates buffer
00151         if (! post_content.empty())
00152             memcpy(ptr, post_content.c_str(), post_content.size());
00153         setMethod(REQUEST_METHOD_POST);
00154         setContentType(CONTENT_TYPE_URLENCODED);
00155     }
00156 
00158     inline void setContent(const std::string &value) {
00159         setContentLength(value.size());
00160         char *ptr = createContentBuffer();
00161         if (! value.empty())
00162             memcpy(ptr, value.c_str(), value.size());
00163     }
00164     
00167     inline void addCookie(const std::string& key, const std::string& value) {
00168         m_cookie_params.insert(std::make_pair(key, value));
00169     }
00170 
00173     inline void changeCookie(const std::string& key, const std::string& value) {
00174         changeValue(m_cookie_params, key, value);
00175     }
00176 
00179     inline void deleteCookie(const std::string& key) {
00180         deleteValue(m_cookie_params, key);
00181     }
00182     
00184     inline void setUser(PionUserPtr user) { m_user_record = user; }
00185     
00187     inline PionUserPtr getUser() const { return m_user_record; }
00188 
00189 
00190 protected:
00191 
00193     virtual void updateFirstLine(void) const {
00194         // start out with the request method
00195         m_first_line = m_method;
00196         m_first_line += ' ';
00197         // append the resource requested
00198         m_first_line += m_resource;
00199         if (! m_query_string.empty()) {
00200             // append query string if not empty
00201             m_first_line += '?';
00202             m_first_line += m_query_string;
00203         }
00204         m_first_line += ' ';
00205         // append HTTP version
00206         m_first_line += getVersionString();
00207     }
00208     
00209     
00210 private:
00211 
00213     std::string                     m_method;
00214 
00216     std::string                     m_resource;
00217 
00219     std::string                     m_original_resource;
00220 
00222     std::string                     m_query_string;
00223     
00225     QueryParams                     m_query_params;
00226 
00228     CookieParams                    m_cookie_params;
00229 
00231     PionUserPtr                     m_user_record;
00232 
00235     std::string                     m_charset;
00236 };
00237 
00238 
00240 typedef boost::shared_ptr<HTTPRequest>      HTTPRequestPtr;
00241 
00242 
00243 }   // end namespace net
00244 }   // end namespace pion
00245 
00246 #endif

Generated on Fri Dec 4 08:54:29 2009 for pion-net by  doxygen 1.4.7