Skip to content

Commit

Permalink
Stop overriding _execute in WebSocketHandler; use get() instead.
Browse files Browse the repository at this point in the history
Revise HTTP1Connection detach protocol.

As a side effect, this now decodes the arguments to open() in the same
way as the arguments to get().
  • Loading branch information
bdarnell committed Mar 17, 2014
1 parent adc7e4f commit c8ce451
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
8 changes: 4 additions & 4 deletions maint/test/websocket/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.options import define, options, parse_command_line
from tornado.websocket import WebSocketConnect
from tornado.websocket import websocket_connect

define('url', default='ws://localhost:9001')
define('name', default='Tornado')

@gen.engine
def run_tests():
url = options.url + '/getCaseCount'
control_ws = yield WebSocketConnect(url, None)
control_ws = yield websocket_connect(url, None)
num_tests = int((yield control_ws.read_message()))
logging.info('running %d cases', num_tests)
msg = yield control_ws.read_message()
Expand All @@ -22,15 +22,15 @@ def run_tests():
for i in range(1, num_tests + 1):
logging.info('running test case %d', i)
url = options.url + '/runCase?case=%d&agent=%s' % (i, options.name)
test_ws = yield WebSocketConnect(url, None)
test_ws = yield websocket_connect(url, None)
while True:
message = yield test_ws.read_message()
if message is None:
break
test_ws.write_message(message, binary=isinstance(message, bytes))

url = options.url + '/updateReports?agent=%s' % options.name
update_ws = yield WebSocketConnect(url, None)
update_ws = yield websocket_connect(url, None)
msg = yield update_ws.read_message()
assert msg is None
IOLoop.instance().stop()
Expand Down
16 changes: 12 additions & 4 deletions tornado/http1connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ def _read_message(self, delegate, is_client, method=None):

self._disconnect_on_finish = not self._can_keep_alive(
start_line, headers)
ret = delegate.headers_received(start_line, headers)
# TODO: finalize the 'detach' interface.
if ret == 'detach':
return
delegate.headers_received(start_line, headers)
if self.stream is None:
# We've been detached.
# TODO: where else do we need to check for detach?
raise gen.Return(False)
skip_body = False
if is_client:
if method == 'HEAD':
Expand All @@ -127,6 +128,8 @@ def _read_message(self, delegate, is_client, method=None):
yield body_future
delegate.finish()
yield self._finish_future
if self.stream is None:
raise gen.Return(False)
except httputil.HTTPMessageException as e:
gen_log.info("Malformed HTTP message from %r: %s",
self.address, e)
Expand Down Expand Up @@ -173,6 +176,11 @@ def close(self):
# cycle and delay garbage collection of this connection.
self._clear_request_state()

def detach(self):
stream = self.stream
self.stream = None
return stream

def write_headers(self, start_line, headers, chunk=None, callback=None):
self._chunking = (
# TODO: should this use self._version or start_line.version?
Expand Down
14 changes: 4 additions & 10 deletions tornado/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,16 @@ def on_close(self):
def __init__(self, application, request, **kwargs):
tornado.web.RequestHandler.__init__(self, application, request,
**kwargs)
self.stream = request.connection.stream
self.ws_connection = None
self.close_code = None
self.close_reason = None

def _execute(self, transforms, *args, **kwargs):
@tornado.web.asynchronous
def get(self, *args, **kwargs):
self.open_args = args
self.open_kwargs = kwargs

# Websocket only supports GET method
if self.request.method != 'GET':
self.stream.write(tornado.escape.utf8(
"HTTP/1.1 405 Method Not Allowed\r\n\r\n"
))
self.stream.close()
return
self.stream = self.request.connection.detach()

# Upgrade header should be present and should be equal to WebSocket
if self.request.headers.get("Upgrade", "").lower() != 'websocket':
Expand Down Expand Up @@ -878,10 +872,10 @@ def headers_received(self, start_line, headers):
self.io_loop.remove_timeout(self._timeout)
self._timeout = None

self.stream = self.connection.detach()
self.stream.set_close_callback(self._on_close)

self.connect_future.set_result(self)
return 'detach'

def write_message(self, message, binary=False):
"""Sends a message to the WebSocket server."""
Expand Down

0 comments on commit c8ce451

Please sign in to comment.