Skip to content

Commit 1fb5fcd

Browse files
committed
Review markups for @Lukasa
1 parent 2f250f9 commit 1fb5fcd

6 files changed

Lines changed: 52 additions & 49 deletions

File tree

h2/config.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ class H2Configuration(object):
3232
``False`` or the empty string.
3333
:type header_encoding: ``str``, ``False``, or ``None``
3434
35-
:param validate_sent_headers: Controls whether the headers emitted
35+
:param validate_outbound_headers: Controls whether the headers emitted
3636
by this object are validated against the rules in RFC 7540.
3737
Disabling this setting will cause outbound header validation to
3838
be skipped, and allow the object to emit headers that may be illegal
3939
according to RFC 7540. Defaults to ``True``.
40-
:type validate_sent_headers: ``bool``
40+
:type validate_outbound_headers: ``bool``
4141
42-
:param normalize_sent_headers: Controls whether the headers emitted
42+
:param normalize_outbound_headers: Controls whether the headers emitted
4343
by this object are normalized before sending. Disabling this setting
4444
will cause outbound header normalization to be skipped, and allow
4545
the object to emit headers that may be illegal according to
4646
RFC 7540. Defaults to ``True``.
47-
:type normalize_sent_headers: ``bool``
47+
:type normalize_outbound_headers: ``bool``
4848
4949
:param validate_inbound_headers: Controls whether the headers received
5050
by this object are validated against the rules in RFC 7540.
@@ -56,13 +56,13 @@ class H2Configuration(object):
5656
def __init__(self,
5757
client_side=True,
5858
header_encoding='utf-8',
59-
validate_sent_headers=True,
60-
normalize_sent_headers=True,
59+
validate_outbound_headers=True,
60+
normalize_outbound_headers=True,
6161
validate_inbound_headers=True):
6262
self._client_side = client_side
6363
self._header_encoding = header_encoding
64-
self._validate_sent_headers = validate_sent_headers
65-
self._normalize_sent_headers = normalize_sent_headers
64+
self._validate_outbound_headers = validate_outbound_headers
65+
self._normalize_outbound_headers = normalize_outbound_headers
6666
self._validate_inbound_headers = validate_inbound_headers
6767

6868
@property
@@ -108,42 +108,42 @@ def header_encoding(self, value):
108108
self._header_encoding = value
109109

110110
@property
111-
def validate_sent_headers(self):
111+
def validate_outbound_headers(self):
112112
"""
113113
Whether the headers emitted by this object are validated against
114114
the rules in RFC 7540. Disabling this setting will cause outbound
115115
header validation to be skipped, and allow the object to emit headers
116116
that may be illegal according to RFC 7540. Defaults to ``True``.
117117
"""
118-
return self._validate_sent_headers
118+
return self._validate_outbound_headers
119119

120-
@validate_sent_headers.setter
121-
def validate_sent_headers(self, value):
120+
@validate_outbound_headers.setter
121+
def validate_outbound_headers(self, value):
122122
"""
123123
Enforces validation of outbound headers.
124124
"""
125125
if not isinstance(value, bool):
126-
raise ValueError("validate_sent_headers must be a bool")
127-
self._validate_sent_headers = value
126+
raise ValueError("validate_outbound_headers must be a bool")
127+
self._validate_outbound_headers = value
128128

129129
@property
130-
def normalize_sent_headers(self):
130+
def normalize_outbound_headers(self):
131131
"""
132132
Whether the headers emitted by this object are normalized before
133133
sending. Disabling this setting will cause outbound header
134134
normalization to be skipped, and allow the object to emit headers
135135
that may be illegal according to RFC 7540. Defaults to ``True``.
136136
"""
137-
return self._normalize_sent_headers
137+
return self._normalize_outbound_headers
138138

139-
@normalize_sent_headers.setter
140-
def normalize_sent_headers(self, value):
139+
@normalize_outbound_headers.setter
140+
def normalize_outbound_headers(self, value):
141141
"""
142142
Enforces normalization of outbound headers.
143143
"""
144144
if not isinstance(value, bool):
145-
raise ValueError("normalize_sent_headers must be a bool")
146-
self._normalize_sent_headers = value
145+
raise ValueError("normalize_outbound_headers must be a bool")
146+
self._normalize_outbound_headers = value
147147

