Remove ENABLE_CONNECT_PROTOCOL from HTTP/2 Settings (#373)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Seth Michael Larson · committed by GitHub · parent e1da6b9 · Browse files · 3 files·+65 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+65 added−1 removed
- tests:+42 added−0 removed
- other:+23 added−1 removed
Provenance
- Commit
b574230- Tree
6a9f4b1- Author
Seth Michael Larson <sethmichaellarson@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
e1da6b9
3 files changed+65 added−1 removed
modified:httpx/dispatch/http2.py+23 added-1 removed
1 unmodified line2 import typing3 4 import h2.connection5 import h2.events6+from h2.settings import SettingCodes, Settings7 8 from ..concurrency.base import BaseEvent, BaseTCPStream, ConcurrencyBackend, TimeoutFlag9 from ..config import TimeoutConfig, TimeoutTypes10 from ..exceptions import ProtocolError54 unmodified lines65 async def close(self) -> None:66 await self.stream.close()67 68 def initiate_connection(self) -> None:69+ # Need to set these manually here instead of manipulating via70+ # __setitem__() otherwise the H2Connection will emit SettingsUpdate71+ # frames in addition to sending the undesired defaults.72+ self.h2_state.local_settings = Settings(73+ client=True,74+ initial_values={75+ # Disable PUSH_PROMISE frames from the server since we don't do anything76+ # with them for now. Maybe when we support caching?77+ SettingCodes.ENABLE_PUSH: 0,78+ # These two are taken from h2 for safe defaults79+ SettingCodes.MAX_CONCURRENT_STREAMS: 100,80+ SettingCodes.MAX_HEADER_LIST_SIZE: 65536,81+ },82+ )83+84+ # Some websites (*cough* Yahoo *cough*) balk at this setting being85+ # present in the initial handshake since it's not defined in the original86+ # RFC despite the RFC mandating ignoring settings you don't know about.87+ del self.h2_state.local_settings[88+ h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL89+ ]90+91 self.h2_state.initiate_connection()92 data_to_send = self.h2_state.data_to_send()93 self.stream.write_no_block(data_to_send)94 self.initialized = True15 unmodified lines110 f"method={request.method!r} "111 f"target={request.url.full_path!r} "112 f"headers={headers!r}"113 )91-114 self.h2_state.send_headers(stream_id, headers)115 data_to_send = self.h2_state.data_to_send()116 await self.stream.write(data_to_send, timeout)117 return stream_id134 unmodified linesmodified:tests/dispatch/test_http2.py+39 added
1 import json2 3+import h2.connection4+import h2.events5+from h2.settings import SettingCodes6+7 from httpx import AsyncClient, Client, Response8 9 from .utils import MockHTTP2Backend10 157 unmodified lines168 assert json.loads(response_1.content) == {"method": "GET", "path": "/1", "body": ""}169 170 assert response_2.status_code == 200171 assert json.loads(response_2.content) == {"method": "GET", "path": "/2", "body": ""}172+173+174+async def test_http2_settings_in_handshake(backend):175+ backend = MockHTTP2Backend(app=app, backend=backend)176+177+ async with AsyncClient(backend=backend) as client:178+ await client.get("http://example.org")179+180+ h2_conn = backend.server.conn181+182+ assert isinstance(h2_conn, h2.connection.H2Connection)183+ expected_settings = {184+ SettingCodes.HEADER_TABLE_SIZE: 4096,185+ SettingCodes.ENABLE_PUSH: 0,186+ SettingCodes.MAX_CONCURRENT_STREAMS: 100,187+ SettingCodes.INITIAL_WINDOW_SIZE: 65535,188+ SettingCodes.MAX_FRAME_SIZE: 16384,189+ SettingCodes.MAX_HEADER_LIST_SIZE: 65536,190+ # This one's here because h2 helpfully populates remote_settings191+ # with default values even if the peer doesn't send the setting.192+ SettingCodes.ENABLE_CONNECT_PROTOCOL: 0,193+ }194+ assert dict(h2_conn.remote_settings) == expected_settings195+196+ # We don't expect the ENABLE_CONNECT_PROTOCOL to be in the handshake197+ expected_settings.pop(SettingCodes.ENABLE_CONNECT_PROTOCOL)198+199+ assert len(backend.server.settings_changed) == 1200+ settings = backend.server.settings_changed[0]201+202+ assert isinstance(settings, h2.events.RemoteSettingsChanged)203+ assert len(settings.changed_settings) == len(expected_settings)204+ for setting_code, changed_setting in settings.changed_settings.items():205+ assert isinstance(changed_setting, h2.settings.ChangedSetting)206+ assert changed_setting.new_value == expected_settings[setting_code]modified:tests/dispatch/utils.py+3 added
39 unmodified lines40 self.requests = {}41 self.close_connection = False42 self.return_data = {}43 self.returning = {}44+ self.settings_changed = []45 46 # TCP stream interface47 48 def get_http_version(self) -> str:32 unmodified lines81 self.send_return_data(key)82 # This will throw an error if the event is for a not-yet created stream83 elif self.returning[event.stream_id]:84 self.send_return_data(event.stream_id)85+ elif isinstance(event, h2.events.RemoteSettingsChanged):86+ self.settings_changed.append(event)87 88 async def write(self, data: bytes, timeout) -> None:89 self.write_no_block(data)90 136 unmodified lines