Skip to content

Support absolute-form request targets - #396

Open
mayur-dhavan wants to merge 6 commits into
pgjones:mainfrom
mayur-dhavan:fix/absolute-form-request-target
Open

mayur-dhavan wants to merge 6 commits into
pgjones:mainfrom
mayur-dhavan:fix/absolute-form-request-target

Conversation

@mayur-dhavan

Copy link
Copy Markdown

RFC 9112, section 3.2.2 requires a server to accept the absolute-form of a
request target:

A server MUST accept the absolute-form in requests even though most
HTTP/1.1 clients will only send the absolute-form to a proxy.

Hypercorn currently places the entire target into the ASGI scope, so an
application routes on a path that is not a path:

GET http://example.org/one/two?a=1 HTTP/1.1
Host: example.org
scope["path"]      == "http://example.org/one/two"   # expected "/one/two"
scope["raw_path"]  == b"http://example.org/one/two"  # expected b"/one/two"

With root_path="/api" the two are concatenated into
"/apihttp://example.org/one/two". The WebSocket handshake scope is
affected the same way.

This change adds reduce_target_to_origin_form() in hypercorn/utils.py
and applies it where the HTTP and WebSocket scopes are built
(protocol/http_stream.py and protocol/ws_stream.py). All protocol
versions build their scopes through these two streams, so h2 and h3
requests pass through the same code path; the change itself was exercised
end-to-end over HTTP/1.1 and the HTTP/1.1 WebSocket upgrade.

An absolute-form target with an empty path becomes "/", matching the
origin-form rule in RFC 9112, section 3.2.1 (a proxy forwarding to an
origin server applies the same replacement, RFC 9110, section 7.7).
Origin-form (/foo), asterisk-form (*) and authority-form
(example.org:443) targets are returned unchanged, so behaviour for the
other target forms is untouched.

Tests: unit cases for the helper covering every target form, plus
scope-level tests for the HTTP and WebSocket streams that fail before the
change and pass after it.

Testing notes: pytest tests/ gives 210 passed with 2 failures,
tests/asyncio/test_sanity.py::test_http2_websocket and
tests/trio/test_sanity.py::test_http2_websocket, both raising
h2.exceptions.ProtocolError('Ordinary CONNECT MUST NOT include :scheme or :path'); both fail identically on a clean checkout of main (h2 library
version incompatibility, unrelated to this change). mypy reports one
error that also pre-exists on main: protocol/http_stream.py:210: error: Missing key "trailers" for TypedDict "HTTPResponseStartEvent".
black --check and flake8 pass on the changed files.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 07:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants