Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made websocket subprotocols conform to spec #181

Merged
merged 3 commits into from
Nov 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ There is simple usage example::
def handle(request):
name = request.match_info.get('name', "Anonymous")
text = "Hello, " + name
return web.Response(body=text.encode('utf-8'))
return web.Response(request, body=text.encode('utf-8'))


@asyncio.coroutine
def init(loop):
app = Application(loop=loop)
app = web.Application(loop=loop)
app.router.add_route('GET', '/{name}', handle)

srv = yield from loop.create_server(app.make_handler, '127.0.0.1', 8080)
Expand Down
1 change: 1 addition & 0 deletions aiohttp/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
client_log = logging.getLogger('aiohttp.client')
internal_log = logging.getLogger('aiohttp.internal')
server_log = logging.getLogger('aiohttp.server')
websocket_log = logging.getLogger('aiohttp.websocket')
7 changes: 4 additions & 3 deletions aiohttp/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import hashlib
import struct
from aiohttp import errors
from aiohttp.log import websocket_log

# Frame opcodes defined in the spec.
OPCODE_CONTINUATION = 0x0
Expand Down Expand Up @@ -220,9 +221,9 @@ def do_handshake(method, headers, transport, protocols=()):
protocol = proto
break
else:
raise errors.HttpBadRequest(
'Client protocols {!r} don’t overlap server-known ones {!r}'
.format(protocols, req_protocols))
websocket_log.warning( # No overlap found: Return no protocol as per spec
'Client protocols %r don’t overlap server-known ones %r',
protocols, req_protocols)

# check supported version
version = headers.get('SEC-WEBSOCKET-VERSION')
Expand Down
17 changes: 13 additions & 4 deletions tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,21 @@ def test_handshake_protocol_agreement(self):

self.assertEqual(protocol, best_proto)

def test_handshake_protocol_unsupported(self):
@unittest.mock.patch('aiohttp.websocket.websocket_log.warning')
def test_handshake_protocol_unsupported(self, m_websocket_warn):
'''Tests if a protocol mismatch handshake warns and returns None'''
warn_called = False
def websocket_warn(msg, *fmts):
nonlocal warn_called
warn_called = True
m_websocket_warn.side_effect = websocket_warn

proto = 'chat'
self.headers.extend(self.gen_ws_headers('test')[0])

self.assertRaises(
errors.HttpBadRequest,
websocket.do_handshake,
_, _, _, _, protocol = websocket.do_handshake(
self.message.method, self.message.headers, self.transport,
protocols=[proto])

self.assertTrue(warn_called, 'protocol mismatch didn’t warn')
self.assertIsNone(protocol)
1 change: 0 additions & 1 deletion tests/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ def wsgi_app(env, start):

def test_dont_unquote_environ_path_info(self):
path = '/path/some%20text'
print(path)
self.message = protocol.RawRequestMessage(
'GET', path, (1, 0), self.headers, True, 'deflate')
environ = self._make_one()
Expand Down