Skip to content
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
21 changes: 7 additions & 14 deletions src/engineio/async_drivers/aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncio
import sys
from urllib.parse import urlsplit

from aiohttp.web import Response, WebSocketResponse

Expand All @@ -22,31 +21,27 @@ def translate_request(request):
"""This function takes the arguments passed to the request handler and
uses them to generate a WSGI compatible environ dictionary.
"""
message = request._message
payload = request._payload

uri_parts = urlsplit(message.path)
environ = {
'wsgi.input': payload,
'wsgi.input': request.content,
'wsgi.errors': sys.stderr,
'wsgi.version': (1, 0),
'wsgi.async': True,
'wsgi.multithread': False,
'wsgi.multiprocess': False,
'wsgi.run_once': False,
'SERVER_SOFTWARE': 'aiohttp',
'REQUEST_METHOD': message.method,
'QUERY_STRING': uri_parts.query or '',
'RAW_URI': message.path,
'SERVER_PROTOCOL': 'HTTP/%s.%s' % message.version,
'REQUEST_METHOD': request.method,
'QUERY_STRING': request.query_string or '',
'RAW_URI': request.path_qs,
'SERVER_PROTOCOL': f'HTTP/{request.version[0]}.{request.version[1]}',
'REMOTE_ADDR': '127.0.0.1',
'REMOTE_PORT': '0',
'SERVER_NAME': 'aiohttp',
'SERVER_PORT': '0',
'aiohttp.request': request
}

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

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

path_info = uri_parts.path

environ['PATH_INFO'] = path_info
environ['PATH_INFO'] = request.path
environ['SCRIPT_NAME'] = ''

return environ
Expand Down
25 changes: 19 additions & 6 deletions tests/async/test_aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from unittest import mock

import yarl
from aiohttp.web import Request

from engineio.async_drivers import aiohttp as async_aiohttp


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

def test_translate_request(self):
request = mock.MagicMock()
request._message.method = 'PUT'
request._message.path = '/foo/bar?baz=1'
request._message.version = (1, 1)
request._message.headers = {
message = mock.MagicMock()
message.url = yarl.URL('https://example.com/foo/bar?baz=1')
message.method = 'PUT'
message.path = '/foo/bar?baz=1'
message.version = (1, 1)
message.headers = {
'a': 'b',
'c-c': 'd',
'c_c': 'e',
'content-type': 'application/json',
'content-length': 123,
}
request._payload = b'hello world'

request = Request(
message,
payload=b'hello world',
protocol=mock.MagicMock(),
payload_writer=mock.MagicMock(),
task=mock.MagicMock(),
loop=mock.MagicMock(),
)

environ = async_aiohttp.translate_request(request)
expected_environ = {
'REQUEST_METHOD': 'PUT',
Expand Down
Loading