148148
@property
149149
def validate_inbound_headers(self):

h2/connection.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,13 @@ def _begin_new_stream(self, stream_id, allowed_ids):
505505
"Invalid stream ID for peer."
506506
)
507507

508-
s = H2Stream(stream_id)
508+
s = H2Stream(stream_id, config=self.config)
509509
s.max_inbound_frame_size = self.max_inbound_frame_size
510510
s.max_outbound_frame_size = self.max_outbound_frame_size
511511
s.outbound_flow_control_window = (
512512
self.remote_settings.initial_window_size
513513
)
514514
s.inbound_flow_control_window = self.local_settings.initial_window_size
515-
s.config = self.config
516515

517516
self.streams[stream_id] = s
518517

h2/stream.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
RstStreamFrame, PushPromiseFrame, AltSvcFrame
1515
)
1616

17-
from .config import H2Configuration
1817
from .errors import STREAM_CLOSED
1918
from .events import (
2019
RequestReceived, ResponseReceived, DataReceived, WindowUpdated,
@@ -27,7 +26,7 @@
2726
)
2827
from .utilities import (
2928
guard_increment_window, is_informational_response, authority_from_headers,
30-
validate_headers, validate_sent_headers, normalize_sent_headers,
29+
validate_headers, validate_outbound_headers, normalize_outbound_headers,
3130
HeaderValidationFlags, extract_method_header
3231
)
3332

@@ -672,7 +671,7 @@ class H2Stream(object):
672671
Attempts to create frames that cannot be sent will raise a
673672
``ProtocolError``.
674673
"""
675-
def __init__(self, stream_id):
674+
def __init__(self, stream_id, config):
676675
self.state_machine = H2StreamStateMachine(stream_id)
677676
self.stream_id = stream_id
678677
self.max_outbound_frame_size = None
@@ -692,7 +691,7 @@ def __init__(self, stream_id):
692691
self._authority = None
693692

694693
# The configuration for this stream.
695-
self.config = H2Configuration()
694+
self.config = config
696695

697696
@property
698697
def open(self):
@@ -769,7 +768,8 @@ def send_headers(self, headers, encoder, end_stream=False):
769768
hf = HeadersFrame(self.stream_id)
770769
hdr_validation_flags = self._build_hdr_validation_flags(events)
771770
frames = self._build_headers_frames(
772-
headers, encoder, hf, hdr_validation_flags)
771+
headers, encoder, hf, hdr_validation_flags
772+
)
773773

774774
if end_stream:
775775
# Not a bug: the END_STREAM flag is valid on the initial HEADERS
@@ -807,7 +807,8 @@ def push_stream_in_band(self, related_stream_id, headers, encoder):
807807
ppf.promised_stream_id = related_stream_id
808808
hdr_validation_flags = self._build_hdr_validation_flags(events)
809809
frames = self._build_headers_frames(
810-
headers, encoder, ppf, hdr_validation_flags)
810+
headers, encoder, ppf, hdr_validation_flags
811+
)
811812

812813
return frames
813814

@@ -931,10 +932,7 @@ def receive_headers(self, headers, end_stream, header_encoding):
931932
raise ProtocolError("Trailers must have END_STREAM set")
932933

933934
if self.config.validate_inbound_headers:
934-
hdr_validation_flags = HeaderValidationFlags(
935-
is_client=self.state_machine.client,
936-
is_trailer=isinstance(events[0], TrailersReceived)
937-
)
935+
hdr_validation_flags = self._build_hdr_validation_flags(events)
938936
headers = validate_headers(headers, hdr_validation_flags)
939937

940938
if header_encoding:
@@ -1039,7 +1037,9 @@ def _build_hdr_validation_flags(self, events):
10391037
and validating header blocks.
10401038
"""
10411039
try:
1042-
is_trailer = isinstance(events[0], _TrailersSent)
1040+
is_trailer = isinstance(
1041+
events[0], (_TrailersSent, TrailersReceived)
1042+
)
10431043
except IndexError:
10441044
is_trailer = False
10451045

