Fix for pull request #133, allow port 443 with non secure scheme, and allow port 80 with secure scheme

This commit is contained in:
sta 2015-06-30 15:43:00 +09:00
parent cb8d1e75f6
commit 2078bfad5a
3 changed files with 27 additions and 58 deletions

View File

@ -40,6 +40,7 @@
/*
* Contributors:
* - Liryna <liryna.stark@gmail.com>
* - Nikola Kovacevic <nikolak@outlook.com>
*/
#endregion
@ -818,14 +819,12 @@ namespace WebSocketSharp
/// A <see cref="string"/> that represents a WebSocket URL to try.
/// </param>
/// <param name="result">
/// When this method returns, a <see cref="Uri"/> that represents
/// a WebSocket URL if <paramref name="uriString"/> is valid;
/// otherwise, <see langword="null"/>.
/// When this method returns, a <see cref="Uri"/> that represents a WebSocket URL,
/// or <see langword="null"/> if <paramref name="uriString"/> is invalid.
/// </param>
/// <param name="message">
/// When this method returns, a <see cref="string"/> that represents
/// an error message if <paramref name="uriString"/> is invalid;
/// otherwise, <see cref="String.Empty"/>.
/// When this method returns, a <see cref="string"/> that represents an error message,
/// or <see cref="String.Empty"/> if <paramref name="uriString"/> is valid.
/// </param>
internal static bool TryCreateWebSocketUri (
this string uriString, out Uri result, out string message)
@ -839,7 +838,7 @@ namespace WebSocketSharp
}
var schm = uri.Scheme;
if (schm != "ws" && schm != "wss") {
if (!(schm == "ws" || schm == "wss")) {
message = "The scheme part isn't 'ws' or 'wss': " + uriString;
return false;
}
@ -850,26 +849,22 @@ namespace WebSocketSharp
}
var port = uri.Port;
if (port > 0) {
if (port > 65535) {
message = "The port part is greater than 65535: " + uriString;
return false;
}
if ((schm == "ws" && port == 443) || (schm == "wss" && port == 80)) {
message = "An invalid pair of scheme and port: " + uriString;
return false;
}
}
else {
uri = new Uri (
result = port > 0
? uri
: new Uri (
String.Format (
"{0}://{1}:{2}{3}", schm, uri.Host, schm == "ws" ? 80 : 443, uri.PathAndQuery));
}
"{0}://{1}:{2}{3}",
schm,
uri.Host,
schm == "ws" ? 80 : 443,
uri.PathAndQuery));
result = uri;
message = String.Empty;
return true;
}

View File

@ -104,12 +104,8 @@ namespace WebSocketSharp.Server
/// <paramref name="port"/> isn't between 1 and 65535 inclusive.
/// </exception>
public HttpServer (int port)
: this (port, port == 443)
{
if (!port.IsPortNumber ())
throw new ArgumentOutOfRangeException (
"port", "Not between 1 and 65535 inclusive: " + port);
init ("*", port, port == 443);
}
/// <summary>
@ -127,9 +123,6 @@ namespace WebSocketSharp.Server
/// A <see cref="bool"/> that indicates providing a secure connection or not.
/// (<c>true</c> indicates providing a secure connection.)
/// </param>
/// <exception cref="ArgumentException">
/// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="port"/> isn't between 1 and 65535 inclusive.
/// </exception>
@ -139,10 +132,6 @@ namespace WebSocketSharp.Server
throw new ArgumentOutOfRangeException (
"port", "Not between 1 and 65535 inclusive: " + port);
if ((port == 80 && secure) || (port == 443 && !secure))
throw new ArgumentException (
String.Format ("An invalid pair of 'port' and 'secure': {0}, {1}", port, secure));
init ("*", port, secure);
}

View File

@ -112,12 +112,8 @@ namespace WebSocketSharp.Server
/// <paramref name="port"/> isn't between 1 and 65535 inclusive.
/// </exception>
public WebSocketServer (int port)
: this (port, port == 443)
{
if (!port.IsPortNumber ())
throw new ArgumentOutOfRangeException (
"port", "Not between 1 and 65535 inclusive: " + port);
init (System.Net.IPAddress.Any, port, port == 443);
}
/// <summary>
@ -188,15 +184,16 @@ namespace WebSocketSharp.Server
/// A <see cref="bool"/> that indicates providing a secure connection or not.
/// (<c>true</c> indicates providing a secure connection.)
/// </param>
/// <exception cref="ArgumentException">
/// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="port"/> isn't between 1 and 65535 inclusive.
/// </exception>
public WebSocketServer (int port, bool secure)
: this (System.Net.IPAddress.Any, port, secure)
{
if (!port.IsPortNumber ())
throw new ArgumentOutOfRangeException (
"port", "Not between 1 and 65535 inclusive: " + port);
init (System.Net.IPAddress.Any, port, secure);
}
/// <summary>
@ -255,15 +252,7 @@ namespace WebSocketSharp.Server
/// <paramref name="address"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// <para>
/// <paramref name="address"/> isn't a local IP address.
/// </para>
/// <para>
/// -or-
/// </para>
/// <para>
/// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
/// </para>
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="port"/> isn't between 1 and 65535 inclusive.
@ -280,10 +269,6 @@ namespace WebSocketSharp.Server
throw new ArgumentOutOfRangeException (
"port", "Not between 1 and 65535 inclusive: " + port);
if ((port == 80 && secure) || (port == 443 && !secure))
throw new ArgumentException (
String.Format ("An invalid pair of 'port' and 'secure': {0}, {1}", port, secure));
init (address, port, secure);
}