namespace System {
[Serializable]
-#if NET_2_0
[TypeConverter (typeof (UriTypeConverter))]
public class Uri : ISerializable {
-#else
- public class Uri : MarshalByRefObject, ISerializable {
-#endif
// NOTES:
// o scheme excludes the scheme delimiter
// o port is -1 to indicate no port is defined
public static readonly string UriSchemeMailto = "mailto";
public static readonly string UriSchemeNews = "news";
public static readonly string UriSchemeNntp = "nntp";
-#if NET_2_0
public static readonly string UriSchemeNetPipe = "net.pipe";
public static readonly string UriSchemeNetTcp = "net.tcp";
-#endif
// Constructors
{
}
-#if NET_2_0
public Uri (string uriString, UriKind uriKind)
{
source = uriString;
throw new UriFormatException("Invalid URI: The format of the URI could not be "
+ "determined: " + uriString);
}
-#else
- public Uri (string uriString, bool dontEscape)
- {
- userEscaped = dontEscape;
- source = uriString;
- Parse ();
- if (!isAbsoluteUri)
- throw new UriFormatException("Invalid URI: The format of the URI could not be "
- + "determined.");
- }
-#endif
public Uri (Uri baseUri, string relativeUri)
{
// FIXME: this should call UriParser.Resolve
}
-#if NET_2_0
[Obsolete ("dontEscape is always false")]
-#endif
public Uri (Uri baseUri, string relativeUri, bool dontEscape)
{
userEscaped = dontEscape;
private void Merge (Uri baseUri, string relativeUri)
{
-#if NET_2_0
if (baseUri == null)
throw new ArgumentNullException ("baseUri");
if (!baseUri.IsAbsoluteUri)
throw new ArgumentOutOfRangeException ("baseUri");
if (relativeUri == null)
relativeUri = String.Empty;
-#else
- if (baseUri == null)
- throw new NullReferenceException ("baseUri");
-#endif
+
// See RFC 2396 Par 5.2 and Appendix C
// Check Windows UNC (for // it is scheme/host separator)
if (relativeUri.Length >= 2 && relativeUri [0] == '\\' && relativeUri [1] == '\\') {
source = relativeUri;
-#if NET_2_0
ParseUri (UriKind.Absolute);
-#else
- Parse ();
-#endif
return;
}
relativeUri.Length > pos + 1 &&
relativeUri [pos + 1] == '/') {
source = relativeUri;
-#if NET_2_0
ParseUri (UriKind.Absolute);
-#else
- Parse ();
-#endif
return;
}
else
if (relativeUri.Length > 0 && relativeUri [0] == '/') {
if (relativeUri.Length > 1 && relativeUri [1] == '/') {
source = scheme + ':' + relativeUri;
-#if NET_2_0
ParseUri (UriKind.Absolute);
-#else
- Parse ();
-#endif
return;
} else {
path = relativeUri;
public string AbsolutePath {
get {
-#if NET_2_0
EnsureAbsoluteUri ();
switch (Scheme) {
case "mailto":
}
return path;
}
-#else
- return path;
-#endif
}
}
UriHostNameType ret = CheckHostName (Host);
if (ret != UriHostNameType.Unknown)
return ret;
-#if NET_2_0
switch (Scheme) {
case "mailto":
return UriHostNameType.Basic;
default:
return (IsFile) ? UriHostNameType.Basic : ret;
}
-#else
- // looks it always returns Basic...
- return UriHostNameType.Basic; //.Unknown;
-#endif
}
}
EnsureAbsoluteUri ();
if (Host.Length == 0) {
-#if NET_2_0
return IsFile;
-#else
- return false;
-#endif
}
if (host == "loopback" || host == "localhost")
else if (System.IO.Path.DirectorySeparatorChar == '\\') {
string h = host;
if (path.Length > 0) {
-#if NET_2_0
if ((path.Length > 1) || (path[0] != '/')) {
h += path.Replace ('/', '\\');
}
-#else
- h += path.Replace ('/', '\\');
-#endif
}
cachedLocalPath = "\\\\" + Unescape (h);
} else
}
}
-#if NET_2_0
[MonoTODO ("add support for IPv6 address")]
public string DnsSafeHost {
get {
public bool IsAbsoluteUri {
get { return isAbsoluteUri; }
}
-#endif
+
// LAMESPEC: source field is supplied in such case that this
// property makes sense. For such case that source field is
// not supplied (i.e. .ctor(Uri, string), this property
// makes no sense. To avoid silly regression it just returns
// ToString() value now. See bug #78374.
-#if NET_2_0
- public
-#else
- internal
-#endif
- string OriginalString {
+ public string OriginalString {
get { return source != null ? source : ToString (); }
}
if (length == 0)
return false;
uint number;
-#if NET_2_0
if (!UInt32.TryParse (captures [i], out number))
return false;
-#else
- try {
- number = UInt32.Parse (captures [i]);
- } catch (Exception) {
- return false;
- }
-#endif
if (number > 255)
return false;
}
}
#if !NET_2_1
-#if NET_2_0
[Obsolete("This method does nothing, it has been obsoleted")]
-#endif
protected virtual void Canonicalize ()
{
//
}
[MonoTODO ("Find out what this should do")]
-#if NET_2_0
[Obsolete]
-#endif
protected virtual void CheckSecurity ()
{
}
private static bool IsAlpha (char c)
{
-#if NET_2_0
// as defined in rfc2234
// %x41-5A / %x61-7A (A-Z / a-z)
int i = (int) c;
return (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
-#else
- // Fx 1.x got this too large
- return Char.IsLetter (c);
-#endif
}
public override bool Equals (object comparant)
// Assumes: uri != null
bool InternalEquals (Uri uri)
{
-#if NET_2_0
if (this.isAbsoluteUri != uri.isAbsoluteUri)
return false;
if (!this.isAbsoluteUri)
return this.source == uri.source;
-#endif
CultureInfo inv = CultureInfo.InvariantCulture;
return this.scheme.ToLower (inv) == uri.scheme.ToLower (inv)
&& this.host.ToLower (inv) == uri.host.ToLower (inv)
&& this.port == uri.port
-#if NET_2_0
&& this.query == uri.query
-#else
- // Note: MS.NET 1.x has bug - ignores query check altogether
- && this.query.ToLower (inv) == uri.query.ToLower (inv)
-#endif
&& this.path == uri.path;
}
-#if NET_2_0
public static bool operator == (Uri u1, Uri u2)
{
return object.Equals(u1, u2);
{
return !(u1 == u2);
}
-#endif
public override int GetHashCode ()
{
cachedHashCode = scheme.ToLower (inv).GetHashCode ()
^ host.ToLower (inv).GetHashCode ()
^ port
-#if NET_2_0
^ query.GetHashCode ()
-#else
- ^ query.ToLower (inv).GetHashCode ()
-#endif
^ path.GetHashCode ();
}
else {
sb.Append (':').Append (port);
if (path.Length > 0) {
-#if NET_2_0
switch (Scheme) {
case "mailto":
case "news":
sb.Append (Reduce (path));
break;
}
-#else
- sb.Append (path);
-#endif
}
return sb.ToString ();
}
IsHexDigit (pattern [index]));
}
-#if NET_2_0
//
// Implemented by copying most of the MakeRelative code
//
}
[Obsolete ("Use MakeRelativeUri(Uri uri) instead.")]
-#endif
public string MakeRelative (Uri toUri)
{
if ((this.Scheme != toUri.Scheme) ||
return cachedToString;
}
-#if NET_2_0
protected void GetObjectData (SerializationInfo info, StreamingContext context)
{
info.AddValue ("AbsoluteUri", this.AbsoluteUri);
}
-#endif
void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
{
// Internal Methods
-#if NET_2_0
[Obsolete]
-#endif
protected virtual void Escape ()
{
path = EscapeString (path);
#if MOONLIGHT
static string EscapeString (string str)
#else
- #if NET_2_0
[Obsolete]
- #endif
protected static string EscapeString (string str)
#endif
{
// On .NET 1.x, this method is called from .ctor(). When overriden, we
// can avoid the "absolute uri" constraints of the .ctor() by
// overriding with custom code.
-#if NET_2_0
[Obsolete("The method has been deprecated. It is not used by the system.")]
-#endif
protected virtual void Parse ()
{
-#if !NET_2_0
- ParseUri (UriKind.Absolute);
-#endif
}
private void ParseUri (UriKind kind)
#if MOONLIGHT
string Unescape (string str)
#else
- #if NET_2_0
[Obsolete]
- #endif
protected virtual string Unescape (string str)
#endif
{
if (pos != -1 && pos != endpos - 1) {
string portStr = uriString.Substring(pos + 1, endpos - (pos + 1));
if (portStr.Length > 0 && portStr[portStr.Length - 1] != ']') {
-#if NET_2_0
if (!Int32.TryParse (portStr, NumberStyles.Integer, CultureInfo.InvariantCulture, out port) ||
port < 0 || port > UInt16.MaxValue)
return "Invalid URI: Invalid port number";
endpos = pos;
-#else
- try {
- port = (int) UInt32.Parse (portStr, CultureInfo.InvariantCulture);
- endpos = pos;
- } catch (Exception) {
- return "Invalid URI: Invalid port number";
- }
-#endif
} else {
if (port == -1) {
port = GetDefaultPort (scheme);
else
badhost = true;
}
-#if NET_2_0
if (badhost && (Parser is DefaultUriParser || Parser == null))
return Locale.GetText ("Invalid URI: The hostname could not be parsed. (" + host + ")");
Parser.InitializeAndValidate (this, out ex);
if (ex != null)
return ex.Message;
-#else
- if (badhost)
- return Locale.GetText ("Invalid URI: The hostname could not be parsed. (" + host + ")");
-#endif
if ((scheme != Uri.UriSchemeMailto) &&
(scheme != Uri.UriSchemeNews) &&
if (current == "..") {
int resultCount = result.Count;
-#if NET_2_0
// in 2.0 profile, skip leading ".." parts
if (resultCount == 0) {
continue;
result.RemoveAt (resultCount - 1);
continue;
-#else
- // in 1.x profile, retain leading ".." parts, and only reduce
- // URI is previous part is not ".."
- if (resultCount > 0) {
- if ((string) result[resultCount - 1] != "..") {
- result.RemoveAt (resultCount - 1);
- continue;
- }
- }
-#endif
}
result.Add (current);
internal static int GetDefaultPort (string scheme)
{
-#if NET_2_0
UriParser parser = UriParser.GetParser (scheme);
if (parser == null)
return -1;
return parser.DefaultPort;
-#else
- for (int i = 0; i < schemes.Length; i++)
- if (schemes [i].scheme == scheme)
- return schemes [i].defaultPort;
- return -1;
-#endif
}
private string GetOpaqueWiseSchemeDelimiter ()
return GetSchemeDelimiter (scheme);
}
-#if NET_2_0
[Obsolete]
-#endif
protected virtual bool IsBadFileSystemCharacter (char ch)
{
// It does not always overlap with InvalidPathChars.
return false;
}
-#if NET_2_0
[Obsolete]
-#endif
protected static bool IsExcludedCharacter (char ch)
{
if (ch <= 32 || ch >= 127)
case "gopher":
case "mailto":
case "news":
-#if NET_2_0
case "net.pipe":
case "net.tcp":
-#endif
return true;
default:
return false;
}
}
-#if NET_2_0
[Obsolete]
-#endif
protected virtual bool IsReservedCharacter (char ch)
{
if (ch == '$' || ch == '&' || ch == '+' || ch == ',' ||
return true;
return false;
}
-#if NET_2_0
+
[NonSerialized]
private UriParser parser;
if (!IsAbsoluteUri)
throw new InvalidOperationException ("This operation is not supported for a relative URI.");
}
-#else
- private void EnsureAbsoluteUri ()
- {
- }
-#endif
}
}