This repository was archived by the owner on Jan 13, 2021. It is now read-only.
File tree 3 files changed +26
-3
lines changed
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):
212
212
213
213
self ._sock .advance_buffer (response .consumed )
214
214
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.
215
221
if (response .status == 101 and
216
- b'upgrade' in headers ['connection' ] and
222
+ b'upgrade' in map ( bytes . lower , headers ['connection' ]) and
217
223
H2C_PROTOCOL .encode ('utf-8' ) in headers ['upgrade' ]):
218
224
raise HTTPUpgrade (H2C_PROTOCOL , self ._sock )
219
225
Original file line number Diff line number Diff line change @@ -282,7 +282,7 @@ def socket_handler(listener):
282
282
b'HTTP/1.1 101 Upgrade\r \n '
283
283
b'Server: socket-level-server\r \n '
284
284
b'Content-Length: 0\r \n '
285
- b'Connection: upgrade \r \n '
285
+ b'Connection: Upgrade \r \n '
286
286
b'Upgrade: h2c\r \n '
287
287
b'\r \n '
288
288
)
Original file line number Diff line number Diff line change 15
15
import random
16
16
import requests
17
17
import threading
18
- from hyper import HTTP20Connection , HTTP11Connection
18
+ from hyper import HTTP20Connection , HTTP11Connection , HTTPConnection
19
+ from hyper .common .util import HTTPVersion
19
20
from hyper .contrib import HTTP20Adapter
20
21
21
22
logging .basicConfig (level = logging .INFO )
@@ -149,3 +150,19 @@ def test_hitting_httpbin_org_http11(self):
149
150
150
151
assert resp .status == 200
151
152
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