From: Ben Darnell Date: Sat, 21 Jan 2012 23:15:12 +0000 (-0800) Subject: Merge remote-tracking branch 'davidgaleano/master' into work X-Git-Tag: v2.2.0~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1cfcc4a7bca6ab4d8e116cd7b8d49225b165e455;p=thirdparty%2Ftornado.git Merge remote-tracking branch 'davidgaleano/master' into work Conflicts: tornado/websocket.py --- 1cfcc4a7bca6ab4d8e116cd7b8d49225b165e455 diff --cc tornado/websocket.py index 7471c4cde,a69a5e8a7..69cca8668 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@@ -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 = 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 = '' + - scheme = "wss" if self.request.protocol == "https" else "ws" # 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