net/include/pion/net/HTTPTypes.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_HTTPTYPES_HEADER__
00011 #define __PION_HTTPTYPES_HEADER__
00012 
00013 #include <string>
00014 #include <cctype>
00015 #include <pion/PionConfig.hpp>
00016 #include <pion/PionHashMap.hpp>
00017 
00018 
00019 namespace pion {    // begin namespace pion
00020 namespace net {     // begin namespace net (Pion Network Library)
00021 
00025 struct PION_NET_API HTTPTypes
00026 {
00028     virtual ~HTTPTypes() {}
00029     
00030     // generic strings used by HTTP
00031     static const std::string    STRING_EMPTY;
00032     static const std::string    STRING_CRLF;
00033     static const std::string    STRING_HTTP_VERSION;
00034     static const std::string    HEADER_NAME_VALUE_DELIMITER;
00035     
00036     // common HTTP header names
00037     static const std::string    HEADER_HOST;
00038     static const std::string    HEADER_COOKIE;
00039     static const std::string    HEADER_SET_COOKIE;
00040     static const std::string    HEADER_CONNECTION;
00041     static const std::string    HEADER_CONTENT_TYPE;
00042     static const std::string    HEADER_CONTENT_LENGTH;
00043     static const std::string    HEADER_CONTENT_LOCATION;
00044     static const std::string    HEADER_CONTENT_ENCODING;
00045     static const std::string    HEADER_LAST_MODIFIED;
00046     static const std::string    HEADER_IF_MODIFIED_SINCE;
00047     static const std::string    HEADER_TRANSFER_ENCODING;
00048     static const std::string    HEADER_LOCATION;
00049     static const std::string    HEADER_AUTHORIZATION;
00050     static const std::string    HEADER_REFERER;
00051     static const std::string    HEADER_USER_AGENT;
00052     static const std::string    HEADER_X_FORWARDED_FOR;
00053 
00054     // common HTTP content types
00055     static const std::string    CONTENT_TYPE_HTML;
00056     static const std::string    CONTENT_TYPE_TEXT;
00057     static const std::string    CONTENT_TYPE_XML;
00058     static const std::string    CONTENT_TYPE_URLENCODED;
00059     
00060     // common HTTP request methods
00061     static const std::string    REQUEST_METHOD_HEAD;
00062     static const std::string    REQUEST_METHOD_GET;
00063     static const std::string    REQUEST_METHOD_PUT;
00064     static const std::string    REQUEST_METHOD_POST;
00065     static const std::string    REQUEST_METHOD_DELETE;
00066     
00067     // common HTTP response messages
00068     static const std::string    RESPONSE_MESSAGE_OK;
00069     static const std::string    RESPONSE_MESSAGE_CREATED;
00070     static const std::string    RESPONSE_MESSAGE_NO_CONTENT;
00071     static const std::string    RESPONSE_MESSAGE_FOUND;
00072     static const std::string    RESPONSE_MESSAGE_UNAUTHORIZED;
00073     static const std::string    RESPONSE_MESSAGE_FORBIDDEN;
00074     static const std::string    RESPONSE_MESSAGE_NOT_FOUND;
00075     static const std::string    RESPONSE_MESSAGE_METHOD_NOT_ALLOWED;
00076     static const std::string    RESPONSE_MESSAGE_NOT_MODIFIED;
00077     static const std::string    RESPONSE_MESSAGE_BAD_REQUEST;
00078     static const std::string    RESPONSE_MESSAGE_SERVER_ERROR;
00079     static const std::string    RESPONSE_MESSAGE_NOT_IMPLEMENTED;
00080     static const std::string    RESPONSE_MESSAGE_CONTINUE;
00081 
00082     // common HTTP response codes
00083     static const unsigned int   RESPONSE_CODE_OK;
00084     static const unsigned int   RESPONSE_CODE_CREATED;
00085     static const unsigned int   RESPONSE_CODE_NO_CONTENT;
00086     static const unsigned int   RESPONSE_CODE_FOUND;
00087     static const unsigned int   RESPONSE_CODE_UNAUTHORIZED;
00088     static const unsigned int   RESPONSE_CODE_FORBIDDEN;
00089     static const unsigned int   RESPONSE_CODE_NOT_FOUND;
00090     static const unsigned int   RESPONSE_CODE_METHOD_NOT_ALLOWED;
00091     static const unsigned int   RESPONSE_CODE_NOT_MODIFIED;
00092     static const unsigned int   RESPONSE_CODE_BAD_REQUEST;
00093     static const unsigned int   RESPONSE_CODE_SERVER_ERROR;
00094     static const unsigned int   RESPONSE_CODE_NOT_IMPLEMENTED;
00095     static const unsigned int   RESPONSE_CODE_CONTINUE;
00096     
00098     struct CaseInsensitiveEqual {
00099         inline bool operator()(const std::string& str1, const std::string& str2) const {
00100             if (str1.size() != str2.size())
00101                 return false;
00102             std::string::const_iterator it1 = str1.begin();
00103             std::string::const_iterator it2 = str2.begin();
00104             while ( (it1!=str1.end()) && (it2!=str2.end()) ) {
00105                 if (tolower(*it1) != tolower(*it2))
00106                     return false;
00107                 ++it1;
00108                 ++it2;
00109             }
00110             return true;
00111         }
00112     };
00113 
00115     struct CaseInsensitiveHash {
00116         inline unsigned long operator()(const std::string& str) const {
00117             unsigned long value = 0;
00118             for (std::string::const_iterator i = str.begin(); i!= str.end(); ++i)
00119                 value = static_cast<unsigned char>(tolower(*i)) + (value << 6) + (value << 16) - value;
00120             return value;
00121         }
00122     };
00123 
00125     struct CaseInsensitiveLess {
00126         inline bool operator()(const std::string& str1, const std::string& str2) const {
00127             std::string::const_iterator it1 = str1.begin();
00128             std::string::const_iterator it2 = str2.begin();
00129             while ( (it1 != str1.end()) && (it2 != str2.end()) ) {
00130                 if (tolower(*it1) != tolower(*it2))
00131                     return (tolower(*it1) < tolower(*it2));
00132                 ++it1;
00133                 ++it2;
00134             }
00135             return (str1.size() < str2.size());
00136         }
00137     };
00138 
00139 #ifdef _MSC_VER
00141     struct CaseInsensitiveHashCompare : public stdext::hash_compare<std::string, CaseInsensitiveLess> {
00142         // makes operator() with two arguments visible, otherwise it would be hidden by the operator() defined here
00143         using stdext::hash_compare<std::string, CaseInsensitiveLess>::operator();
00144 
00145         inline size_t operator()(const std::string& str) const {
00146             return CaseInsensitiveHash()(str);
00147         }
00148     };
00149 #endif
00150 
00152 #ifdef _MSC_VER
00153     typedef PION_HASH_MULTIMAP<std::string, std::string, CaseInsensitiveHashCompare>    Headers;
00154     typedef PION_HASH_MULTIMAP<std::string, std::string, CaseInsensitiveHashCompare>    CookieParams;
00155 #else
00156     typedef PION_HASH_MULTIMAP<std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual >    Headers;
00157     typedef PION_HASH_MULTIMAP<std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual >    CookieParams;
00158 #endif
00159 
00161     typedef PION_HASH_MULTIMAP<std::string, std::string, PION_HASH_STRING > StringDictionary;
00162 
00164     typedef StringDictionary    QueryParams;
00165 
00166     
00173     static bool base64_decode(std::string const &input, std::string & output);
00174 
00181     static bool base64_encode(std::string const &input, std::string & output);
00182 
00184     static std::string url_decode(const std::string& str);
00185 
00187     static std::string url_encode(const std::string& str);
00188     
00190     static std::string get_date_string(const time_t t);
00191 
00193     static std::string make_query_string(const QueryParams& query_params);
00194     
00206     static std::string make_set_cookie_header(const std::string& name,
00207                                               const std::string& value,
00208                                               const std::string& path,
00209                                               const bool has_max_age = false,
00210                                               const unsigned long max_age = 0);     
00211 };
00212 
00213 }   // end namespace net
00214 }   // end namespace pion
00215 
00216 #endif

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