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_user_record.reset();
00053         m_charset.clear();
00054     }
00055 
00057     virtual bool isContentLengthImplied(void) const { return false; }
00058 
00060     inline const std::string& getMethod(void) const { return m_method; }
00061     
00063     inline const std::string& getResource(void) const { return m_resource; }
00064 
00066     inline const std::string& getOriginalResource(void) const { return m_original_resource; }
00067 
00069     inline const std::string& getQueryString(void) const { return m_query_string; }
00070     
00072     inline const std::string& getQuery(const std::string& key) const {
00073         return getValue(m_query_params, key);
00074     }
00075 
00077     inline QueryParams& getQueryParams(void) {
00078         return m_query_params;
00079     }
00080     
00082     inline bool hasQuery(const std::string& key) const {
00083         return(m_query_params.find(key) != m_query_params.end());
00084     }
00085         
00087     inline void setMethod(const std::string& str) { 
00088         m_method = str;
00089         clearFirstLine();
00090     }
00091     
00093     inline void setResource(const std::string& str) {
00094         m_resource = m_original_resource = str;
00095         clearFirstLine();
00096     }
00097 
00099     inline void changeResource(const std::string& str) { m_resource = str; }
00100 
00102     inline void setQueryString(const std::string& str) {
00103         m_query_string = str;
00104         clearFirstLine();
00105     }
00106     
00108     inline void addQuery(const std::string& key, const std::string& value) {
00109         m_query_params.insert(std::make_pair(key, value));
00110     }
00111     
00113     inline void changeQuery(const std::string& key, const std::string& value) {
00114         changeValue(m_query_params, key, value);
00115     }
00116     
00118     inline void deleteQuery(const std::string& key) {
00119         deleteValue(m_query_params, key);
00120     }
00121     
00123     inline void useQueryParamsForQueryString(void) {
00124         setQueryString(make_query_string(m_query_params));
00125     }
00126 
00128     inline void useQueryParamsForPostContent(void) {
00129         std::string post_content(make_query_string(m_query_params));
00130         setContentLength(post_content.size());
00131         char *ptr = createContentBuffer();  // null-terminates buffer
00132         if (! post_content.empty())
00133             memcpy(ptr, post_content.c_str(), post_content.size());
00134         setMethod(REQUEST_METHOD_POST);
00135         setContentType(CONTENT_TYPE_URLENCODED);
00136     }
00137 
00139     inline void setContent(const std::string &value) {
00140         setContentLength(value.size());
00141         char *ptr = createContentBuffer();
00142         if (! value.empty())
00143             memcpy(ptr, value.c_str(), value.size());
00144     }
00145     
00147     inline void setUser(PionUserPtr user) { m_user_record = user; }
00148     
00150     inline PionUserPtr getUser() const { return m_user_record; }
00151 
00152 
00153 protected:
00154 
00156     virtual void updateFirstLine(void) const {
00157         // start out with the request method
00158         m_first_line = m_method;
00159         m_first_line += ' ';
00160         // append the resource requested
00161         m_first_line += m_resource;
00162         if (! m_query_string.empty()) {
00163             // append query string if not empty
00164             m_first_line += '?';
00165             m_first_line += m_query_string;
00166         }
00167         m_first_line += ' ';
00168         // append HTTP version
00169         m_first_line += getVersionString();
00170     }
00171     
00172     
00173 private:
00174 
00176     std::string                     m_method;
00177 
00179     std::string                     m_resource;
00180 
00182     std::string                     m_original_resource;
00183 
00185     std::string                     m_query_string;
00186     
00188     QueryParams                     m_query_params;
00189 
00191     PionUserPtr                     m_user_record;
00192 
00195     std::string                     m_charset;
00196 };
00197 
00198 
00200 typedef boost::shared_ptr<HTTPRequest>      HTTPRequestPtr;
00201 
00202 
00203 }   // end namespace net
00204 }   // end namespace pion
00205 
00206 #endif

Generated on Fri Apr 30 14:48:53 2010 for pion-net by  doxygen 1.4.7