[Modify] Move port checks

This commit is contained in:
sta 2016-06-02 16:43:14 +09:00
parent e61253baea
commit ca31d86c1a
3 changed files with 29 additions and 26 deletions

View File

@ -309,7 +309,7 @@ namespace WebSocketSharp.Net
var host = uri.Host; var host = uri.Host;
var dns = Uri.CheckHostName (host) == UriHostNameType.Dns; var dns = Uri.CheckHostName (host) == UriHostNameType.Dns;
var port = uri.Port; var port = uri.Port.ToString ();
var path = HttpUtility.UrlDecode (uri.AbsolutePath); var path = HttpUtility.UrlDecode (uri.AbsolutePath);
var pathSlash = path[path.Length - 1] == '/' ? path : path + "/"; var pathSlash = path[path.Length - 1] == '/' ? path : path + "/";

View File

@ -87,7 +87,12 @@ namespace WebSocketSharp.Net
if (!addr.IsLocal ()) if (!addr.IsLocal ())
throw new HttpListenerException (87, "Includes an invalid host."); throw new HttpListenerException (87, "Includes an invalid host.");
var port = pref.Port; int port;
if (!Int32.TryParse (pref.Port, out port))
throw new HttpListenerException (87, "Includes an invalid port.");
if (!port.IsPortNumber ())
throw new HttpListenerException (87, "Includes an invalid port.");
var path = pref.Path; var path = pref.Path;
if (path.IndexOf ('%') != -1) if (path.IndexOf ('%') != -1)
@ -146,7 +151,12 @@ namespace WebSocketSharp.Net
if (!addr.IsLocal ()) if (!addr.IsLocal ())
return; return;
var port = pref.Port; int port;
if (!Int32.TryParse (pref.Port, out port))
return;
if (!port.IsPortNumber ())
return;
var path = pref.Path; var path = pref.Path;
if (path.IndexOf ('%') != -1) if (path.IndexOf ('%') != -1)

View File

@ -52,7 +52,7 @@ namespace WebSocketSharp.Net
private HttpListener _listener; private HttpListener _listener;
private string _original; private string _original;
private string _path; private string _path;
private ushort _port; private string _port;
private bool _secure; private bool _secure;
#endregion #endregion
@ -108,9 +108,9 @@ namespace WebSocketSharp.Net
} }
} }
public int Port { public string Port {
get { get {
return (int) _port; return _port;
} }
} }
@ -120,29 +120,26 @@ namespace WebSocketSharp.Net
private void parse (string uriPrefix) private void parse (string uriPrefix)
{ {
var defaultPort = uriPrefix.StartsWith ("https://") ? 443 : 80; if (uriPrefix.StartsWith ("https"))
if (defaultPort == 443)
_secure = true; _secure = true;
var len = uriPrefix.Length; var len = uriPrefix.Length;
var startHost = uriPrefix.IndexOf (':') + 3; var startHost = uriPrefix.IndexOf (':') + 3;
var root = uriPrefix.IndexOf ('/', startHost, len - startHost); var root = uriPrefix.IndexOf ('/', startHost + 1, len - startHost - 1);
var colon = uriPrefix.LastIndexOf (':', root - 1, root - startHost - 1); var colon = uriPrefix.LastIndexOf (':', root - 1, root - startHost - 1);
if (uriPrefix[root - 1] != ']' && colon > startHost) { if (uriPrefix[root - 1] != ']' && colon > startHost) {
_host = uriPrefix.Substring (startHost, colon - startHost); _host = uriPrefix.Substring (startHost, colon - startHost);
_port = (ushort) Int32.Parse (uriPrefix.Substring (colon + 1, root - colon - 1)); _port = uriPrefix.Substring (colon + 1, root - colon - 1);
} }
else { else {
_host = uriPrefix.Substring (startHost, root - startHost); _host = uriPrefix.Substring (startHost, root - startHost);
_port = (ushort) defaultPort; _port = _secure ? "443" : "80";
} }
_path = uriPrefix.Substring (root); var path = uriPrefix.Substring (root);
var pathLen = path.Length;
var pathLen = _path.Length; _path = pathLen > 1 ? path.Substring (0, pathLen - 1) : path;
if (pathLen > 1)
_path = _path.Substring (0, pathLen - 1);
} }
#endregion #endregion
@ -172,18 +169,14 @@ namespace WebSocketSharp.Net
if (root == startHost) if (root == startHost)
throw new ArgumentException ("No host is specified.", "uriPrefix"); throw new ArgumentException ("No host is specified.", "uriPrefix");
if (uriPrefix[len - 1] != '/') if (root == -1 || uriPrefix[len - 1] != '/')
throw new ArgumentException ("Ends without '/'.", "uriPrefix"); throw new ArgumentException ("Ends without '/'.", "uriPrefix");
var colon = uriPrefix.LastIndexOf (':', root - 1, root - startHost - 1); if (uriPrefix[root - 1] == ':')
if (uriPrefix[root - 1] != ']' && colon > startHost) { throw new ArgumentException ("No port is specified.", "uriPrefix");
int port;
if (!Int32.TryParse (uriPrefix.Substring (colon + 1, root - colon - 1), out port) if (root == len - 2)
|| !port.IsPortNumber () throw new ArgumentException ("No path is specified.", "uriPrefix");
) {
throw new ArgumentException ("An invalid port is specified.", "uriPrefix");
}
}
} }
// The Equals and GetHashCode methods are required to detect duplicates in any collection. // The Equals and GetHashCode methods are required to detect duplicates in any collection.