This repository was archived by the owner on Jan 13, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 1515import random
1616import requests
1717import threading
18- from hyper import HTTP20Connection , HTTP11Connection
18+ from hyper import HTTP20Connection , HTTP11Connection , HTTPConnection
19+ from hyper .common .util import HTTPVersion
1920from hyper .contrib import HTTP20Adapter
2021
2122logging .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
You can’t perform that action at this time.
0 commit comments