5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Garrett Rooney (rooneg@electricjellyfish.net)
7 // Ian MacLean (ianm@activestate.com)
8 // Ben Maurer (bmaurer@users.sourceforge.net)
9 // Atsushi Enomoto (atsushi@ximian.com)
10 // Sebastien Pouliot <sebastien@ximian.com>
11 // Stephane Delcroix <stephane@delcroix.org>
13 // (C) 2001 Garrett Rooney
14 // (C) 2003 Ian MacLean
15 // (C) 2003 Ben Maurer
16 // Copyright (C) 2003,2009 Novell, Inc (http://www.novell.com)
17 // Copyright (c) 2009 Stephane Delcroix
19 // Permission is hereby granted, free of charge, to any person obtaining
20 // a copy of this software and associated documentation files (the
21 // "Software"), to deal in the Software without restriction, including
22 // without limitation the rights to use, copy, modify, merge, publish,
23 // distribute, sublicense, and/or sell copies of the Software, and to
24 // permit persons to whom the Software is furnished to do so, subject to
25 // the following conditions:
27 // The above copyright notice and this permission notice shall be
28 // included in all copies or substantial portions of the Software.
30 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 // See RFC 2396 for more info on URI's.
40 // TODO: optimize by parsing host string only once
42 using System.ComponentModel;
45 using System.Runtime.Serialization;
47 using System.Collections;
48 using System.Collections.Generic;
49 using System.Globalization;
52 // Disable warnings on Obsolete methods being used
54 #pragma warning disable 612
59 [TypeConverter (typeof (UriTypeConverter))]
60 public class Uri : ISerializable {
62 // o scheme excludes the scheme delimiter
63 // o port is -1 to indicate no port is defined
64 // o path is empty or starts with / when scheme delimiter == "://"
65 // o query is empty or starts with ? char, escaped.
66 // o fragment is empty or starts with # char, unescaped.
67 // o all class variables are in escaped format when they are escapable,
68 // except cachedToString.
69 // o UNC is supported, as starts with "\\" for windows,
72 private string source;
73 private string scheme = String.Empty;
74 private string host = String.Empty;
75 private int port = -1;
76 private string path = String.Empty;
77 private string query = String.Empty;
78 private string fragment = String.Empty;
79 private string userinfo;
81 private bool isAbsoluteUri = true;
82 private long scope_id;
84 private List<string> segments;
86 private bool userEscaped;
87 private string cachedAbsoluteUri;
88 private string cachedToString;
89 private string cachedLocalPath;
90 private int cachedHashCode;
92 private static bool s_IriParsing;
94 internal static bool IriParsing {
95 get { return s_IriParsing; }
96 set { s_IriParsing = value; }
100 private static readonly string hexUpperChars = "0123456789ABCDEF";
101 private static readonly string [] Empty = new string [0];
102 private static bool isWin32 = (Path.DirectorySeparatorChar == '\\');
104 static readonly char[] hexUpperChars = new [] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
109 public static readonly string SchemeDelimiter = "://";
110 public static readonly string UriSchemeFile = "file";
111 public static readonly string UriSchemeFtp = "ftp";
112 public static readonly string UriSchemeGopher = "gopher";
113 public static readonly string UriSchemeHttp = "http";
114 public static readonly string UriSchemeHttps = "https";
115 public static readonly string UriSchemeMailto = "mailto";
116 public static readonly string UriSchemeNews = "news";
117 public static readonly string UriSchemeNntp = "nntp";
118 public static readonly string UriSchemeNetPipe = "net.pipe";
119 public static readonly string UriSchemeNetTcp = "net.tcp";
121 internal static readonly string UriSchemeTelnet = "telnet";
122 internal static readonly string UriSchemeLdap = "ldap";
123 internal static readonly string UriSchemeUuid = "uuid";
125 private static readonly string [] knownUriSchemes =
147 var iriparsingVar = Environment.GetEnvironmentVariable ("MONO_URI_IRIPARSING");
148 if (iriparsingVar == "true")
150 else if (iriparsingVar == "false")
154 public Uri (string uriString) : this (uriString, false)
158 protected Uri (SerializationInfo serializationInfo, StreamingContext streamingContext)
160 string uri = serializationInfo.GetString ("AbsoluteUri");
161 if (uri.Length > 0) {
163 ParseUri (UriKind.Absolute);
165 uri = serializationInfo.GetString ("RelativeUri");
166 if (uri.Length > 0) {
168 ParseUri (UriKind.Relative);
170 throw new ArgumentException ("Uri string was null or empty.");
175 public Uri (string uriString, UriKind uriKind)
181 case UriKind.Absolute:
183 throw new UriFormatException ("Invalid URI: The format of the URI could not be "
186 case UriKind.Relative:
188 throw new UriFormatException ("Invalid URI: The format of the URI could not be "
189 + "determined because the parameter 'uriString' represents an absolute URI.");
191 case UriKind.RelativeOrAbsolute:
194 string msg = Locale.GetText ("Invalid UriKind value '{0}'.", uriKind);
195 throw new ArgumentException (msg);
200 // An exception-less constructor, returns success
201 // condition on the out parameter `success'.
203 Uri (string uriString, UriKind uriKind, out bool success)
205 if (uriString == null) {
210 if (uriKind != UriKind.RelativeOrAbsolute &&
211 uriKind != UriKind.Absolute &&
212 uriKind != UriKind.Relative) {
213 string msg = Locale.GetText ("Invalid UriKind value '{0}'.", uriKind);
214 throw new ArgumentException (msg);
218 if (ParseNoExceptions (uriKind, uriString) != null)
224 case UriKind.Absolute:
228 case UriKind.Relative:
232 case UriKind.RelativeOrAbsolute:
241 public Uri (Uri baseUri, Uri relativeUri)
243 Merge (baseUri, relativeUri == null ? String.Empty : relativeUri.OriginalString);
244 // FIXME: this should call UriParser.Resolve
247 // note: doc says that dontEscape is always false but tests show otherwise
249 public Uri (string uriString, bool dontEscape)
251 userEscaped = dontEscape;
253 ParseUri (UriKind.Absolute);
255 throw new UriFormatException ("Invalid URI: The format of the URI could not be "
256 + "determined: " + uriString);
259 public Uri (Uri baseUri, string relativeUri)
261 Merge (baseUri, relativeUri);
262 // FIXME: this should call UriParser.Resolve
265 [Obsolete ("dontEscape is always false")]
266 public Uri (Uri baseUri, string relativeUri, bool dontEscape)
268 userEscaped = dontEscape;
269 Merge (baseUri, relativeUri);
272 private void Merge (Uri baseUri, string relativeUri)
275 throw new ArgumentNullException ("baseUri");
276 if (!baseUri.IsAbsoluteUri)
277 throw new ArgumentOutOfRangeException ("baseUri");
278 if (string.IsNullOrEmpty (relativeUri)) {
279 source = baseUri.OriginalString;
280 ParseUri (UriKind.Absolute);
285 bool startsWithSlash = false;
288 if (!UriParseComponents.TryParseComponents (baseUri.OriginalString, UriKind.Absolute, out baseEl, out error))
289 throw new UriFormatException (error);
291 if (relativeUri.StartsWith (baseEl.scheme + ":", StringComparison.Ordinal))
292 relativeUri = relativeUri.Substring (baseEl.scheme.Length + 1);
294 if (relativeUri.Length >= 1 && relativeUri [0] == '/') {
295 if (relativeUri.Length >= 2 && relativeUri [1] == '/') {
296 source = baseEl.scheme + ":" + relativeUri;
297 ParseUri (UriKind.Absolute);
301 relativeUri = relativeUri.Substring (1);
302 startsWithSlash = true;
305 UriElements relativeEl;
306 if (!UriParseComponents.TryParseComponents (relativeUri, UriKind.RelativeOrAbsolute, out relativeEl, out error))
307 throw new UriFormatException (error);
309 if (relativeEl.isAbsoluteUri) {
310 source = relativeUri;
311 ParseUri (UriKind.Absolute);
315 source = baseEl.scheme + baseEl.delimiter;
317 if (baseEl.user != null)
318 source += baseEl.user + "@";
320 source += baseEl.host;
322 if (baseEl.port >= 0)
323 source += ":" + baseEl.port.ToString (CultureInfo.InvariantCulture);
325 var canUseBase = true;
328 if (!string.IsNullOrEmpty (relativeEl.path) || startsWithSlash) {
330 path = relativeEl.path;
332 path = relativeEl.path;
334 var pathEnd = baseEl.path.LastIndexOf ('/');
335 path = (pathEnd > 0)? baseEl.path.Substring (0, pathEnd+1) : "";
336 path += relativeEl.path;
341 if (relativeEl.query != null) {
342 var pathEnd = path.LastIndexOf ('/');
343 path = (pathEnd > 0)? path.Substring (0, pathEnd+1) : "";
348 if ((path.Length == 0 || path [0] != '/') && baseEl.delimiter == SchemeDelimiter)
351 source += UriHelper.Reduce (path, true);
353 if (relativeEl.query != null) {
355 source += "?" + relativeEl.query;
356 } else if (canUseBase && baseEl.query != null)
357 source += "?" + baseEl.query;
359 if (relativeEl.fragment != null)
360 source += "#" + relativeEl.fragment;
361 else if (canUseBase && baseEl.fragment != null)
362 source += "#" + baseEl.fragment;
364 ParseUri (UriKind.Absolute);
371 public string AbsolutePath {
373 EnsureAbsoluteUri ();
374 if (scheme == "mailto" || scheme == "file")
375 // faster (mailto) and special (file) cases
378 if (path.Length == 0) {
379 string start = scheme + SchemeDelimiter;
380 if (path.StartsWith (start, StringComparison.Ordinal))
389 public string AbsoluteUri {
391 EnsureAbsoluteUri ();
393 if (cachedAbsoluteUri == null)
394 cachedAbsoluteUri = GetComponents (UriComponents.AbsoluteUri, UriFormat.UriEscaped);
396 return cachedAbsoluteUri;
400 public string Authority {
402 EnsureAbsoluteUri ();
403 return (GetDefaultPort (Scheme) == port)
404 ? host : host + ":" + port;
408 public string Fragment {
410 EnsureAbsoluteUri ();
417 EnsureAbsoluteUri ();
422 public UriHostNameType HostNameType {
424 EnsureAbsoluteUri ();
425 UriHostNameType ret = CheckHostName (Host);
426 if (ret != UriHostNameType.Unknown)
429 if (scheme == "mailto")
430 return UriHostNameType.Basic;
431 return (IsFile) ? UriHostNameType.Basic : ret;
435 public bool IsDefaultPort {
437 EnsureAbsoluteUri ();
438 return GetDefaultPort (Scheme) == port;
444 EnsureAbsoluteUri ();
445 return (Scheme == UriSchemeFile);
449 public bool IsLoopback {
451 EnsureAbsoluteUri ();
453 if (Host.Length == 0) {
457 if (host == "loopback" || host == "localhost")
461 if (IPAddress.TryParse (host, out result))
462 if (IPAddress.Loopback.Equals (result))
466 if (IPv6Address.TryParse (host, out result6)){
467 if (IPv6Address.IsLoopback (result6))
476 // rule: This should be true only if
477 // - uri string starts from "\\", or
478 // - uri string starts from "//" (Samba way)
480 EnsureAbsoluteUri ();
485 private bool IsLocalIdenticalToAbsolutePath ()
490 if ((scheme == Uri.UriSchemeNews) || (scheme == Uri.UriSchemeNntp) || (scheme == Uri.UriSchemeFtp))
493 return IsWellFormedOriginalString ();
496 public string LocalPath {
498 EnsureAbsoluteUri ();
499 if (cachedLocalPath != null)
500 return cachedLocalPath;
502 var formatFlags = UriHelper.FormatFlags.NoSlashReplace;
505 formatFlags |= UriHelper.FormatFlags.UserEscaped;
507 string unescapedPath = UriHelper.FormatAbsolute (path, scheme,
508 UriComponents.Path, UriFormat.Unescaped, formatFlags);
510 if (path.StartsWith ("/", StringComparison.Ordinal) &&
511 !unescapedPath.StartsWith ("/", StringComparison.Ordinal))
512 unescapedPath = "/" + unescapedPath;
514 if (IsLocalIdenticalToAbsolutePath ()) {
515 cachedLocalPath = unescapedPath;
516 return cachedLocalPath;
520 bool windows = (path.Length > 3 && path [1] == ':' &&
521 (path [2] == '\\' || path [2] == '/'));
524 cachedLocalPath = unescapedPath.Replace ('/', '\\');
526 cachedLocalPath = unescapedPath;
528 // support *nix and W32 styles
529 if (path.Length > 1 && path [1] == ':')
530 cachedLocalPath = unescapedPath.Replace (Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
532 // LAMESPEC: ok, now we cannot determine
533 // if such URI like "file://foo/bar" is
534 // Windows UNC or unix file path, so
535 // they should be handled differently.
536 else if (System.IO.Path.DirectorySeparatorChar == '\\') {
538 if (path.Length > 0) {
539 if ((path.Length > 1) || (path[0] != '/')) {
540 h += unescapedPath.Replace ('/', '\\');
543 cachedLocalPath = "\\\\" + h;
545 cachedLocalPath = unescapedPath;
547 if (cachedLocalPath.Length == 0)
548 cachedLocalPath = Path.DirectorySeparatorChar.ToString ();
549 return cachedLocalPath;
553 public string PathAndQuery {
555 EnsureAbsoluteUri ();
562 EnsureAbsoluteUri ();
567 public string Query {
569 EnsureAbsoluteUri ();
574 public string Scheme {
576 EnsureAbsoluteUri ();
581 public string [] Segments {
583 EnsureAbsoluteUri ();
585 // return a (pre-allocated) empty array
586 if (path.Length == 0)
590 return EmptyArray<string>.Value;
592 // do not return the original array (since items can be changed)
593 if (segments != null)
594 return segments.ToArray ();
596 List<string> list = new List<string> ();
597 StringBuilder current = new StringBuilder ();
598 for (int i = 0; i < path.Length; i++) {
602 current.Append (path [i]);
603 list.Add (current.ToString ());
607 if ((i < path.Length - 2) && (path [i + 1] == '5' && path [i + 2] == 'C')) {
608 current.Append ("%5C");
609 list.Add (current.ToString ());
613 current.Append ('%');
617 current.Append (path [i]);
622 if (current.Length > 0)
623 list.Add (current.ToString ());
625 if (IsFile && (list.Count > 0)) {
626 string first = list [0];
627 if ((first.Length > 1) && (first [1] == ':')) {
628 list.Insert (0, "/");
632 return segments.ToArray ();
636 public bool UserEscaped {
637 get { return userEscaped; }
640 public string UserInfo {
642 EnsureAbsoluteUri ();
643 return userinfo == null ? String.Empty : userinfo;
647 public string DnsSafeHost {
649 EnsureAbsoluteUri ();
651 if (HostNameType == UriHostNameType.IPv6) {
652 host = Host.Substring (1, Host.Length - 2);
654 host += "%" + scope_id.ToString ();
656 return Unescape (host);
660 public bool IsAbsoluteUri {
661 get { return isAbsoluteUri; }
664 public string OriginalString {
665 get { return source; }
670 public static UriHostNameType CheckHostName (string name)
672 if (name == null || name.Length == 0)
673 return UriHostNameType.Unknown;
675 if (IsIPv4Address (name))
676 return UriHostNameType.IPv4;
678 if (IsDomainAddress (name))
679 return UriHostNameType.Dns;
682 if (IPv6Address.TryParse (name, out addr))
683 return UriHostNameType.IPv6;
685 return UriHostNameType.Unknown;
688 internal static bool IsIPv4Address (string name)
690 string [] captures = name.Split (new char [] {'.'});
691 if (captures.Length != 4)
694 for (int i = 0; i < 4; i++) {
697 length = captures [i].Length;
701 if (!UInt32.TryParse (captures [i], out number))
709 internal static bool IsDomainAddress (string name)
711 int len = name.Length;
714 for (int i = 0; i < len; i++) {
717 if (!Char.IsLetterOrDigit (c))
719 } else if (c == '.') {
720 // www..host.com is bad
721 if (i + 1 < len && name [i + 1] == '.')
725 } else if (!Char.IsLetterOrDigit (c) && c != '-' && c != '_') {
736 [Obsolete ("This method does nothing, it has been obsoleted")]
737 protected virtual void Canonicalize ()
740 // This is flagged in the Microsoft documentation as used
741 // internally, no longer in use, and Obsolete.
745 [MonoTODO ("Find out what this should do")]
747 protected virtual void CheckSecurity ()
753 // defined in RFC3986 as = ALPHA *( ALPHA / DIGIT / "+" / "-" / ".")
754 public static bool CheckSchemeName (string schemeName)
756 if (schemeName == null || schemeName.Length == 0)
759 if (!IsAlpha (schemeName [0]))
762 int len = schemeName.Length;
763 for (int i = 1; i < len; i++) {
764 char c = schemeName [i];
765 if (!Char.IsDigit (c) && !IsAlpha (c) && c != '.' && c != '+' && c != '-')
772 private static bool IsAlpha (char c)
774 // as defined in rfc2234
775 // %x41-5A / %x61-7A (A-Z / a-z)
777 return (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
780 public override bool Equals (object comparand)
782 if (comparand == null)
785 Uri uri = comparand as Uri;
786 if ((object) uri == null) {
787 string s = comparand as String;
791 if (!TryCreate (s, UriKind.RelativeOrAbsolute, out uri))
795 return InternalEquals (uri);
798 // Assumes: uri != null
799 bool InternalEquals (Uri uri)
801 if (this.isAbsoluteUri != uri.isAbsoluteUri)
803 if (!this.isAbsoluteUri)
804 return this.source == uri.source;
806 CultureInfo inv = CultureInfo.InvariantCulture;
807 return this.scheme.ToLower (inv) == uri.scheme.ToLower (inv)
808 && this.host.ToLower (inv) == uri.host.ToLower (inv)
809 && this.port == uri.port
810 && this.query == uri.query
811 && this.path == uri.path;
814 public static bool operator == (Uri uri1, Uri uri2)
816 return object.Equals (uri1, uri2);
819 public static bool operator != (Uri uri1, Uri uri2)
821 return !(uri1 == uri2);
824 public override int GetHashCode ()
826 if (cachedHashCode == 0) {
827 CultureInfo inv = CultureInfo.InvariantCulture;
829 cachedHashCode = scheme.ToLower (inv).GetHashCode ()
830 ^ host.ToLower (inv).GetHashCode ()
832 ^ query.GetHashCode ()
833 ^ path.GetHashCode ();
836 cachedHashCode = source.GetHashCode ();
839 return cachedHashCode;
842 public string GetLeftPart (UriPartial part)
844 EnsureAbsoluteUri ();
847 case UriPartial.Scheme :
848 return scheme + GetOpaqueWiseSchemeDelimiter ();
849 case UriPartial.Authority :
850 if ((scheme == Uri.UriSchemeMailto) || (scheme == Uri.UriSchemeNews))
853 StringBuilder s = new StringBuilder ();
855 s.Append (GetOpaqueWiseSchemeDelimiter ());
856 if (path.Length > 1 && path [1] == ':' && (Uri.UriSchemeFile == scheme))
857 s.Append ('/'); // win32 file
858 if (userinfo != null)
859 s.Append (userinfo).Append ('@');
861 defaultPort = GetDefaultPort (scheme);
862 if ((port != -1) && (port != defaultPort))
863 s.Append (':').Append (port);
864 return s.ToString ();
865 case UriPartial.Path :
866 StringBuilder sb = new StringBuilder ();
868 sb.Append (GetOpaqueWiseSchemeDelimiter ());
869 if (path.Length > 1 && path [1] == ':' && (Uri.UriSchemeFile == scheme))
870 sb.Append ('/'); // win32 file
871 if (userinfo != null)
872 sb.Append (userinfo).Append ('@');
874 defaultPort = GetDefaultPort (scheme);
875 if ((port != -1) && (port != defaultPort))
876 sb.Append (':').Append (port);
878 if (path.Length > 0) {
879 if (scheme == "mailto" || scheme == "news")
882 sb.Append (Reduce (path, CompactEscaped (scheme)));
884 return sb.ToString ();
889 public static int FromHex (char digit)
891 if ('0' <= digit && digit <= '9') {
892 return (int) (digit - '0');
895 if ('a' <= digit && digit <= 'f')
896 return (int) (digit - 'a' + 10);
898 if ('A' <= digit && digit <= 'F')
899 return (int) (digit - 'A' + 10);
901 throw new ArgumentException ("digit");
904 public static string HexEscape (char character)
906 if (character > 255) {
907 throw new ArgumentOutOfRangeException ("character");
910 return "%" + hexUpperChars [((character & 0xf0) >> 4)]
911 + hexUpperChars [((character & 0x0f))];
914 public static char HexUnescape (string pattern, ref int index)
917 throw new ArgumentException ("pattern");
919 if (index < 0 || index >= pattern.Length)
920 throw new ArgumentOutOfRangeException ("index");
922 if (!IsHexEncoding (pattern, index))
923 return pattern [index++];
926 int msb = FromHex (pattern [index++]);
927 int lsb = FromHex (pattern [index++]);
928 return (char) ((msb << 4) | lsb);
931 public static bool IsHexDigit (char character)
933 return (('0' <= character && character <= '9') ||
934 ('a' <= character && character <= 'f') ||
935 ('A' <= character && character <= 'F'));
938 public static bool IsHexEncoding (string pattern, int index)
940 if ((index + 3) > pattern.Length)
943 return ((pattern [index++] == '%') &&
944 IsHexDigit (pattern [index++]) &&
945 IsHexDigit (pattern [index]));
949 // Implemented by copying most of the MakeRelative code
951 public Uri MakeRelativeUri (Uri uri)
955 throw new ArgumentNullException ("uri");
957 if (Host != uri.Host || Scheme != uri.Scheme)
960 string result = String.Empty;
961 if (this.path != uri.path){
962 string [] segments = this.Segments;
963 string [] segments2 = uri.Segments;
966 int max = Math.Min (segments.Length, segments2.Length);
968 if (segments [k] != segments2 [k])
971 for (int i = k; i < segments.Length && segments [i].EndsWith ("/", StringComparison.Ordinal); i++)
973 for (int i = k; i < segments2.Length; i++)
974 result += segments2 [i];
976 if (result == string.Empty)
979 uri.AppendQueryAndFragment (ref result);
981 return new Uri (result, UriKind.Relative);
984 [Obsolete ("Use MakeRelativeUri(Uri uri) instead.")]
985 public string MakeRelative (Uri toUri)
987 if ((this.Scheme != toUri.Scheme) ||
988 (this.Authority != toUri.Authority))
989 return toUri.ToString ();
991 string result = String.Empty;
992 if (this.path != toUri.path){
993 string [] segments = this.Segments;
994 string [] segments2 = toUri.Segments;
996 int max = Math.Min (segments.Length, segments2.Length);
998 if (segments [k] != segments2 [k])
1001 for (int i = k + 1; i < segments.Length; i++)
1003 for (int i = k; i < segments2.Length; i++)
1004 result += segments2 [i];
1007 // Important: MakeRelative does not append fragment or query.
1012 void AppendQueryAndFragment (ref string result)
1014 if (query.Length > 0) {
1015 string q = query [0] == '?' ? '?' + Unescape (query.Substring (1), true, false) : Unescape (query, false);
1018 if (fragment.Length > 0)
1019 result += Unescape (fragment, true, false);
1022 public override string ToString ()
1024 if (cachedToString != null)
1025 return cachedToString;
1027 if (isAbsoluteUri) {
1028 if (Parser is DefaultUriParser)
1029 cachedToString = Parser.GetComponentsHelper (this, UriComponents.AbsoluteUri, UriHelper.ToStringUnescape);
1031 cachedToString = Parser.GetComponents (this, UriComponents.AbsoluteUri, UriFormat.SafeUnescaped);
1033 cachedToString = UriHelper.FormatRelative (source, scheme, UriHelper.ToStringUnescape);
1035 return cachedToString;
1038 protected void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
1040 if (this.isAbsoluteUri) {
1041 serializationInfo.AddValue ("AbsoluteUri", this.AbsoluteUri);
1043 serializationInfo.AddValue ("AbsoluteUri", String.Empty);
1044 serializationInfo.AddValue ("RelativeUri", this.OriginalString);
1048 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
1050 GetObjectData (info, context);
1057 protected virtual void Escape ()
1059 path = EscapeString (path);
1063 protected static string EscapeString (string str)
1065 return EscapeString (str, Uri.EscapeCommonHexBrackets);
1068 private const string EscapeCommon = "<>%\"{}|\\^`";
1069 private const string EscapeReserved = ";/?:@&=+$,";
1070 private const string EscapeFragment = "#";
1071 private const string EscapeBrackets = "[]";
1073 private const string EscapeNews = EscapeCommon + EscapeBrackets + "?";
1074 private const string EscapeCommonHex = EscapeCommon + EscapeFragment;
1075 private const string EscapeCommonBrackets = EscapeCommon + EscapeBrackets;
1076 internal const string EscapeCommonHexBrackets = EscapeCommon + EscapeFragment + EscapeBrackets;
1077 internal const string EscapeCommonHexBracketsQuery = EscapeCommonHexBrackets + "?";
1079 internal static string EscapeString (string str, string escape)
1081 return EscapeString (str, escape, true);
1084 internal static string EscapeString (string str, string escape, bool nonAsciiEscape)
1086 if (String.IsNullOrEmpty (str))
1087 return String.Empty;
1089 StringBuilder s = new StringBuilder ();
1090 int len = str.Length;
1091 for (int i = 0; i < len; i++) {
1092 // reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
1093 // mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
1094 // control = <US-ASCII coded characters 00-1F and 7F hexadecimal>
1095 // space = <US-ASCII coded character 20 hexadecimal>
1096 // delims = "<" | ">" | "#" | "%" | <">
1097 // unwise = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"
1099 // check for escape code already placed in str,
1100 // i.e. for encoding that follows the pattern
1101 // "%hexhex" in a string, where "hex" is a digit from 0-9
1102 // or a letter from A-F (case-insensitive).
1103 if (IsHexEncoding (str,i)) {
1104 // if ,yes , copy it as is
1105 s.Append (str.Substring (i, 3));
1111 bool outside_limited_ascii = ((c <= 0x20) || (c >= 0x7f));
1112 bool needs_escape = (escape.IndexOf (c) != -1);
1113 if (nonAsciiEscape && outside_limited_ascii) {
1114 byte [] data = Encoding.UTF8.GetBytes (new char [] { c });
1115 int length = data.Length;
1116 for (int j = 0; j < length; j++) {
1117 c = (char) data [j];
1118 if (needs_escape || nonAsciiEscape)
1119 s.Append (HexEscape (c));
1123 } else if (needs_escape) {
1124 s.Append (HexEscape (c));
1130 return s.ToString ();
1133 // On .NET 1.x, this method is called from .ctor(). When overriden, we
1134 // can avoid the "absolute uri" constraints of the .ctor() by
1135 // overriding with custom code.
1136 [Obsolete("The method has been deprecated. It is not used by the system.")]
1137 protected virtual void Parse ()
1141 private void ParseUri (UriKind kind)
1143 Parse (kind, source);
1148 if (host.Length > 1 && host [0] != '[' && host [host.Length - 1] != ']') {
1149 // host name present (but not an IPv6 address)
1150 host = host.ToLower (CultureInfo.InvariantCulture);
1155 protected virtual string Unescape (string path)
1157 var formatFlags = UriHelper.FormatFlags.NoSlashReplace | UriHelper.FormatFlags.NoReduce;
1158 return UriHelper.FormatAbsolute (path, scheme, UriComponents.Path, UriFormat.Unescaped, formatFlags);
1161 internal static string Unescape (string str, bool excludeSpecial)
1163 return Unescape (str, excludeSpecial, excludeSpecial);
1166 internal static string Unescape (string str, bool excludeSpecial, bool excludeBackslash)
1168 if (String.IsNullOrEmpty (str))
1169 return String.Empty;
1171 StringBuilder s = new StringBuilder ();
1172 int len = str.Length;
1173 for (int i = 0; i < len; i++) {
1177 char x = HexUnescapeMultiByte (str, ref i, out surrogate);
1178 if (excludeSpecial && x == '#')
1180 else if (excludeSpecial && x == '%')
1182 else if (excludeSpecial && x == '?')
1184 else if (excludeBackslash && x == '\\')
1188 if (surrogate != char.MinValue)
1189 s.Append (surrogate);
1195 return s.ToString ();
1201 private void ParseAsWindowsUNC (string uriString)
1203 scheme = UriSchemeFile;
1205 fragment = String.Empty;
1206 query = String.Empty;
1209 uriString = uriString.TrimStart (new char [] {'\\'});
1210 int pos = uriString.IndexOf ('\\');
1212 path = uriString.Substring (pos);
1213 host = uriString.Substring (0, pos);
1214 } else { // "\\\\server"
1216 path = String.Empty;
1218 path = path.Replace ("\\", "/");
1222 // Returns null on success, string with error on failure
1224 private string ParseAsWindowsAbsoluteFilePath (string uriString)
1226 if (uriString.Length > 2 && uriString [2] != '\\' && uriString [2] != '/')
1227 return "Relative file path is not allowed.";
1228 scheme = UriSchemeFile;
1229 host = String.Empty;
1231 path = uriString.Replace ("\\", "/");
1232 fragment = String.Empty;
1233 query = String.Empty;
1238 private void ParseAsUnixAbsoluteFilePath (string uriString)
1240 scheme = UriSchemeFile;
1242 fragment = String.Empty;
1243 query = String.Empty;
1244 host = String.Empty;
1247 if (uriString.Length >= 2 && uriString [0] == '/' && uriString [1] == '/') {
1248 uriString = uriString.TrimStart (new char [] {'/'});
1249 // Now we don't regard //foo/bar as "foo" host.
1251 int pos = uriString.IndexOf ('/');
1253 path = '/' + uriString.Substring (pos + 1);
1254 host = uriString.Substring (0, pos);
1255 } else { // "///server"
1257 path = String.Empty;
1260 path = '/' + uriString;
1267 // This parse method will throw exceptions on failure
1269 private void Parse (UriKind kind, string uriString)
1271 if (uriString == null)
1272 throw new ArgumentNullException ("uriString");
1274 string s = ParseNoExceptions (kind, uriString);
1276 throw new UriFormatException (s);
1279 private bool SupportsQuery ()
1281 return UriHelper.SupportsQuery (scheme);
1285 private string ParseNoExceptions (UriKind kind, string uriString)
1287 UriElements elements;
1289 if (!UriParseComponents.TryParseComponents (source, kind, out elements, out error))
1292 scheme = elements.scheme;
1293 var parser = UriParser.GetParser (scheme);
1294 if (parser != null && !(parser is DefaultUriParser)) {
1295 userinfo = Parser.GetComponents (this, UriComponents.UserInfo, UriFormat.UriEscaped);
1296 host = Parser.GetComponents (this, UriComponents.Host, UriFormat.UriEscaped);
1298 var portStr = Parser.GetComponents (this, UriComponents.StrongPort, UriFormat.UriEscaped);
1299 if (!string.IsNullOrEmpty (portStr))
1300 port = int.Parse (portStr);
1302 path = Parser.GetComponents (this, UriComponents.Path | UriComponents.KeepDelimiter, UriFormat.UriEscaped);
1303 query = Parser.GetComponents (this, UriComponents.Query, UriFormat.UriEscaped);
1304 fragment = Parser.GetComponents (this, UriComponents.StrongPort, UriFormat.UriEscaped);
1309 var formatFlags = UriHelper.FormatFlags.None;
1310 if (UriHelper.HasCharactersToNormalize (uriString))
1311 formatFlags |= UriHelper.FormatFlags.HasUriCharactersToNormalize;
1314 formatFlags |= UriHelper.FormatFlags.UserEscaped;
1316 if (elements.host != null)
1317 formatFlags |= UriHelper.FormatFlags.HasHost;
1319 userinfo = elements.user;
1321 if (elements.host != null) {
1322 host = UriHelper.FormatAbsolute (elements.host, scheme,
1323 UriComponents.Host, UriFormat.UriEscaped, formatFlags);
1326 port = elements.port;
1329 port = GetDefaultPort (scheme);
1331 if (elements.path != null) {
1332 path = UriHelper.FormatAbsolute (elements.path, scheme,
1333 UriComponents.Path, UriFormat.UriEscaped, formatFlags);
1334 if (elements.delimiter == SchemeDelimiter && string.IsNullOrEmpty (path))
1338 if (elements.query != null) {
1339 query = "?" + UriHelper.FormatAbsolute (elements.query, scheme,
1340 UriComponents.Query, UriFormat.UriEscaped, formatFlags);
1343 if (elements.fragment != null) {
1344 fragment = "#" + UriHelper.FormatAbsolute (elements.fragment, scheme,
1345 UriComponents.Fragment, UriFormat.UriEscaped, formatFlags);
1348 isAbsoluteUri = elements.isAbsoluteUri;
1349 isUnc = elements.isUnc;
1350 scope_id = elements.scopeId;
1355 private static string TryGetKnownUriSchemeInstance (string scheme)
1357 foreach (string knownScheme in knownUriSchemes) {
1358 if (knownScheme == scheme)
1365 private static bool CompactEscaped (string scheme)
1367 if (scheme == null || scheme.Length < 4)
1370 char first = scheme [0];
1372 return scheme == "http" || scheme == "https";
1373 } else if (first == 'f' && scheme == "file"){
1375 } else if (first == 'n')
1376 return scheme == "net.pipe" || scheme == "net.tcp";
1381 // replace '\', %5C ('\') and %2f ('/') into '/'
1382 // replace %2e ('.') into '.'
1383 private static string NormalizePath (string path)
1385 StringBuilder res = new StringBuilder ();
1386 for (int i = 0; i < path.Length; i++) {
1393 if (i < path.Length - 2) {
1394 char c1 = path [i + 1];
1395 char c2 = Char.ToUpper (path [i + 2]);
1396 if ((c1 == '2') && (c2 == 'E')) {
1399 } else if (((c1 == '2') && (c2 == 'F')) || ((c1 == '5') && (c2 == 'C'))) {
1408 return res.ToString ();
1411 // This is called "compacting" in the MSDN documentation
1412 private static string Reduce (string path, bool compact_escaped)
1414 // quick out, allocation-free, for a common case
1418 if (compact_escaped && (path.IndexOf ('%') != -1)) {
1419 // replace '\', %2f, %5c with '/' and replace %2e with '.'
1420 // other escaped values seems to survive this step
1421 path = NormalizePath (path);
1423 // (always) replace '\' with '/'
1424 path = path.Replace ('\\', '/');
1427 List<string> result = new List<string> ();
1430 for (int startpos = 0; startpos < path.Length; ) {
1431 int endpos = path.IndexOf ('/', startpos);
1433 endpos = path.Length;
1434 string current = path.Substring (startpos, endpos-startpos);
1435 startpos = endpos + 1;
1436 if ((begin && current.Length == 0) || current == "." ) {
1442 if (current == "..") {
1443 int resultCount = result.Count;
1444 // in 2.0 profile, skip leading ".." parts
1445 if (resultCount == 0) {
1449 result.RemoveAt (resultCount - 1);
1453 result.Add (current);
1456 if (result.Count == 0)
1459 StringBuilder res = new StringBuilder ();
1461 if (path [0] == '/')
1465 foreach (string part in result) {
1474 if (path [path.Length - 1] == '/')
1477 return res.ToString ();
1480 // A variant of HexUnescape() which can decode multi-byte escaped
1481 // sequences such as (e.g.) %E3%81%8B into a single character
1482 internal static char HexUnescapeMultiByte (string pattern, ref int index, out char surrogate)
1485 return HexUnescapeMultiByte (pattern, ref index, out surrogate, out invalidEscape);
1488 internal static char HexUnescapeMultiByte (string pattern, ref int index, out char surrogate, out bool invalidEscape)
1490 surrogate = char.MinValue;
1491 invalidEscape = false;
1493 if (pattern == null)
1494 throw new ArgumentException ("pattern");
1496 if (index < 0 || index >= pattern.Length)
1497 throw new ArgumentOutOfRangeException ("index");
1499 if (!IsHexEncoding (pattern, index))
1500 return pattern [index++];
1502 int orig_index = index++;
1503 int msb = FromHex (pattern [index++]);
1504 int lsb = FromHex (pattern [index++]);
1506 // We might be dealing with a multi-byte character:
1507 // The number of ones at the top-end of the first byte will tell us
1508 // how many bytes will make up this character.
1511 while ((msb_copy & 0x8) == 0x8) {
1516 // We might be dealing with a single-byte character:
1517 // If there was only 0 or 1 leading ones then we're not dealing
1518 // with a multi-byte character.
1519 if (num_bytes <= 1) {
1520 var c = (char) ((msb << 4) | lsb);
1521 invalidEscape = c > 0x7F;
1525 // Now that we know how many bytes *should* follow, we'll check them
1526 // to ensure we are dealing with a valid multi-byte character.
1527 byte [] chars = new byte [num_bytes];
1528 bool all_invalid = false;
1529 chars[0] = (byte) ((msb << 4) | lsb);
1531 for (int i = 1; i < num_bytes; i++) {
1532 if (!IsHexEncoding (pattern, index++)) {
1537 // All following bytes must be in the form 10xxxxxx
1538 int cur_msb = FromHex (pattern [index++]);
1539 if ((cur_msb & 0xc) != 0x8) {
1544 int cur_lsb = FromHex (pattern [index++]);
1545 chars[i] = (byte) ((cur_msb << 4) | cur_lsb);
1548 // If what looked like a multi-byte character is invalid, then we'll
1549 // just return the first byte as a single byte character.
1551 invalidEscape = true;
1552 index = orig_index + 3;
1553 return (char) chars[0];
1556 // Otherwise, we're dealing with a valid multi-byte character.
1557 // We need to ignore the leading ones from the first byte:
1558 byte mask = (byte) 0xFF;
1559 mask >>= (num_bytes + 1);
1560 int result = chars[0] & mask;
1562 // The result will now be built up from the following bytes.
1563 for (int i = 1; i < num_bytes; i++) {
1564 // Ignore upper two bits
1566 result |= (chars[i] & 0x3F);
1569 if (result <= 0xFFFF) {
1570 return (char) result;
1572 // We need to handle this as a UTF16 surrogate (i.e. return
1575 surrogate = (char) ((result & 0x3FF) | 0xDC00);
1576 return (char) ((result >> 10) | 0xD800);
1580 private struct UriScheme
1582 public string scheme;
1583 public string delimiter;
1584 public int defaultPort;
1586 public UriScheme (string s, string d, int p)
1594 static UriScheme [] schemes = new UriScheme [] {
1595 new UriScheme (UriSchemeHttp, SchemeDelimiter, 80),
1596 new UriScheme (UriSchemeHttps, SchemeDelimiter, 443),
1597 new UriScheme (UriSchemeFtp, SchemeDelimiter, 21),
1598 new UriScheme (UriSchemeFile, SchemeDelimiter, -1),
1599 new UriScheme (UriSchemeMailto, ":", 25),
1600 new UriScheme (UriSchemeNews, ":", 119),
1601 new UriScheme (UriSchemeUuid, ":", -1),
1602 new UriScheme (UriSchemeNntp, SchemeDelimiter, 119),
1603 new UriScheme (UriSchemeGopher, SchemeDelimiter, 70),
1606 internal static string GetSchemeDelimiter (string scheme)
1608 for (int i = 0; i < schemes.Length; i++)
1609 if (schemes [i].scheme == scheme)
1610 return schemes [i].delimiter;
1611 return Uri.SchemeDelimiter;
1614 internal static int GetDefaultPort (string scheme)
1616 UriParser parser = UriParser.GetParser (scheme);
1619 return parser.DefaultPort;
1622 private string GetOpaqueWiseSchemeDelimiter ()
1624 return GetSchemeDelimiter (scheme);
1628 protected virtual bool IsBadFileSystemCharacter (char character)
1630 // It does not always overlap with InvalidPathChars.
1631 int chInt = (int) character;
1632 if (chInt < 32 || (chInt < 64 && chInt > 57))
1651 protected static bool IsExcludedCharacter (char character)
1653 if (character <= 32 || character >= 127)
1656 if (character == '"' || character == '#' || character == '%' || character == '<' ||
1657 character == '>' || character == '[' || character == '\\' || character == ']' ||
1658 character == '^' || character == '`' || character == '{' || character == '|' ||
1664 internal static bool MaybeUri (string s)
1666 int p = s.IndexOf (':');
1673 return IsPredefinedScheme (s.Substring (0, p));
1677 // Using a simple block of if's is twice as slow as the compiler generated
1678 // switch statement. But using this tuned code is faster than the
1679 // compiler generated code, with a million loops on x86-64:
1681 // With "http": .10 vs .51 (first check)
1682 // with "https": .16 vs .51 (second check)
1683 // with "foo": .22 vs .31 (never found)
1684 // with "mailto": .12 vs .51 (last check)
1687 private static bool IsPredefinedScheme (string scheme)
1689 if (scheme == null || scheme.Length < 3)
1692 char c = scheme [0];
1694 return (scheme == "http" || scheme == "https");
1696 return (scheme == "file" || scheme == "ftp");
1701 return (scheme == "news" || scheme == "net.pipe" || scheme == "net.tcp");
1702 if (scheme == "nntp")
1706 if ((c == 'g' && scheme == "gopher") || (c == 'm' && scheme == "mailto"))
1713 protected virtual bool IsReservedCharacter (char character)
1715 if (character == '$' || character == '&' || character == '+' || character == ',' ||
1716 character == '/' || character == ':' || character == ';' || character == '=' ||
1723 private UriParser parser;
1725 private UriParser Parser {
1727 if (parser == null) {
1728 parser = UriParser.GetParser (scheme);
1729 // no specific parser ? then use a default one
1731 parser = new DefaultUriParser ("*");
1735 set { parser = value; }
1738 public string GetComponents (UriComponents components, UriFormat format)
1740 if ((components & UriComponents.SerializationInfoString) == 0)
1741 EnsureAbsoluteUri ();
1743 return Parser.GetComponents (this, components, format);
1746 public bool IsBaseOf (Uri uri)
1750 throw new ArgumentNullException ("uri");
1752 return Parser.IsBaseOf (this, uri);
1755 public bool IsWellFormedOriginalString ()
1757 // funny, but it does not use the Parser's IsWellFormedOriginalString().
1758 // Also, it seems we need to *not* escape hex.
1759 return EscapeString (OriginalString, EscapeCommonBrackets) == OriginalString;
1764 private const int MaxUriLength = 32766;
1766 public static int Compare (Uri uri1, Uri uri2, UriComponents partsToCompare, UriFormat compareFormat, StringComparison comparisonType)
1768 if ((comparisonType < StringComparison.CurrentCulture) || (comparisonType > StringComparison.OrdinalIgnoreCase)) {
1769 string msg = Locale.GetText ("Invalid StringComparison value '{0}'", comparisonType);
1770 throw new ArgumentException ("comparisonType", msg);
1773 if ((uri1 == null) && (uri2 == null))
1780 string s1 = uri1.GetComponents (partsToCompare, compareFormat);
1781 string s2 = uri2.GetComponents (partsToCompare, compareFormat);
1782 return String.Compare (s1, s2, comparisonType);
1786 // The rules for EscapeDataString
1788 static bool NeedToEscapeDataChar (char b)
1790 if ((b >= 'A' && b <= 'Z') ||
1791 (b >= 'a' && b <= 'z') ||
1792 (b >= '0' && b <= '9'))
1819 public static string EscapeDataString (string stringToEscape)
1821 if (stringToEscape == null)
1822 throw new ArgumentNullException ("stringToEscape");
1824 if (stringToEscape.Length > MaxUriLength) {
1825 throw new UriFormatException (string.Format ("Uri is longer than the maximum {0} characters.", MaxUriLength));
1828 bool escape = false;
1829 foreach (char c in stringToEscape){
1830 if (NeedToEscapeDataChar (c)){
1836 return stringToEscape;
1839 StringBuilder sb = new StringBuilder ();
1840 byte [] bytes = Encoding.UTF8.GetBytes (stringToEscape);
1841 foreach (byte b in bytes){
1842 if (NeedToEscapeDataChar ((char) b))
1843 sb.Append (HexEscape ((char) b));
1845 sb.Append ((char) b);
1847 return sb.ToString ();
1851 // The rules for EscapeUriString
1853 static bool NeedToEscapeUriChar (char b)
1855 if ((b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') || (b >= '&' && b <= ';'))
1878 public static string EscapeUriString (string stringToEscape)
1880 if (stringToEscape == null)
1881 throw new ArgumentNullException ("stringToEscape");
1883 if (stringToEscape.Length > MaxUriLength) {
1884 throw new UriFormatException (string.Format ("Uri is longer than the maximum {0} characters.", MaxUriLength));
1887 bool escape = false;
1888 foreach (char c in stringToEscape){
1889 if (NeedToEscapeUriChar (c)){
1895 return stringToEscape;
1897 StringBuilder sb = new StringBuilder ();
1898 byte [] bytes = Encoding.UTF8.GetBytes (stringToEscape);
1899 foreach (byte b in bytes){
1900 if (NeedToEscapeUriChar ((char) b))
1901 sb.Append (HexEscape ((char) b));
1903 sb.Append ((char) b);
1905 return sb.ToString ();
1908 public static bool IsWellFormedUriString (string uriString, UriKind uriKind)
1910 if (uriString == null)
1914 if (Uri.TryCreate (uriString, uriKind, out uri))
1915 return uri.IsWellFormedOriginalString ();
1919 public static bool TryCreate (string uriString, UriKind uriKind, out Uri result)
1923 Uri r = new Uri (uriString, uriKind, out success);
1932 // [MonoTODO ("rework code to avoid exception catching")]
1933 public static bool TryCreate (Uri baseUri, string relativeUri, out Uri result)
1936 if (relativeUri == null)
1940 Uri relative = new Uri (relativeUri, UriKind.RelativeOrAbsolute);
1941 if ((baseUri != null) && baseUri.IsAbsoluteUri) {
1942 // FIXME: this should call UriParser.Resolve
1943 result = new Uri (baseUri, relative);
1944 } else if (relative.IsAbsoluteUri) {
1945 // special case - see unit tests
1948 return (result != null);
1949 } catch (UriFormatException) {
1954 //[MonoTODO ("rework code to avoid exception catching")]
1955 public static bool TryCreate (Uri baseUri, Uri relativeUri, out Uri result)
1958 if ((baseUri == null) || !baseUri.IsAbsoluteUri)
1961 if (relativeUri == null)
1965 // FIXME: this should call UriParser.Resolve
1966 result = new Uri (baseUri, relativeUri.OriginalString);
1968 } catch (UriFormatException) {
1973 public static string UnescapeDataString (string stringToUnescape)
1975 return UnescapeDataString (stringToUnescape, false);
1978 internal static string UnescapeDataString (string stringToUnescape, bool safe)
1980 if (stringToUnescape == null)
1981 throw new ArgumentNullException ("stringToUnescape");
1983 if (stringToUnescape.IndexOf ('%') == -1 && stringToUnescape.IndexOf ('+') == -1)
1984 return stringToUnescape;
1986 StringBuilder output = new StringBuilder ();
1987 long len = stringToUnescape.Length;
1988 MemoryStream bytes = new MemoryStream ();
1991 for (int i = 0; i < len; i++) {
1992 if (stringToUnescape [i] == '%' && i + 2 < len && stringToUnescape [i + 1] != '%') {
1993 if (stringToUnescape [i + 1] == 'u' && i + 5 < len) {
1994 if (bytes.Length > 0) {
1995 output.Append (GetChars (bytes, Encoding.UTF8));
1996 bytes.SetLength (0);
1999 xchar = GetChar (stringToUnescape, i + 2, 4, safe);
2001 output.Append ((char) xchar);
2005 output.Append ('%');
2008 else if ((xchar = GetChar (stringToUnescape, i + 1, 2, safe)) != -1) {
2009 bytes.WriteByte ((byte) xchar);
2013 output.Append ('%');
2018 if (bytes.Length > 0) {
2019 output.Append (GetChars (bytes, Encoding.UTF8));
2020 bytes.SetLength (0);
2023 output.Append (stringToUnescape [i]);
2026 if (bytes.Length > 0) {
2027 output.Append (GetChars (bytes, Encoding.UTF8));
2031 return output.ToString ();
2034 private static int GetInt (byte b)
2037 if (c >= '0' && c <= '9')
2040 if (c >= 'a' && c <= 'f')
2041 return c - 'a' + 10;
2043 if (c >= 'A' && c <= 'F')
2044 return c - 'A' + 10;
2049 private static int GetChar (string str, int offset, int length, bool safe)
2052 int end = length + offset;
2053 for (int i = offset; i < end; i++) {
2058 int current = GetInt ((byte) c);
2061 val = (val << 4) + current;
2067 switch ((char) val) {
2074 case '&': // not documented
2081 private static char [] GetChars (MemoryStream b, Encoding e)
2083 return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
2087 private void EnsureAbsoluteUri ()
2090 throw new InvalidOperationException ("This operation is not supported for a relative URI.");