diff --git a/websocket-sharp/Net/EndPointManager.cs b/websocket-sharp/Net/EndPointManager.cs index fa92a7af..e24c7aa5 100644 --- a/websocket-sharp/Net/EndPointManager.cs +++ b/websocket-sharp/Net/EndPointManager.cs @@ -81,7 +81,7 @@ namespace WebSocketSharp.Net throw new HttpListenerException (400, "Invalid path."); // TODO: Code? // 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); } @@ -124,7 +124,7 @@ namespace WebSocketSharp.Net if (pref.Path.IndexOf ("//", StringComparison.Ordinal) != -1) 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); } diff --git a/websocket-sharp/Net/ListenerPrefix.cs b/websocket-sharp/Net/ListenerPrefix.cs index 1ead612e..044eebb0 100644 --- a/websocket-sharp/Net/ListenerPrefix.cs +++ b/websocket-sharp/Net/ListenerPrefix.cs @@ -47,8 +47,9 @@ namespace WebSocketSharp.Net { #region Private Fields - IPAddress [] _addresses; + IPAddress[] _addresses; string _host; + HttpListener _listener; string _original; string _path; ushort _port; @@ -56,12 +57,6 @@ namespace WebSocketSharp.Net #endregion - #region Public Fields - - public HttpListener Listener; - - #endregion - #region Public Constructors // Must be called after calling ListenerPrefix.CheckUriPrefix. @@ -75,7 +70,7 @@ namespace WebSocketSharp.Net #region Public Properties - public IPAddress [] Addresses { + public IPAddress[] Addresses { get { 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 { get { return _path; @@ -103,12 +114,6 @@ namespace WebSocketSharp.Net } } - public bool Secure { - get { - return _secure; - } - } - #endregion #region Private Methods @@ -119,25 +124,25 @@ namespace WebSocketSharp.Net if (defaultPort == 443) _secure = true; - var length = uriPrefix.Length; + var len = uriPrefix.Length; var startHost = uriPrefix.IndexOf (':') + 3; - var colon = uriPrefix.IndexOf (':', startHost, length - startHost); - int root; + var colon = uriPrefix.IndexOf (':', startHost, len - startHost); + var root = 0; if (colon > 0) { - root = uriPrefix.IndexOf ('/', colon, length - colon); + root = uriPrefix.IndexOf ('/', colon, len - colon); _host = uriPrefix.Substring (startHost, colon - startHost); _port = (ushort) Int32.Parse (uriPrefix.Substring (colon + 1, root - colon - 1)); - _path = uriPrefix.Substring (root); } else { - root = uriPrefix.IndexOf ('/', startHost, length - startHost); + root = uriPrefix.IndexOf ('/', startHost, len - startHost); _host = uriPrefix.Substring (startHost, root - startHost); _port = (ushort) defaultPort; - _path = uriPrefix.Substring (root); } - if (_path.Length > 1) - _path = _path.Substring (0, _path.Length - 1); + _path = uriPrefix.Substring (root); + var pathLen = _path.Length; + if (pathLen > 1) + _path = _path.Substring (0, pathLen - 1); } #endregion @@ -149,46 +154,46 @@ namespace WebSocketSharp.Net if (uriPrefix == null) throw new ArgumentNullException ("uriPrefix"); - if (!uriPrefix.StartsWith ("http://") && !uriPrefix.StartsWith ("https://")) - throw new ArgumentException ("Only 'http' and 'https' schemes are supported."); + var len = uriPrefix.Length; + 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; - if (startHost >= length) - throw new ArgumentException ("No host specified."); + if (startHost >= len) + throw new ArgumentException ("No host is specified."); - var colon = uriPrefix.IndexOf (':', startHost, length - startHost); + var colon = uriPrefix.IndexOf (':', startHost, len - startHost); if (startHost == colon) - throw new ArgumentException ("No host specified."); + throw new ArgumentException ("No host is specified."); - int root; if (colon > 0) { - root = uriPrefix.IndexOf ('/', colon, length - colon); + var root = uriPrefix.IndexOf ('/', colon, len - colon); if (root == -1) - throw new ArgumentException ("No path specified."); + throw new ArgumentException ("No path is specified."); int port; if (!Int32.TryParse (uriPrefix.Substring (colon + 1, root - colon - 1), out port) || - (port <= 0 || port >= 65536)) - throw new ArgumentException ("Invalid port."); + !port.IsPortNumber ()) + throw new ArgumentException ("An invalid port is specified."); } else { - root = uriPrefix.IndexOf ('/', startHost, length - startHost); + var root = uriPrefix.IndexOf ('/', startHost, len - startHost); if (root == -1) - throw new ArgumentException ("No path specified."); + throw new ArgumentException ("No path is specified."); } - if (uriPrefix [uriPrefix.Length - 1] != '/') - throw new ArgumentException ("The URI prefix must end with '/'."); + if (uriPrefix[len - 1] != '/') + throw new ArgumentException ("Ends without '/'."); } // 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; - return other != null - ? _original == other._original - : false; + var pref = obj as ListenerPrefix; + return pref != null && pref._original == _original; } public override int GetHashCode ()