Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #313 from vfaronov/fix-upgrade-h2c
Browse files Browse the repository at this point in the history
Fix upgrading to h2c
  • Loading branch information
Lukasa committed Mar 4, 2017
2 parents cbee16c + cb49fef commit 8a95ad6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
8 changes: 7 additions & 1 deletion hyper/http11/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,14 @@ def get_response(self):

self._sock.advance_buffer(response.consumed)

# Check for a successful "switching protocols to h2c" response.
# "Connection: upgrade" is not strictly necessary on the receiving end,
# but we want to fail fast on broken servers or intermediaries:
# https://github.com/Lukasa/hyper/issues/312.
# Connection options are case-insensitive, while upgrade tokens are
# case-sensitive: https://github.com/httpwg/http11bis/issues/8.
if (response.status == 101 and
b'upgrade' in headers['connection'] and
b'upgrade' in map(bytes.lower, headers['connection']) and
H2C_PROTOCOL.encode('utf-8') in headers['upgrade']):
raise HTTPUpgrade(H2C_PROTOCOL, self._sock)

Expand Down
2 changes: 1 addition & 1 deletion test/test_integration_http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def socket_handler(listener):
b'HTTP/1.1 101 Upgrade\r\n'
b'Server: socket-level-server\r\n'
b'Content-Length: 0\r\n'
b'Connection: upgrade\r\n'
b'Connection: Upgrade\r\n'
b'Upgrade: h2c\r\n'
b'\r\n'
)
Expand Down
19 changes: 18 additions & 1 deletion test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import random
import requests
import threading
from hyper import HTTP20Connection, HTTP11Connection
from hyper import HTTP20Connection, HTTP11Connection, HTTPConnection
from hyper.common.util import HTTPVersion
from hyper.contrib import HTTP20Adapter

logging.basicConfig(level=logging.INFO)
Expand Down Expand Up @@ -149,3 +150,19 @@ def test_hitting_httpbin_org_http11(self):

assert resp.status == 200
assert resp.read()

def test_hitting_nghttp2_org_via_h2c_upgrade(self):
"""
This tests our support for cleartext HTTP/1.1 -> HTTP/2 upgrade
against the most common open source HTTP/2 server implementation.
"""
c = HTTPConnection('nghttp2.org:80')

# Make the request.
c.request('GET', '/')
response = c.get_response()

# Check that the response is OK and that we did upgrade to HTTP/2.
assert response.status == 200
assert response.read()
assert response.version == HTTPVersion.http20

0 comments on commit 8a95ad6

Please sign in to comment.