Преглед изворни кода

Fix WebRequest.RegisterPrefix / Create to match SL in some corner cases

* System.Net/WebRequest_2_1.cs: (Create) Find the prefix that is the
closest match to the specified Uri and use it's associated creator.
(RegisterPrefix) We cannot register 'http[s]:' even if registering
'http[s]' is possible (but does not work as expected) as are the (fully)
valid 'http[s]:/[/]' values.
Sebastien Pouliot пре 15 година
родитељ
комит
fcdbcb49c7
1 измењених фајлова са 18 додато и 18 уклоњено
  1. 18 18
      mcs/class/System.Net/System.Net/WebRequest_2_1.cs

+ 18 - 18
mcs/class/System.Net/System.Net/WebRequest_2_1.cs

@@ -110,27 +110,22 @@ namespace System.Net {
 				throw new InvalidOperationException ("This operation is not supported for a relative URI.");
 
 			IWebRequestCreate creator = null;
-
-			// first we look if a domain is registred
-			string scheme = uri.Scheme + Uri.SchemeDelimiter;
-			string domain = scheme + uri.DnsSafeHost;
-			if (!registred_prefixes.TryGetValue (domain, out creator)) {
-				// next we look if the protocol is registred (the delimiter '://' is important)
-				if (!registred_prefixes.TryGetValue (scheme, out creator)) {
-					scheme = uri.Scheme; // without the delimiter
-					// then we default to SL
-					switch (scheme) {
-					case "http":
-					case "https":
-						creator = default_creator;
-						break;
-					default:
-						registred_prefixes.TryGetValue (scheme, out creator);
-						break;
-					}
+			int n = -1;
+			// look for the most promising match in the registred prefixes
+			foreach (KeyValuePair<string, IWebRequestCreate> kvp in registred_prefixes) {
+				string key = kvp.Key;
+				if ((key.Length > n) && uri.AbsoluteUri.StartsWith (key)) {
+					creator = kvp.Value;
+					n = key.Length;
 				}
 			}
 
+			// 'http:/[/]' or 'https:/[/]' needs to be registred otherwise it gets ignored
+			// note that this is unlike other protocols (e.g. 'ftp') - see unit tests
+			string scheme = uri.Scheme;
+			if ((scheme == "http" && n <= 5) || (scheme == "https" && n <= 6))
+				creator = default_creator;
+
 			if (creator == null)
 				throw new NotSupportedException (string.Format ("Scheme {0} not supported", scheme));
 
@@ -179,6 +174,11 @@ namespace System.Net {
 				prefix = uri.Scheme + Uri.SchemeDelimiter + uri.DnsSafeHost;
 			}
 
+			// registering 'http', 'http://' or even 'http:/' are all ok - but *never* would 'http:' be correct!
+			if ((String.Compare (prefix, "http:", StringComparison.OrdinalIgnoreCase) == 0) ||
+			    (String.Compare (prefix, "https:", StringComparison.OrdinalIgnoreCase) == 0))
+				return false;
+
 			if (registred_prefixes.ContainsKey (prefix))
 				return false;