]>
git.saurik.com Git - wxWidgets.git/blob - src/common/uri.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implementation of a uri parser
7 // Copyright: (c) 2004 Ryan Norton
9 /////////////////////////////////////////////////////////////////////////////
11 // ===========================================================================
13 // ===========================================================================
15 // ---------------------------------------------------------------------------
17 // ---------------------------------------------------------------------------
19 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma implementation "uri.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
32 // ---------------------------------------------------------------------------
34 // ---------------------------------------------------------------------------
36 IMPLEMENT_CLASS(wxURI
, wxObject
);
38 // ===========================================================================
40 // ===========================================================================
42 // ---------------------------------------------------------------------------
44 // ---------------------------------------------------------------------------
46 // ---------------------------------------------------------------------------
50 // ---------------------------------------------------------------------------
52 // ---------------------------------------------------------------------------
54 // ---------------------------------------------------------------------------
56 wxURI::wxURI() : m_hostType(wxURI_REGNAME
), m_fields(0)
60 wxURI::wxURI(const wxString
& uri
) : m_hostType(wxURI_REGNAME
), m_fields(0)
65 wxURI::wxURI(const wxURI
& uri
) : wxObject(), m_hostType(wxURI_REGNAME
), m_fields(0)
70 // ---------------------------------------------------------------------------
71 // Destructor and cleanup
72 // ---------------------------------------------------------------------------
81 m_scheme
= m_userinfo
= m_server
= m_port
= m_path
=
82 m_query
= m_fragment
= wxEmptyString
;
84 m_hostType
= wxURI_REGNAME
;
89 // ---------------------------------------------------------------------------
92 // This creates the URI - all we do here is call the main parsing method
93 // ---------------------------------------------------------------------------
95 const wxChar
* wxURI::Create(const wxString
& uri
)
103 // ---------------------------------------------------------------------------
106 // TranslateEscape unencodes a 3 character URL escape sequence
108 // Escape encodes an invalid URI character into a 3 character sequence
110 // IsEscape determines if the input string contains an escape sequence,
111 // if it does, then it moves the input string past the escape sequence
113 // Unescape unencodes all 3 character URL escape sequences in a wxString
114 // ---------------------------------------------------------------------------
116 wxChar
wxURI::TranslateEscape(const wxChar
* s
)
118 wxASSERT_MSG(IsHex(*s
) && IsHex(*(s
+1)), wxT("Invalid escape!"));
121 return (wxChar
)( CharToHex(*s
) << 4 ) | CharToHex(*++s
);
124 wxString
wxURI::Unescape(const wxString
& uri
)
128 for(size_t i
= 0; i
< uri
.length(); ++i
)
130 if (uri
[i
] == wxT('%'))
132 new_uri
+= wxURI::TranslateEscape( &(uri
.c_str()[i
+1]) );
142 void wxURI::Escape(wxString
& s
, const wxChar
& c
)
144 const wxChar
* hdig
= wxT("0123456789abcdef");
146 s
+= hdig
[(c
>> 4) & 15];
150 bool wxURI::IsEscape(const wxChar
*& uri
)
152 // pct-encoded = "%" HEXDIG HEXDIG
153 if(*uri
== wxT('%') && IsHex(*(uri
+1)) && IsHex(*(uri
+2)))
162 // ---------------------------------------------------------------------------
166 // Gets the username and password via the old URL method.
167 // ---------------------------------------------------------------------------
168 wxString
wxURI::GetUser() const
170 size_t dwPasswordPos
= m_userinfo
.find(':');
172 if (dwPasswordPos
== wxString::npos
)
175 return m_userinfo(0, dwPasswordPos
);
178 wxString
wxURI::GetPassword() const
180 size_t dwPasswordPos
= m_userinfo
.find(':');
182 if (dwPasswordPos
== wxString::npos
)
185 return m_userinfo(dwPasswordPos
+1, m_userinfo
.length() + 1);
188 // ---------------------------------------------------------------------------
191 // BuildURI() builds the entire URI into a useable
192 // representation, including proper identification characters such as slashes
194 // BuildUnescapedURI() does the same thing as BuildURI(), only it unescapes
195 // the components that accept escape sequences
196 // ---------------------------------------------------------------------------
198 wxString
wxURI::BuildURI() const
203 ret
= ret
+ m_scheme
+ wxT(":");
210 ret
= ret
+ m_userinfo
+ wxT("@");
215 ret
= ret
+ wxT(":") + m_port
;
221 ret
= ret
+ wxT("?") + m_query
;
224 ret
= ret
+ wxT("#") + m_fragment
;
229 wxString
wxURI::BuildUnescapedURI() const
234 ret
= ret
+ m_scheme
+ wxT(":");
241 ret
= ret
+ wxURI::Unescape(m_userinfo
) + wxT("@");
243 if (m_hostType
== wxURI_REGNAME
)
244 ret
+= wxURI::Unescape(m_server
);
249 ret
= ret
+ wxT(":") + m_port
;
252 ret
+= wxURI::Unescape(m_path
);
255 ret
= ret
+ wxT("?") + wxURI::Unescape(m_query
);
258 ret
= ret
+ wxT("#") + wxURI::Unescape(m_fragment
);
263 // ---------------------------------------------------------------------------
265 // ---------------------------------------------------------------------------
267 wxURI
& wxURI::Assign(const wxURI
& uri
)
270 m_fields
= uri
.m_fields
;
272 //ref over components
273 m_scheme
= uri
.m_scheme
;
274 m_userinfo
= uri
.m_userinfo
;
275 m_server
= uri
.m_server
;
276 m_hostType
= uri
.m_hostType
;
279 m_query
= uri
.m_query
;
280 m_fragment
= uri
.m_fragment
;
285 wxURI
& wxURI::operator = (const wxURI
& uri
)
290 wxURI
& wxURI::operator = (const wxString
& string
)
296 // ---------------------------------------------------------------------------
298 // ---------------------------------------------------------------------------
300 bool wxURI::operator == (const wxURI
& uri
) const
304 if(m_scheme
!= uri
.m_scheme
)
307 else if (uri
.HasScheme())
315 if (m_userinfo
!= uri
.m_userinfo
)
318 else if (uri
.HasUserInfo())
321 if (m_server
!= uri
.m_server
||
322 m_hostType
!= uri
.m_hostType
)
327 if(m_port
!= uri
.m_port
)
330 else if (uri
.HasPort())
333 else if (uri
.HasServer())
339 if(m_path
!= uri
.m_path
)
342 else if (uri
.HasPath())
347 if (m_query
!= uri
.m_query
)
350 else if (uri
.HasQuery())
355 if (m_fragment
!= uri
.m_fragment
)
358 else if (uri
.HasFragment())
364 // ---------------------------------------------------------------------------
367 // if there is no authority or scheme, it is a reference
368 // ---------------------------------------------------------------------------
370 bool wxURI::IsReference() const
371 { return !HasScheme() || !HasServer(); }
373 // ---------------------------------------------------------------------------
376 // Master URI parsing method. Just calls the individual parsing methods
378 // URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
379 // URI-reference = URI / relative-URITestCase
380 // ---------------------------------------------------------------------------
382 const wxChar
* wxURI::Parse(const wxChar
* uri
)
384 uri
= ParseScheme(uri
);
385 uri
= ParseAuthority(uri
);
386 uri
= ParsePath(uri
);
387 uri
= ParseQuery(uri
);
388 return ParseFragment(uri
);
391 // ---------------------------------------------------------------------------
394 // Individual parsers for each URI component
395 // ---------------------------------------------------------------------------
397 const wxChar
* wxURI::ParseScheme(const wxChar
* uri
)
399 wxASSERT(uri
!= NULL
);
401 //copy of the uri - used for figuring out
402 //length of each component
403 const wxChar
* uricopy
= uri
;
405 //Does the uri have a scheme (first character alpha)?
410 //scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
411 while (IsAlpha(*uri
) || IsDigit(*uri
) ||
420 if (*uri
== wxT(':'))
422 //mark the scheme as valid
423 m_fields
|= wxURI_SCHEME
;
425 //move reference point up to input buffer
429 //relative uri with relative path reference
430 m_scheme
= wxEmptyString
;
433 //relative uri with _possible_ relative path reference
438 const wxChar
* wxURI::ParseAuthority(const wxChar
* uri
)
440 // authority = [ userinfo "@" ] host [ ":" port ]
441 if (*uri
== wxT('/') && *(uri
+1) == wxT('/'))
445 uri
= ParseUserInfo(uri
);
446 uri
= ParseServer(uri
);
447 return ParsePort(uri
);
453 const wxChar
* wxURI::ParseUserInfo(const wxChar
* uri
)
455 wxASSERT(uri
!= NULL
);
457 //copy of the uri - used for figuring out
458 //length of each component
459 const wxChar
* uricopy
= uri
;
461 // userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
462 while(*uri
&& *uri
!= wxT('@') && *uri
!= wxT('/') && *uri
!= wxT('#') && *uri
!= wxT('?'))
464 if(IsUnreserved(*uri
) || IsEscape(uri
) ||
465 IsSubDelim(*uri
) || *uri
== wxT(':'))
466 m_userinfo
+= *uri
++;
468 Escape(m_userinfo
, *uri
++);
474 m_fields
|= wxURI_USERINFO
;
479 m_userinfo
= wxEmptyString
;
484 const wxChar
* wxURI::ParseServer(const wxChar
* uri
)
486 wxASSERT(uri
!= NULL
);
488 //copy of the uri - used for figuring out
489 //length of each component
490 const wxChar
* uricopy
= uri
;
492 // host = IP-literal / IPv4address / reg-name
493 // IP-literal = "[" ( IPv6address / IPvFuture ) "]"
494 if (*uri
== wxT('['))
496 ++uri
; //some compilers don't support *&ing a ++*
497 if (ParseIPv6address(uri
) && *uri
== wxT(']'))
500 m_hostType
= wxURI_IPV6ADDRESS
;
502 wxStringBufferLength
theBuffer(m_server
, uri
- uricopy
);
503 wxTmemcpy(theBuffer
, uricopy
, uri
-uricopy
);
504 theBuffer
.SetLength(uri
-uricopy
);
510 ++uri
; //some compilers don't support *&ing a ++*
511 if (ParseIPvFuture(uri
) && *uri
== wxT(']'))
514 m_hostType
= wxURI_IPVFUTURE
;
516 wxStringBufferLength
theBuffer(m_server
, uri
- uricopy
);
517 wxTmemcpy(theBuffer
, uricopy
, uri
-uricopy
);
518 theBuffer
.SetLength(uri
-uricopy
);
526 if (ParseIPv4address(uri
))
528 m_hostType
= wxURI_IPV4ADDRESS
;
530 wxStringBufferLength
theBuffer(m_server
, uri
- uricopy
);
531 wxTmemcpy(theBuffer
, uricopy
, uri
-uricopy
);
532 theBuffer
.SetLength(uri
-uricopy
);
538 if(m_hostType
== wxURI_REGNAME
)
541 // reg-name = *( unreserved / pct-encoded / sub-delims )
542 while(*uri
&& *uri
!= wxT('/') && *uri
!= wxT(':') && *uri
!= wxT('#') && *uri
!= wxT('?'))
544 if(IsUnreserved(*uri
) || IsEscape(uri
) || IsSubDelim(*uri
))
547 Escape(m_server
, *uri
++);
551 //mark the server as valid
552 m_fields
|= wxURI_SERVER
;
558 const wxChar
* wxURI::ParsePort(const wxChar
* uri
)
560 wxASSERT(uri
!= NULL
);
571 //mark the port as valid
572 m_fields
|= wxURI_PORT
;
578 const wxChar
* wxURI::ParsePath(const wxChar
* uri
, bool bReference
, bool bNormalize
)
580 wxASSERT(uri
!= NULL
);
582 //copy of the uri - used for figuring out
583 //length of each component
584 const wxChar
* uricopy
= uri
;
586 /// hier-part = "//" authority path-abempty
591 /// relative-part = "//" authority path-abempty
596 /// path-abempty = *( "/" segment )
597 /// path-absolute = "/" [ segment-nz *( "/" segment ) ]
598 /// path-noscheme = segment-nz-nc *( "/" segment )
599 /// path-rootless = segment-nz *( "/" segment )
600 /// path-empty = 0<pchar>
603 /// segment-nz = 1*pchar
604 /// segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
605 /// ; non-zero-length segment without any colon ":"
607 /// pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
608 if (*uri
== wxT('/'))
612 while(*uri
&& *uri
!= wxT('#') && *uri
!= wxT('?'))
614 if( IsUnreserved(*uri
) || IsSubDelim(*uri
) || IsEscape(uri
) ||
615 *uri
== wxT(':') || *uri
== wxT('@') || *uri
== wxT('/'))
618 Escape(m_path
, *uri
++);
623 wxStringBufferLength
theBuffer(m_path
, m_path
.length() + 1);
625 wxTmemcpy(theBuffer
, m_path
.c_str(), m_path
.length()+1);
627 Normalize(theBuffer
, true);
628 theBuffer
.SetLength(wxStrlen(theBuffer
));
630 //mark the path as valid
631 m_fields
|= wxURI_PATH
;
633 else if(*uri
) //Relative path
638 while(*uri
&& *uri
!= wxT('#') && *uri
!= wxT('?'))
640 if(IsUnreserved(*uri
) || IsSubDelim(*uri
) || IsEscape(uri
) ||
641 *uri
== wxT('@') || *uri
== wxT('/'))
644 Escape(m_path
, *uri
++);
649 while(*uri
&& *uri
!= wxT('#') && *uri
!= wxT('?'))
651 if(IsUnreserved(*uri
) || IsSubDelim(*uri
) || IsEscape(uri
) ||
652 *uri
== wxT(':') || *uri
== wxT('@') || *uri
== wxT('/'))
655 Escape(m_path
, *uri
++);
663 wxStringBufferLength
theBuffer(m_path
, m_path
.length() + 1);
665 wxTmemcpy(theBuffer
, m_path
.c_str(), m_path
.length()+1);
667 Normalize(theBuffer
);
668 theBuffer
.SetLength(wxStrlen(theBuffer
));
671 //mark the path as valid
672 m_fields
|= wxURI_PATH
;
680 const wxChar
* wxURI::ParseQuery(const wxChar
* uri
)
682 wxASSERT(uri
!= NULL
);
684 // query = *( pchar / "/" / "?" )
685 if (*uri
== wxT('?'))
688 while(*uri
&& *uri
!= wxT('#'))
690 if (IsUnreserved(*uri
) || IsSubDelim(*uri
) || IsEscape(uri
) ||
691 *uri
== wxT(':') || *uri
== wxT('@') || *uri
== wxT('/') || *uri
== wxT('?'))
694 Escape(m_query
, *uri
++);
697 //mark the server as valid
698 m_fields
|= wxURI_QUERY
;
705 const wxChar
* wxURI::ParseFragment(const wxChar
* uri
)
707 wxASSERT(uri
!= NULL
);
709 // fragment = *( pchar / "/" / "?" )
710 if (*uri
== wxT('#'))
715 if (IsUnreserved(*uri
) || IsSubDelim(*uri
) || IsEscape(uri
) ||
716 *uri
== wxT(':') || *uri
== wxT('@') || *uri
== wxT('/') || *uri
== wxT('?'))
717 m_fragment
+= *uri
++;
719 Escape(m_fragment
, *uri
++);
722 //mark the server as valid
723 m_fields
|= wxURI_FRAGMENT
;
729 // ---------------------------------------------------------------------------
732 // Builds missing components of this uri from a base uri
734 // A version of the algorithm outlined in the RFC is used here
735 // (it is shown in comments)
737 // Note that an empty URI inherits all components
738 // ---------------------------------------------------------------------------
740 void wxURI::Resolve(const wxURI
& base
, int flags
)
742 wxASSERT_MSG(!base
.IsReference(),
743 wxT("wxURI to inherit from must not be a reference!"));
745 // If we arn't being strict, enable the older (pre-RFC2396)
746 // loophole that allows this uri to inherit other
747 // properties from the base uri - even if the scheme
749 if ( !(flags
& wxURI_STRICT
) &&
750 HasScheme() && base
.HasScheme() &&
751 m_scheme
== base
.m_scheme
)
753 m_fields
-= wxURI_SCHEME
;
757 // Do nothing if this is an absolute wxURI
758 // if defined(R.scheme) then
759 // T.scheme = R.scheme;
760 // T.authority = R.authority;
761 // T.path = remove_dot_segments(R.path);
762 // T.query = R.query;
768 //No scheme - inherit
769 m_scheme
= base
.m_scheme
;
770 m_fields
|= wxURI_SCHEME
;
772 // All we need to do for relative URIs with an
773 // authority component is just inherit the scheme
774 // if defined(R.authority) then
775 // T.authority = R.authority;
776 // T.path = remove_dot_segments(R.path);
777 // T.query = R.query;
783 //No authority - inherit
784 if (base
.HasUserInfo())
786 m_userinfo
= base
.m_userinfo
;
787 m_fields
|= wxURI_USERINFO
;
790 m_server
= base
.m_server
;
791 m_hostType
= base
.m_hostType
;
792 m_fields
|= wxURI_SERVER
;
796 m_port
= base
.m_port
;
797 m_fields
|= wxURI_PORT
;
801 // Simple path inheritance from base
804 // T.path = Base.path;
805 m_path
= base
.m_path
;
806 m_fields
|= wxURI_PATH
;
809 // if defined(R.query) then
810 // T.query = R.query;
812 // T.query = Base.query;
816 m_query
= base
.m_query
;
817 m_fields
|= wxURI_QUERY
;
822 // if (R.path starts-with "/") then
823 // T.path = remove_dot_segments(R.path);
825 // T.path = merge(Base.path, R.path);
826 // T.path = remove_dot_segments(T.path);
828 // T.query = R.query;
829 if (m_path
[0u] != wxT('/'))
832 const wxChar
* op
= m_path
.c_str();
833 const wxChar
* bp
= base
.m_path
.c_str() + base
.m_path
.Length();
835 //not a ending directory? move up
836 if (base
.m_path
[0] && *(bp
-1) != wxT('/'))
837 UpTree(base
.m_path
, bp
);
839 //normalize directories
840 while(*op
== wxT('.') && *(op
+1) == wxT('.') &&
841 (*(op
+2) == '\0' || *(op
+2) == wxT('/')) )
843 UpTree(base
.m_path
, bp
);
851 m_path
= base
.m_path
.substr(0, bp
- base
.m_path
.c_str()) +
852 m_path
.substr((op
- m_path
.c_str()), m_path
.Length());
856 //T.fragment = R.fragment;
859 // ---------------------------------------------------------------------------
862 // Moves a URI path up a directory
863 // ---------------------------------------------------------------------------
866 void wxURI::UpTree(const wxChar
* uristart
, const wxChar
*& uri
)
868 if (uri
!= uristart
&& *(uri
-1) == wxT('/'))
873 for(;uri
!= uristart
; --uri
)
875 if (*uri
== wxT('/'))
883 if (uri
== uristart
&& *uri
== wxT('/'))
888 // ---------------------------------------------------------------------------
891 // Normalizes directories in-place
893 // I.E. ./ and . are ignored
895 // ../ and .. are removed if a directory is before it, along
896 // with that directory (leading .. and ../ are kept)
897 // ---------------------------------------------------------------------------
900 void wxURI::Normalize(wxChar
* s
, bool bIgnoreLeads
)
910 if (*cp
== wxT('.') && (*(cp
+1) == wxT('/') || *(cp
+1) == '\0')
911 && (bp
== cp
|| *(cp
-1) == wxT('/')))
919 else if (*cp
== wxT('.') && *(cp
+1) == wxT('.') &&
920 (*(cp
+2) == wxT('/') || *(cp
+2) == '\0')
921 && (bp
== cp
|| *(cp
-1) == wxT('/')))
923 //.. _or_ ../ - go up the tree
926 UpTree((const wxChar
*)bp
, (const wxChar
*&)s
);
933 else if (!bIgnoreLeads
)
958 // ---------------------------------------------------------------------------
961 // Parses 1 to 4 hex values. Returns true if the first character of the input
962 // string is a valid hex character. It is the caller's responsability to move
963 // the input string back to its original position on failure.
964 // ---------------------------------------------------------------------------
966 bool wxURI::ParseH16(const wxChar
*& uri
)
972 if(IsHex(*++uri
) && IsHex(*++uri
) && IsHex(*++uri
))
978 // ---------------------------------------------------------------------------
981 // Parses a certain version of an IP address and moves the input string past
982 // it. Returns true if the input string contains the proper version of an ip
983 // address. It is the caller's responsability to move the input string back
984 // to its original position on failure.
985 // ---------------------------------------------------------------------------
987 bool wxURI::ParseIPv4address(const wxChar
*& uri
)
989 //IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
991 //dec-octet = DIGIT ; 0-9
992 // / %x31-39 DIGIT ; 10-99
993 // / "1" 2DIGIT ; 100-199
994 // / "2" %x30-34 DIGIT ; 200-249
995 // / "25" %x30-35 ; 250-255
1002 //each ip part must be between 0-255 (dupe of version in for loop)
1003 if( IsDigit(*++uri
) && IsDigit(*++uri
) &&
1004 //100 or less (note !)
1005 !( (*(uri
-2) < wxT('2')) ||
1007 (*(uri
-2) == wxT('2') &&
1008 (*(uri
-1) < wxT('5') || (*(uri
-1) == wxT('5') && *uri
<= wxT('5')))
1016 if(IsDigit(*uri
))++uri
;
1018 //compilers should unroll this loop
1019 for(; iIPv4
< 4; ++iIPv4
)
1021 if (*uri
!= wxT('.') || !IsDigit(*++uri
))
1024 //each ip part must be between 0-255
1025 if( IsDigit(*++uri
) && IsDigit(*++uri
) &&
1026 //100 or less (note !)
1027 !( (*(uri
-2) < wxT('2')) ||
1029 (*(uri
-2) == wxT('2') &&
1030 (*(uri
-1) < wxT('5') || (*(uri
-1) == wxT('5') && *uri
<= wxT('5')))
1037 if(IsDigit(*uri
))++uri
;
1043 bool wxURI::ParseIPv6address(const wxChar
*& uri
)
1045 // IPv6address = 6( h16 ":" ) ls32
1046 // / "::" 5( h16 ":" ) ls32
1047 // / [ h16 ] "::" 4( h16 ":" ) ls32
1048 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1049 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1050 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1051 // / [ *4( h16 ":" ) h16 ] "::" ls32
1052 // / [ *5( h16 ":" ) h16 ] "::" h16
1053 // / [ *6( h16 ":" ) h16 ] "::"
1055 size_t numPrefix
= 0,
1058 bool bEndHex
= false;
1060 for( ; numPrefix
< 6; ++numPrefix
)
1069 if(*uri
!= wxT(':'))
1075 if(!bEndHex
&& !ParseH16(uri
))
1082 if (*uri
== wxT(':'))
1084 if (*++uri
!= wxT(':'))
1094 if (*uri
!= wxT(':') || *(uri
+1) != wxT(':'))
1099 while (*--uri
!= wxT(':')) {}
1102 const wxChar
* uristart
= uri
;
1104 // ls32 = ( h16 ":" h16 ) / IPv4address
1105 if (ParseH16(uri
) && *uri
== wxT(':') && ParseH16(uri
))
1110 if (ParseIPv4address(uri
))
1122 maxPostfix
= 4 - numPrefix
;
1126 bool bAllowAltEnding
= maxPostfix
== 0;
1128 for(; maxPostfix
!= 0; --maxPostfix
)
1130 if(!ParseH16(uri
) || *uri
!= wxT(':'))
1136 const wxChar
* uristart
= uri
;
1138 // ls32 = ( h16 ":" h16 ) / IPv4address
1139 if (ParseH16(uri
) && *uri
== wxT(':') && ParseH16(uri
))
1144 if (ParseIPv4address(uri
))
1149 if (!bAllowAltEnding
)
1153 if(numPrefix
<= 5 && ParseH16(uri
))
1159 bool wxURI::ParseIPvFuture(const wxChar
*& uri
)
1161 // IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
1162 if (*++uri
!= wxT('v') || !IsHex(*++uri
))
1165 while (IsHex(*++uri
)) {}
1167 if (*uri
!= wxT('.') || !(IsUnreserved(*++uri
) || IsSubDelim(*uri
) || *uri
== wxT(':')))
1170 while(IsUnreserved(*++uri
) || IsSubDelim(*uri
) || *uri
== wxT(':')) {}
1176 // ---------------------------------------------------------------------------
1179 // Converts a character into a numeric hexidecimal value, or 0 if the
1180 // passed in character is not a valid hex character
1181 // ---------------------------------------------------------------------------
1184 wxChar
wxURI::CharToHex(const wxChar
& c
)
1186 if ((c
>= wxT('A')) && (c
<= wxT('Z'))) return wxChar(c
- wxT('A') + 0x0A);
1187 if ((c
>= wxT('a')) && (c
<= wxT('z'))) return wxChar(c
- wxT('a') + 0x0a);
1188 if ((c
>= wxT('0')) && (c
<= wxT('9'))) return wxChar(c
- wxT('0') + 0x00);
1193 // ---------------------------------------------------------------------------
1196 // Returns true if the passed in character meets the criteria of the method
1197 // ---------------------------------------------------------------------------
1199 //! unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
1200 bool wxURI::IsUnreserved (const wxChar
& c
)
1201 { return IsAlpha(c
) || IsDigit(c
) ||
1205 c
== wxT('~') //tilde
1209 bool wxURI::IsReserved (const wxChar
& c
)
1211 return IsGenDelim(c
) || IsSubDelim(c
);
1214 //! gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
1215 bool wxURI::IsGenDelim (const wxChar
& c
)
1217 return c
== wxT(':') ||
1226 //! sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
1227 //! / "*" / "+" / "," / ";" / "="
1228 bool wxURI::IsSubDelim (const wxChar
& c
)
1230 return c
== wxT('!') ||
1244 bool wxURI::IsHex(const wxChar
& c
)
1245 { return IsDigit(c
) || (c
>= wxT('a') && c
<= wxT('f')) || (c
>= wxT('A') && c
<= wxT('F')); }
1247 bool wxURI::IsAlpha(const wxChar
& c
)
1248 { return (c
>= wxT('a') && c
<= wxT('z')) || (c
>= wxT('A') && c
<= wxT('Z')); }
1250 bool wxURI::IsDigit(const wxChar
& c
)
1251 { return c
>= wxT('0') && c
<= wxT('9'); }
1254 // ---------------------------------------------------------------------------
1256 // wxURL Compatibility
1258 // ---------------------------------------------------------------------------
1262 #if WXWIN_COMPATIBILITY_2_4
1266 wxString
wxURL::GetProtocolName() const
1271 wxString
wxURL::GetHostName() const
1276 wxString
wxURL::GetPath() const
1281 //Note that this old code really doesn't convert to a URI that well and looks
1282 //more like a dirty hack than anything else...
1284 wxString
wxURL::ConvertToValidURI(const wxString
& uri
, const wxChar
* delims
)
1290 for (i
= 0; i
< uri
.Len(); i
++)
1292 wxChar c
= uri
.GetChar(i
);
1296 // GRG, Apr/2000: changed to "%20" instead of '+'
1298 out_str
+= wxT("%20");
1302 // GRG, Apr/2000: modified according to the URI definition (RFC 2396)
1304 // - Alphanumeric characters are never escaped
1305 // - Unreserved marks are never escaped
1306 // - Delimiters must be escaped if they appear within a component
1307 // but not if they are used to separate components. Here we have
1308 // no clear way to distinguish between these two cases, so they
1309 // are escaped unless they are passed in the 'delims' parameter
1310 // (allowed delimiters).
1312 static const wxChar marks
[] = wxT("-_.!~*()'");
1314 if ( !wxIsalnum(c
) && !wxStrchr(marks
, c
) && !wxStrchr(delims
, c
) )
1316 hexa_code
.Printf(wxT("%%%02X"), c
);
1317 out_str
+= hexa_code
;
1329 wxString
wxURL::ConvertFromURI(const wxString
& uri
)
1331 return wxURI::Unescape(uri
);
1334 #endif //WXWIN_COMPATIBILITY_2_4