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

Commit d76543d

Browse files
committed
Made some review changes
1 parent 7c00e5a commit d76543d

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

hyper/common/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def request(self, method, url, body=None, headers={}):
9292
# the world of HTTP/2. Replace the backing object and insert the
9393
# socket into it.
9494
assert e.negotiated in H2_NPN_PROTOCOLS
95-
95+
9696
self._conn = HTTP20Connection(
9797
self._host, self._port, **self._h2_kwargs
9898
)

hyper/http11/connection.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,9 @@ def request(self, method, url, body=None, headers={}):
132132

133133
if self._sock is None:
134134
self.connect()
135-
136-
#add HTTP Upgrade headers
137-
headers[b'connection'] = b'Upgrade, HTTP2-Settings'
138-
headers[b'upgrade'] = H2C_PROTOCOL
139135

140-
#need to encode SETTINGS frame payload in Base64 and put into the HTTP-2 Settings header
141-
http2_settings = SettingsFrame(0)
142-
http2_settings.settings[SettingsFrame.INITIAL_WINDOW_SIZE] = 65535
143-
headers[b'HTTP2-Settings'] = base64.b64encode(http2_settings.serialize_body())
136+
# TODO: Only send upgrade headers on first request
137+
self._add_upgrade_headers(headers)
144138

145139
# We may need extra headers.
146140
if body:
@@ -178,8 +172,8 @@ def get_response(self):
178172

179173
self._sock.advance_buffer(response.consumed)
180174

181-
if(response.status == 101 and
182-
b'upgrade' in headers['connection'] and bytes(H2C_PROTOCOL, 'utf-8') in headers['upgrade']):
175+
if (response.status == 101 and
176+
b'upgrade' in headers['connection'] and H2C_PROTOCOL.decode('utf-8') in headers['upgrade']):
183177
raise HTTPUpgrade(H2C_PROTOCOL, self._sock)
184178

185179
return HTTP11Response(
@@ -233,6 +227,16 @@ def _add_body_headers(self, headers, body):
233227
headers[b'transfer-encoding'] = b'chunked'
234228
return BODY_CHUNKED
235229

230+
def _add_upgrade_headers(self, headers):
231+
# Add HTTP Upgrade headers.
232+
headers[b'connection'] = b'Upgrade, HTTP2-Settings'
233+
headers[b'upgrade'] = H2C_PROTOCOL
234+
235+
# Encode SETTINGS frame payload in Base64 and put into the HTTP-2 Settings header.
236+
http2_settings = SettingsFrame(0)
237+
http2_settings.settings[SettingsFrame.INITIAL_WINDOW_SIZE] = 65535
238+
headers[b'HTTP2-Settings'] = base64.b64encode(http2_settings.serialize_body())
239+
236240
def _send_body(self, body, body_type):
237241
"""
238242
Handles the HTTP/1.1 logic for sending HTTP bodies. This does magical

hyper/http20/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def connect(self):
227227
proto = H2C_PROTOCOL
228228

229229
log.debug("Selected NPN protocol: %s", proto)
230-
assert proto in (H2_NPN_PROTOCOLS, H2C_PROTOCOL)
230+
assert proto in H2_NPN_PROTOCOLS or proto == H2C_PROTOCOL
231231

232232
self._sock = BufferedSocket(sock, self.network_buffer_size)
233233

0 commit comments

Comments
 (0)