Description | /// <summary>
/// Initializes an instance by parsing a URL string.
/// </summary>
public URL(string url)
{
Scheme = UrlScheme.HTTP;
HostName = "localhost";
Port = 0;
Path = null;
string buffer = url;
// extract the scheme (default is http).
int index = buffer.IndexOf("://");
if (index >= 0)
{
Scheme = buffer.Substring(0, index);
buffer = buffer.Substring(index + 3);
}
index = buffer.IndexOfAny(new char[] {'/'});
if (index < 0)
{
Path = buffer;
return;
}
string hostPortString = buffer.Substring(0, index);
IPAddress address;
try
{
address = IPAddress.Parse(hostPortString);
}
catch
{
address = null;
}
if (address != null && address.AddressFamily == AddressFamily.InterNetworkV6)
{
if (hostPortString.Contains("]"))
{
HostName = hostPortString.Substring(0, hostPortString.IndexOf("]") + 1);
if (hostPortString.Substring(hostPortString.IndexOf(']')).Contains(":"))
{
string portString = hostPortString.Substring(hostPortString.LastIndexOf(':') + 1);
if (portString != "")
{
try
{
Port = System.Convert.ToUInt16(portString);
}
catch
{
Port = 0;
}
}
else
{
Port = 0;
}
}
else
{
Port = 0;
}
Path = buffer.Substring(index + 1);
}
else
{
HostName = "[" + hostPortString + "]";
Port = 0;
}
Path = buffer.Substring(index + 1);
}
else
{
// extract the hostname (default is localhost).
index = buffer.IndexOfAny(new char[] {':', '/'});
if (index < 0)
{
Path = buffer;
return;
}
HostName = buffer.Substring(0, index);
// extract the port number (default is 0).
if (buffer[index] == ':')
{
buffer = buffer.Substring(index + 1);
index = buffer.IndexOf("/");
string port = null;
if (index >= 0)
{
port = buffer.Substring(0, index);
buffer = buffer.Substring(index + 1);
}
else
{
port = buffer;
buffer = "";
}
try
{
Port = (int) System.Convert.ToUInt16(port);
}
catch
{
Port = 0;
}
}
else
{
buffer = buffer.Substring(index + 1);
}
// extract the path.
Path = buffer;
}
}
|
---|