Add configurable redirect status codes with zero-cost optimization#33
Merged
MinecraftFuns merged 5 commits intomainfrom Dec 8, 2025
Merged
Conversation
Co-authored-by: MinecraftFuns <25814618+MinecraftFuns@users.noreply.github.com>
Co-authored-by: MinecraftFuns <25814618+MinecraftFuns@users.noreply.github.com>
Co-authored-by: MinecraftFuns <25814618+MinecraftFuns@users.noreply.github.com>
Co-authored-by: MinecraftFuns <25814618+MinecraftFuns@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add configuration options for redirect status codes
Add configurable redirect status codes with zero-cost optimization
Dec 8, 2025
MinecraftFuns
approved these changes
Dec 8, 2025
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.
Implements configurable HTTP redirect status codes (301/302/303/307/308) via
REDIRECT_STATUS_CODEenvironment variable, defaulting to 308. ReplacesRedirectwrapper with direct(StatusCode, HeaderMap)construction for zero runtime overhead.Changes
Config validation:
RedirectModeenum validates redirect codes at startup viaTryFrom<u16>, preventing invalid codes (e.g., 200, 404)Permanent(308),Temporary(307),SeeOther(303),MovedPermanently(301),Found(302)Zero-cost optimization: Store
StatusCodedirectly inRedirectState, construct responses with(StatusCode, [(LOCATION, HeaderValue)])for fast path or(StatusCode, HeaderMap)for timing headersError handling: Graceful handling of invalid URLs with detailed logging but generic user messages to prevent information disclosure
Example
Configuration:
Invalid codes rejected at startup with clear error message.
Original prompt
Can you add configuration options for the specific redirect code used (301/302/303/307/308) defaults to 308 if nothing is configured; use a custom type to validate the configuration options, and convert to Status Code in the State storage so you can use it directly during the redirect path without additional cost.
you should follow Rust best practices and write efficient code to maximize performance. attached is the documentation FYI.
To support configurable redirects (HTTP 301/302/307/308) without performance penalties, you should avoid the
Redirectstruct wrapper and instead construct the response directly using the(StatusCode, HeaderMap)or(StatusCode, [(HeaderName, HeaderValue)])pattern. This is the "rawest" and fastest way to return a response in Axum, bypassing the slight overhead of theRedirectstruct's internal assertions and construction.Store the desired
StatusCodein yourRedirectState(or a config struct within it). Accessing this field is a simple memory read and introduces zero runtime overhead compared to the hardcoded constant, while the header construction cost remains identical toRedirect::permanent.Here is the implementation: