Skip to content

Add pluggable redirect strategy support to RedirectConfiguration - #6

Open
o-nnerb wants to merge 3 commits into
betafrom
feature/redirect-custom-handler
Open

Add pluggable redirect strategy support to RedirectConfiguration#6
o-nnerb wants to merge 3 commits into
betafrom
feature/redirect-custom-handler

Conversation

@o-nnerb

@o-nnerb o-nnerb commented Sep 3, 2026

Copy link
Copy Markdown
Member

Summary

  • Adds RedirectConfiguration.strategy(_:) and a HTTPClientRedirectStrategy protocol, letting callers intercept every redirect-eligible response and decide whether/how to follow it, instead of being limited to .disallow/.follow(max:allowCycles:).
  • HTTPClientRedirectContext bundles the candidate request (already through the standard method/header rewrite rules, including cross-origin Authorization/Cookie/Origin/Proxy-Authorization stripping), the response head, the full per-request history, and redirectCount.
  • redirectDecision(for:) can throw, 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.
  • No built-in redirect-count/cycle limit in this mode (it replaces, not composes with, .follow's limits); the strategy enforces its own policy using the history/redirectCount it's handed.
  • Wired into the Swift Concurrency execute(_:deadline:logger:) family only. The delegate-based execute(request:delegate:...) API fails fast with .invalidRedirectConfiguration if this mode is configured.

Related upstream discussion: swift-server#923.

Test plan

  • swift build
  • swift test (full suite green aside from a pre-existing testConnectTimeout flake, confirmed to also fail on unmodified main)
  • Tests covering: rewriting the redirect request, refusing a redirect, a real HTTPClientRedirectStrategy type detecting cycles via history, a strategy throwing to fail the request, and the delegate-based API failing fast when this mode is configured

🤖 Generated with Claude Code

o-nnerb and others added 2 commits September 3, 2026 15:42
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>
@o-nnerb o-nnerb changed the title Add custom redirect handler support to RedirectConfiguration Add pluggable redirect strategy support to RedirectConfiguration Sep 3, 2026
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>
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.

1 participant