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
This commit is contained in:
Ryan Norton
2004-10-28 09:57:43 +00:00
parent cedea72ffa
commit 86470d432f
8 changed files with 126 additions and 116 deletions

View File

@@ -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{wxRealPoint}{wxrealpoint}}{Representation of a point using floating point numbers}
\twocolitem{\helpref{wxSize}{wxsize}}{Representation of a size} \twocolitem{\helpref{wxSize}{wxsize}}{Representation of a size}
\twocolitem{\helpref{wxTimeSpan}{wxtimespan}}{A time interval.} \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} \twocolitem{\helpref{wxVariant}{wxvariant}}{A class for storing arbitrary types that may change at run-time}
\end{twocollist} \end{twocollist}

View File

@@ -88,6 +88,28 @@ Copies this URI from another URI.
\docparam{uri}{URI (Uniform Resource Identifier) to initialize with} \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} \membersection{wxURI::Create}\label{wxuricreate}
\func{void}{Create}{\param{const wxChar* }{uri}} \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} \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} \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 mode some compatability layers are enabled to allow loopholes from RFCs prior
to 2396} 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}

View File

@@ -151,13 +151,3 @@ Sets the proxy to use for this URL.
\helpref{wxURL::SetDefaultProxy}{wxurlsetdefaultproxy} \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.

View File

@@ -61,7 +61,7 @@ public:
virtual ~wxURI(); virtual ~wxURI();
void Create(const wxString& uri); const wxChar* Create(const wxString& uri);
bool HasScheme() const { return (m_fields & wxURI_SCHEME) == wxURI_SCHEME; } bool HasScheme() const { return (m_fields & wxURI_SCHEME) == wxURI_SCHEME; }
bool HasUser() const { return (m_fields & wxURI_USER) == wxURI_USER; } bool HasUser() const { return (m_fields & wxURI_USER) == wxURI_USER; }
@@ -80,7 +80,8 @@ public:
const wxString& GetServer() const { return m_server; } const wxString& GetServer() const { return m_server; }
const wxURIHostType& GetHostType() const { return m_hostType; } 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); void Resolve(const wxURI& base, int flags = wxURI_STRICT);
bool IsReference() const; bool IsReference() const;
@@ -89,6 +90,8 @@ public:
wxURI& operator = (const wxString& string); wxURI& operator = (const wxString& string);
bool operator == (const wxURI& uri) const; bool operator == (const wxURI& uri) const;
static wxString Unescape (const wxString& szEscapedURI);
protected: protected:
wxURI& Assign(const wxURI& uri); wxURI& Assign(const wxURI& uri);
@@ -112,16 +115,14 @@ protected:
static bool ParseIPv6address(const wxChar*& uri); static bool ParseIPv6address(const wxChar*& uri);
static bool ParseIPvFuture(const wxChar*& uri); static bool ParseIPvFuture(const wxChar*& uri);
static void Normalize(wxChar* uri, bool bIgnoreLeads = false); static void Normalize(wxChar* uri, bool bIgnoreLeads = false);
static void UpTree(const wxChar* uristart, const wxChar*& uri); 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 void Escape (wxString& s, const wxChar& c);
static bool IsEscape(const wxChar*& uri); 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 IsUnreserved (const wxChar& c);
static bool IsReserved (const wxChar& c); static bool IsReserved (const wxChar& c);

View File

@@ -72,11 +72,16 @@ public:
void SetProxy(const wxString& url_proxy); void SetProxy(const wxString& url_proxy);
#endif // wxUSE_SOCKETS #endif // wxUSE_SOCKETS
#if WXWIN_COMPATIBILITY_2_4
//Use wxURI instead - delims is ignored
static wxString ConvertToValidURI( static wxString ConvertToValidURI(
const wxString& uri, const wxString& uri,
const wxChar* delims = wxT(";/?:@&=+$,") const wxChar* delims = wxT(";/?:@&=+$,")
); );
//Use wxURI::Unescape instead
static wxString ConvertFromURI(const wxString& uri); static wxString ConvertFromURI(const wxString& uri);
#endif
protected: protected:
static wxProtoInfo *ms_protocols; static wxProtoInfo *ms_protocols;

View File

