Fix for extensions for server
This commit is contained in:
		@@ -426,11 +426,6 @@ namespace WebSocketSharp
 | 
			
		||||
             : stream.ToByteArray ();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal static bool Equals (this string value, CompressionMethod method)
 | 
			
		||||
    {
 | 
			
		||||
      return value == method.ToCompressionExtension ();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Determines whether the specified <see cref="int"/> equals the specified
 | 
			
		||||
    /// <see cref="char"/>, and invokes the specified Action<int> delegate
 | 
			
		||||
@@ -709,10 +704,9 @@ namespace WebSocketSharp
 | 
			
		||||
        null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal static string RemovePrefix (
 | 
			
		||||
      this string value, params string [] prefixes)
 | 
			
		||||
    internal static string RemovePrefix (this string value, params string [] prefixes)
 | 
			
		||||
    {
 | 
			
		||||
      int i = 0;
 | 
			
		||||
      var i = 0;
 | 
			
		||||
      foreach (var prefix in prefixes) {
 | 
			
		||||
        if (value.StartsWith (prefix)) {
 | 
			
		||||
          i = prefix.Length;
 | 
			
		||||
@@ -728,13 +722,16 @@ namespace WebSocketSharp
 | 
			
		||||
    internal static IEnumerable<string> SplitHeaderValue (
 | 
			
		||||
      this string value, params char [] separator)
 | 
			
		||||
    {
 | 
			
		||||
      var len = value.Length;
 | 
			
		||||
      var separators = new string (separator);
 | 
			
		||||
      var buffer = new StringBuilder (64);
 | 
			
		||||
      int len = value.Length;
 | 
			
		||||
      bool quoted = false;
 | 
			
		||||
      bool escaped = false;
 | 
			
		||||
      for (int i = 0; i < len; i++) {
 | 
			
		||||
        char c = value [i];
 | 
			
		||||
 | 
			
		||||
      var buffer = new StringBuilder (32);
 | 
			
		||||
      var quoted = false;
 | 
			
		||||
      var escaped = false;
 | 
			
		||||
 | 
			
		||||
      char c;
 | 
			
		||||
      for (var i = 0; i < len; i++) {
 | 
			
		||||
        c = value [i];
 | 
			
		||||
        if (c == '"') {
 | 
			
		||||
          if (escaped)
 | 
			
		||||
            escaped = !escaped;
 | 
			
		||||
@@ -749,6 +746,7 @@ namespace WebSocketSharp
 | 
			
		||||
          if (!quoted) {
 | 
			
		||||
            yield return buffer.ToString ();
 | 
			
		||||
            buffer.Length = 0;
 | 
			
		||||
 | 
			
		||||
            continue;
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
@@ -791,22 +789,22 @@ namespace WebSocketSharp
 | 
			
		||||
             : buffer.Reverse ().ToArray ();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal static string ToCompressionExtension (this CompressionMethod method)
 | 
			
		||||
    internal static CompressionMethod ToCompressionMethod (this string value)
 | 
			
		||||
    {
 | 
			
		||||
      foreach (CompressionMethod method in Enum.GetValues (typeof (CompressionMethod)))
 | 
			
		||||
        if (method.ToExtensionString () == value)
 | 
			
		||||
          return method;
 | 
			
		||||
 | 
			
		||||
      return CompressionMethod.NONE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal static string ToExtensionString (this CompressionMethod method)
 | 
			
		||||
    {
 | 
			
		||||
      return method != CompressionMethod.NONE
 | 
			
		||||
             ? String.Format ("permessage-{0}", method.ToString ().ToLower ())
 | 
			
		||||
             : String.Empty;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal static CompressionMethod ToCompressionMethod (this string value)
 | 
			
		||||
    {
 | 
			
		||||
      foreach (CompressionMethod method in Enum.GetValues (typeof (CompressionMethod)))
 | 
			
		||||
        if (value.Equals (method))
 | 
			
		||||
          return method;
 | 
			
		||||
 | 
			
		||||
      return CompressionMethod.NONE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    internal static System.Net.IPAddress ToIPAddress (
 | 
			
		||||
      this string hostNameOrAddress)
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
@@ -593,9 +593,7 @@ namespace WebSocketSharp
 | 
			
		||||
    {
 | 
			
		||||
      _logger.Debug (
 | 
			
		||||
        String.Format (
 | 
			
		||||
          "A WebSocket connection request from {0}:\n{1}",
 | 
			
		||||
          _context.UserEndPoint,
 | 
			
		||||
          _context));
 | 
			
		||||
          "A WebSocket connection request from {0}:\n{1}", _context.UserEndPoint, _context));
 | 
			
		||||
 | 
			
		||||
      var msg = checkIfValidHandshakeRequest (_context);
 | 
			
		||||
      if (msg != null) {
 | 
			
		||||
@@ -608,13 +606,12 @@ namespace WebSocketSharp
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if (_protocol != null &&
 | 
			
		||||
          !_context.SecWebSocketProtocols.Contains (
 | 
			
		||||
            protocol => protocol == _protocol))
 | 
			
		||||
          !_context.SecWebSocketProtocols.Contains (protocol => protocol == _protocol))
 | 
			
		||||
        _protocol = null;
 | 
			
		||||
 | 
			
		||||
      var extensions = _context.Headers ["Sec-WebSocket-Extensions"];
 | 
			
		||||
      if (extensions != null && extensions.Length > 0)
 | 
			
		||||
        processRequestedExtensions (extensions);
 | 
			
		||||
        acceptSecWebSocketExtensionsHeader (extensions);
 | 
			
		||||
 | 
			
		||||
      return send (createHandshakeResponse ());
 | 
			
		||||
    }
 | 
			
		||||
@@ -636,6 +633,34 @@ namespace WebSocketSharp
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // As server
 | 
			
		||||
    private void acceptSecWebSocketExtensionsHeader (string value)
 | 
			
		||||
    {
 | 
			
		||||
      var extensions = new StringBuilder (32);
 | 
			
		||||
 | 
			
		||||
      var compress = false;
 | 
			
		||||
      foreach (var extension in value.SplitHeaderValue (',')) {
 | 
			
		||||
        var trimed = extension.Trim ();
 | 
			
		||||
        var unprefixed = trimed.RemovePrefix ("x-webkit-");
 | 
			
		||||
 | 
			
		||||
        if (!compress && unprefixed.IsCompressionExtension ()) {
 | 
			
		||||
          var method = unprefixed.ToCompressionMethod ();
 | 
			
		||||
          if (method != CompressionMethod.NONE) {
 | 
			
		||||
            _compression = method;
 | 
			
		||||
            compress = true;
 | 
			
		||||
 | 
			
		||||
            extensions.Append (trimed + ", ");
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      var len = extensions.Length;
 | 
			
		||||
      if (len > 0) {
 | 
			
		||||
        extensions.Length = len - 2;
 | 
			
		||||
        _extensions = extensions.ToString ();
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private bool acceptUnsupportedFrame (
 | 
			
		||||
      WsFrame frame, CloseStatusCode code, string reason)
 | 
			
		||||
    {
 | 
			
		||||
@@ -682,8 +707,7 @@ namespace WebSocketSharp
 | 
			
		||||
               ? "Invalid Host header."
 | 
			
		||||
               : !validateSecWebSocketKeyHeader (headers ["Sec-WebSocket-Key"])
 | 
			
		||||
                 ? "Invalid Sec-WebSocket-Key header."
 | 
			
		||||
                 : !validateSecWebSocketVersionClientHeader (
 | 
			
		||||
                     headers ["Sec-WebSocket-Version"])
 | 
			
		||||
                 : !validateSecWebSocketVersionClientHeader (headers ["Sec-WebSocket-Version"])
 | 
			
		||||
                   ? "Invalid Sec-WebSocket-Version header."
 | 
			
		||||
                   : !validateCookies (context.CookieCollection, _cookies)
 | 
			
		||||
                     ? "Invalid Cookies."
 | 
			
		||||
@@ -883,8 +907,9 @@ namespace WebSocketSharp
 | 
			
		||||
    private string createExtensionsRequest ()
 | 
			
		||||
    {
 | 
			
		||||
      var extensions = new StringBuilder (32);
 | 
			
		||||
 | 
			
		||||
      if (_compression != CompressionMethod.NONE)
 | 
			
		||||
        extensions.Append (_compression.ToCompressionExtension ());
 | 
			
		||||
        extensions.Append (_compression.ToExtensionString ());
 | 
			
		||||
 | 
			
		||||
      return extensions.Length > 0
 | 
			
		||||
             ? extensions.ToString ()
 | 
			
		||||
@@ -1015,28 +1040,6 @@ namespace WebSocketSharp
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // As server
 | 
			
		||||
    private void processRequestedExtensions (string extensions)
 | 
			
		||||
    {
 | 
			
		||||
      var comp = false;
 | 
			
		||||
      var buffer = new List<string> ();
 | 
			
		||||
      foreach (var e in extensions.SplitHeaderValue (',')) {
 | 
			
		||||
        var extension = e.Trim ();
 | 
			
		||||
        var tmp = extension.RemovePrefix ("x-webkit-");
 | 
			
		||||
        if (!comp && tmp.IsCompressionExtension ()) {
 | 
			
		||||
          var method = tmp.ToCompressionMethod ();
 | 
			
		||||
          if (method != CompressionMethod.NONE) {
 | 
			
		||||
            _compression = method;
 | 
			
		||||
            comp = true;
 | 
			
		||||
            buffer.Add (extension);
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if (buffer.Count > 0)
 | 
			
		||||
        _extensions = buffer.ToArray ().ToString (", ");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // As client
 | 
			
		||||
    private HandshakeResponse receiveHandshakeResponse ()
 | 
			
		||||
    {
 | 
			
		||||
@@ -1374,7 +1377,7 @@ namespace WebSocketSharp
 | 
			
		||||
 | 
			
		||||
      var extensions = value.SplitHeaderValue (',');
 | 
			
		||||
      if (extensions.Contains (
 | 
			
		||||
            extension => !extension.Trim ().Equals (_compression)))
 | 
			
		||||
            extension => extension.Trim () != _compression.ToExtensionString ()))
 | 
			
		||||
        return false;
 | 
			
		||||
 | 
			
		||||
      _extensions = value;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user