2014-06-11 08:34:28 +00:00
|
|
|
"""Implementation of the WebSocket protocol.
|
|
|
|
|
|
|
|
`WebSockets <http://dev.w3.org/html5/websockets/>`_ allow for bidirectional
|
|
|
|
communication between the browser and server.
|
|
|
|
|
2014-06-29 10:05:33 +00:00
|
|
|
WebSockets are supported in the current versions of all major browsers,
|
|
|
|
although older versions that do not support WebSockets are still in use
|
|
|
|
(refer to http://caniuse.com/websockets for details).
|
|
|
|
|
|
|
|
This module implements the final version of the WebSocket protocol as
|
|
|
|
defined in `RFC 6455 <http://tools.ietf.org/html/rfc6455>`_. Certain
|
|
|
|
browser versions (notably Safari 5.x) implemented an earlier draft of
|
|
|
|
the protocol (known as "draft 76") and are not compatible with this module.
|
|
|
|
|
|
|
|
.. versionchanged:: 4.0
|
|
|
|
Removed support for the draft 76 protocol version.
|
2014-06-11 08:34:28 +00:00
|
|
|
"""
|
|
|
|
|
2017-08-07 16:00:36 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2014-06-11 08:34:28 +00:00
|
|
|
# Author: Jacob Kristhammar, 2010
|
|
|
|
|
|
|
|
import base64
|
|
|
|
import collections
|
|
|
|
import hashlib
|
|
|
|
import os
|
|
|
|
import struct
|
|
|
|
import tornado.escape
|
|
|
|
import tornado.web
|
2014-10-14 04:24:01 +00:00
|
|
|
import zlib
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
from tornado.concurrent import TracebackFuture
|
2014-06-17 04:54:00 +00:00
|
|
|
from tornado.escape import utf8, native_str, to_unicode
|
2017-08-07 16:00:36 +00:00
|
|
|
from tornado import gen, httpclient, httputil
|
|
|
|
from tornado.ioloop import IOLoop, PeriodicCallback
|
2014-06-11 08:34:28 +00:00
|
|
|
from tornado.iostream import StreamClosedError
|
|
|
|
from tornado.log import gen_log, app_log
|
|
|
|
from tornado import simple_httpclient
|
2014-06-17 04:54:00 +00:00
|
|
|
from tornado.tcpclient import TCPClient
|
2016-09-30 22:40:12 +00:00
|
|
|
from tornado.util import _websocket_mask, PY3
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2016-09-30 22:40:12 +00:00
|
|
|
if PY3:
|
2015-04-27 19:06:19 +00:00
|
|
|
from urllib.parse import urlparse # py2
|
2016-09-30 22:40:12 +00:00
|
|
|
xrange = range
|
|
|
|
else:
|
2015-04-27 19:06:19 +00:00
|
|
|
from urlparse import urlparse # py3
|
2014-06-17 04:54:00 +00:00
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
class WebSocketError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class WebSocketClosedError(WebSocketError):
|
|
|
|
"""Raised by operations on a closed connection.
|
|
|
|
|
|
|
|
.. versionadded:: 3.2
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class WebSocketHandler(tornado.web.RequestHandler):
|
|
|
|
"""Subclass this class to create a basic WebSocket handler.
|
|
|
|
|
|
|
|
Override `on_message` to handle incoming messages, and use
|
|
|
|
`write_message` to send messages to the client. You can also
|
|
|
|
override `open` and `on_close` to handle opened and closed
|
|
|
|
connections.
|
|
|
|
|
2017-08-07 16:00:36 +00:00
|
|
|
Custom upgrade response headers can be sent by overriding
|
|
|
|
`~tornado.web.RequestHandler.set_default_headers` or
|
|
|
|
`~tornado.web.RequestHandler.prepare`.
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
See http://dev.w3.org/html5/websockets/ for details on the
|
|
|
|
JavaScript interface. The protocol is specified at
|
|
|
|
http://tools.ietf.org/html/rfc6455.
|
|
|
|
|
|
|
|
Here is an example WebSocket handler that echos back all received messages
|
2015-04-27 19:06:19 +00:00
|
|
|
back to the client:
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2015-04-27 19:06:19 +00:00
|
|
|
.. testcode::
|
|
|
|
|
|
|
|
class EchoWebSocket(tornado.websocket.WebSocketHandler):
|
2014-06-11 08:34:28 +00:00
|
|
|
def open(self):
|
2015-04-27 19:06:19 +00:00
|
|
|
print("WebSocket opened")
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
def on_message(self, message):
|
|
|
|
self.write_message(u"You said: " + message)
|
|
|
|
|
|
|
|
def on_close(self):
|
2015-04-27 19:06:19 +00:00
|
|
|
print("WebSocket closed")
|
|
|
|
|
|
|
|
.. testoutput::
|
|
|
|
:hide:
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
WebSockets are not standard HTTP connections. The "handshake" is
|
|
|
|
HTTP, but after the handshake, the protocol is
|
|
|
|
message-based. Consequently, most of the Tornado HTTP facilities
|
|
|
|
are not available in handlers of this type. The only communication
|
|
|
|
methods available to you are `write_message()`, `ping()`, and
|
|
|
|
`close()`. Likewise, your request handler class should implement
|
|
|
|
`open()` method rather than ``get()`` or ``post()``.
|
|
|
|
|
|
|
|
If you map the handler above to ``/websocket`` in your application, you can
|
|
|
|
invoke it in JavaScript with::
|
|
|
|
|
|
|
|
var ws = new WebSocket("ws://localhost:8888/websocket");
|
|
|
|
ws.onopen = function() {
|
|
|
|
ws.send("Hello, world");
|
|
|
|
};
|
|
|
|
ws.onmessage = function (evt) {
|
|
|
|
alert(evt.data);
|
|
|
|
};
|
|
|
|
|
|
|
|
This script pops up an alert box that says "You said: Hello, world".
|
2014-10-14 04:24:01 +00:00
|
|
|
|
|
|
|
Web browsers allow any site to open a websocket connection to any other,
|
|
|
|
instead of using the same-origin policy that governs other network
|
|
|
|
access from javascript. This can be surprising and is a potential
|
|
|
|
security hole, so since Tornado 4.0 `WebSocketHandler` requires
|
|
|
|
applications that wish to receive cross-origin websockets to opt in
|
|
|
|
by overriding the `~WebSocketHandler.check_origin` method (see that
|
|
|
|
method's docs for details). Failure to do so is the most likely
|
|
|
|
cause of 403 errors when making a websocket connection.
|
|
|
|
|
|
|
|
When using a secure websocket connection (``wss://``) with a self-signed
|
|
|
|
certificate, the connection from a browser may fail because it wants
|
|
|
|
to show the "accept this certificate" dialog but has nowhere to show it.
|
|
|
|
You must first visit a regular HTML page using the same certificate
|
|
|
|
to accept it before the websocket connection will succeed.
|
2017-08-07 16:00:36 +00:00
|
|
|
|
|
|
|
If the application setting ``websocket_ping_interval`` has a non-zero
|
|
|
|
value, a ping will be sent periodically, and the connection will be
|
|
|
|
closed if a response is not received before the ``websocket_ping_timeout``.
|
|
|
|
|
|
|
|
Messages larger than the ``websocket_max_message_size`` application setting
|
|
|
|
(default 10MiB) will not be accepted.
|
|
|
|
|
|
|
|
.. versionchanged:: 4.5
|
|
|
|
Added ``websocket_ping_interval``, ``websocket_ping_timeout``, and
|
|
|
|
``websocket_max_message_size``.
|
2014-06-11 08:34:28 +00:00
|
|
|
"""
|
|
|
|
def __init__(self, application, request, **kwargs):
|
2015-12-23 04:15:36 +00:00
|
|
|
super(WebSocketHandler, self).__init__(application, request, **kwargs)
|
2014-06-11 08:34:28 +00:00
|
|
|
self.ws_connection = None
|
2014-06-17 04:54:00 +00:00
|
|
|
self.close_code = None
|
|
|
|
self.close_reason = None
|
|
|
|
self.stream = None
|
2015-04-27 19:06:19 +00:00
|
|
|
self._on_close_called = False
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2014-06-17 04:54:00 +00:00
|
|
|
@tornado.web.asynchronous
|
|
|
|
def get(self, *args, **kwargs):
|
2014-06-11 08:34:28 +00:00
|
|
|
self.open_args = args
|
|
|
|
self.open_kwargs = kwargs
|
|
|
|
|
|
|
|
# Upgrade header should be present and should be equal to WebSocket
|
|
|
|
if self.request.headers.get("Upgrade", "").lower() != 'websocket':
|
2014-06-17 04:54:00 +00:00
|
|
|
self.set_status(400)
|
2015-04-27 19:06:19 +00:00
|
|
|
log_msg = "Can \"Upgrade\" only to \"WebSocket\"."
|
|
|
|
self.finish(log_msg)
|
|
|
|
gen_log.debug(log_msg)
|
2014-06-11 08:34:28 +00:00
|
|
|
return
|
|
|
|
|
2015-04-27 19:06:19 +00:00
|
|
|
# Connection header should be upgrade.
|
|
|
|
# Some proxy servers/load balancers
|
2014-06-11 08:34:28 +00:00
|
|
|
# might mess with it.
|
|
|
|
headers = self.request.headers
|
2015-04-27 19:06:19 +00:00
|
|
|
connection = map(lambda s: s.strip().lower(),
|
|
|
|
headers.get("Connection", "").split(","))
|
2014-06-11 08:34:28 +00:00
|
|
|
if 'upgrade' not in connection:
|
2014-06-17 04:54:00 +00:00
|
|
|
self.set_status(400)
|
2015-04-27 19:06:19 +00:00
|
|
|
log_msg = "\"Connection\" must be \"Upgrade\"."
|
|
|
|
self.finish(log_msg)
|
|
|
|
gen_log.debug(log_msg)
|
2014-06-11 08:34:28 +00:00
|
|
|
return
|
|
|
|
|
2014-06-17 04:54:00 +00:00
|
|
|
# Handle WebSocket Origin naming convention differences
|
2014-06-11 08:34:28 +00:00
|
|
|
# The difference between version 8 and 13 is that in 8 the
|
|
|
|
# client sends a "Sec-Websocket-Origin" header and in 13 it's
|
|
|
|
# simply "Origin".
|
2014-06-17 04:54:00 +00:00
|
|
|
if "Origin" in self.request.headers:
|
|
|
|
origin = self.request.headers.get("Origin")
|
|
|
|
else:
|
|
|
|
origin = self.request.headers.get("Sec-Websocket-Origin", None)
|
|
|
|
|
|
|
|
# If there was an origin header, check to make sure it matches
|
|
|
|
# according to check_origin. When the origin is None, we assume it
|
|
|
|
# did not come from a browser and that it can be passed on.
|
|
|
|
if origin is not None and not self.check_origin(origin):
|
|
|
|
self.set_status(403)
|
2015-04-27 19:06:19 +00:00
|
|
|
log_msg = "Cross origin websockets not allowed"
|
|
|
|
self.finish(log_msg)
|
|
|
|
gen_log.debug(log_msg)
|
2014-06-17 04:54:00 +00:00
|
|
|
return
|
|
|
|
|
2015-04-27 19:06:19 +00:00
|
|
|
self.ws_connection = self.get_websocket_protocol()
|
|
|
|
if self.ws_connection:
|
2014-06-11 08:34:28 +00:00
|
|
|
self.ws_connection.accept_connection()
|
|
|
|
else:
|
2017-08-07 16:00:36 +00:00
|
|
|
self.set_status(426, "Upgrade Required")
|
|
|
|
self.set_header("Sec-WebSocket-Version", "7, 8, 13")
|
|
|
|
self.finish()
|
|
|
|
|
|
|
|
stream = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ping_interval(self):
|
|
|
|
"""The interval for websocket keep-alive pings.
|
|
|
|
|
|
|
|
Set websocket_ping_interval = 0 to disable pings.
|
|
|
|
"""
|
|
|
|
return self.settings.get('websocket_ping_interval', None)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ping_timeout(self):
|
|
|
|
"""If no ping is received in this many seconds,
|
|
|
|
close the websocket connection (VPNs, etc. can fail to cleanly close ws connections).
|
|
|
|
Default is max of 3 pings or 30 seconds.
|
|
|
|
"""
|
|
|
|
return self.settings.get('websocket_ping_timeout', None)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_message_size(self):
|
|
|
|
"""Maximum allowed message size.
|
|
|
|
|
|
|
|
If the remote peer sends a message larger than this, the connection
|
|
|
|
will be closed.
|
|
|
|
|
|
|
|
Default is 10MiB.
|
|
|
|
"""
|
|
|
|
return self.settings.get('websocket_max_message_size', None)
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
def write_message(self, message, binary=False):
|
|
|
|
"""Sends the given message to the client of this Web Socket.
|
|
|
|
|
|
|
|
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 the connection is already closed, raises `WebSocketClosedError`.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.2
|
|
|
|
`WebSocketClosedError` was added (previously a closed connection
|
|
|
|
would raise an `AttributeError`)
|
2015-12-23 04:15:36 +00:00
|
|
|
|
|
|
|
.. versionchanged:: 4.3
|
|
|
|
Returns a `.Future` which can be used for flow control.
|
2014-06-11 08:34:28 +00:00
|
|
|
"""
|
|
|
|
if self.ws_connection is None:
|
|
|
|
raise WebSocketClosedError()
|
|
|
|
if isinstance(message, dict):
|
|
|
|
message = tornado.escape.json_encode(message)
|
2015-12-23 04:15:36 +00:00
|
|
|
return self.ws_connection.write_message(message, binary=binary)
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
def select_subprotocol(self, subprotocols):
|
|
|
|
"""Invoked when a new WebSocket requests specific subprotocols.
|
|
|
|
|
|
|
|
``subprotocols`` is a list of strings identifying the
|
|
|
|
subprotocols proposed by the client. This method may be
|
|
|
|
overridden to return one of those strings to select it, or
|
|
|
|
``None`` to not select a subprotocol. Failure to select a
|
|
|
|
subprotocol does not automatically abort the connection,
|
|
|
|
although clients may close the connection if none of their
|
|
|
|
proposed subprotocols was selected.
|
|
|
|
"""
|
|
|
|
return None
|
|
|
|
|
2014-10-14 04:24:01 +00:00
|
|
|
def get_compression_options(self):
|
|
|
|
"""Override to return compression options for the connection.
|
|
|
|
|
|
|
|
If this method returns None (the default), compression will
|
|
|
|
be disabled. If it returns a dict (even an empty one), it
|
|
|
|
will be enabled. The contents of the dict may be used to
|
2017-08-07 16:00:36 +00:00
|
|
|
control the following compression options:
|
|
|
|
|
|
|
|
``compression_level`` specifies the compression level.
|
|
|
|
|
|
|
|
``mem_level`` specifies the amount of memory used for the internal compression state.
|
|
|
|
|
|
|
|
These parameters are documented in details here:
|
|
|
|
https://docs.python.org/3.6/library/zlib.html#zlib.compressobj
|
2014-10-14 04:24:01 +00:00
|
|
|
|
|
|
|
.. versionadded:: 4.1
|
2017-08-07 16:00:36 +00:00
|
|
|
|
|
|
|
.. versionchanged:: 4.5
|
|
|
|
|
|
|
|
Added ``compression_level`` and ``mem_level``.
|
2014-10-14 04:24:01 +00:00
|
|
|
"""
|
2017-08-07 16:00:36 +00:00
|
|
|
# TODO: Add wbits option.
|
2014-10-14 04:24:01 +00:00
|
|
|
return None
|
|
|
|
|
2015-04-27 19:06:19 +00:00
|
|
|
def open(self, *args, **kwargs):
|
2014-06-11 08:34:28 +00:00
|
|
|
"""Invoked when a new WebSocket is opened.
|
|
|
|
|
|
|
|
The arguments to `open` are extracted from the `tornado.web.URLSpec`
|
|
|
|
regular expression, just like the arguments to
|
|
|
|
`tornado.web.RequestHandler.get`.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def on_message(self, message):
|
|
|
|
"""Handle incoming messages on the WebSocket
|
|
|
|
|
|
|
|
This method must be overridden.
|
2017-08-07 16:00:36 +00:00
|
|
|
|
|
|
|
.. versionchanged:: 4.5
|
|
|
|
|
|
|
|
``on_message`` can be a coroutine.
|
2014-06-11 08:34:28 +00:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def ping(self, data):
|
|
|
|
"""Send ping frame to the remote end."""
|
|
|
|
if self.ws_connection is None:
|
|
|
|
raise WebSocketClosedError()
|
|
|
|
self.ws_connection.write_ping(data)
|
|
|
|
|
|
|
|
def on_pong(self, data):
|
|
|
|
"""Invoked when the response to a ping frame is received."""
|
|
|
|
pass
|
|
|
|
|
2017-08-07 16:00:36 +00:00
|
|
|
def on_ping(self, data):
|
|
|
|
"""Invoked when the a ping frame is received."""
|
|
|
|
pass
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
def on_close(self):
|
2014-06-17 04:54:00 +00:00
|
|
|
"""Invoked when the WebSocket is closed.
|
|
|
|
|
|
|
|
If the connection was closed cleanly and a status code or reason
|
|
|
|
phrase was supplied, these values will be available as the attributes
|
|
|
|
``self.close_code`` and ``self.close_reason``.
|
|
|
|
|
|
|
|
.. versionchanged:: 4.0
|
|
|
|
|
|
|
|
Added ``close_code`` and ``close_reason`` attributes.
|
|
|
|
"""
|
2014-06-11 08:34:28 +00:00
|
|
|
pass
|
|
|
|
|
2014-06-17 04:54:00 +00:00
|
|
|
def close(self, code=None, reason=None):
|
2014-06-11 08:34:28 +00:00
|
|
|
"""Closes this Web Socket.
|
|
|
|
|
|
|
|
Once the close handshake is successful the socket will be closed.
|
2014-06-17 04:54:00 +00:00
|
|
|
|
|
|
|
``code`` may be a numeric status code, taken from the values
|
|
|
|
defined in `RFC 6455 section 7.4.1
|
|
|
|
<https://tools.ietf.org/html/rfc6455#section-7.4.1>`_.
|
|
|
|
``reason`` may be a textual message about why the connection is
|
|
|
|
closing. These values are made available to the client, but are
|
|
|
|
not otherwise interpreted by the websocket protocol.
|
|
|
|
|
|
|
|
.. versionchanged:: 4.0
|
|
|
|
|
|
|
|
Added the ``code`` and ``reason`` arguments.
|
2014-06-11 08:34:28 +00:00
|
|
|
"""
|
|
|
|
if self.ws_connection:
|
2014-06-17 04:54:00 +00:00
|
|
|
self.ws_connection.close(code, reason)
|
2014-06-11 08:34:28 +00:00
|
|
|
self.ws_connection = None
|
|
|
|
|
2014-06-17 04:54:00 +00:00
|
|
|
def check_origin(self, origin):
|
|
|
|
"""Override to enable support for allowing alternate origins.
|
|
|
|
|
|
|
|
The ``origin`` argument is the value of the ``Origin`` HTTP
|
|
|
|
header, the url responsible for initiating this request. This
|
|
|
|
method is not called for clients that do not send this header;
|
|
|
|
such requests are always allowed (because all browsers that
|
|
|
|
implement WebSockets support this header, and non-browser
|
|
|
|
clients do not have the same cross-site security concerns).
|
|
|
|
|
|
|
|
Should return True to accept the request or False to reject it.
|
|
|
|
By default, rejects all requests with an origin on a host other
|
|
|
|
than this one.
|
|
|
|
|
|
|
|
This is a security protection against cross site scripting attacks on
|
|
|
|
browsers, since WebSockets are allowed to bypass the usual same-origin
|
|
|
|
policies and don't use CORS headers.
|
|
|
|
|
2016-09-30 22:40:12 +00:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
This is an important security measure; don't disable it
|
|
|
|
without understanding the security implications. In
|
2017-08-07 16:00:36 +00:00
|
|
|
particular, if your authentication is cookie-based, you
|
2016-09-30 22:40:12 +00:00
|
|
|
must either restrict the origins allowed by
|
|
|
|
``check_origin()`` or implement your own XSRF-like
|
|
|
|
protection for websocket connections. See `these
|
|
|
|
<https://www.christian-schneider.net/CrossSiteWebSocketHijacking.html>`_
|
|
|
|
`articles
|
|
|
|
<https://devcenter.heroku.com/articles/websocket-security>`_
|
|
|
|
for more.
|
|
|
|
|
2014-10-14 04:24:01 +00:00
|
|
|
To accept all cross-origin traffic (which was the default prior to
|
|
|
|
Tornado 4.0), simply override this method to always return true::
|
|
|
|
|
|
|
|
def check_origin(self, origin):
|
|
|
|
return True
|
|
|
|
|
|
|
|
To allow connections from any subdomain of your site, you might
|
|
|
|
do something like::
|
|
|
|
|
|
|
|
def check_origin(self, origin):
|
|
|
|
parsed_origin = urllib.parse.urlparse(origin)
|
|
|
|
return parsed_origin.netloc.endswith(".mydomain.com")
|
|
|
|
|
2014-06-17 04:54:00 +00:00
|
|
|
.. versionadded:: 4.0
|
2016-09-30 22:40:12 +00:00
|
|
|
|
2014-06-17 04:54:00 +00:00
|
|
|
"""
|
|
|
|
parsed_origin = urlparse(origin)
|
|
|
|
origin = parsed_origin.netloc
|
|
|
|
origin = origin.lower()
|
|
|
|
|
|
|
|
host = self.request.headers.get("Host")
|
|
|
|
|
|
|
|
# Check to see that origin matches host directly, including ports
|
|
|
|
return origin == host
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
def set_nodelay(self, value):
|
|
|
|
"""Set the no-delay flag for this stream.
|
|
|
|
|
|
|
|
By default, small messages may be delayed and/or combined to minimize
|
|
|
|
the number of packets sent. This can sometimes cause 200-500ms delays
|
|
|
|
due to the interaction between Nagle's algorithm and TCP delayed
|
|
|
|
ACKs. To reduce this delay (at the expense of possibly increasing
|
|
|
|
bandwidth usage), call ``self.set_nodelay(True)`` once the websocket
|
|
|
|
connection is established.
|
|
|
|
|
|
|
|
See `.BaseIOStream.set_nodelay` for additional details.
|
|
|
|
|
|
|
|
.. versionadded:: 3.1
|
|
|
|
"""
|
|
|
|
self.stream.set_nodelay(value)
|
|
|
|
|
|
|
|
def on_connection_close(self):
|
|
|
|
if self.ws_connection:
|
|
|
|
self.ws_connection.on_connection_close()
|
|
|
|
self.ws_connection = None
|
2015-04-27 19:06:19 +00:00
|
|
|
if not self._on_close_called:
|
|
|
|
self._on_close_called = True
|
2014-06-11 08:34:28 +00:00
|
|
|
self.on_close()
|
2017-08-07 16:00:36 +00:00
|
|
|
self._break_cycles()
|
|
|
|
|
|
|
|
def _break_cycles(self):
|
|
|
|
# WebSocketHandlers call finish() early, but we don't want to
|
|
|
|
# break up reference cycles (which makes it impossible to call
|
|
|
|
# self.render_string) until after we've really closed the
|
|
|
|
# connection (if it was established in the first place,
|
|
|
|
# indicated by status code 101).
|
|
|
|
if self.get_status() != 101 or self._on_close_called:
|
|
|
|
super(WebSocketHandler, self)._break_cycles()
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2014-10-14 04:24:01 +00:00
|
|
|
def send_error(self, *args, **kwargs):
|
|
|
|
if self.stream is None:
|
|
|
|
super(WebSocketHandler, self).send_error(*args, **kwargs)
|
|
|
|
else:
|
|
|
|
# If we get an uncaught exception during the handshake,
|
|
|
|
# we have no choice but to abruptly close the connection.
|
|
|
|
# TODO: for uncaught exceptions after the handshake,
|
|
|
|
# we can close the connection more gracefully.
|
|
|
|
self.stream.close()
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2015-04-27 19:06:19 +00:00
|
|
|
def get_websocket_protocol(self):
|
|
|
|
websocket_version = self.request.headers.get("Sec-WebSocket-Version")
|
|
|
|
if websocket_version in ("7", "8", "13"):
|
|
|
|
return WebSocketProtocol13(
|
|
|
|
self, compression_options=self.get_compression_options())
|
|
|
|
|
2017-08-07 16:00:36 +00:00
|
|
|
def _attach_stream(self):
|
|
|
|
self.stream = self.request.connection.detach()
|
|
|
|
self.stream.set_close_callback(self.on_connection_close)
|
|
|
|
# disable non-WS methods
|
|
|
|
for method in ["write", "redirect", "set_header", "set_cookie",
|
|
|
|
"set_status", "flush", "finish"]:
|
|
|
|
setattr(self, method, _raise_not_supported_for_websockets)
|
2015-04-27 19:06:19 +00:00
|
|
|
|
2017-08-07 16:00:36 +00:00
|
|
|
|
|
|
|
def _raise_not_supported_for_websockets(*args, **kwargs):
|
|
|
|
raise RuntimeError("Method not supported for Web Sockets")
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WebSocketProtocol(object):
|
|
|
|
"""Base class for WebSocket protocol versions.
|
|
|
|
"""
|
|
|
|
def __init__(self, handler):
|
|
|
|
self.handler = handler
|
|
|
|
self.request = handler.request
|
|
|
|
self.stream = handler.stream
|
|
|
|
self.client_terminated = False
|
|
|
|
self.server_terminated = False
|
|
|
|
|
2014-06-17 04:54:00 +00:00
|
|
|
def _run_callback(self, callback, *args, **kwargs):
|
|
|
|
"""Runs the given callback with exception handling.
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2017-08-07 16:00:36 +00:00
|
|
|
If the callback is a coroutine, returns its Future. On error, aborts the
|
|
|
|
websocket connection and returns None.
|
2014-06-11 08:34:28 +00:00
|
|
|
"""
|
2014-06-17 04:54:00 +00:00
|
|
|
try:
|
2017-08-07 16:00:36 +00:00
|
|
|
result = callback(*args, **kwargs)
|
2014-06-17 04:54:00 +00:00
|
|
|
except Exception:
|
|
|
|
app_log.error("Uncaught exception in %s",
|
2017-08-07 16:00:36 +00:00
|
|
|
getattr(self.request, 'path', None), exc_info=True)
|
2014-06-17 04:54:00 +00:00
|
|
|
self._abort()
|
2017-08-07 16:00:36 +00:00
|
|
|
else:
|
|
|
|
if result is not None:
|
|
|
|
result = gen.convert_yielded(result)
|
|
|
|
self.stream.io_loop.add_future(result, lambda f: f.result())
|
|
|
|
return result
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
def on_connection_close(self):
|
|
|
|
self._abort()
|
|
|
|
|
|
|
|
def _abort(self):
|
|
|
|
"""Instantly aborts the WebSocket connection by closing the socket"""
|
|
|
|
self.client_terminated = True
|
|
|
|
self.server_terminated = True
|
|
|
|
self.stream.close() # forcibly tear down the connection
|
|
|
|
self.close() # let the subclass cleanup
|
|
|
|
|
|
|
|
|
2014-10-14 04:24:01 +00:00
|
|
|
class _PerMessageDeflateCompressor(object):
|
2017-08-07 16:00:36 +00:00
|
|
|
def __init__(self, persistent, max_wbits, compression_options=None):
|
2014-10-14 04:24:01 +00:00
|
|
|
if max_wbits is None:
|
|
|
|
max_wbits = zlib.MAX_WBITS
|
|
|
|
# There is no symbolic constant for the minimum wbits value.
|
|
|
|
if not (8 <= max_wbits <= zlib.MAX_WBITS):
|
|
|
|
raise ValueError("Invalid max_wbits value %r; allowed range 8-%d",
|
|
|
|
max_wbits, zlib.MAX_WBITS)
|
|
|
|
self._max_wbits = max_wbits
|
2017-08-07 16:00:36 +00:00
|
|
|
|
|
|
|
if compression_options is None or 'compression_level' not in compression_options:
|
|
|
|
self._compression_level = tornado.web.GZipContentEncoding.GZIP_LEVEL
|
|
|
|
else:
|
|
|
|
self._compression_level = compression_options['compression_level']
|
|
|
|
|
|
|
|
if compression_options is None or 'mem_level' not in compression_options:
|
|
|
|
self._mem_level = 8
|
|
|
|
else:
|
|
|
|
self._mem_level = compression_options['mem_level']
|
|
|
|
|
2014-10-14 04:24:01 +00:00
|
|
|
if persistent:
|
|
|
|
self._compressor = self._create_compressor()
|
|
|
|
else:
|
|
|
|
self._compressor = None
|
|
|
|
|
|
|
|
def _create_compressor(self):
|
2017-08-07 16:00:36 +00:00
|
|
|
return zlib.compressobj(self._compression_level, zlib.DEFLATED, -self._max_wbits, self._mem_level)
|
2014-10-14 04:24:01 +00:00
|
|
|
|
|
|
|
def compress(self, data):
|
|
|
|
compressor = self._compressor or self._create_compressor()
|
|
|
|
data = (compressor.compress(data) +
|
|
|
|
compressor.flush(zlib.Z_SYNC_FLUSH))
|
|
|
|
assert data.endswith(b'\x00\x00\xff\xff')
|
|
|
|
return data[:-4]
|
|
|
|
|
|
|
|
|
|
|
|
class _PerMessageDeflateDecompressor(object):
|
2017-08-07 16:00:36 +00:00
|
|
|
def __init__(self, persistent, max_wbits, compression_options=None):
|
2014-10-14 04:24:01 +00:00
|
|
|
if max_wbits is None:
|
|
|
|
max_wbits = zlib.MAX_WBITS
|
|
|
|
if not (8 <= max_wbits <= zlib.MAX_WBITS):
|
|
|
|
raise ValueError("Invalid max_wbits value %r; allowed range 8-%d",
|
|
|
|
max_wbits, zlib.MAX_WBITS)
|
|
|
|
self._max_wbits = max_wbits
|
|
|
|
if persistent:
|
|
|
|
self._decompressor = self._create_decompressor()
|
|
|
|
else:
|
|
|
|
self._decompressor = None
|
|
|
|
|
|
|
|
def _create_decompressor(self):
|
|
|
|
return zlib.decompressobj(-self._max_wbits)
|
|
|
|
|
|
|
|
def decompress(self, data):
|
|
|
|
decompressor = self._decompressor or self._create_decompressor()
|
|
|
|
return decompressor.decompress(data + b'\x00\x00\xff\xff')
|
|
|
|
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
class WebSocketProtocol13(WebSocketProtocol):
|
|
|
|
"""Implementation of the WebSocket protocol from RFC 6455.
|
|
|
|
|
|
|
|
This class supports versions 7 and 8 of the protocol in addition to the
|
|
|
|
final version 13.
|
|
|
|
"""
|
2014-10-14 04:24:01 +00:00
|
|
|
# Bit masks for the first byte of a frame.
|
|
|
|
FIN = 0x80
|
|
|
|
RSV1 = 0x40
|
|
|
|
RSV2 = 0x20
|
|
|
|
RSV3 = 0x10
|
|
|
|
RSV_MASK = RSV1 | RSV2 | RSV3
|
|
|
|
OPCODE_MASK = 0x0f
|
|
|
|
|
|
|
|
def __init__(self, handler, mask_outgoing=False,
|
|
|
|
compression_options=None):
|
2014-06-11 08:34:28 +00:00
|
|
|
WebSocketProtocol.__init__(self, handler)
|
|
|
|
self.mask_outgoing = mask_outgoing
|
|
|
|
self._final_frame = False
|
|
|
|
self._frame_opcode = None
|
|
|
|
self._masked_frame = None
|
|
|
|
self._frame_mask = None
|
|
|
|
self._frame_length = None
|
|
|
|
self._fragmented_message_buffer = None
|
|
|
|
self._fragmented_message_opcode = None
|
|
|
|
self._waiting = None
|
2014-10-14 04:24:01 +00:00
|
|
|
self._compression_options = compression_options
|
|
|
|
self._decompressor = None
|
|
|
|
self._compressor = None
|
|
|
|
self._frame_compressed = None
|
|
|
|
# The total uncompressed size of all messages received or sent.
|
|
|
|
# Unicode messages are encoded to utf8.
|
|
|
|
# Only for testing; subject to change.
|
|
|
|
self._message_bytes_in = 0
|
|
|
|
self._message_bytes_out = 0
|
|
|
|
# The total size of all packets received or sent. Includes
|
|
|
|
# the effect of compression, frame overhead, and control frames.
|
|
|
|
self._wire_bytes_in = 0
|
|
|
|
self._wire_bytes_out = 0
|
2017-08-07 16:00:36 +00:00
|
|
|
self.ping_callback = None
|
|
|
|
self.last_ping = 0
|
|
|
|
self.last_pong = 0
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
def accept_connection(self):
|
|
|
|
try:
|
|
|
|
self._handle_websocket_headers()
|
|
|
|
self._accept_connection()
|
|
|
|
except ValueError:
|
2015-04-27 19:06:19 +00:00
|
|
|
gen_log.debug("Malformed WebSocket request received",
|
|
|
|
exc_info=True)
|
2014-06-11 08:34:28 +00:00
|
|
|
self._abort()
|
|
|
|
return
|
|
|
|
|
|
|
|
def _handle_websocket_headers(self):
|
|
|
|
"""Verifies all invariant- and required headers
|
|
|
|
|
|
|
|
If a header is missing or have an incorrect value ValueError will be
|
|
|
|
raised
|
|
|
|
"""
|
|
|
|
fields = ("Host", "Sec-Websocket-Key", "Sec-Websocket-Version")
|
|
|
|
if not all(map(lambda f: self.request.headers.get(f), fields)):
|
|
|
|
raise ValueError("Missing/Invalid WebSocket headers")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def compute_accept_value(key):
|
|
|
|
"""Computes the value for the Sec-WebSocket-Accept header,
|
|
|
|
given the value for Sec-WebSocket-Key.
|
|
|
|
"""
|
|
|
|
sha1 = hashlib.sha1()
|
|
|
|
sha1.update(utf8(key))
|
|
|
|
sha1.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11") # Magic value
|
|
|
|
return native_str(base64.b64encode(sha1.digest()))
|
|
|
|
|
|
|
|
def _challenge_response(self):
|
|
|
|
return WebSocketProtocol13.compute_accept_value(
|
|
|
|
self.request.headers.get("Sec-Websocket-Key"))
|
|
|
|
|
|
|
|
def _accept_connection(self):
|
|
|
|
subprotocols = self.request.headers.get("Sec-WebSocket-Protocol", '')
|
|
|
|
subprotocols = [s.strip() for s in subprotocols.split(',')]
|
|
|
|
if subprotocols:
|
|
|
|
selected = self.handler.select_subprotocol(subprotocols)
|
|
|
|
if selected:
|
|
|
|
assert selected in subprotocols
|
2017-08-07 16:00:36 +00:00
|
|
|
self.handler.set_header("Sec-WebSocket-Protocol", selected)
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2014-10-14 04:24:01 +00:00
|
|
|
extensions = self._parse_extensions_header(self.request.headers)
|
|
|
|
for ext in extensions:
|
|
|
|
if (ext[0] == 'permessage-deflate' and
|
2015-04-27 19:06:19 +00:00
|
|
|
self._compression_options is not None):
|
2014-10-14 04:24:01 +00:00
|
|
|
# TODO: negotiate parameters if compression_options
|
|
|
|
# specifies limits.
|
2017-08-07 16:00:36 +00:00
|
|
|
self._create_compressors('server', ext[1], self._compression_options)
|
2014-10-14 04:24:01 +00:00
|
|
|
if ('client_max_window_bits' in ext[1] and
|
2015-04-27 19:06:19 +00:00
|
|
|
ext[1]['client_max_window_bits'] is None):
|
2014-10-14 04:24:01 +00:00
|
|
|
# Don't echo an offered client_max_window_bits
|
|
|
|
# parameter with no value.
|
|
|
|
del ext[1]['client_max_window_bits']
|
2017-08-07 16:00:36 +00:00
|
|
|
self.handler.set_header("Sec-WebSocket-Extensions",
|
|
|
|
httputil._encode_header(
|
|
|
|
'permessage-deflate', ext[1]))
|
2014-10-14 04:24:01 +00:00
|
|
|
break
|
|
|
|
|
2017-08-07 16:00:36 +00:00
|
|
|
self.handler.clear_header("Content-Type")
|
|
|
|
self.handler.set_status(101)
|
|
|
|
self.handler.set_header("Upgrade", "websocket")
|
|
|
|
self.handler.set_header("Connection", "Upgrade")
|
|
|
|
self.handler.set_header("Sec-WebSocket-Accept", self._challenge_response())
|
|
|
|
self.handler.finish()
|
|
|
|
|
|
|
|
self.handler._attach_stream()
|
|
|
|
self.stream = self.handler.stream
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2017-08-07 16:00:36 +00:00
|
|
|
self.start_pinging()
|
2014-06-17 04:54:00 +00:00
|
|
|
self._run_callback(self.handler.open, *self.handler.open_args,
|
|
|
|
**self.handler.open_kwargs)
|
2014-06-11 08:34:28 +00:00
|
|
|
self._receive_frame()
|
|
|
|
|
2014-10-14 04:24:01 +00:00
|
|
|
def _parse_extensions_header(self, headers):
|
|
|
|
extensions = headers.get("Sec-WebSocket-Extensions", '')
|
|
|
|
if extensions:
|
|
|
|
return [httputil._parse_header(e.strip())
|
|
|
|
for e in extensions.split(',')]
|
|
|
|
return []
|
|
|
|
|
|
|
|
def _process_server_headers(self, key, headers):
|
|
|
|
"""Process the headers sent by the server to this client connection.
|
|
|
|
|
|
|
|
'key' is the websocket handshake challenge/response key.
|
|
|
|
"""
|
|
|
|
assert headers['Upgrade'].lower() == 'websocket'
|
|
|
|
assert headers['Connection'].lower() == 'upgrade'
|
|
|
|
accept = self.compute_accept_value(key)
|
|
|
|
assert headers['Sec-Websocket-Accept'] == accept
|
|
|
|
|
|
|
|
extensions = self._parse_extensions_header(headers)
|
|
|
|
for ext in extensions:
|
|
|
|
if (ext[0] == 'permessage-deflate' and
|
2015-04-27 19:06:19 +00:00
|
|
|
self._compression_options is not None):
|
2014-10-14 04:24:01 +00:00
|
|
|
self._create_compressors('client', ext[1])
|
|
|
|
else:
|
|
|
|
raise ValueError("unsupported extension %r", ext)
|
|
|
|
|
2017-08-07 16:00:36 +00:00
|
|
|
def _get_compressor_options(self, side, agreed_parameters, compression_options=None):
|
2014-10-14 04:24:01 +00:00
|
|
|
"""Converts a websocket agreed_parameters set to keyword arguments
|
|
|
|
for our compressor objects.
|
|
|
|
"""
|
|
|
|
options = dict(
|
|
|
|
persistent=(side + '_no_context_takeover') not in agreed_parameters)
|
|
|
|
wbits_header = agreed_parameters.get(side + '_max_window_bits', None)
|
|
|
|
if wbits_header is None:
|
|
|
|
options['max_wbits'] = zlib.MAX_WBITS
|
|
|
|
else:
|
|
|
|
options['max_wbits'] = int(wbits_header)
|
2017-08-07 16:00:36 +00:00
|
|
|
options['compression_options'] = compression_options
|
2014-10-14 04:24:01 +00:00
|
|
|
return options
|
|
|
|
|
2017-08-07 16:00:36 +00:00
|
|
|
def _create_compressors(self, side, agreed_parameters, compression_options=None):
|
2014-10-14 04:24:01 +00:00
|
|
|
# TODO: handle invalid parameters gracefully
|
|
|
|
allowed_keys = set(['server_no_context_takeover',
|
|
|
|
'client_no_context_takeover',
|
|
|
|
'server_max_window_bits',
|
|
|
|
'client_max_window_bits'])
|
|
|
|
for key in agreed_parameters:
|
|
|
|
if key not in allowed_keys:
|
|
|
|
raise ValueError("unsupported compression parameter %r" % key)
|
|
|
|
other_side = 'client' if (side == 'server') else 'server'
|
|
|
|
self._compressor = _PerMessageDeflateCompressor(
|
2017-08-07 16:00:36 +00:00
|
|
|
**self._get_compressor_options(side, agreed_parameters, compression_options))
|
2014-10-14 04:24:01 +00:00
|
|
|
self._decompressor = _PerMessageDeflateDecompressor(
|
2017-08-07 16:00:36 +00:00
|
|
|
**self._get_compressor_options(other_side, agreed_parameters, compression_options))
|
2014-10-14 04:24:01 +00:00
|
|
|
|
|
|
|
def _write_frame(self, fin, opcode, data, flags=0):
|
2014-06-11 08:34:28 +00:00
|
|
|
if fin:
|
2014-10-14 04:24:01 +00:00
|
|
|
finbit = self.FIN
|
2014-06-11 08:34:28 +00:00
|
|
|
else:
|
|
|
|
finbit = 0
|
2014-10-14 04:24:01 +00:00
|
|
|
frame = struct.pack("B", finbit | opcode | flags)
|
2014-06-11 08:34:28 +00:00
|
|
|
l = len(data)
|
|
|
|
if self.mask_outgoing:
|
|
|
|
mask_bit = 0x80
|
|
|
|
else:
|
|
|
|
mask_bit = 0
|
|
|
|
if l < 126:
|
|
|
|
frame += struct.pack("B", l | mask_bit)
|
|
|
|
elif l <= 0xFFFF:
|
|
|
|
frame += struct.pack("!BH", 126 | mask_bit, l)
|
|
|
|
else:
|
|
|
|
frame += struct.pack("!BQ", 127 | mask_bit, l)
|
|
|
|
if self.mask_outgoing:
|
|
|
|
mask = os.urandom(4)
|
|
|
|
data = mask + _websocket_mask(mask, data)
|
|
|
|
frame += data
|
2014-10-14 04:24:01 +00:00
|
|
|
self._wire_bytes_out += len(frame)
|
|
|
|
try:
|
2015-12-23 04:15:36 +00:00
|
|
|
return self.stream.write(frame)
|
2014-10-14 04:24:01 +00:00
|
|
|
except StreamClosedError:
|
|
|
|
self._abort()
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
def write_message(self, message, binary=False):
|
|
|
|
"""Sends the given message to the client of this Web Socket."""
|
|
|
|
if binary:
|
|
|
|
opcode = 0x2
|
|
|
|
else:
|
|
|
|
opcode = 0x1
|
|
|
|
message = tornado.escape.utf8(message)
|
2014-10-14 04:24:01 +00:00
|
|
|
assert isinstance(message, bytes)
|
|
|
|
self._message_bytes_out += len(message)
|
|
|
|
flags = 0
|
|
|
|
if self._compressor:
|
|
|
|
message = self._compressor.compress(message)
|
|
|
|
flags |= self.RSV1
|
2015-12-23 04:15:36 +00:00
|
|
|
return self._write_frame(True, opcode, message, flags=flags)
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
def write_ping(self, data):
|
|
|
|
"""Send ping frame."""
|
2014-10-14 04:24:01 +00:00
|
|
|
assert isinstance(data, bytes)
|
2014-06-11 08:34:28 +00:00
|
|
|
self._write_frame(True, 0x9, data)
|
|
|
|
|
|
|
|
def _receive_frame(self):
|
|
|
|
try:
|
|
|
|
self.stream.read_bytes(2, self._on_frame_start)
|
|
|
|
except StreamClosedError:
|
|
|
|
self._abort()
|
|
|
|
|
|
|
|
def _on_frame_start(self, data):
|
2014-10-14 04:24:01 +00:00
|
|
|
self._wire_bytes_in += len(data)
|
2014-06-11 08:34:28 +00:00
|
|
|
header, payloadlen = struct.unpack("BB", data)
|
2014-10-14 04:24:01 +00:00
|
|
|
self._final_frame = header & self.FIN
|
|
|
|
reserved_bits = header & self.RSV_MASK
|
|
|
|
self._frame_opcode = header & self.OPCODE_MASK
|
2014-06-11 08:34:28 +00:00
|
|
|
self._frame_opcode_is_control = self._frame_opcode & 0x8
|
2015-12-23 04:15:36 +00:00
|
|
|
if self._decompressor is not None and self._frame_opcode != 0:
|
2014-10-14 04:24:01 +00:00
|
|
|
self._frame_compressed = bool(reserved_bits & self.RSV1)
|
|
|
|
reserved_bits &= ~self.RSV1
|
2014-06-11 08:34:28 +00:00
|
|
|
if reserved_bits:
|
|
|
|
# client is using as-yet-undefined extensions; abort
|
|
|
|
self._abort()
|
|
|
|
return
|
|
|
|
self._masked_frame = bool(payloadlen & 0x80)
|
|
|
|
payloadlen = payloadlen & 0x7f
|
|
|
|
if self._frame_opcode_is_control and payloadlen >= 126:
|
|
|
|
# control frames must have payload < 126
|
|
|
|
self._abort()
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
if payloadlen < 126:
|
|
|
|
self._frame_length = payloadlen
|
|
|
|
if self._masked_frame:
|
|
|
|
self.stream.read_bytes(4, self._on_masking_key)
|
|
|
|
else:
|
2017-08-07 16:00:36 +00:00
|
|
|
self._read_frame_data(False)
|
2014-06-11 08:34:28 +00:00
|
|
|
elif payloadlen == 126:
|
|
|
|
self.stream.read_bytes(2, self._on_frame_length_16)
|
|
|
|
elif payloadlen == 127:
|
|
|
|
self.stream.read_bytes(8, self._on_frame_length_64)
|
|
|
|
except StreamClosedError:
|
|
|
|
self._abort()
|
|
|
|
|
2017-08-07 16:00:36 +00:00
|
|
|
def _read_frame_data(self, masked):
|
|
|
|
new_len = self._frame_length
|
|
|
|
if self._fragmented_message_buffer is not None:
|
|
|
|
new_len += len(self._fragmented_message_buffer)
|
|
|
|
if new_len > (self.handler.max_message_size or 10 * 1024 * 1024):
|
|
|
|
self.close(1009, "message too big")
|
|
|
|
return
|
|
|
|
self.stream.read_bytes(
|
|
|
|
self._frame_length,
|
|
|
|
self._on_masked_frame_data if masked else self._on_frame_data)
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
def _on_frame_length_16(self, data):
|
2014-10-14 04:24:01 +00:00
|
|
|
self._wire_bytes_in += len(data)
|
2014-06-11 08:34:28 +00:00
|
|
|
self._frame_length = struct.unpack("!H", data)[0]
|
|
|
|
try:
|
|
|
|
if self._masked_frame:
|
|
|
|
self.stream.read_bytes(4, self._on_masking_key)
|
|
|
|
else:
|
2017-08-07 16:00:36 +00:00
|
|
|
self._read_frame_data(False)
|
2014-06-11 08:34:28 +00:00
|
|
|
except StreamClosedError:
|
|
|
|
self._abort()
|
|
|
|
|
|
|
|
def _on_frame_length_64(self, data):
|
2014-10-14 04:24:01 +00:00
|
|
|
self._wire_bytes_in += len(data)
|
2014-06-11 08:34:28 +00:00
|
|
|
self._frame_length = struct.unpack("!Q", data)[0]
|
|
|
|
try:
|
|
|
|
if self._masked_frame:
|
|
|
|
self.stream.read_bytes(4, self._on_masking_key)
|
|
|
|
else:
|
2017-08-07 16:00:36 +00:00
|
|
|
self._read_frame_data(False)
|
2014-06-11 08:34:28 +00:00
|
|
|
except StreamClosedError:
|
|
|
|
self._abort()
|
|
|
|
|
|
|
|
def _on_masking_key(self, data):
|
2014-10-14 04:24:01 +00:00
|
|
|
self._wire_bytes_in += len(data)
|
2014-06-11 08:34:28 +00:00
|
|
|
self._frame_mask = data
|
|
|
|
try:
|
2017-08-07 16:00:36 +00:00
|
|
|
self._read_frame_data(True)
|
2014-06-11 08:34:28 +00:00
|
|
|
except StreamClosedError:
|
|
|
|
self._abort()
|
|
|
|
|
|
|
|
def _on_masked_frame_data(self, data):
|
2014-10-14 04:24:01 +00:00
|
|
|
# Don't touch _wire_bytes_in; we'll do it in _on_frame_data.
|
2014-06-11 08:34:28 +00:00
|
|
|
self._on_frame_data(_websocket_mask(self._frame_mask, data))
|
|
|
|
|
|
|
|
def _on_frame_data(self, data):
|
2017-08-07 16:00:36 +00:00
|
|
|
handled_future = None
|
|
|
|
|
2014-10-14 04:24:01 +00:00
|
|
|
self._wire_bytes_in += len(data)
|
2014-06-11 08:34:28 +00:00
|
|
|
if self._frame_opcode_is_control:
|
|
|
|
# control frames may be interleaved with a series of fragmented
|
|
|
|
# data frames, so control frames must not interact with
|
|
|
|
# self._fragmented_*
|
|
|
|
if not self._final_frame:
|
|
|
|
# control frames must not be fragmented
|
|
|
|
self._abort()
|
|
|
|
return
|
|
|
|
opcode = self._frame_opcode
|
|
|
|
elif self._frame_opcode == 0: # continuation frame
|
|
|
|
if self._fragmented_message_buffer is None:
|
|
|
|
# nothing to continue
|
|
|
|
self._abort()
|
|
|
|
return
|
|
|
|
self._fragmented_message_buffer += data
|
|
|
|
if self._final_frame:
|
|
|
|
opcode = self._fragmented_message_opcode
|
|
|
|
data = self._fragmented_message_buffer
|
|
|
|
self._fragmented_message_buffer = None
|
|
|
|
else: # start of new data message
|
|
|
|
if self._fragmented_message_buffer is not None:
|
|
|
|
# can't start new message until the old one is finished
|
|
|
|
self._abort()
|
|
|
|
return
|
|
|
|
if self._final_frame:
|
|
|
|
opcode = self._frame_opcode
|
|
|
|
else:
|
|
|
|
self._fragmented_message_opcode = self._frame_opcode
|
|
|
|
self._fragmented_message_buffer = data
|
|
|
|
|
|
|
|
if self._final_frame:
|
2017-08-07 16:00:36 +00:00
|
|
|
handled_future = self._handle_message(opcode, data)
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
if not self.client_terminated:
|
2017-08-07 16:00:36 +00:00
|
|
|
if handled_future:
|
|
|
|
# on_message is a coroutine, process more frames once it's done.
|
|
|
|
handled_future.add_done_callback(
|
|
|
|
lambda future: self._receive_frame())
|
|
|
|
else:
|
|
|
|
self._receive_frame()
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
def _handle_message(self, opcode, data):
|
2017-08-07 16:00:36 +00:00
|
|
|
"""Execute on_message, returning its Future if it is a coroutine."""
|
2014-06-11 08:34:28 +00:00
|
|
|
if self.client_terminated:
|
|
|
|
return
|
|
|
|
|
2014-10-14 04:24:01 +00:00
|
|
|
if self._frame_compressed:
|
|
|
|
data = self._decompressor.decompress(data)
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
if opcode == 0x1:
|
|
|
|
# UTF-8 data
|
2014-10-14 04:24:01 +00:00
|
|
|
self._message_bytes_in += len(data)
|
2014-06-11 08:34:28 +00:00
|
|
|
try:
|
|
|
|
decoded = data.decode("utf-8")
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
self._abort()
|
|
|
|
return
|
2017-08-07 16:00:36 +00:00
|
|
|
return self._run_callback(self.handler.on_message, decoded)
|
2014-06-11 08:34:28 +00:00
|
|
|
elif opcode == 0x2:
|
|
|
|
# Binary data
|
2014-10-14 04:24:01 +00:00
|
|
|
self._message_bytes_in += len(data)
|
2017-08-07 16:00:36 +00:00
|
|
|
return self._run_callback(self.handler.on_message, data)
|
2014-06-11 08:34:28 +00:00
|
|
|
elif opcode == 0x8:
|
|
|
|
# Close
|
|
|
|
self.client_terminated = True
|
2014-06-17 04:54:00 +00:00
|
|
|
if len(data) >= 2:
|
|
|
|
self.handler.close_code = struct.unpack('>H', data[:2])[0]
|
|
|
|
if len(data) > 2:
|
|
|
|
self.handler.close_reason = to_unicode(data[2:])
|
2015-05-18 18:08:55 +00:00
|
|
|
# Echo the received close code, if any (RFC 6455 section 5.5.1).
|
|
|
|
self.close(self.handler.close_code)
|
2014-06-11 08:34:28 +00:00
|
|
|
elif opcode == 0x9:
|
|
|
|
# Ping
|
|
|
|
self._write_frame(True, 0xA, data)
|
2017-08-07 16:00:36 +00:00
|
|
|
self._run_callback(self.handler.on_ping, data)
|
2014-06-11 08:34:28 +00:00
|
|
|
elif opcode == 0xA:
|
|
|
|
# Pong
|
2017-08-07 16:00:36 +00:00
|
|
|
self.last_pong = IOLoop.current().time()
|
|
|
|
return self._run_callback(self.handler.on_pong, data)
|
2014-06-11 08:34:28 +00:00
|
|
|
else:
|
|
|
|
self._abort()
|
|
|
|
|
2014-06-17 04:54:00 +00:00
|
|
|
def close(self, code=None, reason=None):
|
2014-06-11 08:34:28 +00:00
|
|
|
"""Closes the WebSocket connection."""
|
|
|
|
if not self.server_terminated:
|
|
|
|
if not self.stream.closed():
|
2014-06-17 04:54:00 +00:00
|
|
|
if code is None and reason is not None:
|
|
|
|
code = 1000 # "normal closure" status code
|
|
|
|
if code is None:
|
|
|
|
close_data = b''
|
|
|
|
else:
|
|
|
|
close_data = struct.pack('>H', code)
|
|
|
|
if reason is not None:
|
|
|
|
close_data += utf8(reason)
|
|
|
|
self._write_frame(True, 0x8, close_data)
|
2014-06-11 08:34:28 +00:00
|
|
|
self.server_terminated = True
|
|
|
|
if self.client_terminated:
|
|
|
|
if self._waiting is not None:
|
|
|
|
self.stream.io_loop.remove_timeout(self._waiting)
|
|
|
|
self._waiting = None
|
|
|
|
self.stream.close()
|
|
|
|
elif self._waiting is None:
|
|
|
|
# Give the client a few seconds to complete a clean shutdown,
|
|
|
|
# otherwise just close the connection.
|
|
|
|
self._waiting = self.stream.io_loop.add_timeout(
|
|
|
|
self.stream.io_loop.time() + 5, self._abort)
|
|
|
|
|
2017-08-07 16:00:36 +00:00
|
|
|
@property
|
|
|
|
def ping_interval(self):
|
|
|
|
interval = self.handler.ping_interval
|
|
|
|
if interval is not None:
|
|
|
|
return interval
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ping_timeout(self):
|
|
|
|
timeout = self.handler.ping_timeout
|
|
|
|
if timeout is not None:
|
|
|
|
return timeout
|
|
|
|
return max(3 * self.ping_interval, 30)
|
|
|
|
|
|
|
|
def start_pinging(self):
|
|
|
|
"""Start sending periodic pings to keep the connection alive"""
|
|
|
|
if self.ping_interval > 0:
|
|
|
|
self.last_ping = self.last_pong = IOLoop.current().time()
|
|
|
|
self.ping_callback = PeriodicCallback(
|
|
|
|
self.periodic_ping, self.ping_interval * 1000)
|
|
|
|
self.ping_callback.start()
|
|
|
|
|
|
|
|
def periodic_ping(self):
|
|
|
|
"""Send a ping to keep the websocket alive
|
|
|
|
|
|
|
|
Called periodically if the websocket_ping_interval is set and non-zero.
|
|
|
|
"""
|
|
|
|
if self.stream.closed() and self.ping_callback is not None:
|
|
|
|
self.ping_callback.stop()
|
|
|
|
return
|
|
|
|
|
|
|
|
# Check for timeout on pong. Make sure that we really have
|
|
|
|
# sent a recent ping in case the machine with both server and
|
|
|
|
# client has been suspended since the last ping.
|
|
|
|
now = IOLoop.current().time()
|
|
|
|
since_last_pong = now - self.last_pong
|
|
|
|
since_last_ping = now - self.last_ping
|
|
|
|
if (since_last_ping < 2 * self.ping_interval and
|
|
|
|
since_last_pong > self.ping_timeout):
|
|
|
|
self.close()
|
|
|
|
return
|
|
|
|
|
|
|
|
self.write_ping(b'')
|
|
|
|
self.last_ping = now
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
class WebSocketClientConnection(simple_httpclient._HTTPConnection):
|
|
|
|
"""WebSocket client connection.
|
|
|
|
|
|
|
|
This class should not be instantiated directly; use the
|
|
|
|
`websocket_connect` function instead.
|
|
|
|
"""
|
2015-04-27 19:06:19 +00:00
|
|
|
def __init__(self, io_loop, request, on_message_callback=None,
|
2017-08-07 16:00:36 +00:00
|
|
|
compression_options=None, ping_interval=None, ping_timeout=None,
|
|
|
|
max_message_size=None):
|
2014-10-14 04:24:01 +00:00
|
|
|
self.compression_options = compression_options
|
2014-06-11 08:34:28 +00:00
|
|
|
self.connect_future = TracebackFuture()
|
2015-04-27 19:06:19 +00:00
|
|
|
self.protocol = None
|
2014-06-11 08:34:28 +00:00
|
|
|
self.read_future = None
|
|
|
|
self.read_queue = collections.deque()
|
|
|
|
self.key = base64.b64encode(os.urandom(16))
|
2015-04-27 19:06:19 +00:00
|
|
|
self._on_message_callback = on_message_callback
|
2015-05-18 18:08:55 +00:00
|
|
|
self.close_code = self.close_reason = None
|
2017-08-07 16:00:36 +00:00
|
|
|
self.ping_interval = ping_interval
|
|
|
|
self.ping_timeout = ping_timeout
|
|
|
|
self.max_message_size = max_message_size
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
scheme, sep, rest = request.url.partition(':')
|
|
|
|
scheme = {'ws': 'http', 'wss': 'https'}[scheme]
|
|
|
|
request.url = scheme + sep + rest
|
|
|
|
request.headers.update({
|
|
|
|
'Upgrade': 'websocket',
|
|
|
|
'Connection': 'Upgrade',
|
|
|
|
'Sec-WebSocket-Key': self.key,
|
|
|
|
'Sec-WebSocket-Version': '13',
|
|
|
|
})
|
2014-10-14 04:24:01 +00:00
|
|
|
if self.compression_options is not None:
|
|
|
|
# Always offer to let the server set our max_wbits (and even though
|
|
|
|
# we don't offer it, we will accept a client_no_context_takeover
|
|
|
|
# from the server).
|
|
|
|
# TODO: set server parameters for deflate extension
|
|
|
|
# if requested in self.compression_options.
|
|
|
|
request.headers['Sec-WebSocket-Extensions'] = (
|
|
|
|
'permessage-deflate; client_max_window_bits')
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2014-06-17 04:54:00 +00:00
|
|
|
self.tcp_client = TCPClient(io_loop=io_loop)
|
2014-06-11 08:34:28 +00:00
|
|
|
super(WebSocketClientConnection, self).__init__(
|
|
|
|
io_loop, None, request, lambda: None, self._on_http_response,
|
2015-04-27 19:06:19 +00:00
|
|
|
104857600, self.tcp_client, 65536, 104857600)
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2014-06-17 04:54:00 +00:00
|
|
|
def close(self, code=None, reason=None):
|
2014-06-11 08:34:28 +00:00
|
|
|
"""Closes the websocket connection.
|
|
|
|
|
2014-06-17 04:54:00 +00:00
|
|
|
``code`` and ``reason`` are documented under
|
|
|
|
`WebSocketHandler.close`.
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
.. versionadded:: 3.2
|
2014-06-17 04:54:00 +00:00
|
|
|
|
|
|
|
.. versionchanged:: 4.0
|
|
|
|
|
|
|
|
Added the ``code`` and ``reason`` arguments.
|
2014-06-11 08:34:28 +00:00
|
|
|
"""
|
|
|
|
if self.protocol is not None:
|
2014-06-17 04:54:00 +00:00
|
|
|
self.protocol.close(code, reason)
|
2014-06-11 08:34:28 +00:00
|
|
|
self.protocol = None
|
|
|
|
|
2014-10-14 04:24:01 +00:00
|
|
|
def on_connection_close(self):
|
|
|
|
if not self.connect_future.done():
|
|
|
|
self.connect_future.set_exception(StreamClosedError())
|
2014-06-11 08:34:28 +00:00
|
|
|
self.on_message(None)
|
2014-10-14 04:24:01 +00:00
|
|
|
self.tcp_client.close()
|
|
|
|
super(WebSocketClientConnection, self).on_connection_close()
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
def _on_http_response(self, response):
|
|
|
|
if not self.connect_future.done():
|
|
|
|
if response.error:
|
|
|
|
self.connect_future.set_exception(response.error)
|
|
|
|
else:
|
|
|
|
self.connect_future.set_exception(WebSocketError(
|
|
|
|
"Non-websocket response"))
|
|
|
|
|
2014-06-17 04:54:00 +00:00
|
|
|
def headers_received(self, start_line, headers):
|
|
|
|
if start_line.code != 101:
|
|
|
|
return super(WebSocketClientConnection, self).headers_received(
|
|
|
|
start_line, headers)
|
|
|
|
|
|
|
|
self.headers = headers
|
2015-04-27 19:06:19 +00:00
|
|
|
self.protocol = self.get_websocket_protocol()
|
2014-10-14 04:24:01 +00:00
|
|
|
self.protocol._process_server_headers(self.key, self.headers)
|
2017-08-07 16:00:36 +00:00
|
|
|
self.protocol.start_pinging()
|
2014-06-11 08:34:28 +00:00
|
|
|
self.protocol._receive_frame()
|
|
|
|
|
|
|
|
if self._timeout is not None:
|
|
|
|
self.io_loop.remove_timeout(self._timeout)
|
|
|
|
self._timeout = None
|
|
|
|
|
2014-06-17 04:54:00 +00:00
|
|
|
self.stream = self.connection.detach()
|
2014-10-14 04:24:01 +00:00
|
|
|
self.stream.set_close_callback(self.on_connection_close)
|
|
|
|
# Once we've taken over the connection, clear the final callback
|
|
|
|
# we set on the http request. This deactivates the error handling
|
|
|
|
# in simple_httpclient that would otherwise interfere with our
|
|
|
|
# ability to see exceptions.
|
|
|
|
self.final_callback = None
|
2014-06-17 04:54:00 +00:00
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
self.connect_future.set_result(self)
|
|
|
|
|
|
|
|
def write_message(self, message, binary=False):
|
|
|
|
"""Sends a message to the WebSocket server."""
|
2015-12-23 04:15:36 +00:00
|
|
|
return self.protocol.write_message(message, binary)
|
2014-06-11 08:34:28 +00:00
|
|
|
|
|
|
|
def read_message(self, callback=None):
|
|
|
|
"""Reads a message from the WebSocket server.
|
|
|
|
|
2015-04-27 19:06:19 +00:00
|
|
|
If on_message_callback was specified at WebSocket
|
|
|
|
initialization, this function will never return messages
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
Returns a future whose result is the message, or None
|
|
|
|
if the connection is closed. If a callback argument
|
|
|
|
is given it will be called with the future when it is
|
|
|
|
ready.
|
|
|
|
"""
|
|
|
|
assert self.read_future is None
|
|
|
|
future = TracebackFuture()
|
|
|
|
if self.read_queue:
|
|
|
|
future.set_result(self.read_queue.popleft())
|
|
|
|
else:
|
|
|
|
self.read_future = future
|
|
|
|
if callback is not None:
|
|
|
|
self.io_loop.add_future(future, callback)
|
|
|
|
return future
|
|
|
|
|
|
|
|
def on_message(self, message):
|
2015-04-27 19:06:19 +00:00
|
|
|
if self._on_message_callback:
|
|
|
|
self._on_message_callback(message)
|
|
|
|
elif self.read_future is not None:
|
2014-06-11 08:34:28 +00:00
|
|
|
self.read_future.set_result(message)
|
|
|
|
self.read_future = None
|
|
|
|
else:
|
|
|
|
self.read_queue.append(message)
|
|
|
|
|
|
|
|
def on_pong(self, data):
|
|
|
|
pass
|
|
|
|
|
2017-08-07 16:00:36 +00:00
|
|
|
def on_ping(self, data):
|
|
|
|
pass
|
|
|
|
|
2015-04-27 19:06:19 +00:00
|
|
|
def get_websocket_protocol(self):
|
|
|
|
return WebSocketProtocol13(self, mask_outgoing=True,
|
|
|
|
compression_options=self.compression_options)
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
|
2014-10-14 04:24:01 +00:00
|
|
|
def websocket_connect(url, io_loop=None, callback=None, connect_timeout=None,
|
2017-08-07 16:00:36 +00:00
|
|
|
on_message_callback=None, compression_options=None,
|
|
|
|
ping_interval=None, ping_timeout=None,
|
|
|
|
max_message_size=None):
|
2014-06-11 08:34:28 +00:00
|
|
|
"""Client-side websocket support.
|
|
|
|
|
|
|
|
Takes a url and returns a Future whose result is a
|
|
|
|
`WebSocketClientConnection`.
|
|
|
|
|
2014-10-14 04:24:01 +00:00
|
|
|
``compression_options`` is interpreted in the same way as the
|
|
|
|
return value of `.WebSocketHandler.get_compression_options`.
|
|
|
|
|
2015-04-27 19:06:19 +00:00
|
|
|
The connection supports two styles of operation. In the coroutine
|
|
|
|
style, the application typically calls
|
|
|
|
`~.WebSocketClientConnection.read_message` in a loop::
|
|
|
|
|
2015-12-23 04:15:36 +00:00
|
|
|
conn = yield websocket_connect(url)
|
2015-04-27 19:06:19 +00:00
|
|
|
while True:
|
|
|
|
msg = yield conn.read_message()
|
|
|
|
if msg is None: break
|
|
|
|
# Do something with msg
|
|
|
|
|
|
|
|
In the callback style, pass an ``on_message_callback`` to
|
|
|
|
``websocket_connect``. In both styles, a message of ``None``
|
|
|
|
indicates that the connection has been closed.
|
|
|
|
|
2014-06-11 08:34:28 +00:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
Also accepts ``HTTPRequest`` objects in place of urls.
|
2014-10-14 04:24:01 +00:00
|
|
|
|
|
|
|
.. versionchanged:: 4.1
|
2015-04-27 19:06:19 +00:00
|
|
|
Added ``compression_options`` and ``on_message_callback``.
|
|
|
|
The ``io_loop`` argument is deprecated.
|
2017-08-07 16:00:36 +00:00
|
|
|
|
|
|
|
.. versionchanged:: 4.5
|
|
|
|
Added the ``ping_interval``, ``ping_timeout``, and ``max_message_size``
|
|
|
|
arguments, which have the same meaning as in `WebSocketHandler`.
|
2014-06-11 08:34:28 +00:00
|
|
|
"""
|
|
|
|
if io_loop is None:
|
|
|
|
io_loop = IOLoop.current()
|
|
|
|
if isinstance(url, httpclient.HTTPRequest):
|
|
|
|
assert connect_timeout is None
|
|
|
|
request = url
|
|
|
|
# Copy and convert the headers dict/object (see comments in
|
|
|
|
# AsyncHTTPClient.fetch)
|
|
|
|
request.headers = httputil.HTTPHeaders(request.headers)
|
|
|
|
else:
|
|
|
|
request = httpclient.HTTPRequest(url, connect_timeout=connect_timeout)
|
|
|
|
request = httpclient._RequestProxy(
|
|
|
|
request, httpclient.HTTPRequest._DEFAULTS)
|
2015-04-27 19:06:19 +00:00
|
|
|
conn = WebSocketClientConnection(io_loop, request,
|
|
|
|
on_message_callback=on_message_callback,
|
2017-08-07 16:00:36 +00:00
|
|
|
compression_options=compression_options,
|
|
|
|
ping_interval=ping_interval,
|
|
|
|
ping_timeout=ping_timeout,
|
|
|
|
max_message_size=max_message_size)
|
2014-06-11 08:34:28 +00:00
|
|
|
if callback is not None:
|
|
|
|
io_loop.add_future(conn.connect_future, callback)
|
|
|
|
return conn.connect_future
|