Skip to content

Commit f7cb71c

Browse files
authored
Update aiohttp async driver. Use native public aiohttp API (#419)
1 parent 04e7c4d commit f7cb71c

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/engineio/async_drivers/aiohttp.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import sys
3-
from urllib.parse import urlsplit
43

54
from aiohttp.web import Response, WebSocketResponse
65

@@ -22,31 +21,27 @@ def translate_request(request):
2221
"""This function takes the arguments passed to the request handler and
2322
uses them to generate a WSGI compatible environ dictionary.
2423
"""
25-
message = request._message
26-
payload = request._payload
27-
28-
uri_parts = urlsplit(message.path)
2924
environ = {
30-
'wsgi.input': payload,
25+
'wsgi.input': request.content,
3126
'wsgi.errors': sys.stderr,
3227
'wsgi.version': (1, 0),
3328
'wsgi.async': True,
3429
'wsgi.multithread': False,
3530
'wsgi.multiprocess': False,
3631
'wsgi.run_once': False,
3732
'SERVER_SOFTWARE': 'aiohttp',
38-
'REQUEST_METHOD': message.method,
39-
'QUERY_STRING': uri_parts.query or '',
40-
'RAW_URI': message.path,
41-
'SERVER_PROTOCOL': 'HTTP/%s.%s' % message.version,
33+
'REQUEST_METHOD': request.method,
34+
'QUERY_STRING': request.query_string or '',
35+
'RAW_URI': request.path_qs,
36+
'SERVER_PROTOCOL': f'HTTP/{request.version[0]}.{request.version[1]}',
4237
'REMOTE_ADDR': '127.0.0.1',
4338
'REMOTE_PORT': '0',
4439
'SERVER_NAME': 'aiohttp',
4540
'SERVER_PORT': '0',
4641
'aiohttp.request': request
4742
}
4843

49-
for hdr_name, hdr_value in message.headers.items():
44+
for hdr_name, hdr_value in request.headers.items():
5045
hdr_name = hdr_name.upper()
5146
if hdr_name == 'CONTENT-TYPE':
5247
environ['CONTENT_TYPE'] = hdr_value
@@ -63,9 +58,7 @@ def translate_request(request):
6358

6459
environ['wsgi.url_scheme'] = environ.get('HTTP_X_FORWARDED_PROTO', 'http')
6560

66-
path_info = uri_parts.path
67-
68-
environ['PATH_INFO'] = path_info
61+
environ['PATH_INFO'] = request.path
6962
environ['SCRIPT_NAME'] = ''
7063

7164
return environ

tests/async/test_aiohttp.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from unittest import mock
22

3+
import yarl
4+
from aiohttp.web import Request
5+
36
from engineio.async_drivers import aiohttp as async_aiohttp
47

58

@@ -12,18 +15,28 @@ def test_create_route(self):
1215
app.router.add_post.assert_any_call('/foo', mock_server.handle_request)
1316

1417
def test_translate_request(self):
15-
request = mock.MagicMock()
16-
request._message.method = 'PUT'
17-
request._message.path = '/foo/bar?baz=1'
18-
request._message.version = (1, 1)
19-
request._message.headers = {
18+
message = mock.MagicMock()
19+
message.url = yarl.URL('https://example.com/foo/bar?baz=1')
20+
message.method = 'PUT'
21+
message.path = '/foo/bar?baz=1'
22+
message.version = (1, 1)
23+
message.headers = {
2024
'a': 'b',
2125
'c-c': 'd',
2226
'c_c': 'e',
2327
'content-type': 'application/json',
2428
'content-length': 123,
2529
}
26-
request._payload = b'hello world'
30+
31+
request = Request(
32+
message,
33+
payload=b'hello world',
34+
protocol=mock.MagicMock(),
35+
payload_writer=mock.MagicMock(),
36+
task=mock.MagicMock(),
37+
loop=mock.MagicMock(),
38+
)
39+
2740
environ = async_aiohttp.translate_request(request)
2841
expected_environ = {
2942
'REQUEST_METHOD': 'PUT',

0 commit comments

Comments
 (0)