From 86470d432f52318dfaac17bfd6cf41e5123e82c9 Mon Sep 17 00:00:00 2001 From: Ryan Norton Date: Thu, 28 Oct 2004 09:57:43 +0000 Subject: [PATCH] final wxURI API changes. Changed Get to BuildURI to better reflect what its doing. Added wxURI::Unescape to unescape characters in a url or uri. Added wxURI::BuildUnescapedURI. Changed the wxURL convertXXXuri methods to use uri methods instead, and depreciated these methods. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@30138 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/category.tex | 1 + docs/latex/wx/uri.tex | 46 +++++++++++--- docs/latex/wx/url.tex | 10 --- include/wx/uri.h | 13 ++-- include/wx/url.h | 5 ++ src/common/uri.cpp | 150 ++++++++++++++++++++------------------------- src/common/url.cpp | 4 +- tests/uris/uris.cpp | 13 ++-- 8 files changed, 126 insertions(+), 116 deletions(-) diff --git a/docs/latex/wx/category.tex b/docs/latex/wx/category.tex index 1b4c257..b62983d 100644 --- a/docs/latex/wx/category.tex +++ b/docs/latex/wx/category.tex @@ -296,6 +296,7 @@ These are the data structure classes supported by wxWidgets. \twocolitem{\helpref{wxRealPoint}{wxrealpoint}}{Representation of a point using floating point numbers} \twocolitem{\helpref{wxSize}{wxsize}}{Representation of a size} \twocolitem{\helpref{wxTimeSpan}{wxtimespan}}{A time interval.} +\twocolitem{\helpref{wxURI}{wxuri}}{Represents a Uniform Resource Identifier} \twocolitem{\helpref{wxVariant}{wxvariant}}{A class for storing arbitrary types that may change at run-time} \end{twocollist} diff --git a/docs/latex/wx/uri.tex b/docs/latex/wx/uri.tex index a09ec94..cfe1933 100644 --- a/docs/latex/wx/uri.tex +++ b/docs/latex/wx/uri.tex @@ -88,6 +88,28 @@ Copies this URI from another URI. \docparam{uri}{URI (Uniform Resource Identifier) to initialize with} +\membersection{wxURI::BuildURI}\label{wxuribuilduri} + +\constfunc{wxString}{BuildURI}{\void} + +Builds the URI from its individual components and adds proper seperators. + +If the URI is not a reference or is not resolved, +the URI that is returned from Get is the same one +passed to Create. + + +\membersection{wxURI::BuildUnescapedURI}\label{wxuribuildunescapeduri} + +\constfunc{wxString}{BuildUnescapedURI}{\void} + +Builds the URI from its individual components, adds proper seperators, and +returns escape sequences to normal characters. + +Note that it is preferred to call this over Unescape(BuildURI()) since +BuildUnescapedURI performs some optimizations over the plain method. + + \membersection{wxURI::Create}\label{wxuricreate} \func{void}{Create}{\param{const wxChar* }{uri}} @@ -96,15 +118,6 @@ Creates this URI from the string \arg{uri}. \docparam{uri}{string to initialize from} -\membersection{wxURI::Get}\label{wxuriget} - -\constfunc{wxString}{Get}{\void} - -Obtains the full URI. - -If the URI is not a reference or is not resolved, -the URI that is returned from Get is the same one -passed to Create. \membersection{wxURI::GetFragment}\label{wxurigetfragment} @@ -300,3 +313,18 @@ of the base's is merged with this URI's path, resulting in the URI mode some compatability layers are enabled to allow loopholes from RFCs prior to 2396} +\membersection{wxURI::Unescape}\label{wxuriunescape} + +\func{wxString}{Unescape}{\param{const wxString\& }{uri}} + +Translates all escape sequences (% hex hex) of \arg{uri} into +normal characters and returns the result. + +This is the preferred over wxURL::ConvertFromURI. + +If you want to unescape an entire wxURI, use BuildUnescapedURI instead, +as it performs some optimizations over this method. + +\docparam{uri}{string with escaped characters to convert} + + diff --git a/docs/latex/wx/url.tex b/docs/latex/wx/url.tex index 59a734a..04b4bfb 100644 --- a/docs/latex/wx/url.tex +++ b/docs/latex/wx/url.tex @@ -151,13 +151,3 @@ Sets the proxy to use for this URL. \helpref{wxURL::SetDefaultProxy}{wxurlsetdefaultproxy} -% -% SetProxy -% -\membersection{wxURL::ConvertToValidURI}\label{wxurlconverttovaliduri} - -\func{static wxString}{ConvertToValidURI}{\param{const wxString\&}{ uri}} - -It converts a non-standardized URI to a valid network URI. It encodes non -standard characters. - diff --git a/include/wx/uri.h b/include/wx/uri.h index 473ab42..852c0bd 100644 --- a/include/wx/uri.h +++ b/include/wx/uri.h @@ -61,7 +61,7 @@ public: virtual ~wxURI(); - void Create(const wxString& uri); + const wxChar* Create(const wxString& uri); bool HasScheme() const { return (m_fields & wxURI_SCHEME) == wxURI_SCHEME; } bool HasUser() const { return (m_fields & wxURI_USER) == wxURI_USER; } @@ -80,7 +80,8 @@ public: const wxString& GetServer() const { return m_server; } const wxURIHostType& GetHostType() const { return m_hostType; } - wxString Get() const; + wxString BuildURI() const; + wxString BuildUnescapedURI() const; void Resolve(const wxURI& base, int flags = wxURI_STRICT); bool IsReference() const; @@ -88,6 +89,8 @@ public: wxURI& operator = (const wxURI& uri); wxURI& operator = (const wxString& string); bool operator == (const wxURI& uri) const; + + static wxString Unescape (const wxString& szEscapedURI); protected: wxURI& Assign(const wxURI& uri); @@ -112,16 +115,14 @@ protected: static bool ParseIPv6address(const wxChar*& uri); static bool ParseIPvFuture(const wxChar*& uri); - static void Normalize(wxChar* uri, bool bIgnoreLeads = false); static void UpTree(const wxChar* uristart, const wxChar*& uri); - static wxChar Unescape(const wxChar* s); + static wxChar TranslateEscape(const wxChar* s); static void Escape (wxString& s, const wxChar& c); static bool IsEscape(const wxChar*& uri); - static int CharToHex(const wxChar& c); - + static wxInt32 CharToHex(const wxChar& c); static bool IsUnreserved (const wxChar& c); static bool IsReserved (const wxChar& c); diff --git a/include/wx/url.h b/include/wx/url.h index 3e41a47..36af3f2 100644 --- a/include/wx/url.h +++ b/include/wx/url.h @@ -72,11 +72,16 @@ public: void SetProxy(const wxString& url_proxy); #endif // wxUSE_SOCKETS +#if WXWIN_COMPATIBILITY_2_4 + //Use wxURI instead - delims is ignored static wxString ConvertToValidURI( const wxString& uri, const wxChar* delims = wxT(";/?:@&=+$,") ); + + //Use wxURI::Unescape instead static wxString ConvertFromURI(const wxString& uri); +#endif protected: static wxProtoInfo *ms_protocols; diff --git a/src/common/uri.cpp b/src/common/uri.cpp index 0c9a16f..23c5317 100644 --- a/src/common/uri.cpp +++ b/src/common/uri.cpp @@ -97,30 +97,46 @@ void wxURI::Clear() // This creates the URI - all we do here is call the main parsing method // --------------------------------------------------------------------------- -void wxURI::Create(const wxString& uri) +const wxChar* wxURI::Create(const wxString& uri) { if (m_fields) Clear(); - Parse(uri); + return Parse(uri); } // --------------------------------------------------------------------------- -// Escape/Unescape/IsEscape +// Escape/TranslateEscape/IsEscape // -// Unescape unencodes a 3 character URL escape sequence +// TranslateEscape unencodes a 3 character URL escape sequence // Escape encodes an invalid URI character into a 3 character sequence // IsEscape determines if the input string contains an escape sequence, // if it does, then it moves the input string past the escape sequence // --------------------------------------------------------------------------- -wxChar wxURI::Unescape(const wxChar* s) +wxChar wxURI::TranslateEscape(const wxChar* s) { wxASSERT_MSG(IsHex(*s) && IsHex(*(s+1)), wxT("Invalid escape!")); return CharToHex(*s) * 0x10 + CharToHex(*++s); } +wxString wxURI::Unescape(const wxString& uri) +{ + wxString new_uri; + + for(size_t i = 0; i < uri.length(); ++i) + { + if (uri[i] == wxT('%')) + { + new_uri += wxURI::TranslateEscape( &(uri.c_str()[i+1]) ); + i += 2; + } + } + + return new_uri; +} + void wxURI::Escape(wxString& s, const wxChar& c) { const wxChar* hdig = wxT("0123456789abcdef"); @@ -141,13 +157,13 @@ bool wxURI::IsEscape(const wxChar*& uri) } // --------------------------------------------------------------------------- -// Get +// BuildURI // -// Get() actually builds the entire URI into a useable +// BuildURI() builds the entire URI into a useable // representation, including proper identification characters such as slashes // --------------------------------------------------------------------------- -wxString wxURI::Get() const +wxString wxURI::BuildURI() const { wxString ret; @@ -178,6 +194,40 @@ wxString wxURI::Get() const return ret; } +wxString wxURI::BuildUnescapedURI() const +{ + wxString ret; + + if (HasScheme()) + ret = ret + m_scheme + wxT(":"); + + if (HasServer()) + { + ret += wxT("//"); + + if (HasUser()) + ret = ret + wxURI::Unescape(m_user) + wxT("@"); + + if (m_hostType == wxURI_REGNAME) + ret += wxURI::Unescape(m_server); + else + ret += m_server; + + if (HasPort()) + ret = ret + wxT(":") + m_port; + } + + ret += wxURI::Unescape(m_path); + + if (HasQuery()) + ret = ret + wxT("?") + wxURI::Unescape(m_query); + + if (HasFragment()) + ret = ret + wxT("#") + wxURI::Unescape(m_fragment); + + return ret; +} + // --------------------------------------------------------------------------- // operator = and == // --------------------------------------------------------------------------- @@ -1057,7 +1107,7 @@ bool wxURI::ParseIPvFuture(const wxChar*& uri) // Misc methods - IsXXX and CharToHex // --------------------------------------------------------------------------- -int wxURI::CharToHex(const wxChar& c) +wxInt32 wxURI::CharToHex(const wxChar& c) { if ((c >= 'A') && (c <= 'Z')) return c - 'A' + 0x0A; if ((c >= 'a') && (c <= 'z')) return c - 'a' + 0x0a; @@ -1125,94 +1175,26 @@ bool wxURI::IsDigit(const wxChar& c) // // wxURL Compatability // -// TODO: Use wxURI instead here... // --------------------------------------------------------------------------- #if wxUSE_URL +#if WXWIN_COMPATIBILITY_2_4 + #include "wx/url.h" -wxString wxURL::ConvertToValidURI(const wxString& uri, const wxChar* delims) +wxString wxURL::ConvertToValidURI(const wxString& uri, const wxChar* WXUNUSED(delims)) { - wxString out_str; - wxString hexa_code; - size_t i; - - for (i = 0; i < uri.Len(); i++) - { - wxChar c = uri.GetChar(i); - - if (c == wxT(' ')) - { - // GRG, Apr/2000: changed to "%20" instead of '+' - - out_str += wxT("%20"); - } - else - { - // GRG, Apr/2000: modified according to the URI definition (RFC 2396) - // - // - Alphanumeric characters are never escaped - // - Unreserved marks are never escaped - // - Delimiters must be escaped if they appear within a component - // but not if they are used to separate components. Here we have - // no clear way to distinguish between these two cases, so they - // are escaped unless they are passed in the 'delims' parameter - // (allowed delimiters). - - static const wxChar marks[] = wxT("-_.!~*()'"); - - if ( !wxIsalnum(c) && !wxStrchr(marks, c) && !wxStrchr(delims, c) ) - { - hexa_code.Printf(wxT("%%%02X"), c); - out_str += hexa_code; - } - else - { - out_str += c; - } - } - } - - return out_str; + return wxURI(uri).BuildURI(); } wxString wxURL::ConvertFromURI(const wxString& uri) { - wxString new_uri; - - size_t i = 0; - while (i < uri.Len()) - { - int code; - if (uri[i] == wxT('%')) - { - i++; - if (uri[i] >= wxT('A') && uri[i] <= wxT('F')) - code = (uri[i] - wxT('A') + 10) * 16; - else if (uri[i] >= wxT('a') && uri[i] <= wxT('f')) - code = (uri[i] - wxT('a') + 10) * 16; - else - code = (uri[i] - wxT('0')) * 16; - - i++; - if (uri[i] >= wxT('A') && uri[i] <= wxT('F')) - code += (uri[i] - wxT('A')) + 10; - else if (uri[i] >= wxT('a') && uri[i] <= wxT('f')) - code += (uri[i] - wxT('a')) + 10; - else - code += (uri[i] - wxT('0')); - - i++; - new_uri += (wxChar)code; - continue; - } - new_uri += uri[i]; - i++; - } - return new_uri; + return wxURI::Unescape(uri); } +#endif //WXWIN_COMPATIBILITY_2_4 + #endif //wxUSE_URL //end of uri.cpp diff --git a/src/common/url.cpp b/src/common/url.cpp index 7d2da23..b6b9169 100644 --- a/src/common/url.cpp +++ b/src/common/url.cpp @@ -62,14 +62,14 @@ wxURL::wxURL(const wxString& url) : wxURI(url) wxURL::wxURL(const wxURI& url) : wxURI(url) { - Init(url.Get()); + Init(url.BuildURI()); ParseURL(); } wxURL& wxURL::operator = (const wxURI& url) { wxURI::operator = (url); - Init(url.Get()); + Init(url.BuildURI()); ParseURL(); return *this; } diff --git a/tests/uris/uris.cpp b/tests/uris/uris.cpp index 1aaf9df..2bc61ae 100644 --- a/tests/uris/uris.cpp +++ b/tests/uris/uris.cpp @@ -26,7 +26,7 @@ #include "wx/cppunit.h" // Test wxURL & wxURI compat? -#define TEST_URL 0 +#define TEST_URL 1 // ---------------------------------------------------------------------------- // test class @@ -65,7 +65,7 @@ private: void Assignment(); void Comparison(); -#if 1 +#if TEST_URL void URLCompat(); #endif @@ -159,14 +159,14 @@ void URITestCase::Paths() uri->GetPath() == wxT("/path/")); URI_TEST("path/john/../../../joe", - uri->Get() == wxT("../joe")); + uri->BuildURI() == wxT("../joe")); } #undef URI_TEST #define URI_TEST_RESOLVE(string, eq, strict) \ uri = new wxURI(wxT(string));\ uri->Resolve(masteruri, strict);\ - CPPUNIT_ASSERT(uri->Get() == wxT(eq));\ + CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\ delete uri; #define URI_TEST(string, eq) \ @@ -267,7 +267,7 @@ void URITestCase::Assignment() uri2 = uri1; - CPPUNIT_ASSERT(uri1.Get() == uri2.Get()); + CPPUNIT_ASSERT(uri1.BuildURI() == uri2.BuildURI()); } void URITestCase::Comparison() @@ -305,6 +305,9 @@ void URITestCase::URLCompat() CPPUNIT_ASSERT( uricopy == url ); CPPUNIT_ASSERT( uricopy == urlcopy ); CPPUNIT_ASSERT( uricopy == uri ); +#if WXWIN_COMPATIBILITY_2_4 + CPPUNIT_ASSERT( wxURL::ConvertFromURI(wxT("%20%41%20")) == wxT(" A ") ); +#endif } #endif -- 2.7.4