Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option to pass raw h11 headers #117

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/how_to_guides/configuring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ read_timeout ``--read-timeout`` Seconds to wait before
group ``-g``, ``--group`` Group to own any unix sockets.
h11_max_incomplete_size N/A The max HTTP/1.1 request line + headers 16KiB
size in bytes.
h11_pass_raw_headers N/A Pass the raw headers from h11 to the ``False``
Request object, which preserves header
casing.
h2_max_concurrent_streams N/A Maximum number of HTTP/2 concurrent 100
streams.
h2_max_header_list_size N/A Maximum number of HTTP/2 headers. 65536
Expand Down
1 change: 1 addition & 0 deletions src/hypercorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Config:
read_timeout: Optional[int] = None
group: Optional[int] = None
h11_max_incomplete_size = 16 * 1024 * BYTES
h11_pass_raw_headers = False
h2_max_concurrent_streams = 100
h2_max_header_list_size = 2**16
h2_max_inbound_frame_size = 2**14 * OCTETS
Expand Down
9 changes: 8 additions & 1 deletion src/hypercorn/protocol/h11.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,17 @@ async def _create_stream(self, request: h11.Request) -> None:
self.stream_send,
STREAM_ID,
)

if self.config.h11_pass_raw_headers:
# uses unmodified header names as they are passed by the client
headers = request.headers.raw_items()
else:
headers = list(request.headers)

await self.stream.handle(
Request(
stream_id=STREAM_ID,
headers=list(request.headers),
headers=headers,
http_version=request.http_version.decode(),
method=request.method.decode("ascii").upper(),
raw_path=request.target,
Expand Down
27 changes: 27 additions & 0 deletions tests/protocol/test_h11.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,33 @@ async def test_protocol_handle_request(protocol: H11Protocol) -> None:
]


@pytest.mark.asyncio
async def test_protocol_handle_request_with_raw_headers(protocol: H11Protocol) -> None:
protocol.config.h11_pass_raw_headers = True
client = h11.Connection(h11.CLIENT)
headers = BASIC_HEADERS + [('FOO_BAR', 'foobar')]
await protocol.handle(
RawData(data=client.send(h11.Request(method="GET", target="/?a=b", headers=headers)))
)
protocol.stream.handle.assert_called() # type: ignore
assert protocol.stream.handle.call_args_list == [ # type: ignore
call(
Request(
stream_id=1,
headers=[
(b"Host", b"hypercorn"),
(b"Connection", b"close"),
(b"FOO_BAR", b"foobar"),
],
http_version="1.1",
method="GET",
raw_path=b"/?a=b",
)
),
call(EndBody(stream_id=1)),
]


@pytest.mark.asyncio
async def test_protocol_handle_protocol_error(protocol: H11Protocol) -> None:
await protocol.handle(RawData(data=b"broken nonsense\r\n\r\n"))
Expand Down