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

Respect CONNECT method to implement a proxy server #847

Merged
merged 1 commit into from
Jul 7, 2016
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
8 changes: 7 additions & 1 deletion aiohttp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,13 @@ def start(self):
self._timeout_handle = None

# request may not have payload
if (message.headers.get(hdrs.CONTENT_LENGTH, 0) or
try:
content_length = int(message.headers.get(hdrs.CONTENT_LENGTH, 0))
except ValueError:
content_length = 0

if (content_length > 0 or
message.method == 'CONNECT' or
hdrs.SEC_WEBSOCKET_KEY1 in message.headers or
'chunked' in message.headers.get(
hdrs.TRANSFER_ENCODING, '')):
Expand Down
31 changes: 31 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,34 @@ def test_keep_alive_timeout_default(srv):
def test_keep_alive_timeout_nondefault(make_srv):
srv = make_srv(keep_alive=10)
assert 10 == srv.keep_alive_timeout


def test_supports_connect_method(srv, loop):
transport = mock.Mock()
srv.connection_made(transport)

with mock.patch.object(srv, 'handle_request') as m_handle_request:
srv.reader.feed_data(
b'CONNECT aiohttp.readthedocs.org:80 HTTP/1.0\r\n'
b'Content-Length: 0\r\n\r\n')

loop.run_until_complete(srv._request_handler)

assert m_handle_request.called
assert m_handle_request.call_args[0] != (mock.ANY, server.EMPTY_PAYLOAD)


def test_content_length_0(srv, loop):
transport = mock.Mock()
srv.connection_made(transport)

with mock.patch.object(srv, 'handle_request') as m_handle_request:
srv.reader.feed_data(
b'GET / HTTP/1.1\r\n'
b'Host: example.org\r\n'
b'Content-Length: 0\r\n\r\n')

loop.run_until_complete(srv._request_handler)

assert m_handle_request.called
assert m_handle_request.call_args[0] == (mock.ANY, server.EMPTY_PAYLOAD)