]>
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
[0]) && IsHex(s
[1]), wxT("Invalid escape sequence!"));
120 return (wxChar
)( CharToHex(s
[0]) << 4 ) | CharToHex(s
[1]);
123 wxString
wxURI::Unescape(const wxString
& uri
)
127 for(size_t i
= 0; i
< uri
.length(); ++i
)
129 if (uri
[i
] == wxT('%'))
131 new_uri
+= wxURI::TranslateEscape( &(uri
.c_str()[i
+1]) );
141 void wxURI::Escape(wxString
& s
, const wxChar
& c
)
143 const wxChar
* hdig
= wxT("0123456789abcdef");
145 s
+= hdig
[(c
>> 4) & 15];
149 bool wxURI::IsEscape(const wxChar
*& uri
)
151 // pct-encoded = "%" HEXDIG HEXDIG
152 if(*uri
== wxT('%') && IsHex(*(uri
+1)) && IsHex(*(uri
+2)))
161 // ---------------------------------------------------------------------------
165 // Gets the username and password via the old URL method.
166 // ---------------------------------------------------------------------------
167 wxString
wxURI::GetUser() const
169 size_t dwPasswordPos
= m_userinfo
.find(':');
171 if (dwPasswordPos
== wxString::npos
)
174 return m_userinfo(0, dwPasswordPos
);
177 wxString
wxURI::GetPassword() const
179 size_t dwPasswordPos
= m_userinfo
.find(':');
181 if (dwPasswordPos
== wxString::npos
)
184 return m_userinfo(dwPasswordPos
+1, m_userinfo
.length() + 1);
187 // ---------------------------------------------------------------------------
190 // BuildURI() builds the entire URI into a useable
191 // representation, including proper identification characters such as slashes
193 // BuildUnescapedURI() does the same thing as BuildURI(), only it unescapes
194 // the components that accept escape sequences
195 // ---------------------------------------------------------------------------
197 wxString
wxURI::BuildURI() const
202 ret
= ret
+ m_scheme
+ wxT(":");
209 ret
= ret
+ m_userinfo
+ wxT("@");
214 ret
= ret
+ wxT(":") + m_port
;
220 ret
= ret
+ wxT("?") + m_query
;
223 ret
= ret
+ wxT("#") + m_fragment
;
228 wxString
wxURI::BuildUnescapedURI() const
233 ret
= ret
+ m_scheme
+ wxT(":");
240 ret
= ret
+ wxURI::Unescape(m_userinfo
) + wxT("@");
242 if (m_hostType
== wxURI_REGNAME
)
243 ret
+= wxURI::Unescape(m_server
);
248 ret
= ret
+ wxT(":") + m_port
;
251 ret
+= wxURI::Unescape(m_path
);
254 ret
= ret
+ wxT("?") + wxURI::Unescape(m_query
);
257 ret
= ret
+ wxT("#") + wxURI::Unescape(m_fragment
);
262 // ---------------------------------------------------------------------------
264 // ---------------------------------------------------------------------------
266 wxURI
& wxURI::Assign(const wxURI
& uri
)
269 m_fields
= uri
.m_fields
;
271 //ref over components
272 m_scheme
= uri
.m_scheme
;
273 m_userinfo
= uri
.m_userinfo
;
274 m_server
= uri
.m_server
;
275 m_hostType
= uri
.m_hostType
;
278 m_query
= uri
.m_query
;
279 m_fragment
= uri
.m_fragment
;
284 wxURI
& wxURI::operator = (const wxURI
& uri
)
289 wxURI
& wxURI::operator = (const wxString
& string
)
295 // ---------------------------------------------------------------------------
297 // ---------------------------------------------------------------------------
299 bool wxURI::operator == (const wxURI
& uri
) const
303 if(m_scheme
!= uri
.m_scheme
)
306 else if (uri
.HasScheme())
314 if (m_userinfo
!= uri
.m_userinfo
)
317 else if (uri
.HasUserInfo())
320 if (m_server
!= uri
.m_server
||
321 m_hostType
!= uri
.m_hostType
)
326 if(m_port
!= uri
.m_port
)
329 else if (uri
.HasPort())
332 else if (uri
.HasServer())
338 if(m_path
!= uri
.m_path
)
341 else if (uri
.HasPath())
346 if (m_query
!= uri
.m_query
)
349 else if (uri
.HasQuery())
354 if (m_fragment
!= uri
.m_fragment
)
357 else if (uri
.HasFragment())
363 // ---------------------------------------------------------------------------
366 // if there is no authority or scheme, it is a reference
367 // ---------------------------------------------------------------------------
369 bool wxURI::IsReference() const
370 { return !HasScheme() || !HasServer(); }
372 // ---------------------------------------------------------------------------
375 // Master URI parsing method. Just calls the individual parsing methods
377 // URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
378 // URI-reference = URI / relative
379 // ---------------------------------------------------------------------------
381 const wxChar
* wxURI::Parse(const wxChar
* uri
)
383 uri
= ParseScheme(uri
);
384 uri
= ParseAuthority(uri
);
385 uri
= ParsePath(uri
);
386 uri
= ParseQuery(uri
);
387 return ParseFragment(uri
);
390 // ---------------------------------------------------------------------------
393 // Individual parsers for each URI component
394 // ---------------------------------------------------------------------------
396 const wxChar
* wxURI::ParseScheme(const wxChar
* uri
)
398 wxASSERT(uri
!= NULL
);
400 //copy of the uri - used for figuring out
401 //length of each component
402 const wxChar
* uricopy
= uri
;
404 //Does the uri have a scheme (first character alpha)?
409 //scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
410 while (IsAlpha(*uri
) || IsDigit(*uri
) ||
419 if (*uri
== wxT(':'))
421 //mark the scheme as valid
422 m_fields
|= wxURI_SCHEME
;
424 //move reference point up to input buffer
428 //relative uri with relative path reference
429 m_scheme
= wxEmptyString
;
432 //relative uri with _possible_ relative path reference
437 const wxChar
* wxURI::ParseAuthority(const wxChar
* uri
)
439 // authority = [ userinfo "@" ] host [ ":" port ]
440 if (*uri
== wxT('/') && *(uri
+1) == wxT('/'))
444 uri
= ParseUserInfo(uri
);
445 uri
= ParseServer(uri
);
446 return ParsePort(uri
);
452 const wxChar
* wxURI::ParseUserInfo(const wxChar
* uri
)
454 wxASSERT(uri
!= NULL
);
456 //copy of the uri - used for figuring out
457 //length of each component
458 const wxChar
* uricopy
= uri
;
460 // userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
461 while(*uri
&& *uri
!= wxT('@') && *uri
!= wxT('/') && *uri
!= wxT('#') && *uri
!= wxT('?'))
463 if(IsUnreserved(*uri
) || IsEscape(uri
) ||
464 IsSubDelim(*uri
) || *uri
== wxT(':'))
465 m_userinfo
+= *uri
++;
467 Escape(m_userinfo
, *uri
++);
473 m_fields
|= wxURI_USERINFO
;
478 m_userinfo
= wxEmptyString
;
483 const wxChar
* wxURI::ParseServer(const wxChar
* uri
)
485 wxASSERT(uri
!= NULL
);
487 //copy of the uri - used for figuring out
488 //length of each component
489 const wxChar
* uricopy
= uri
;
491 // host = IP-literal / IPv4address / reg-name
492 // IP-literal = "[" ( IPv6address / IPvFuture ) "]"
493 if (*uri
== wxT('['))
495 ++uri
; //some compilers don't support *&ing a ++*
496 if (ParseIPv6address(uri
) && *uri
== wxT(']'))
499 m_hostType
= wxURI_IPV6ADDRESS
;
501 wxStringBufferLength
theBuffer(m_server
, uri
- uricopy
);
502 wxTmemcpy(theBuffer
, uricopy
, uri
-uricopy
);
503 theBuffer
.SetLength(uri
-uricopy
);
509 ++uri
; //some compilers don't support *&ing a ++*
510 if (ParseIPvFuture(uri
) && *uri
== wxT(']'))
513 m_hostType
= wxURI_IPVFUTURE
;
515 wxStringBufferLength
theBuffer(m_server
, uri
- uricopy
);
516 wxTmemcpy(theBuffer
, uricopy
, uri
-uricopy
);
517 theBuffer
.SetLength(uri
-uricopy
);
525 if (ParseIPv4address(uri
))
527 m_hostType
= wxURI_IPV4ADDRESS
;
529 wxStringBufferLength
theBuffer(m_server
, uri
- uricopy
);
530 wxTmemcpy(theBuffer
, uricopy
, uri
-uricopy
);
531 theBuffer
.SetLength(uri
-uricopy
);
537 if(m_hostType
== wxURI_REGNAME
)
540 // reg-name = *( unreserved / pct-encoded / sub-delims )
541 while(*uri
&& *uri
!= wxT('/') && *uri
!= wxT(':') && *uri
!= wxT('#') && *uri
!= wxT('?'))
543 if(IsUnreserved(*uri
) || IsEscape(uri
) || IsSubDelim(*uri
))
546 Escape(m_server
, *uri
++);
550 //mark the server as valid
551 m_fields
|= wxURI_SERVER
;
557 const wxChar
* wxURI::ParsePort(const wxChar
* uri
)
559 wxASSERT(uri
!= NULL
);
570 //mark the port as valid
571 m_fields
|= wxURI_PORT
;
577 const wxChar
* wxURI::ParsePath(const wxChar
* uri
, bool bReference
, bool bNormalize
)
579 wxASSERT(uri
!= NULL
);
581 //copy of the uri - used for figuring out
582 //length of each component
583 const wxChar
* uricopy
= uri
;
585 /// hier-part = "//" authority path-abempty
590 /// relative-part = "//" authority path-abempty
595 /// path-abempty = *( "/" segment )
596 /// path-absolute = "/" [ segment-nz *( "/" segment ) ]
597 /// path-noscheme = segment-nz-nc *( "/" segment )
598 /// path-rootless = segment-nz *( "/" segment )
599 /// path-empty = 0<pchar>
602 /// segment-nz = 1*pchar
603 /// segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
604 /// ; non-zero-length segment without any colon ":"
606 /// pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
607 if (*uri
== wxT('/'))
611 while(*uri
&& *uri
!= wxT('#') && *uri
!= wxT('?'))
613 if( IsUnreserved(*uri
) || IsSubDelim(*uri
) || IsEscape(uri
) ||
614 *uri
== wxT(':') || *uri
== wxT('@') || *uri
== wxT('/'))
617 Escape(m_path
, *uri
++);
622 wxStringBufferLength
theBuffer(m_path
, m_path
.length() + 1);
624 wxTmemcpy(theBuffer
, m_path
.c_str(), m_path
.length()+1);
626 Normalize(theBuffer
, true);
627 theBuffer
.SetLength(wxStrlen(theBuffer
));
629 //mark the path as valid
630 m_fields
|= wxURI_PATH
;
632 else if(*uri
) //Relative path
637 while(*uri
&& *uri
!= wxT('#') && *uri
!= wxT('?'))
639 if(IsUnreserved(*uri
) || IsSubDelim(*uri
) || IsEscape(uri
) ||
640 *uri
== wxT('@') || *uri
== wxT('/'))
643 Escape(m_path
, *uri
++);
648 while(*uri
&& *uri
!= wxT('#') && *uri
!= wxT('?'))
650 if(IsUnreserved(*uri
) || IsSubDelim(*uri
) || IsEscape(uri
) ||
651 *uri
== wxT(':') || *uri
== wxT('@') || *uri
== wxT('/'))
654 Escape(m_path
, *uri
++);
662 wxStringBufferLength
theBuffer(m_path
, m_path
.length() + 1);
664 wxTmemcpy(theBuffer
, m_path
.c_str(), m_path
.length()+1);
666 Normalize(theBuffer
);
667 theBuffer
.SetLength(wxStrlen(theBuffer
));
670 //mark the path as valid
671 m_fields
|= wxURI_PATH
;
679 const wxChar
* wxURI::ParseQuery(const wxChar
* uri
)
681 wxASSERT(uri
!= NULL
);
683 // query = *( pchar / "/" / "?" )
684 if (*uri
== wxT('?'))
687 while(*uri
&& *uri
!= wxT('#'))
689 if (IsUnreserved(*uri
) || IsSubDelim(*uri
) || IsEscape(uri
) ||
690 *uri
== wxT(':') || *uri
== wxT('@') || *uri
== wxT('/') || *uri
== wxT('?'))
693 Escape(m_query
, *uri
++);
696 //mark the server as valid
697 m_fields
|= wxURI_QUERY
;
704 const wxChar
* wxURI::ParseFragment(const wxChar
* uri
)
706 wxASSERT(uri
!= NULL
);
708 // fragment = *( pchar / "/" / "?" )
709 if (*uri
== wxT('#'))
714 if (IsUnreserved(*uri
) || IsSubDelim(*uri
) || IsEscape(uri
) ||
715 *uri
== wxT(':') || *uri
== wxT('@') || *uri
== wxT('/') || *uri
== wxT('?'))
716 m_fragment
+= *uri
++;
718 Escape(m_fragment
, *uri
++);
721 //mark the server as valid
722 m_fields
|= wxURI_FRAGMENT
;
728 // ---------------------------------------------------------------------------
731 // Builds missing components of this uri from a base uri
733 // A version of the algorithm outlined in the RFC is used here
734 // (it is shown in comments)
736 // Note that an empty URI inherits all components
737 // ---------------------------------------------------------------------------
739 void wxURI::Resolve(const wxURI
& base
, int flags
)
741 wxASSERT_MSG(!base
.IsReference(),
742 wxT("wxURI to inherit from must not be a reference!"));
744 // If we arn't being strict, enable the older (pre-RFC2396)
745 // loophole that allows this uri to inherit other
746 // properties from the base uri - even if the scheme
748 if ( !(flags
& wxURI_STRICT
) &&
749 HasScheme() && base
.HasScheme() &&
750 m_scheme
== base
.m_scheme
)
752 m_fields
-= wxURI_SCHEME
;
756 // Do nothing if this is an absolute wxURI
757 // if defined(R.scheme) then
758 // T.scheme = R.scheme;
759 // T.authority = R.authority;
760 // T.path = remove_dot_segments(R.path);
761 // T.query = R.query;
767 //No scheme - inherit
768 m_scheme
= base
.m_scheme
;
769 m_fields
|= wxURI_SCHEME
;
771 // All we need to do for relative URIs with an
772 // authority component is just inherit the scheme
773 // if defined(R.authority) then
774 // T.authority = R.authority;
775 // T.path = remove_dot_segments(R.path);
776 // T.query = R.query;
782 //No authority - inherit
783 if (base
.HasUserInfo())
785 m_userinfo
= base
.m_userinfo
;
786 m_fields
|= wxURI_USERINFO
;
789 m_server
= base
.m_server
;
790 m_hostType
= base
.m_hostType
;
791 m_fields
|= wxURI_SERVER
;
795 m_port
= base
.m_port
;
796 m_fields
|= wxURI_PORT
;
800 // Simple path inheritance from base
803 // T.path = Base.path;
804 m_path
= base
.m_path
;
805 m_fields
|= wxURI_PATH
;
808 // if defined(R.query) then
809 // T.query = R.query;
811 // T.query = Base.query;
815 m_query
= base
.m_query
;
816 m_fields
|= wxURI_QUERY
;
821 // if (R.path starts-with "/") then
822 // T.path = remove_dot_segments(R.path);
824 // T.path = merge(Base.path, R.path);
825 // T.path = remove_dot_segments(T.path);
827 // T.query = R.query;
828 if (m_path
[0u] != wxT('/'))
831 const wxChar
* op
= m_path
.c_str();
832 const wxChar
* bp
= base
.m_path
.c_str() + base
.m_path
.Length();
834 //not a ending directory? move up
835 if (base
.m_path
[0] && *(bp
-1) != wxT('/'))
836 UpTree(base
.m_path
, bp
);
838 //normalize directories
839 while(*op
== wxT('.') && *(op
+1) == wxT('.') &&
840 (*(op
+2) == '\0' || *(op
+2) == wxT('/')) )
842 UpTree(base
.m_path
, bp
);
850 m_path
= base
.m_path
.substr(0, bp
- base
.m_path
.c_str()) +
851 m_path
.substr((op
- m_path
.c_str()), m_path
.Length());
855 //T.fragment = R.fragment;
858 // ---------------------------------------------------------------------------
861 // Moves a URI path up a directory
862 // ---------------------------------------------------------------------------
865 void wxURI::UpTree(const wxChar
* uristart
, const wxChar
*& uri
)
867 if (uri
!= uristart
&& *(uri
-1) == wxT('/'))
872 for(;uri
!= uristart
; --uri
)
874 if (*uri
== wxT('/'))
882 if (uri
== uristart
&& *uri
== wxT('/'))
887 // ---------------------------------------------------------------------------
890 // Normalizes directories in-place
892 // I.E. ./ and . are ignored
894 // ../ and .. are removed if a directory is before it, along
895 // with that directory (leading .. and ../ are kept)
896 // ---------------------------------------------------------------------------
899 void wxURI::Normalize(wxChar
* s
, bool bIgnoreLeads
)
909 if (*cp
== wxT('.') && (*(cp
+1) == wxT('/') || *(cp
+1) == '\0')
910 && (bp
== cp
|| *(cp
-1) == wxT('/')))
918 else if (*cp
== wxT('.') && *(cp
+1) == wxT('.') &&
919 (*(cp
+2) == wxT('/') || *(cp
+2) == '\0')
920 && (bp
== cp
|| *(cp
-1) == wxT('/')))
922 //.. _or_ ../ - go up the tree
925 UpTree((const wxChar
*)bp
, (const wxChar
*&)s
);
932 else if (!bIgnoreLeads
)
957 // ---------------------------------------------------------------------------
960 // Parses 1 to 4 hex values. Returns true if the first character of the input
961 // string is a valid hex character. It is the caller's responsability to move
962 // the input string back to its original position on failure.
963 // ---------------------------------------------------------------------------
965 bool wxURI::ParseH16(const wxChar
*& uri
)
971 if(IsHex(*++uri
) && IsHex(*++uri
) && IsHex(*++uri
))
977 // ---------------------------------------------------------------------------
980 // Parses a certain version of an IP address and moves the input string past
981 // it. Returns true if the input string contains the proper version of an ip
982 // address. It is the caller's responsability to move the input string back
983 // to its original position on failure.
984 // ---------------------------------------------------------------------------
986 bool wxURI::ParseIPv4address(const wxChar
*& uri
)
988 //IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
990 //dec-octet = DIGIT ; 0-9
991 // / %x31-39 DIGIT ; 10-99
992 // / "1" 2DIGIT ; 100-199
993 // / "2" %x30-34 DIGIT ; 200-249
994 // / "25" %x30-35 ; 250-255
1001 //each ip part must be between 0-255 (dupe of version in for loop)
1002 if( IsDigit(*++uri
) && IsDigit(*++uri
) &&
1003 //100 or less (note !)
1004 !( (*(uri
-2) < wxT('2')) ||
1006 (*(uri
-2) == wxT('2') &&
1007 (*(uri
-1) < wxT('5') || (*(uri
-1) == wxT('5') && *uri
<= wxT('5')))
1015 if(IsDigit(*uri
))++uri
;
1017 //compilers should unroll this loop
1018 for(; iIPv4
< 4; ++iIPv4
)
1020 if (*uri
!= wxT('.') || !IsDigit(*++uri
))
1023 //each ip part must be between 0-255
1024 if( IsDigit(*++uri
) && IsDigit(*++uri
) &&
1025 //100 or less (note !)
1026 !( (*(uri
-2) < wxT('2')) ||
1028 (*(uri
-2) == wxT('2') &&
1029 (*(uri
-1) < wxT('5') || (*(uri
-1) == wxT('5') && *uri
<= wxT('5')))
1036 if(IsDigit(*uri
))++uri
;
1042 bool wxURI::ParseIPv6address(const wxChar
*& uri
)
1044 // IPv6address = 6( h16 ":" ) ls32
1045 // / "::" 5( h16 ":" ) ls32
1046 // / [ h16 ] "::" 4( h16 ":" ) ls32
1047 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1048 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1049 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1050 // / [ *4( h16 ":" ) h16 ] "::" ls32
1051 // / [ *5( h16 ":" ) h16 ] "::" h16
1052 // / [ *6( h16 ":" ) h16 ] "::"
1054 size_t numPrefix
= 0,
1057 bool bEndHex
= false;
1059 for( ; numPrefix
< 6; ++numPrefix
)
1068 if(*uri
!= wxT(':'))
1074 if(!bEndHex
&& !ParseH16(uri
))
1081 if (*uri
== wxT(':'))
1083 if (*++uri
!= wxT(':'))
1093 if (*uri
!= wxT(':') || *(uri
+1) != wxT(':'))
1098 while (*--uri
!= wxT(':')) {}
1101 const wxChar
* uristart
= uri
;
1103 // ls32 = ( h16 ":" h16 ) / IPv4address
1104 if (ParseH16(uri
) && *uri
== wxT(':') && ParseH16(uri
))
1109 if (ParseIPv4address(uri
))
1121 maxPostfix
= 4 - numPrefix
;
1125 bool bAllowAltEnding
= maxPostfix
== 0;
1127 for(; maxPostfix
!= 0; --maxPostfix
)
1129 if(!ParseH16(uri
) || *uri
!= wxT(':'))
1135 const wxChar
* uristart
= uri
;
1137 // ls32 = ( h16 ":" h16 ) / IPv4address
1138 if (ParseH16(uri
) && *uri
== wxT(':') && ParseH16(uri
))
1143 if (ParseIPv4address(uri
))
1148 if (!bAllowAltEnding
)
1152 if(numPrefix
<= 5 && ParseH16(uri
))
1158 bool wxURI::ParseIPvFuture(const wxChar
*& uri
)
1160 // IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
1161 if (*++uri
!= wxT('v') || !IsHex(*++uri
))
1164 while (IsHex(*++uri
)) {}
1166 if (*uri
!= wxT('.') || !(IsUnreserved(*++uri
) || IsSubDelim(*uri
) || *uri
== wxT(':')))
1169 while(IsUnreserved(*++uri
) || IsSubDelim(*uri
) || *uri
== wxT(':')) {}
1175 // ---------------------------------------------------------------------------
1178 // Converts a character into a numeric hexidecimal value, or 0 if the
1179 // passed in character is not a valid hex character
1180 // ---------------------------------------------------------------------------
1183 wxChar
wxURI::CharToHex(const wxChar
& c
)
1185 if ((c
>= wxT('A')) && (c
<= wxT('Z'))) return wxChar(c
- wxT('A') + 0x0A);
1186 if ((c
>= wxT('a')) && (c
<= wxT('z'))) return wxChar(c
- wxT('a') + 0x0a);
1187 if ((c
>= wxT('0')) && (c
<= wxT('9'))) return wxChar(c
- wxT('0') + 0x00);
1192 // ---------------------------------------------------------------------------
1195 // Returns true if the passed in character meets the criteria of the method
1196 // ---------------------------------------------------------------------------
1198 //! unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
1199 bool wxURI::IsUnreserved (const wxChar
& c
)
1200 { return IsAlpha(c
) || IsDigit(c
) ||
1204 c
== wxT('~') //tilde
1208 bool wxURI::IsReserved (const wxChar
& c
)
1210 return IsGenDelim(c
) || IsSubDelim(c
);
1213 //! gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
1214 bool wxURI::IsGenDelim (const wxChar
& c
)
1216 return c
== wxT(':') ||
1225 //! sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
1226 //! / "*" / "+" / "," / ";" / "="
1227 bool wxURI::IsSubDelim (const wxChar
& c
)
1229 return c
== wxT('!') ||
1243 bool wxURI::IsHex(const wxChar
& c
)
1244 { return IsDigit(c
) || (c
>= wxT('a') && c
<= wxT('f')) || (c
>= wxT('A') && c
<= wxT('F')); }
1246 bool wxURI::IsAlpha(const wxChar
& c
)
1247 { return (c
>= wxT('a') && c
<= wxT('z')) || (c
>= wxT('A') && c
<= wxT('Z')); }
1249 bool wxURI::IsDigit(const wxChar
& c
)
1250 { return c
>= wxT('0') && c
<= wxT('9'); }
1253 // ---------------------------------------------------------------------------
1255 // wxURL Compatibility
1257 // ---------------------------------------------------------------------------
1261 #if WXWIN_COMPATIBILITY_2_4
1265 wxString
wxURL::GetProtocolName() const
1270 wxString
wxURL::GetHostName() const
1275 wxString
wxURL::GetPath() const
1280 //Note that this old code really doesn't convert to a URI that well and looks
1281 //more like a dirty hack than anything else...
1283 wxString
wxURL::ConvertToValidURI(const wxString
& uri
, const wxChar
* delims
)
1289 for (i
= 0; i
< uri
.Len(); i
++)
1291 wxChar c
= uri
.GetChar(i
);
1295 // GRG, Apr/2000: changed to "%20" instead of '+'
1297 out_str
+= wxT("%20");
1301 // GRG, Apr/2000: modified according to the URI definition (RFC 2396)
1303 // - Alphanumeric characters are never escaped
1304 // - Unreserved marks are never escaped
1305 // - Delimiters must be escaped if they appear within a component
1306 // but not if they are used to separate components. Here we have
1307 // no clear way to distinguish between these two cases, so they
1308 // are escaped unless they are passed in the 'delims' parameter
1309 // (allowed delimiters).
1311 static const wxChar marks
[] = wxT("-_.!~*()'");
1313 if ( !wxIsalnum(c
) && !wxStrchr(marks
, c
) && !wxStrchr(delims
, c
) )
1315 hexa_code
.Printf(wxT("%%%02X"), c
);
1316 out_str
+= hexa_code
;
1328 wxString
wxURL::ConvertFromURI(const wxString
& uri
)
1330 return wxURI::Unescape(uri
);
1333 #endif //WXWIN_COMPATIBILITY_2_4