package reqimport ()// RedirectPolicy represents the redirect policy for Client.typeRedirectPolicyfunc(req *http.Request, via []*http.Request) error// MaxRedirectPolicy specifies the max number of redirectfunc ( int) RedirectPolicy {returnfunc( *http.Request, []*http.Request) error {iflen() >= {returnfmt.Errorf("stopped after %d redirects", ) }returnnil }}// DefaultRedirectPolicy allows up to 10 redirectsfunc () RedirectPolicy {returnMaxRedirectPolicy(10)}// NoRedirectPolicy disable redirect behaviourfunc () RedirectPolicy {returnfunc( *http.Request, []*http.Request) error {returnhttp.ErrUseLastResponse }}// SameDomainRedirectPolicy allows redirect only if the redirected domain// is the same as original domain, e.g. redirect to "www.imroc.cc" from// "imroc.cc" is allowed, but redirect to "google.com" is not allowed.func () RedirectPolicy {returnfunc( *http.Request, []*http.Request) error {ifgetDomain(.URL.Host) != getDomain([0].URL.Host) {returnerrors.New("different domain name is not allowed") }returnnil }}// SameHostRedirectPolicy allows redirect only if the redirected host// is the same as original host, e.g. redirect to "www.imroc.cc" from// "imroc.cc" is not the allowed.func () RedirectPolicy {returnfunc( *http.Request, []*http.Request) error {ifgetHostname(.URL.Host) != getHostname([0].URL.Host) {returnerrors.New("different host name is not allowed") }returnnil }}// AllowedHostRedirectPolicy allows redirect only if the redirected host// match one of the host that specified.func ( ...string) RedirectPolicy { := make(map[string]struct{})for , := range { [strings.ToLower(getHostname())] = struct{}{} }returnfunc( *http.Request, []*http.Request) error { := getHostname(.URL.Host)if , := []; ! {returnfmt.Errorf("redirect host [%s] is not allowed", ) }returnnil }}// AllowedDomainRedirectPolicy allows redirect only if the redirected domain// match one of the domain that specified.func ( ...string) RedirectPolicy { := make(map[string]struct{})for , := range { [strings.ToLower(getDomain())] = struct{}{} }returnfunc( *http.Request, []*http.Request) error { := getDomain(.URL.Host)if , := []; ! {returnfmt.Errorf("redirect domain [%s] is not allowed", ) }returnnil }}func getHostname( string) ( string) {ifstrings.Index(, ":") > 0 { , _, _ = net.SplitHostPort() } = strings.ToLower()return}func getDomain( string) string { = getHostname() := strings.Split(, ".")iflen() < 3 {return } = [1:]returnstrings.Join(, ".")}// AlwaysCopyHeaderRedirectPolicy ensures that the given sensitive headers will// always be copied on redirect.// By default, golang will copy all of the original request's headers on redirect,// unless they're sensitive, like "Authorization" or "Www-Authenticate". Only send// sensitive ones to the same origin, or subdomains thereof (https://go-review.googlesource.com/c/go/+/28930/)// Check discussion: https://github.com/golang/go/issues/4800// For example://// client.SetRedirectPolicy(req.AlwaysCopyHeaderRedirectPolicy("Authorization"))func ( ...string) RedirectPolicy {returnfunc( *http.Request, []*http.Request) error {for , := range {iflen(.Header.Values()) > 0 {continue } := [0].Header.Values()for , := range { .Header.Add(, ) } }returnnil }}// SensitiveHeadersRedirectPolicy strips the given sensitive headers when the// redirect target is a different domain. This is useful for custom authentication// headers (e.g. "X-API-Key", "X-Auth-Token") that are not automatically stripped// by the standard library on cross-domain redirects.//// By default, Go's net/http only strips "Authorization", "Cookie", and// "Proxy-Authorization" on cross-domain redirects. Custom headers set via// SetCommonHeader or SetHeader are forwarded to the redirect target, which can// lead to credential leakage (CWE-200).//// For example://// client.SetRedirectPolicy(req.SensitiveHeadersRedirectPolicy("X-API-Key", "X-Auth-Token"))func ( ...string) RedirectPolicy {returnfunc( *http.Request, []*http.Request) error {iflen() == 0 {returnnil }// Only strip headers when redirecting to a different domain.ifgetDomain(.URL.Host) == getDomain([0].URL.Host) {returnnil }for , := range { .Header.Del() }returnnil }}
The pages are generated with Goldsv0.8.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 @zigo_101 (reachable from the left QR code) to get the latest news of Golds.