jellyfin/SocketHttpListener/Net/ListenerPrefix.cs

103 lines
2.7 KiB
C#
Raw Normal View History

2016-11-11 19:55:12 +00:00
using System;
using System.Net;
using MediaBrowser.Model.Net;
namespace SocketHttpListener.Net
{
2018-09-12 17:26:21 +00:00
internal sealed class ListenerPrefix
2016-11-11 19:55:12 +00:00
{
2018-09-12 17:26:21 +00:00
private string _original;
private string _host;
private ushort _port;
private string _path;
private bool _secure;
private IPAddress[] _addresses;
internal HttpListener _listener;
2016-11-11 19:55:12 +00:00
public ListenerPrefix(string prefix)
{
2018-09-12 17:26:21 +00:00
_original = prefix;
2016-11-11 19:55:12 +00:00
Parse(prefix);
}
public override string ToString()
{
2018-09-12 17:26:21 +00:00
return _original;
2016-11-11 19:55:12 +00:00
}
2017-09-03 02:42:13 +00:00
public IPAddress[] Addresses
2016-11-11 19:55:12 +00:00
{
2018-09-12 17:26:21 +00:00
get { return _addresses; }
set { _addresses = value; }
2016-11-11 19:55:12 +00:00
}
public bool Secure
{
2018-09-12 17:26:21 +00:00
get { return _secure; }
2016-11-11 19:55:12 +00:00
}
public string Host
{
2018-09-12 17:26:21 +00:00
get { return _host; }
2016-11-11 19:55:12 +00:00
}
public int Port
{
2018-09-12 17:26:21 +00:00
get { return _port; }
2016-11-11 19:55:12 +00:00
}
public string Path
{
2018-09-12 17:26:21 +00:00
get { return _path; }
2016-11-11 19:55:12 +00:00
}
// Equals and GetHashCode are required to detect duplicates in HttpListenerPrefixCollection.
public override bool Equals(object o)
{
ListenerPrefix other = o as ListenerPrefix;
if (other == null)
return false;
2018-09-12 17:26:21 +00:00
return (_original == other._original);
2016-11-11 19:55:12 +00:00
}
public override int GetHashCode()
{
2018-09-12 17:26:21 +00:00
return _original.GetHashCode();
2016-11-11 19:55:12 +00:00
}
2018-09-12 17:26:21 +00:00
private void Parse(string uri)
2016-11-11 19:55:12 +00:00
{
ushort default_port = 80;
if (uri.StartsWith("https://"))
{
default_port = 443;
2018-09-12 17:26:21 +00:00
_secure = true;
2016-11-11 19:55:12 +00:00
}
int length = uri.Length;
int start_host = uri.IndexOf(':') + 3;
if (start_host >= length)
2018-09-12 17:26:21 +00:00
throw new ArgumentException("net_listener_host");
2016-11-11 19:55:12 +00:00
int colon = uri.IndexOf(':', start_host, length - start_host);
int root;
if (colon > 0)
{
2018-09-12 17:26:21 +00:00
_host = uri.Substring(start_host, colon - start_host);
2016-11-11 19:55:12 +00:00
root = uri.IndexOf('/', colon, length - colon);
2018-09-12 17:26:21 +00:00
_port = (ushort)int.Parse(uri.Substring(colon + 1, root - colon - 1));
_path = uri.Substring(root);
2016-11-11 19:55:12 +00:00
}
else
{
root = uri.IndexOf('/', start_host, length - start_host);
2018-09-12 17:26:21 +00:00
_host = uri.Substring(start_host, root - start_host);
_port = default_port;
_path = uri.Substring(root);
2016-11-11 19:55:12 +00:00
}
2018-09-12 17:26:21 +00:00
if (_path.Length != 1)
_path = _path.Substring(0, _path.Length - 1);
2016-11-11 19:55:12 +00:00
}
}
}