]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Merge remote-tracking branch 'davidgaleano/master' into work
authorBen Darnell <ben@bendarnell.com>
Sat, 21 Jan 2012 23:15:12 +0000 (15:15 -0800)
committerBen Darnell <ben@bendarnell.com>
Sat, 21 Jan 2012 23:15:12 +0000 (15:15 -0800)
Conflicts:
tornado/websocket.py

1  2 
tornado/websocket.py

index 7471c4cde18b415f7cdfd6d271e7f017c97cc607,a69a5e8a7ef076bdb34919b8ca1ba7368e58879c..69cca86683fc5b6fbaea1b8379b95b447906219a
@@@ -100,18 -90,18 +100,22 @@@ class WebSocketHandler(tornado.web.Requ
                  "Sec-WebSocket-Version: 8\r\n\r\n"))
              self.stream.close()
  
 -        else:
 -            self.ws_connection = WebSocketProtocol76(self)
 -            self.ws_connection.accept_connection()
 +    def write_message(self, message, binary=False):
 +        """Sends the given message to the client of this Web Socket.
  
 -    def write_message(self, message):
 -        """Sends the given message to the client of this Web Socket."""
 -        self.ws_connection.write_message(message)
 +        The message may be either a string or a dict (which will be
 +        encoded as json).  If the ``binary`` argument is false, the
 +        message will be sent as utf8; in binary mode any byte string
 +        is allowed.
 +        """
 +        if isinstance(message, dict):
 +            message = tornado.escape.json_encode(message)
 +        self.ws_connection.write_message(message, binary=binary)
  
+     def validate_subprotocol(self, subprotocols):
+         """Invoked when a new WebSocket requests specific subprotocols."""
+         return None
      def open(self, *args, **kwargs):
          """Invoked when a new WebSocket is opened."""
          pass
@@@ -240,7 -203,19 +244,19 @@@ class WebSocketProtocol76(WebSocketProt
              logging.debug("Malformed WebSocket request received")
              self._abort()
              return
 -        scheme = "wss" if self.request.protocol == "https" else "ws"
 +        scheme = self.handler.get_websocket_scheme()
+         subprotocols = self.request.headers.get("Sec-WebSocket-Protocol", None)
+         if subprotocols:
+             subprotocol = self.handler.validate_subprotocol(subprotocols)
+             if not subprotocol:
+                 logging.debug("Subprotocol rejected by handler.")
+                 self._abort()
+                 return
+             subprotocol = "Sec-WebSocket-Protocol: %s\r\n" % subprotocol
+         else:
+             subprotocol = ''
          # Write the initial headers before attempting to read the challenge.
          # This is necessary when using proxies (such as HAProxy), which
          # need to see the Upgrade headers before passing through the