NptUri.h

00001 /*****************************************************************
00002 |
00003 |   Neptune - URI
00004 |
00005 |   (c) 2001-2006 Gilles Boccon-Gibod
00006 |   Author: Gilles Boccon-Gibod (bok@bok.net)
00007 |
00008  ****************************************************************/
00009 
00010 #ifndef _NPT_URI_H_
00011 #define _NPT_URI_H_
00012 
00013 /*----------------------------------------------------------------------
00014 |   includes
00015 +---------------------------------------------------------------------*/
00016 #include "NptStrings.h"
00017 #include "NptList.h"
00018 
00019 /*----------------------------------------------------------------------
00020 |   constants
00021 +---------------------------------------------------------------------*/
00022 const NPT_UInt16 NPT_URL_INVALID_PORT = 0;
00023 
00024 /*----------------------------------------------------------------------
00025 |   NPT_Uri
00026 +---------------------------------------------------------------------*/
00027 class NPT_Uri {
00028 public:
00029     // types
00030     typedef enum {
00031         SCHEME_ID_UNKNOWN,
00032         SCHEME_ID_HTTP
00033     } SchemeId;
00034 
00035     // constants. use as a parameter to Encode()
00036     static const char* const PathCharsToEncode;
00037     static const char* const QueryCharsToEncode;
00038     static const char* const FragmentCharsToEncode;
00039     static const char* const UnsafeCharsToEncode;
00040 
00041     // class methods
00042     static NPT_String PercentEncode(const char* str, const char* chars, bool encode_percents=true);
00043     static NPT_String PercentDecode(const char* str);
00044     static SchemeId   ParseScheme(const NPT_String& scheme);
00045 
00046     // methods
00047     NPT_Uri() : m_SchemeId(SCHEME_ID_UNKNOWN) {}
00048     virtual ~NPT_Uri() {}
00049     const NPT_String& GetScheme() const {
00050         return m_Scheme;
00051     }
00052     void SetScheme(const char* scheme);
00053     NPT_Result SetSchemeFromUri(const char* uri);
00054     SchemeId GetSchemeId() const {
00055         return m_SchemeId;
00056     }
00057 
00058 protected:
00059     // members
00060     NPT_String m_Scheme;
00061     SchemeId   m_SchemeId;
00062 };
00063 
00064 /*----------------------------------------------------------------------
00065 |   NPT_UrlQuery
00066 +---------------------------------------------------------------------*/
00067 class NPT_UrlQuery
00068 {
00069 public:
00070     // class methods
00071     static NPT_String UrlEncode(const char* str, bool encode_percents=true);
00072     static NPT_String UrlDecode(const char* str);
00073     
00074     // types
00075     struct Field {
00076         Field(const char* name, const char* value) :
00077             m_Name(name), m_Value(value) {}
00078         NPT_String m_Name;
00079         NPT_String m_Value;
00080     };
00081 
00082     // constructor
00083     NPT_UrlQuery() {}
00084     NPT_UrlQuery(const char* query);
00085 
00086     // accessors
00087     NPT_List<Field>& GetFields() { return m_Fields; }
00088 
00089     // methods
00090     NPT_Result  Parse(const char* query);
00091     NPT_Result  AddField(const char* name, const char* value);
00092     const char* GetField(const char* name);
00093     NPT_String  ToString();
00094 
00095 private:
00096     // members
00097     NPT_List<Field> m_Fields;
00098 };
00099 
00100 /*----------------------------------------------------------------------
00101 |   NPT_Url
00102 +---------------------------------------------------------------------*/
00103 class NPT_Url : public NPT_Uri {
00104 public:
00105     // constructors and destructor
00106     NPT_Url();
00107     NPT_Url(const char* url, 
00108             SchemeId    expected_scheme = SCHEME_ID_UNKNOWN, 
00109             NPT_UInt16  default_port = NPT_URL_INVALID_PORT);
00110     NPT_Url(const char* scheme,
00111             const char* host, 
00112             NPT_UInt16  port, 
00113             const char* path,
00114             const char* query = NULL,
00115             const char* fragment = NULL);
00116 
00117     // methods
00118     const NPT_String&  GetHost() const     { return m_Host;     }
00119     NPT_UInt16         GetPort() const     { return m_Port;     }
00120     const NPT_String&  GetPath() const     { return m_Path;     }
00121     const NPT_String&  GetQuery() const    { return m_Query;    }
00122     const NPT_String&  GetFragment() const { return m_Fragment; }
00123     virtual bool       IsValid() const;
00124     bool               HasQuery()    const { return m_HasQuery;    } 
00125     bool               HasFragment() const { return m_HasFragment; }
00126     NPT_Result         SetHost(const char*  host);
00127     NPT_Result         SetPort(NPT_UInt16 port);
00128     NPT_Result         SetPath(const char* path);
00129     NPT_Result         SetPathPlus(const char* path_plus);
00130     NPT_Result         SetQuery(const char* query);
00131     NPT_Result         SetFragment(const char* fragment);
00132     virtual NPT_String ToRequestString(bool with_fragment = false) const;
00133     virtual NPT_String ToStringWithDefaultPort(NPT_UInt16 default_port, bool with_fragment = true) const;
00134     virtual NPT_String ToString(bool with_fragment = true) const;
00135 
00136 protected:
00137     // members
00138     NPT_String m_Host;
00139     NPT_UInt16 m_Port;
00140     NPT_String m_Path;
00141     bool       m_HasQuery;
00142     NPT_String m_Query;
00143     bool       m_HasFragment;
00144     NPT_String m_Fragment;
00145 };
00146 
00147 #endif // _NPT_URI_H_