// Copyright (c) 2015-2023 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.// resty source code and usage is governed by a MIT style// license that can be found in the LICENSE file.package restyimport ()var (// Since v2.8.0ErrAutoRedirectDisabled = errors.New("auto redirect is disabled"))type (// RedirectPolicy to regulate the redirects in the resty client. // Objects implementing the RedirectPolicy interface can be registered as // // Apply function should return nil to continue the redirect journey, otherwise // return error to stop the redirect.RedirectPolicyinterface {Apply(req *http.Request, via []*http.Request) error }// The RedirectPolicyFunc type is an adapter to allow the use of ordinary functions as RedirectPolicy. // If f is a function with the appropriate signature, RedirectPolicyFunc(f) is a RedirectPolicy object that calls f.RedirectPolicyFuncfunc(*http.Request, []*http.Request) error)// Apply calls f(req, via).func ( RedirectPolicyFunc) ( *http.Request, []*http.Request) error {return (, )}// NoRedirectPolicy is used to disable redirects in the HTTP client//// resty.SetRedirectPolicy(NoRedirectPolicy())func () RedirectPolicy {returnRedirectPolicyFunc(func( *http.Request, []*http.Request) error {returnErrAutoRedirectDisabled })}// FlexibleRedirectPolicy is convenient method to create No of redirect policy for HTTP client.//// resty.SetRedirectPolicy(FlexibleRedirectPolicy(20))func ( int) RedirectPolicy {returnRedirectPolicyFunc(func( *http.Request, []*http.Request) error {iflen() >= {returnfmt.Errorf("stopped after %d redirects", ) }checkHostAndAddHeaders(, [0])returnnil })}// DomainCheckRedirectPolicy is convenient method to define domain name redirect rule in resty client.// Redirect is allowed for only mentioned host in the policy.//// resty.SetRedirectPolicy(DomainCheckRedirectPolicy("host1.com", "host2.org", "host3.net"))func ( ...string) RedirectPolicy { := make(map[string]bool)for , := range { [strings.ToLower()] = true } := RedirectPolicyFunc(func( *http.Request, []*http.Request) error {if := [getHostname(.URL.Host)]; ! {returnerrors.New("redirect is not allowed as per DomainCheckRedirectPolicy") }returnnil })return}//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾// Package Unexported methods//_______________________________________________________________________func getHostname( string) ( string) {ifstrings.Index(, ":") > 0 { , _, _ = net.SplitHostPort() } = strings.ToLower()return}// By default Golang will not redirect request headers// after go throwing various discussion comments from thread// https://github.com/golang/go/issues/4800// Resty will add all the headers during a redirect for the same hostfunc checkHostAndAddHeaders( *http.Request, *http.Request) { := getHostname(.URL.Host) := getHostname(.URL.Host)ifstrings.EqualFold(, ) {for , := range .Header { .Header[] = } } else { // only library User-Agent header is added .Header.Set(hdrUserAgentKey, hdrUserAgentValue) }}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.