Bug 1137008 - Implement missing parameters of WebSocket permessage compression extension, r=jduell
authorMichal Novotny <michal.novotny@gmail.com>
Fri, 31 Jul 2015 07:12:53 +0200
changeset 287246 7b5a9b0979d9dc24c64e80185dfb0d3b320f97de
parent 287245 856f588ad29e8bd7c29b12f6d5fca4c7b125ccc4
child 287247 c12f87f250009df14c6ee2f8c00f39dbe15dd553
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjduell
bugs1137008
milestone42.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1137008 - Implement missing parameters of WebSocket permessage compression extension, r=jduell
netwerk/protocol/websocket/WebSocketChannel.cpp
--- a/netwerk/protocol/websocket/WebSocketChannel.cpp
+++ b/netwerk/protocol/websocket/WebSocketChannel.cpp
@@ -754,31 +754,33 @@ NS_IMPL_ISUPPORTS(CallOnTransportAvailab
 
 //-----------------------------------------------------------------------------
 // PMCECompression
 //-----------------------------------------------------------------------------
 
 class PMCECompression
 {
 public:
-  explicit PMCECompression(bool aNoContextTakeover)
+  PMCECompression(bool aNoContextTakeover,
+                  int32_t aClientMaxWindowBits,
+                  int32_t aServerMaxWindowBits)
     : mActive(false)
     , mNoContextTakeover(aNoContextTakeover)
     , mResetDeflater(false)
     , mMessageDeflated(false)
   {
     MOZ_COUNT_CTOR(PMCECompression);
 
     mDeflater.zalloc = mInflater.zalloc = Z_NULL;
     mDeflater.zfree  = mInflater.zfree  = Z_NULL;
     mDeflater.opaque = mInflater.opaque = Z_NULL;
 
-    if (deflateInit2(&mDeflater, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15,
-                     8, Z_DEFAULT_STRATEGY) == Z_OK) {
-      if (inflateInit2(&mInflater, -15) == Z_OK) {
+    if (deflateInit2(&mDeflater, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
+                     -aClientMaxWindowBits, 8, Z_DEFAULT_STRATEGY) == Z_OK) {
+      if (inflateInit2(&mInflater, -aServerMaxWindowBits) == Z_OK) {
         mActive = true;
       } else {
         deflateEnd(&mDeflater);
       }
     }
   }
 
   ~PMCECompression()
@@ -2428,67 +2430,156 @@ WebSocketChannel::HandleExtensions()
                            NS_LITERAL_CSTRING("permessage-deflate"))) {
         if (!mAllowPMCE) {
           LOG(("WebSocketChannel::HandleExtensions: "
                "Recvd permessage-deflate which wasn't offered\n"));
           AbortSession(NS_ERROR_ILLEGAL_VALUE);
           return NS_ERROR_ILLEGAL_VALUE;
         }
 
-        nsAutoCString param;
-
-        int32_t delimPos = extensions.FindChar(';');
-        if (delimPos != kNotFound) {
-          param = Substring(extensions, delimPos + 1);
-          param.CompressWhitespace(true, false);
-          extensions.Truncate(delimPos);
-          extensions.CompressWhitespace(false, true);
-        }
- 
-        if (!extensions.EqualsLiteral("permessage-deflate")) {
-          LOG(("WebSocketChannel::HandleExtensions: "
-               "HTTP Sec-WebSocket-Extensions negotiated unknown value %s\n",
-               extensions.get()));
-          AbortSession(NS_ERROR_ILLEGAL_VALUE);
-          return NS_ERROR_ILLEGAL_VALUE;
-        }
-
-        bool noContextTakeover = false;
-        if (!param.IsEmpty()) {
-          if (param.EqualsLiteral("client_no_context_takeover")) {
-            noContextTakeover = true;
+        bool skipExtensionName = false;
+        bool clientNoContextTakeover = false;
+        bool serverNoContextTakeover = false;
+        int32_t clientMaxWindowBits = -1;
+        int32_t serverMaxWindowBits = -1;
+
+        while (!extensions.IsEmpty()) {
+          nsAutoCString paramName;
+          nsAutoCString paramValue;
+
+          int32_t delimPos = extensions.FindChar(';');
+          if (delimPos != kNotFound) {
+            paramName = Substring(extensions, 0, delimPos);
+            extensions = Substring(extensions, delimPos + 1);
           } else {
-            LOG(("WebSocketChannel::HandleExtensions: "
-                 "HTTP permessage-deflate extension negotiated unknown "
-                 "parameter %s\n", param.get()));
+            paramName = extensions;
+            extensions.Truncate();
+          }
+          paramName.CompressWhitespace();
+          extensions.CompressWhitespace();
+
+          delimPos = paramName.FindChar('=');
+          if (delimPos != kNotFound) {
+            paramValue = Substring(paramName, delimPos + 1);
+            paramName.Truncate(delimPos);
+          }
+
+          if (!skipExtensionName) {
+            skipExtensionName = true;
+            if (!paramName.EqualsLiteral("permessage-deflate") ||
+                !paramValue.IsEmpty()) {
+              LOG(("WebSocketChannel::HandleExtensions: HTTP "
+                   "Sec-WebSocket-Extensions negotiated unknown extension\n"));
+              AbortSession(NS_ERROR_ILLEGAL_VALUE);
+              return NS_ERROR_ILLEGAL_VALUE;
+            }
+          } else if (paramName.EqualsLiteral("client_no_context_takeover")) {
+            if (!paramValue.IsEmpty()) {
+              LOG(("WebSocketChannel::HandleExtensions: parameter "
+                   "client_no_context_takeover must not have value, found %s\n",
+                   paramValue.get()));
+              AbortSession(NS_ERROR_ILLEGAL_VALUE);
+              return NS_ERROR_ILLEGAL_VALUE;
+            }
+            if (clientNoContextTakeover) {
+              LOG(("WebSocketChannel::HandleExtensions: found multiple "
+                   "parameters client_no_context_takeover\n"));
+              AbortSession(NS_ERROR_ILLEGAL_VALUE);
+              return NS_ERROR_ILLEGAL_VALUE;
+            }
+            clientNoContextTakeover = true;
+          } else if (paramName.EqualsLiteral("server_no_context_takeover")) {
+            if (!paramValue.IsEmpty()) {
+              LOG(("WebSocketChannel::HandleExtensions: parameter "
+                   "server_no_context_takeover must not have value, found %s\n",
+                   paramValue.get()));
+              AbortSession(NS_ERROR_ILLEGAL_VALUE);
+              return NS_ERROR_ILLEGAL_VALUE;
+            }
+            if (serverNoContextTakeover) {
+              LOG(("WebSocketChannel::HandleExtensions: found multiple "
+                   "parameters server_no_context_takeover\n"));
+              AbortSession(NS_ERROR_ILLEGAL_VALUE);
+              return NS_ERROR_ILLEGAL_VALUE;
+            }
+            serverNoContextTakeover = true;
+          } else if (paramName.EqualsLiteral("client_max_window_bits")) {
+            if (clientMaxWindowBits != -1) {
+              LOG(("WebSocketChannel::HandleExtensions: found multiple "
+                   "parameters client_max_window_bits\n"));
+              AbortSession(NS_ERROR_ILLEGAL_VALUE);
+              return NS_ERROR_ILLEGAL_VALUE;
+            }
+
+            nsresult errcode;
+            clientMaxWindowBits = paramValue.ToInteger(&errcode);
+            if (NS_FAILED(errcode) || clientMaxWindowBits < 8 ||
+                clientMaxWindowBits > 15) {
+              LOG(("WebSocketChannel::HandleExtensions: found invalid "
+                   "parameter client_max_window_bits %s\n", paramValue.get()));
+              AbortSession(NS_ERROR_ILLEGAL_VALUE);
+              return NS_ERROR_ILLEGAL_VALUE;
+            }
+          } else if (paramName.EqualsLiteral("server_max_window_bits")) {
+            if (serverMaxWindowBits != -1) {
+              LOG(("WebSocketChannel::HandleExtensions: found multiple "
+                   "parameters server_max_window_bits\n"));
+              AbortSession(NS_ERROR_ILLEGAL_VALUE);
+              return NS_ERROR_ILLEGAL_VALUE;
+            }
+
+            nsresult errcode;
+            serverMaxWindowBits = paramValue.ToInteger(&errcode);
+            if (NS_FAILED(errcode) || serverMaxWindowBits < 8 ||
+                serverMaxWindowBits > 15) {
+              LOG(("WebSocketChannel::HandleExtensions: found invalid "
+                   "parameter server_max_window_bits %s\n", paramValue.get()));
+              AbortSession(NS_ERROR_ILLEGAL_VALUE);
+              return NS_ERROR_ILLEGAL_VALUE;
+            }
+          } else {
+            LOG(("WebSocketChannel::HandleExtensions: found unknown "
+                 "parameter %s\n", paramName.get()));
             AbortSession(NS_ERROR_ILLEGAL_VALUE);
             return NS_ERROR_ILLEGAL_VALUE;
           }
         }
 
-        mPMCECompressor = new PMCECompression(noContextTakeover);
+        if (clientMaxWindowBits == -1) {
+          clientMaxWindowBits = 15;
+        }
+        if (serverMaxWindowBits == -1) {
+          serverMaxWindowBits = 15;
+        }
+
+        mPMCECompressor = new PMCECompression(clientNoContextTakeover,
+                                              clientMaxWindowBits,
+                                              serverMaxWindowBits);
         if (mPMCECompressor->Active()) {
           LOG(("WebSocketChannel::HandleExtensions: PMCE negotiated, %susing "
-               "context takeover\n", noContextTakeover ? "NOT " : ""));
+               "context takeover, clientMaxWindowBits=%d, "
+               "serverMaxWindowBits=%d\n",
+               clientNoContextTakeover ? "NOT " : "", clientMaxWindowBits,
+               serverMaxWindowBits));
+
+          mNegotiatedExtensions = "permessage-deflate";
         } else {
           LOG(("WebSocketChannel::HandleExtensions: Cannot init PMCE "
                "compression object\n"));
           mPMCECompressor = nullptr;
           AbortSession(NS_ERROR_UNEXPECTED);
           return NS_ERROR_UNEXPECTED;
         }
       } else {
         LOG(("WebSocketChannel::HandleExtensions: "
              "HTTP Sec-WebSocket-Extensions negotiated unknown value %s\n",
              extensions.get()));
         AbortSession(NS_ERROR_ILLEGAL_VALUE);
         return NS_ERROR_ILLEGAL_VALUE;
       }
-
-      mNegotiatedExtensions = extensions;
     }
   }
 
   return NS_OK;
 }
 
 nsresult
 WebSocketChannel::SetupRequest()