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  AddField(const char* name, const char* value);
00091     const char* GetField(const char* name);
00092     NPT_String  ToString();
00093 
00094 private:
00095     // members
00096     NPT_List<Field> m_Fields;
00097 };
00098 
00099 /*----------------------------------------------------------------------
00100 |   NPT_Url
00101 +---------------------------------------------------------------------*/
00102 class NPT_Url : public NPT_Uri {
00103 public:
00104     // constructors and destructor
00105     NPT_Url();
00106     NPT_Url(const char* url, 
00107             SchemeId    expected_scheme = SCHEME_ID_UNKNOWN, 
00108             NPT_UInt16  default_port = NPT_URL_INVALID_PORT);
00109     NPT_Url(const char* host, 
00110             NPT_UInt16  port, 
00111             const char* path,
00112             const char* query = NULL,
00113             const char* fragment = NULL);
00114 
00115     // methods
00116     const NPT_String&  GetHost() const     { return m_Host;     }
00117     NPT_UInt16         GetPort() const     { return m_Port;     }
00118     const NPT_String&  GetPath() const     { return m_Path;     }
00119     const NPT_String&  GetQuery() const    { return m_Query;    }
00120     const NPT_String&  GetFragment() const { return m_Fragment; }
00121     virtual bool       IsValid() const;
00122     bool               HasQuery()    const { return m_HasQuery;    } 
00123     bool               HasFragment() const { return m_HasFragment; }
00124     NPT_Result         SetHost(const char*  host);
00125     NPT_Result         SetPort(NPT_UInt16 port);
00126     NPT_Result         SetPath(const char* path);
00127     NPT_Result         SetPathPlus(const char* path_plus);
00128     NPT_Result         SetQuery(const char* query);
00129     NPT_Result         SetFragment(const char* fragment);
00130     virtual NPT_String ToRequestString(bool with_fragment = false) const;
00131     virtual NPT_String ToStringWithDefaultPort(NPT_UInt16 default_port, bool with_fragment = true) const;
00132     virtual NPT_String ToString(bool with_fragment = true) const;
00133 
00134 protected:
00135     // members
00136     NPT_String m_Host;
00137     NPT_UInt16 m_Port;
00138     NPT_String m_Path;
00139     bool       m_HasQuery;
00140     NPT_String m_Query;
00141     bool       m_HasFragment;
00142     NPT_String m_Fragment;
00143 };
00144 
00145 #endif // _NPT_URI_H_