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

Commit 8a95ad6

Browse files
authored
Merge pull request #313 from vfaronov/fix-upgrade-h2c
Fix upgrading to h2c
2 parents cbee16c + cb49fef commit 8a95ad6

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

hyper/http11/connection.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,14 @@ def get_response(self):
212212

213213
self._sock.advance_buffer(response.consumed)
214214

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

test/test_integration_http11.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def socket_handler(listener):
282282
b'HTTP/1.1 101 Upgrade\r\n'
283283
b'Server: socket-level-server\r\n'
284284
b'Content-Length: 0\r\n'
285-
b'Connection: upgrade\r\n'
285+
b'Connection: Upgrade\r\n'
286286
b'Upgrade: h2c\r\n'
287287
b'\r\n'
288288
)

test_release.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import random
1616
import requests
1717
import threading
18-
from hyper import HTTP20Connection, HTTP11Connection
18+
from hyper import HTTP20Connection, HTTP11Connection, HTTPConnection
19+
from hyper.common.util import HTTPVersion
1920
from hyper.contrib import HTTP20Adapter
2021

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

150151
assert resp.status == 200
151152
assert resp.read()
153+
154+
def test_hitting_nghttp2_org_via_h2c_upgrade(self):
155+
"""
156+
This tests our support for cleartext HTTP/1.1 -> HTTP/2 upgrade
157+
against the most common open source HTTP/2 server implementation.
158+
"""
159+
c = HTTPConnection('nghttp2.org:80')
160+
161+
# Make the request.
162+
c.request('GET', '/')
163+
response = c.get_response()
164+
165+
# Check that the response is OK and that we did upgrade to HTTP/2.
166+
assert response.status == 200
167+
assert response.read()
168+
assert response.version == HTTPVersion.http20

0 commit comments

Comments
 (0)