feat: add PreserveHost extension to skip Host header removal#177
Open
bearded-giant wants to merge 2 commits intoomjadas:mainfrom
Open
feat: add PreserveHost extension to skip Host header removal#177bearded-giant wants to merge 2 commits intoomjadas:mainfrom
bearded-giant wants to merge 2 commits intoomjadas:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat: add
PreserveHostextension to opt out of Host header removalSummary
This adds a
PreserveHostmarker type that can be inserted into request extensions duringhandle_requestto preventnormalize_requestfrom stripping theHostheader. The current behavior (unconditional removal) is preserved as the default.Problem
normalize_requestunconditionally removes theHostheader before forwarding:This runs after
HttpHandler::handle_requestreturns, so anyHostheader set by the handler is always discarded. Hyper then regenerates it from the URI authority.For reverse proxy use cases where the request URI is rewritten to a local target (e.g.
http://localhost:3000) but the upstream server needs the originalHostheader (e.g.Host: store.example.com) for virtual hosting, cookie domains, or redirect generation, there is no way to preserve it. The handler can set it, butnormalize_requestdeletes it unconditionally.Solution
A zero-cost opt-in using
http::Extensions:Usage in a handler:
Design choices
HttpHandlertrait,ProxyBuilder, or any public API. Extensions are already the idiomatic hyper mechanism for passing per-request metadata through middleware.Hostheader is only preserved when the handler explicitly requests it.Option::is_none()check on aTypeMaplookup. No allocations, no branching in the common path beyond the type check.Tests
removes_host_headertest passes unchangedpreserves_host_header_with_extensiontest verifies the opt-in pathCompatibility
HttpHandlerimplementations