Refactored ListenerPrefix.cs
This commit is contained in:
parent
292a904ad7
commit
cf82b45163
@ -81,7 +81,7 @@ namespace WebSocketSharp.Net
|
|||||||
throw new HttpListenerException (400, "Invalid path."); // TODO: Code?
|
throw new HttpListenerException (400, "Invalid path."); // TODO: Code?
|
||||||
|
|
||||||
// Always listens on all the interfaces, no matter the host name/ip used.
|
// Always listens on all the interfaces, no matter the host name/ip used.
|
||||||
var epl = getEndPointListener (IPAddress.Any, pref.Port, pref.Secure, httpListener);
|
var epl = getEndPointListener (IPAddress.Any, pref.Port, pref.IsSecure, httpListener);
|
||||||
epl.AddPrefix (pref, httpListener);
|
epl.AddPrefix (pref, httpListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ namespace WebSocketSharp.Net
|
|||||||
if (pref.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
|
if (pref.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var epl = getEndPointListener (IPAddress.Any, pref.Port, pref.Secure, httpListener);
|
var epl = getEndPointListener (IPAddress.Any, pref.Port, pref.IsSecure, httpListener);
|
||||||
epl.RemovePrefix (pref, httpListener);
|
epl.RemovePrefix (pref, httpListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,8 +47,9 @@ namespace WebSocketSharp.Net
|
|||||||
{
|
{
|
||||||
#region Private Fields
|
#region Private Fields
|
||||||
|
|
||||||
IPAddress [] _addresses;
|
IPAddress[] _addresses;
|
||||||
string _host;
|
string _host;
|
||||||
|
HttpListener _listener;
|
||||||
string _original;
|
string _original;
|
||||||
string _path;
|
string _path;
|
||||||
ushort _port;
|
ushort _port;
|
||||||
@ -56,12 +57,6 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public Fields
|
|
||||||
|
|
||||||
public HttpListener Listener;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Public Constructors
|
#region Public Constructors
|
||||||
|
|
||||||
// Must be called after calling ListenerPrefix.CheckUriPrefix.
|
// Must be called after calling ListenerPrefix.CheckUriPrefix.
|
||||||
@ -75,7 +70,7 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
#region Public Properties
|
#region Public Properties
|
||||||
|
|
||||||
public IPAddress [] Addresses {
|
public IPAddress[] Addresses {
|
||||||
get {
|
get {
|
||||||
return _addresses;
|
return _addresses;
|
||||||
}
|
}
|
||||||
@ -91,6 +86,22 @@ namespace WebSocketSharp.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsSecure {
|
||||||
|
get {
|
||||||
|
return _secure;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpListener Listener {
|
||||||
|
get {
|
||||||
|
return _listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
set {
|
||||||
|
_listener = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string Path {
|
public string Path {
|
||||||
get {
|
get {
|
||||||
return _path;
|
return _path;
|
||||||
@ -103,12 +114,6 @@ namespace WebSocketSharp.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Secure {
|
|
||||||
get {
|
|
||||||
return _secure;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Private Methods
|
#region Private Methods
|
||||||
@ -119,25 +124,25 @@ namespace WebSocketSharp.Net
|
|||||||
if (defaultPort == 443)
|
if (defaultPort == 443)
|
||||||
_secure = true;
|
_secure = true;
|
||||||
|
|
||||||
var length = uriPrefix.Length;
|
var len = uriPrefix.Length;
|
||||||
var startHost = uriPrefix.IndexOf (':') + 3;
|
var startHost = uriPrefix.IndexOf (':') + 3;
|
||||||
var colon = uriPrefix.IndexOf (':', startHost, length - startHost);
|
var colon = uriPrefix.IndexOf (':', startHost, len - startHost);
|
||||||
int root;
|
var root = 0;
|
||||||
if (colon > 0) {
|
if (colon > 0) {
|
||||||
root = uriPrefix.IndexOf ('/', colon, length - colon);
|
root = uriPrefix.IndexOf ('/', colon, len - colon);
|
||||||
_host = uriPrefix.Substring (startHost, colon - startHost);
|
_host = uriPrefix.Substring (startHost, colon - startHost);
|
||||||
_port = (ushort) Int32.Parse (uriPrefix.Substring (colon + 1, root - colon - 1));
|
_port = (ushort) Int32.Parse (uriPrefix.Substring (colon + 1, root - colon - 1));
|
||||||
_path = uriPrefix.Substring (root);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
root = uriPrefix.IndexOf ('/', startHost, length - startHost);
|
root = uriPrefix.IndexOf ('/', startHost, len - startHost);
|
||||||
_host = uriPrefix.Substring (startHost, root - startHost);
|
_host = uriPrefix.Substring (startHost, root - startHost);
|
||||||
_port = (ushort) defaultPort;
|
_port = (ushort) defaultPort;
|
||||||
_path = uriPrefix.Substring (root);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_path.Length > 1)
|
_path = uriPrefix.Substring (root);
|
||||||
_path = _path.Substring (0, _path.Length - 1);
|
var pathLen = _path.Length;
|
||||||
|
if (pathLen > 1)
|
||||||
|
_path = _path.Substring (0, pathLen - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -149,46 +154,46 @@ namespace WebSocketSharp.Net
|
|||||||
if (uriPrefix == null)
|
if (uriPrefix == null)
|
||||||
throw new ArgumentNullException ("uriPrefix");
|
throw new ArgumentNullException ("uriPrefix");
|
||||||
|
|
||||||
if (!uriPrefix.StartsWith ("http://") && !uriPrefix.StartsWith ("https://"))
|
var len = uriPrefix.Length;
|
||||||
throw new ArgumentException ("Only 'http' and 'https' schemes are supported.");
|
if (len == 0)
|
||||||
|
throw new ArgumentException ("An empty string.");
|
||||||
|
|
||||||
|
if (!(uriPrefix.StartsWith ("http://") || uriPrefix.StartsWith ("https://")))
|
||||||
|
throw new ArgumentException ("The scheme isn't 'http' or 'https'.");
|
||||||
|
|
||||||
var length = uriPrefix.Length;
|
|
||||||
var startHost = uriPrefix.IndexOf (':') + 3;
|
var startHost = uriPrefix.IndexOf (':') + 3;
|
||||||
if (startHost >= length)
|
if (startHost >= len)
|
||||||
throw new ArgumentException ("No host specified.");
|
throw new ArgumentException ("No host is specified.");
|
||||||
|
|
||||||
var colon = uriPrefix.IndexOf (':', startHost, length - startHost);
|
var colon = uriPrefix.IndexOf (':', startHost, len - startHost);
|
||||||
if (startHost == colon)
|
if (startHost == colon)
|
||||||
throw new ArgumentException ("No host specified.");
|
throw new ArgumentException ("No host is specified.");
|
||||||
|
|
||||||
int root;
|
|
||||||
if (colon > 0) {
|
if (colon > 0) {
|
||||||
root = uriPrefix.IndexOf ('/', colon, length - colon);
|
var root = uriPrefix.IndexOf ('/', colon, len - colon);
|
||||||
if (root == -1)
|
if (root == -1)
|
||||||
throw new ArgumentException ("No path specified.");
|
throw new ArgumentException ("No path is specified.");
|
||||||
|
|
||||||
int port;
|
int port;
|
||||||
if (!Int32.TryParse (uriPrefix.Substring (colon + 1, root - colon - 1), out port) ||
|
if (!Int32.TryParse (uriPrefix.Substring (colon + 1, root - colon - 1), out port) ||
|
||||||
(port <= 0 || port >= 65536))
|
!port.IsPortNumber ())
|
||||||
throw new ArgumentException ("Invalid port.");
|
throw new ArgumentException ("An invalid port is specified.");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
root = uriPrefix.IndexOf ('/', startHost, length - startHost);
|
var root = uriPrefix.IndexOf ('/', startHost, len - startHost);
|
||||||
if (root == -1)
|
if (root == -1)
|
||||||
throw new ArgumentException ("No path specified.");
|
throw new ArgumentException ("No path is specified.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uriPrefix [uriPrefix.Length - 1] != '/')
|
if (uriPrefix[len - 1] != '/')
|
||||||
throw new ArgumentException ("The URI prefix must end with '/'.");
|
throw new ArgumentException ("Ends without '/'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equals and GetHashCode are required to detect duplicates in HttpListenerPrefixCollection.
|
// Equals and GetHashCode are required to detect duplicates in HttpListenerPrefixCollection.
|
||||||
public override bool Equals (object obj)
|
public override bool Equals (Object obj)
|
||||||
{
|
{
|
||||||
var other = obj as ListenerPrefix;
|
var pref = obj as ListenerPrefix;
|
||||||
return other != null
|
return pref != null && pref._original == _original;
|
||||||
? _original == other._original
|
|
||||||
: false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int GetHashCode ()
|
public override int GetHashCode ()
|
||||||
|
Loading…
Reference in New Issue
Block a user