Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DefaultRedirectPolicy #403

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,20 +321,10 @@ func (c *Client) GetTLSClientConfig() *tls.Config {
return c.TLSClientConfig
}

func (c *Client) defaultCheckRedirect(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return errors.New("stopped after 10 redirects")
}
if c.DebugLog {
c.log.Debugf("<redirect> %s %s", req.Method, req.URL.String())
}
return nil
}

// SetRedirectPolicy set the RedirectPolicy which controls the behavior of receiving redirect
// responses (usually responses with 301 and 302 status code), see the predefined
// AllowedDomainRedirectPolicy, AllowedHostRedirectPolicy, MaxRedirectPolicy, NoRedirectPolicy,
// SameDomainRedirectPolicy and SameHostRedirectPolicy.
// AllowedDomainRedirectPolicy, AllowedHostRedirectPolicy, DefaultRedirectPolicy, MaxRedirectPolicy,
// NoRedirectPolicy, SameDomainRedirectPolicy and SameHostRedirectPolicy.
func (c *Client) SetRedirectPolicy(policies ...RedirectPolicy) *Client {
if len(policies) == 0 {
return c
Expand Down Expand Up @@ -1565,7 +1555,7 @@ func C() *Client {
xmlUnmarshal: xml.Unmarshal,
cookiejarFactory: memoryCookieJarFactory,
}
httpClient.CheckRedirect = c.defaultCheckRedirect
c.SetRedirectPolicy(DefaultRedirectPolicy())
c.initCookieJar()

c.initTransport()
Expand Down
4 changes: 4 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ func TestRedirect(t *testing.T) {
tests.AssertNotNil(t, err)
tests.AssertContains(t, err.Error(), "stopped after 3 redirects", true)

_, err = tc().SetRedirectPolicy(MaxRedirectPolicy(20)).SetRedirectPolicy(DefaultRedirectPolicy()).R().Get("/unlimited-redirect")
tests.AssertNotNil(t, err)
tests.AssertContains(t, err.Error(), "stopped after 10 redirects", true)

_, err = tc().SetRedirectPolicy(SameDomainRedirectPolicy()).R().Get("/redirect-to-other")
tests.AssertNotNil(t, err)
tests.AssertContains(t, err.Error(), "different domain name is not allowed", true)
Expand Down
5 changes: 5 additions & 0 deletions redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func MaxRedirectPolicy(noOfRedirect int) RedirectPolicy {
}
}

// DefaultRedirectPolicy allows up to 10 redirects
func DefaultRedirectPolicy() RedirectPolicy {
return MaxRedirectPolicy(10)
}

// NoRedirectPolicy disable redirect behaviour
func NoRedirectPolicy() RedirectPolicy {
return func(req *http.Request, via []*http.Request) error {
Expand Down