Add pluggable redirect strategy support to RedirectConfiguration - #6
Open
o-nnerb wants to merge 3 commits into
Open
Add pluggable redirect strategy support to RedirectConfiguration#6o-nnerb wants to merge 3 commits into
o-nnerb wants to merge 3 commits into
Conversation
Adds RedirectConfiguration.custom(_:), letting callers intercept every redirect-eligible response and decide whether/how to follow it instead of being limited to disallow/follow(max:allowCycles:). The candidate request handed to the handler has already gone through the same method/header rewrite rules `.follow` applies (POST->GET on 303, stripping Authorization/Cookie/Origin/Proxy-Authorization cross-origin), so callers only need to make further adjustments — e.g. stripping additional sensitive headers before a cross-host redirect is followed. Wired into the Swift Concurrency execute(_:deadline:logger:) family only; the delegate-based execute(request:delegate:...) API fails fast with .invalidRedirectConfiguration since it has no HTTPClientRequest to hand the handler. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…tegy Replaces the bare-closure `.custom(_:)` from the previous commit with HTTPClientRedirectStrategy, a protocol callers can conform their own types to (not just closures), addressing the two gaps a maintainer flagged on the upstream issue thread: pluggable strategies as actual types, and access to more than a bare redirect count — the strategy now receives the full per-request history alongside the candidate request and response. - HTTPClientRedirectContext bundles redirectRequest/response/history/ redirectCount into one value instead of four positional parameters. - HTTPClientRedirectStrategy.redirectDecision(for:) can throw, so a strategy can fail the whole execute() call with a custom error instead of only following/refusing. - RedirectConfiguration.strategy(_:) is the primary entry point; .custom(_:) remains as a closure-based convenience over it via an internal ClosureRedirectStrategy adapter. - Mode.custom renamed to Mode.strategy to match. Still scoped to the Swift Concurrency execute(_:deadline:logger:) family only; the delegate-based API continues to fail fast with .invalidRedirectConfiguration. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
The previous two CI runs on this branch failed before any job started
("reference to workflow should be either a valid branch, tag, or
commit") because beta's swift-ci.yaml referenced a since-deleted
request-dl/.github branch. That's now fixed on beta (f360eae); this
empty commit just re-triggers the pull_request check with no code
changes.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.
Summary
RedirectConfiguration.strategy(_:)and aHTTPClientRedirectStrategyprotocol, letting callers intercept every redirect-eligible response and decide whether/how to follow it, instead of being limited to.disallow/.follow(max:allowCycles:).HTTPClientRedirectContextbundles the candidate request (already through the standard method/header rewrite rules, including cross-originAuthorization/Cookie/Origin/Proxy-Authorizationstripping), the response head, the full per-requesthistory, andredirectCount.redirectDecision(for:)canthrow, so a strategy can fail the whole request with a custom error instead of only following/refusing..custom(_:)remains as a closure-based convenience over.strategy(_:)for policies that don't need their own type..follow's limits); the strategy enforces its own policy using thehistory/redirectCountit's handed.execute(_:deadline:logger:)family only. The delegate-basedexecute(request:delegate:...)API fails fast with.invalidRedirectConfigurationif this mode is configured.Related upstream discussion: swift-server#923.
Test plan
swift buildswift test(full suite green aside from a pre-existingtestConnectTimeoutflake, confirmed to also fail on unmodifiedmain)HTTPClientRedirectStrategytype detecting cycles viahistory, a strategy throwing to fail the request, and the delegate-based API failing fast when this mode is configured🤖 Generated with Claude Code