Fix for pull request #133, allow port 443 with non secure scheme, and allow port 80 with secure scheme
This commit is contained in:
		@@ -40,6 +40,7 @@
 | 
				
			|||||||
/*
 | 
					/*
 | 
				
			||||||
 * Contributors:
 | 
					 * Contributors:
 | 
				
			||||||
 * - Liryna <liryna.stark@gmail.com>
 | 
					 * - Liryna <liryna.stark@gmail.com>
 | 
				
			||||||
 | 
					 * - Nikola Kovacevic <nikolak@outlook.com>
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
#endregion
 | 
					#endregion
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -818,14 +819,12 @@ namespace WebSocketSharp
 | 
				
			|||||||
    /// A <see cref="string"/> that represents a WebSocket URL to try.
 | 
					    /// A <see cref="string"/> that represents a WebSocket URL to try.
 | 
				
			||||||
    /// </param>
 | 
					    /// </param>
 | 
				
			||||||
    /// <param name="result">
 | 
					    /// <param name="result">
 | 
				
			||||||
    /// When this method returns, a <see cref="Uri"/> that represents
 | 
					    /// When this method returns, a <see cref="Uri"/> that represents a WebSocket URL,
 | 
				
			||||||
    /// a WebSocket URL if <paramref name="uriString"/> is valid;
 | 
					    /// or <see langword="null"/> if <paramref name="uriString"/> is invalid.
 | 
				
			||||||
    /// otherwise, <see langword="null"/>.
 | 
					 | 
				
			||||||
    /// </param>
 | 
					    /// </param>
 | 
				
			||||||
    /// <param name="message">
 | 
					    /// <param name="message">
 | 
				
			||||||
    /// When this method returns, a <see cref="string"/> that represents
 | 
					    /// When this method returns, a <see cref="string"/> that represents an error message,
 | 
				
			||||||
    /// an error message if <paramref name="uriString"/> is invalid;
 | 
					    /// or <see cref="String.Empty"/> if <paramref name="uriString"/> is valid.
 | 
				
			||||||
    /// otherwise, <see cref="String.Empty"/>.
 | 
					 | 
				
			||||||
    /// </param>
 | 
					    /// </param>
 | 
				
			||||||
    internal static bool TryCreateWebSocketUri (
 | 
					    internal static bool TryCreateWebSocketUri (
 | 
				
			||||||
      this string uriString, out Uri result, out string message)
 | 
					      this string uriString, out Uri result, out string message)
 | 
				
			||||||
@@ -839,7 +838,7 @@ namespace WebSocketSharp
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      var schm = uri.Scheme;
 | 
					      var schm = uri.Scheme;
 | 
				
			||||||
      if (schm != "ws" && schm != "wss") {
 | 
					      if (!(schm == "ws" || schm == "wss")) {
 | 
				
			||||||
        message = "The scheme part isn't 'ws' or 'wss': " + uriString;
 | 
					        message = "The scheme part isn't 'ws' or 'wss': " + uriString;
 | 
				
			||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@@ -850,26 +849,22 @@ namespace WebSocketSharp
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      var port = uri.Port;
 | 
					      var port = uri.Port;
 | 
				
			||||||
      if (port > 0) {
 | 
					      if (port > 65535) {
 | 
				
			||||||
        if (port > 65535) {
 | 
					        message = "The port part is greater than 65535: " + uriString;
 | 
				
			||||||
          message = "The port part is greater than 65535: " + uriString;
 | 
					        return false;
 | 
				
			||||||
          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 (
 | 
					 | 
				
			||||||
          String.Format (
 | 
					 | 
				
			||||||
            "{0}://{1}:{2}{3}", schm, uri.Host, schm == "ws" ? 80 : 443, uri.PathAndQuery));
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      result = uri;
 | 
					      result = port > 0
 | 
				
			||||||
 | 
					               ? uri
 | 
				
			||||||
 | 
					               : new Uri (
 | 
				
			||||||
 | 
					                   String.Format (
 | 
				
			||||||
 | 
					                     "{0}://{1}:{2}{3}",
 | 
				
			||||||
 | 
					                     schm,
 | 
				
			||||||
 | 
					                     uri.Host,
 | 
				
			||||||
 | 
					                     schm == "ws" ? 80 : 443,
 | 
				
			||||||
 | 
					                     uri.PathAndQuery));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      message = String.Empty;
 | 
					      message = String.Empty;
 | 
				
			||||||
 | 
					 | 
				
			||||||
      return true;
 | 
					      return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -104,12 +104,8 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
    /// <paramref name="port"/> isn't between 1 and 65535 inclusive.
 | 
					    /// <paramref name="port"/> isn't between 1 and 65535 inclusive.
 | 
				
			||||||
    /// </exception>
 | 
					    /// </exception>
 | 
				
			||||||
    public HttpServer (int port)
 | 
					    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>
 | 
					    /// <summary>
 | 
				
			||||||
@@ -127,9 +123,6 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
    /// A <see cref="bool"/> that indicates providing a secure connection or not.
 | 
					    /// A <see cref="bool"/> that indicates providing a secure connection or not.
 | 
				
			||||||
    /// (<c>true</c> indicates providing a secure connection.)
 | 
					    /// (<c>true</c> indicates providing a secure connection.)
 | 
				
			||||||
    /// </param>
 | 
					    /// </param>
 | 
				
			||||||
    /// <exception cref="ArgumentException">
 | 
					 | 
				
			||||||
    /// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
 | 
					 | 
				
			||||||
    /// </exception>
 | 
					 | 
				
			||||||
    /// <exception cref="ArgumentOutOfRangeException">
 | 
					    /// <exception cref="ArgumentOutOfRangeException">
 | 
				
			||||||
    /// <paramref name="port"/> isn't between 1 and 65535 inclusive.
 | 
					    /// <paramref name="port"/> isn't between 1 and 65535 inclusive.
 | 
				
			||||||
    /// </exception>
 | 
					    /// </exception>
 | 
				
			||||||
@@ -139,10 +132,6 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
        throw new ArgumentOutOfRangeException (
 | 
					        throw new ArgumentOutOfRangeException (
 | 
				
			||||||
          "port", "Not between 1 and 65535 inclusive: " + port);
 | 
					          "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);
 | 
					      init ("*", port, secure);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -112,12 +112,8 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
    /// <paramref name="port"/> isn't between 1 and 65535 inclusive.
 | 
					    /// <paramref name="port"/> isn't between 1 and 65535 inclusive.
 | 
				
			||||||
    /// </exception>
 | 
					    /// </exception>
 | 
				
			||||||
    public WebSocketServer (int port)
 | 
					    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>
 | 
					    /// <summary>
 | 
				
			||||||
@@ -188,15 +184,16 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
    /// A <see cref="bool"/> that indicates providing a secure connection or not.
 | 
					    /// A <see cref="bool"/> that indicates providing a secure connection or not.
 | 
				
			||||||
    /// (<c>true</c> indicates providing a secure connection.)
 | 
					    /// (<c>true</c> indicates providing a secure connection.)
 | 
				
			||||||
    /// </param>
 | 
					    /// </param>
 | 
				
			||||||
    /// <exception cref="ArgumentException">
 | 
					 | 
				
			||||||
    /// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
 | 
					 | 
				
			||||||
    /// </exception>
 | 
					 | 
				
			||||||
    /// <exception cref="ArgumentOutOfRangeException">
 | 
					    /// <exception cref="ArgumentOutOfRangeException">
 | 
				
			||||||
    /// <paramref name="port"/> isn't between 1 and 65535 inclusive.
 | 
					    /// <paramref name="port"/> isn't between 1 and 65535 inclusive.
 | 
				
			||||||
    /// </exception>
 | 
					    /// </exception>
 | 
				
			||||||
    public WebSocketServer (int port, bool secure)
 | 
					    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>
 | 
					    /// <summary>
 | 
				
			||||||
@@ -255,15 +252,7 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
    /// <paramref name="address"/> is <see langword="null"/>.
 | 
					    /// <paramref name="address"/> is <see langword="null"/>.
 | 
				
			||||||
    /// </exception>
 | 
					    /// </exception>
 | 
				
			||||||
    /// <exception cref="ArgumentException">
 | 
					    /// <exception cref="ArgumentException">
 | 
				
			||||||
    ///   <para>
 | 
					    /// <paramref name="address"/> isn't a local IP address.
 | 
				
			||||||
    ///   <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>
 | 
				
			||||||
    /// <exception cref="ArgumentOutOfRangeException">
 | 
					    /// <exception cref="ArgumentOutOfRangeException">
 | 
				
			||||||
    /// <paramref name="port"/> isn't between 1 and 65535 inclusive.
 | 
					    /// <paramref name="port"/> isn't between 1 and 65535 inclusive.
 | 
				
			||||||
@@ -280,10 +269,6 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
        throw new ArgumentOutOfRangeException (
 | 
					        throw new ArgumentOutOfRangeException (
 | 
				
			||||||
          "port", "Not between 1 and 65535 inclusive: " + port);
 | 
					          "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);
 | 
					      init (address, port, secure);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user