| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wx/uri.h |
| 3 | // Purpose: wxURI - Class for parsing URIs |
| 4 | // Author: Ryan Norton |
| 5 | // Vadim Zeitlin (UTF-8 URI support, many other changes) |
| 6 | // Created: 07/01/2004 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 2004 Ryan Norton |
| 9 | // 2008 Vadim Zeitlin |
| 10 | // Licence: wxWindows Licence |
| 11 | ///////////////////////////////////////////////////////////////////////////// |
| 12 | |
| 13 | #ifndef _WX_URI_H_ |
| 14 | #define _WX_URI_H_ |
| 15 | |
| 16 | #include "wx/defs.h" |
| 17 | #include "wx/object.h" |
| 18 | #include "wx/string.h" |
| 19 | #include "wx/arrstr.h" |
| 20 | |
| 21 | // Host Type that the server component can be |
| 22 | enum wxURIHostType |
| 23 | { |
| 24 | wxURI_REGNAME, // Host is a normal register name (www.mysite.com etc.) |
| 25 | wxURI_IPV4ADDRESS, // Host is a version 4 ip address (192.168.1.100) |
| 26 | wxURI_IPV6ADDRESS, // Host is a version 6 ip address [aa:aa:aa:aa::aa:aa]:5050 |
| 27 | wxURI_IPVFUTURE // Host is a future ip address (wxURI is unsure what kind) |
| 28 | }; |
| 29 | |
| 30 | // Component Flags |
| 31 | enum wxURIFieldType |
| 32 | { |
| 33 | wxURI_SCHEME = 1, |
| 34 | wxURI_USERINFO = 2, |
| 35 | wxURI_SERVER = 4, |
| 36 | wxURI_PORT = 8, |
| 37 | wxURI_PATH = 16, |
| 38 | wxURI_QUERY = 32, |
| 39 | wxURI_FRAGMENT = 64 |
| 40 | }; |
| 41 | |
| 42 | // Miscellaneous other flags |
| 43 | enum wxURIFlags |
| 44 | { |
| 45 | wxURI_STRICT = 1 |
| 46 | }; |
| 47 | |
| 48 | |
| 49 | // Generic class for parsing URIs. |
| 50 | // |
| 51 | // See RFC 3986 |
| 52 | class WXDLLIMPEXP_BASE wxURI : public wxObject |
| 53 | { |
| 54 | public: |
| 55 | wxURI(); |
| 56 | wxURI(const wxString& uri); |
| 57 | |
| 58 | // default copy ctor, assignment operator and dtor are ok |
| 59 | |
| 60 | bool Create(const wxString& uri); |
| 61 | |
| 62 | wxURI& operator=(const wxString& string) |
| 63 | { |
| 64 | Create(string); |
| 65 | return *this; |
| 66 | } |
| 67 | |
| 68 | bool operator==(const wxURI& uri) const; |
| 69 | |
| 70 | // various accessors |
| 71 | |
| 72 | bool HasScheme() const { return (m_fields & wxURI_SCHEME) != 0; } |
| 73 | bool HasUserInfo() const { return (m_fields & wxURI_USERINFO) != 0; } |
| 74 | bool HasServer() const { return (m_fields & wxURI_SERVER) != 0; } |
| 75 | bool HasPort() const { return (m_fields & wxURI_PORT) != 0; } |
| 76 | bool HasPath() const { return (m_fields & wxURI_PATH) != 0; } |
| 77 | bool HasQuery() const { return (m_fields & wxURI_QUERY) != 0; } |
| 78 | bool HasFragment() const { return (m_fields & wxURI_FRAGMENT) != 0; } |
| 79 | |
| 80 | const wxString& GetScheme() const { return m_scheme; } |
| 81 | const wxString& GetPath() const { return m_path; } |
| 82 | const wxString& GetQuery() const { return m_query; } |
| 83 | const wxString& GetFragment() const { return m_fragment; } |
| 84 | const wxString& GetPort() const { return m_port; } |
| 85 | const wxString& GetUserInfo() const { return m_userinfo; } |
| 86 | const wxString& GetServer() const { return m_server; } |
| 87 | wxURIHostType GetHostType() const { return m_hostType; } |
| 88 | |
| 89 | // these functions only work if the user information part of the URI is in |
| 90 | // the usual (but insecure and hence explicitly recommended against by the |
| 91 | // RFC) "user:password" form |
| 92 | wxString GetUser() const; |
| 93 | wxString GetPassword() const; |
| 94 | |
| 95 | |
| 96 | // combine all URI components into a single string |
| 97 | // |
| 98 | // BuildURI() returns the real URI suitable for use with network libraries, |
| 99 | // for example, while BuildUnescapedURI() returns a string suitable to be |
| 100 | // shown to the user. |
| 101 | wxString BuildURI() const { return DoBuildURI(&wxURI::Nothing); } |
| 102 | wxString BuildUnescapedURI() const { return DoBuildURI(&wxURI::Unescape); } |
| 103 | |
| 104 | // the escaped URI should contain only ASCII characters, including possible |
| 105 | // escape sequences |
| 106 | static wxString Unescape(const wxString& escapedURI); |
| 107 | |
| 108 | |
| 109 | void Resolve(const wxURI& base, int flags = wxURI_STRICT); |
| 110 | bool IsReference() const; |
| 111 | |
| 112 | protected: |
| 113 | void Clear(); |
| 114 | |
| 115 | // common part of BuildURI() and BuildUnescapedURI() |
| 116 | wxString DoBuildURI(wxString (*funcDecode)(const wxString&)) const; |
| 117 | |
| 118 | // function which returns its argument unmodified, this is used by |
| 119 | // BuildURI() to tell DoBuildURI() that nothing needs to be done with the |
| 120 | // URI components |
| 121 | static wxString Nothing(const wxString& value) { return value; } |
| 122 | |
| 123 | bool Parse(const char* uri); |
| 124 | |
| 125 | const char* ParseAuthority (const char* uri); |
| 126 | const char* ParseScheme (const char* uri); |
| 127 | const char* ParseUserInfo (const char* uri); |
| 128 | const char* ParseServer (const char* uri); |
| 129 | const char* ParsePort (const char* uri); |
| 130 | const char* ParsePath (const char* uri); |
| 131 | const char* ParseQuery (const char* uri); |
| 132 | const char* ParseFragment (const char* uri); |
| 133 | |
| 134 | |
| 135 | static bool ParseH16(const char*& uri); |
| 136 | static bool ParseIPv4address(const char*& uri); |
| 137 | static bool ParseIPv6address(const char*& uri); |
| 138 | static bool ParseIPvFuture(const char*& uri); |
| 139 | |
| 140 | // should be called with i pointing to '%', returns the encoded character |
| 141 | // following it or -1 if invalid and advances i past it (so that it points |
| 142 | // to the last character consumed on return) |
| 143 | static int DecodeEscape(wxString::const_iterator& i); |
| 144 | |
| 145 | // append next character pointer to by p to the string in an escaped form |
| 146 | // and advance p past it |
| 147 | // |
| 148 | // if the next character is '%' and it's followed by 2 hex digits, they are |
| 149 | // not escaped (again) by this function, this allows to keep (backwards- |
| 150 | // compatible) ambiguity about the input format to wxURI::Create(): it can |
| 151 | // be either already escaped or not |
| 152 | void AppendNextEscaped(wxString& s, const char *& p); |
| 153 | |
| 154 | // convert hexadecimal digit to its value; return -1 if c isn't valid |
| 155 | static int CharToHex(char c); |
| 156 | |
| 157 | // split an URI path string in its component segments (including empty and |
| 158 | // "." ones, no post-processing is done) |
| 159 | static wxArrayString SplitInSegments(const wxString& path); |
| 160 | |
| 161 | // various URI grammar helpers |
| 162 | static bool IsUnreserved(char c); |
| 163 | static bool IsReserved(char c); |
| 164 | static bool IsGenDelim(char c); |
| 165 | static bool IsSubDelim(char c); |
| 166 | static bool IsHex(char c); |
| 167 | static bool IsAlpha(char c); |
| 168 | static bool IsDigit(char c); |
| 169 | static bool IsEndPath(char c); |
| 170 | |
| 171 | wxString m_scheme; |
| 172 | wxString m_path; |
| 173 | wxString m_query; |
| 174 | wxString m_fragment; |
| 175 | |
| 176 | wxString m_userinfo; |
| 177 | wxString m_server; |
| 178 | wxString m_port; |
| 179 | |
| 180 | wxURIHostType m_hostType; |
| 181 | |
| 182 | size_t m_fields; |
| 183 | |
| 184 | DECLARE_DYNAMIC_CLASS(wxURI) |
| 185 | }; |
| 186 | |
| 187 | #endif // _WX_URI_H_ |
| 188 | |