@@ -97,30 +97,46 @@ void wxURI::Clear()
// This creates the URI - all we do here is call the main parsing method // 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) if (m_fields)
Clear(); 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 // Escape encodes an invalid URI character into a 3 character sequence
// IsEscape determines if the input string contains an escape sequence, // IsEscape determines if the input string contains an escape sequence,
// if it does, then it moves the input string past the 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!")); wxASSERT_MSG(IsHex(*s) && IsHex(*(s+1)), wxT("Invalid escape!"));
return CharToHex(*s) * 0x10 + CharToHex(*++s); 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) void wxURI::Escape(wxString& s, const wxChar& c)
{ {
const wxChar* hdig = wxT("0123456789abcdef"); 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 // representation, including proper identification characters such as slashes
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
wxString wxURI::Get() const wxString wxURI::BuildURI() const
{ {
wxString ret; wxString ret;
@@ -178,6 +194,40 @@ wxString wxURI::Get() const
return ret; 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 == // operator = and ==
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -1057,7 +1107,7 @@ bool wxURI::ParseIPvFuture(const wxChar*& uri)
// Misc methods - IsXXX and CharToHex // 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;
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 // wxURL Compatability
// //
// TODO: Use wxURI instead here...
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
#if wxUSE_URL #if wxUSE_URL
#if WXWIN_COMPATIBILITY_2_4
#include "wx/url.h" #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; return wxURI(uri).BuildURI();
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;
} }
wxString wxURL::ConvertFromURI(const wxString& uri) wxString wxURL::ConvertFromURI(const wxString& uri)
{ {
wxString new_uri; return wxURI::Unescape(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;
} }
#endif //WXWIN_COMPATIBILITY_2_4
#endif //wxUSE_URL #endif //wxUSE_URL
//end of uri.cpp //end of uri.cpp

View File

@@ -62,14 +62,14 @@ wxURL::wxURL(const wxString& url) : wxURI(url)
wxURL::wxURL(const wxURI& url) : wxURI(url) wxURL::wxURL(const wxURI& url) : wxURI(url)
{ {
Init(url.Get()); Init(url.BuildURI());
ParseURL(); ParseURL();
} }
wxURL& wxURL::operator = (const wxURI& url) wxURL& wxURL::operator = (const wxURI& url)
{ {
wxURI::operator = (url); wxURI::operator = (url);
Init(url.Get()); Init(url.BuildURI());
ParseURL(); ParseURL();
return *this; return *this;
} }

View File

@@ -26,7 +26,7 @@
#include "wx/cppunit.h" #include "wx/cppunit.h"
// Test wxURL & wxURI compat? // Test wxURL & wxURI compat?
#define TEST_URL 0 #define TEST_URL 1
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// test class // test class
@@ -65,7 +65,7 @@ private:
void Assignment(); void Assignment();
void Comparison(); void Comparison();
#if 1 #if TEST_URL
void URLCompat(); void URLCompat();
#endif #endif
@@ -159,14 +159,14 @@ void URITestCase::Paths()
uri->GetPath() == wxT("/path/")); uri->GetPath() == wxT("/path/"));
URI_TEST("path/john/../../../joe", URI_TEST("path/john/../../../joe",
uri->Get() == wxT("../joe")); uri->BuildURI() == wxT("../joe"));
} }
#undef URI_TEST #undef URI_TEST
#define URI_TEST_RESOLVE(string, eq, strict) \ #define URI_TEST_RESOLVE(string, eq, strict) \
uri = new wxURI(wxT(string));\ uri = new wxURI(wxT(string));\
uri->Resolve(masteruri, strict);\ uri->Resolve(masteruri, strict);\
CPPUNIT_ASSERT(uri->Get() == wxT(eq));\ CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
delete uri; delete uri;
#define URI_TEST(string, eq) \ #define URI_TEST(string, eq) \
@@ -267,7 +267,7 @@ void URITestCase::Assignment()
uri2 = uri1; uri2 = uri1;
CPPUNIT_ASSERT(uri1.Get() == uri2.Get()); CPPUNIT_ASSERT(uri1.BuildURI() == uri2.BuildURI());
} }
void URITestCase::Comparison() void URITestCase::Comparison()
@@ -305,6 +305,9 @@ void URITestCase::URLCompat()
CPPUNIT_ASSERT( uricopy == url ); CPPUNIT_ASSERT( uricopy == url );
CPPUNIT_ASSERT( uricopy == urlcopy ); CPPUNIT_ASSERT( uricopy == urlcopy );
CPPUNIT_ASSERT( uricopy == uri ); CPPUNIT_ASSERT( uricopy == uri );
#if WXWIN_COMPATIBILITY_2_4
CPPUNIT_ASSERT( wxURL::ConvertFromURI(wxT("%20%41%20")) == wxT(" A ") );
#endif
} }
#endif #endif