# 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     }
    00054
    00056     virtual bool isContentLengthImplied(void) const { return false; }
    00057
    00059     inline const std::string& getMethod(void) const { return m_method; }
    00060
    00062     inline const std::string& getResource(void) const { return m_resource; }
    00063
    00065     inline const std::string& getOriginalResource(void) const { return m_original_resource; }
    00066
    00068     inline const std::string& getQueryString(void) const { return m_query_string; }
    00069
    00071     inline const std::string& getQuery(const std::string& key) const {
    00072         return getValue(m_query_params, key);
    00073     }
    00074
    00076     inline QueryParams& getQueryParams(void) {
    00077         return m_query_params;
    00078     }
    00079
    00081     inline bool hasQuery(const std::string& key) const {
    00082         return(m_query_params.find(key) != m_query_params.end());
    00083     }
    00084
    00086     inline void setMethod(const std::string& str) {
    00087         m_method = str;
    00088         clearFirstLine();
    00089     }
    00090
    00092     inline void setResource(const std::string& str) {
    00093         m_resource = m_original_resource = str;
    00094         clearFirstLine();
    00095     }
    00096
    00098     inline void changeResource(const std::string& str) { m_resource = str; }
    00099
    00101     inline void setQueryString(const std::string& str) {
    00102         m_query_string = str;
    00103         clearFirstLine();
    00104     }
    00105
    00107     inline void addQuery(const std::string& key, const std::string& value) {
    00108         m_query_params.insert(std::make_pair(key, value));
    00109     }
    00110
    00112     inline void changeQuery(const std::string& key, const std::string& value) {
    00113         changeValue(m_query_params, key, value);
    00114     }
    00115
    00117     inline void deleteQuery(const std::string& key) {
    00118         deleteValue(m_query_params, key);
    00119     }
    00120
    00122     inline void useQueryParamsForQueryString(void) {
    00123         setQueryString(make_query_string(m_query_params));
    00124     }
    00125
    00127     inline void useQueryParamsForPostContent(void) {
    00128         std::string post_content(make_query_string(m_query_params));
    00129         setContentLength(post_content.size());
    00130         char *ptr = createContentBuffer();  // null-terminates buffer
    00131         if (! post_content.empty())
    00132             memcpy(ptr, post_content.c_str(), post_content.size());
    00133         setMethod(REQUEST_METHOD_POST);
    00134         setContentType(CONTENT_TYPE_URLENCODED);
    00135     }
    00136
    00138     inline void setContent(const std::string &value) {
    00139         setContentLength(value.size());
    00140         char *ptr = createContentBuffer();
    00141         if (! value.empty())
    00142             memcpy(ptr, value.c_str(), value.size());
    00143     }
    00144
    00146     inline void setUser(PionUserPtr user) { m_user_record = user; }
    00147
    00149     inline PionUserPtr getUser() const { return m_user_record; }
    00150
    00151
    00152 protected:
    00153
    00155     virtual void updateFirstLine(void) const {
    00156         // start out with the request method
    00157         m_first_line = m_method;
    00158         m_first_line += ' ';
    00159         // append the resource requested
    00160         m_first_line += m_resource;
    00161         if (! m_query_string.empty()) {
    00162             // append query string if not empty
    00163             m_first_line += '?';
    00164             m_first_line += m_query_string;
    00165         }
    00166         m_first_line += ' ';
    00167         // append HTTP version
    00168         m_first_line += getVersionString();
    00169     }
    00170
    00171
    00172 private:
    00173
    00175     std::string                     m_method;
    00176
    00178     std::string                     m_resource;
    00179
    00181     std::string                     m_original_resource;
    00182
    00184     std::string                     m_query_string;
    00185
    00187     QueryParams                     m_query_params;
    00188
    00190     PionUserPtr                     m_user_record;
    00191 };
    00192
    00193
    00195 typedef boost::shared_ptr<HTTPRequest>      HTTPRequestPtr;
    00196
    00197
    00198 }   // end namespace net
    00199 }   // end namespace pion
    00200
    00201 #endif