EnsureAbsoluteUri ();
if (cachedLocalPath != null)
return cachedLocalPath;
- if (!IsFile && IsWellFormedOriginalString ())
+ if (!IsFile && (scheme != Uri.UriSchemeNews) && IsWellFormedOriginalString ())
return AbsolutePath;
- bool windows = (path.Length > 3 && path [1] == ':' &&
- (path [2] == '\\' || path [2] == '/'));
-
if (!IsUnc) {
string p = Unescape (path);
- bool replace = windows;
-#if ONLY_1_1
- replace |= (System.IO.Path.DirectorySeparatorChar == '\\');
-#endif
- if (replace)
+ bool windows = (path.Length > 3 && path [1] == ':' &&
+ (path [2] == '\\' || path [2] == '/'));
+
+ if (windows)
cachedLocalPath = p.Replace ('/', '\\');
else
cachedLocalPath = p;
protected static string EscapeString (string str)
#endif
{
- return EscapeString (str, false, true, true);
+ return EscapeString (str, Uri.EscapeCommonHexBrackets);
}
-
- internal static string EscapeString (string str, bool escapeReserved, bool escapeHex, bool escapeBrackets)
+
+ private const string EscapeCommon = "<>%\"{}|\\^`";
+ private const string EscapeReserved = ";/?:@&=+$,";
+ private const string EscapeHex = "#";
+ private const string EscapeBrackets = "[]";
+
+ private const string EscapeNews = EscapeCommon + EscapeBrackets + "?";
+ private const string EscapeCommonHex = EscapeCommon + EscapeHex;
+ private const string EscapeCommonBrackets = EscapeCommon + EscapeBrackets;
+ internal const string EscapeCommonHexBrackets = EscapeCommon + EscapeHex + EscapeBrackets;
+
+ internal static string EscapeString (string str, string escape)
{
- if (str == null)
+ if (String.IsNullOrEmpty (str))
return String.Empty;
StringBuilder s = new StringBuilder ();
int length = data.Length;
for (int j = 0; j < length; j++) {
char c = (char) data [j];
- if ((c <= 0x20) || (c >= 0x7f) ||
- ("<>%\"{}|\\^`".IndexOf (c) != -1) ||
- (escapeHex && (c == '#')) ||
- (escapeBrackets && (c == '[' || c == ']')) ||
- (escapeReserved && (";/?:@&=+$,".IndexOf (c) != -1))) {
+ if ((c <= 0x20) || (c >= 0x7f) || (escape.IndexOf (c) != -1))
s.Append (HexEscape (c));
- continue;
- }
- s.Append (c);
+ else
+ s.Append (c);
}
}
if (userEscaped)
return;
- host = EscapeString (host, false, true, false);
+ host = EscapeString (host, EscapeCommonHex);
if (host.Length > 1 && host [0] != '[' && host [host.Length - 1] != ']') {
// host name present (but not an IPv6 address)
host = host.ToLower (CultureInfo.InvariantCulture);
internal static string Unescape (string str, bool excludeSpecial)
{
- if (str == null)
+ if (String.IsNullOrEmpty (str))
return String.Empty;
+
StringBuilder s = new StringBuilder ();
int len = str.Length;
for (int i = 0; i < len; i++) {
endpos = pos;
}
+ // special case: there is no query part for 'news'
+ if (scheme == Uri.UriSchemeNews) {
+ pos = scheme.Length + 1;
+ path = EscapeString (uriString.Substring (pos, endpos - pos), EscapeNews);
+ return null;
+ }
+
// 6 query
pos = uriString.IndexOf ('?', startpos, endpos-startpos);
if (pos != -1) {
{
// funny, but it does not use the Parser's IsWellFormedOriginalString().
// Also, it seems we need to *not* escape hex.
- return EscapeString (OriginalString, false, false, true) == OriginalString;
+ return EscapeString (OriginalString, EscapeCommonBrackets) == OriginalString;
}
// static methods
if (value == null || value.Length == 0) {\r
path = "/";\r
} else {\r
- path = Uri.EscapeString (value.Replace ('\\', '/'), false, true, true);\r
+ path = Uri.EscapeString (value.Replace ('\\', '/'), Uri.EscapeCommonHexBrackets);\r
}\r
modified = true;\r
}\r
switch (format) {
case UriFormat.UriEscaped:
- return Uri.EscapeString (s, false, true, true);
+ return Uri.EscapeString (s, Uri.EscapeCommonHexBrackets);
case UriFormat.SafeUnescaped:
// TODO subset of escape rules
s = Uri.Unescape (s, false);
InternalRegister (newtable, new DefaultUriParser (), Uri.UriSchemeHttp, 80);
InternalRegister (newtable, new DefaultUriParser (), Uri.UriSchemeHttps, 443);
InternalRegister (newtable, new DefaultUriParser (), Uri.UriSchemeMailto, 25);
-#if NET_2_0
InternalRegister (newtable, new DefaultUriParser (), Uri.UriSchemeNetPipe, -1);
InternalRegister (newtable, new DefaultUriParser (), Uri.UriSchemeNetTcp, -1);
-#endif
- InternalRegister (newtable, new DefaultUriParser (), Uri.UriSchemeNews, 119);
+ InternalRegister (newtable, new DefaultUriParser (), Uri.UriSchemeNews, -1);
InternalRegister (newtable, new DefaultUriParser (), Uri.UriSchemeNntp, 119);
// not defined in Uri.UriScheme* but a parser class exists
InternalRegister (newtable, new DefaultUriParser (), "ldap", 389);
Assert.AreEqual ("subdir/", segments [2].Replace ("%5C", "/"), "s[2]");
Assert.AreEqual ("file", segments [3], "s[3]");
}
+
+ [Test]
+ public void NewsScheme ()
+ {
+ Uri uri = new Uri ("news:novell.mono.moonlight/uri?query");
+
+ Assert.AreEqual ("novell.mono.moonlight/uri%3Fquery", uri.AbsolutePath, "AbsolutePath");
+ Assert.AreEqual ("news:novell.mono.moonlight/uri%3Fquery", uri.AbsoluteUri, "AbsoluteUri");
+ Assert.AreEqual (String.Empty, uri.Authority, "Authority");
+ Assert.AreEqual (String.Empty, uri.DnsSafeHost, "DnsSafeHost");
+ Assert.AreEqual (String.Empty, uri.Fragment, "Fragment");
+ Assert.AreEqual (String.Empty, uri.Host, "Host");
+ Assert.AreEqual (UriHostNameType.Unknown, uri.HostNameType, "HostNameType");
+ Assert.IsTrue (uri.IsAbsoluteUri, "IsAbsoluteUri");
+ Assert.IsTrue (uri.IsDefaultPort, "IsDefaultPort");
+ Assert.IsFalse (uri.IsFile, "IsFile");
+ Assert.IsFalse (uri.IsLoopback, "IsLoopback");
+ Assert.IsFalse (uri.IsUnc, "IsUnc");
+ Assert.AreEqual ("novell.mono.moonlight/uri?query", uri.LocalPath, "LocalPath");
+ Assert.AreEqual ("news:novell.mono.moonlight/uri?query", uri.OriginalString, "OriginalString");
+ Assert.AreEqual ("novell.mono.moonlight/uri%3Fquery", uri.PathAndQuery, "PathAndQuery");
+ Assert.AreEqual (-1, uri.Port, "Port");
+ Assert.AreEqual (String.Empty, uri.Query, "Query");
+ Assert.AreEqual ("news", uri.Scheme, "Scheme");
+ Assert.AreEqual ("novell.mono.moonlight/", uri.Segments [0], "Segments [0]");
+ Assert.AreEqual ("uri%3Fquery", uri.Segments [1], "Segments [1]");
+ Assert.IsFalse (uri.UserEscaped, "UserEscaped");
+ Assert.AreEqual (String.Empty, uri.UserInfo, "UserInfo");
+
+ // special escaped characters - they differs a bit from other URI
+ uri = new Uri ("news:novell.mono.moonlight/<>%\"{}|\\^`;/?:@&=+$,[]#abc");
+ Assert.AreEqual ("novell.mono.moonlight/%3C%3E%25%22%7B%7D%7C%5C%5E%60;/%3F:@&=+$,%5B%5D", uri.AbsolutePath, "Special");
+ Assert.AreEqual ("#abc", uri.Fragment, "Special/Fragment");
+ }
}\r
}\r