@@ -1058,10 +1058,14 @@ def _build_headers_frames(self,
10581058
"""
10591059
# We need to lowercase the header names, and to ensure that secure
10601060
# header fields are kept out of compression contexts.
1061-
if self.config.normalize_sent_headers:
1062-
headers = normalize_sent_headers(headers, hdr_validation_flags)
1063-
if self.config.validate_sent_headers:
1064-
headers = validate_sent_headers(headers, hdr_validation_flags)
1061+
if self.config.normalize_outbound_headers:
1062+
headers = normalize_outbound_headers(
1063+
headers, hdr_validation_flags
1064+
)
1065+
if self.config.validate_outbound_headers:
1066+
headers = validate_outbound_headers(
1067+
headers, hdr_validation_flags
1068+
)
10651069

10661070
encoded_headers = encoder.encode(headers)
10671071

h2/utilities.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,14 @@ def _validate_host_authority_header(headers):
317317
# an :authority header nor a Host header.
318318
if not authority_present and not host_present:
319319
raise ProtocolError(
320-
"Request header block must have an :authority or Host header."
320+
"Request header block does not have an :authority or Host header."
321321
)
322322

323323
# If we receive both headers, they should definitely match.
324324
if authority_present and host_present:
325325
if authority_header_val != host_header_val:
326326
raise ProtocolError(
327-
"Request header block must have matching :authority and "
327+
"Request header block has mismatched :authority and "
328328
"Host headers: %r / %r"
329329
% (authority_header_val, host_header_val)
330330
)
@@ -373,7 +373,7 @@ def _check_sent_host_authority_header(headers, hdr_validation_flags):
373373
return _validate_host_authority_header(headers)
374374

375375

376-
def normalize_sent_headers(headers, hdr_validation_flags):
376+
def normalize_outbound_headers(headers, hdr_validation_flags):
377377
"""
378378
Normalizes a header sequence that we are about to send.
379379
@@ -386,7 +386,7 @@ def normalize_sent_headers(headers, hdr_validation_flags):
386386
return headers
387387

388388

389-
def validate_sent_headers(headers, hdr_validation_flags):
389+
def validate_outbound_headers(headers, hdr_validation_flags):
390390
"""
391391
Validates and normalizes a header sequence that we are about to send.
392392

test/test_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def test_defaults(self):
2424

2525
boolean_config_options = [
2626
'client_side',
27-
'validate_sent_headers',
28-
'normalize_sent_headers',
27+
'validate_outbound_headers',
28+
'normalize_outbound_headers',
2929
'validate_inbound_headers'
3030
]
3131

test/test_invalid_headers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ def test_headers_event(self, frame_factory, headers):
144144
@pytest.mark.parametrize('headers', invalid_header_blocks)
145145
def test_headers_event_skipping_validation(self, frame_factory, headers):
146146
"""
147-
If we have ``validate_sent_headers`` disabled, then all of these
147+
If we have ``validate_outbound_headers`` disabled, then all of these
148148
invalid header blocks are allowed to pass.
149149
"""
150150
config = h2.config.H2Configuration(
151-
validate_sent_headers=False)
151+
validate_outbound_headers=False)
152152

153153
c = h2.connection.H2Connection(config=config)
154154
c.initiate_connection()
@@ -160,12 +160,12 @@ def test_headers_event_skipping_validation(self, frame_factory, headers):
160160
@pytest.mark.parametrize('headers', invalid_header_blocks)
161161
def test_headers_event_skip_normalization(self, frame_factory, headers):
162162
"""
163-
If we have ``normalize_sent_headers`` disabled, then all of these
163+
If we have ``normalize_outbound_headers`` disabled, then all of these
164164
invalid header blocks are sent through unmodified.
165165
"""
166166
config = h2.config.H2Configuration(
167-
validate_sent_headers=False,
168-
normalize_sent_headers=False)
167+
validate_outbound_headers=False,
168+
normalize_outbound_headers=False)
169169

170170
c = h2.connection.H2Connection(config=config)
171171
c.initiate_connection()
@@ -192,7 +192,7 @@ class TestFilter(object):
192192
"""
193193
validation_functions = [
194194
h2.utilities.validate_headers,
195-
h2.utilities.validate_sent_headers
195+
h2.utilities.validate_outbound_headers
196196
]
197197

198198
hdr_validation_combos = [

0 commit comments

Comments
 (0)