package req

import (
	
	
	
	
	
)

// RedirectPolicy represents the redirect policy for Client.
type RedirectPolicy func(req *http.Request, via []*http.Request) error

// MaxRedirectPolicy specifies the max number of redirect
func ( int) RedirectPolicy {
	return func( *http.Request,  []*http.Request) error {
		if len() >=  {
			return fmt.Errorf("stopped after %d redirects", )
		}
		return nil
	}
}

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

// NoRedirectPolicy disable redirect behaviour
func () RedirectPolicy {
	return func( *http.Request,  []*http.Request) error {
		return http.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 {
	return func( *http.Request,  []*http.Request) error {
		if getDomain(.URL.Host) != getDomain([0].URL.Host) {
			return errors.New("different domain name is not allowed")
		}
		return nil
	}
}

// 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 {
	return func( *http.Request,  []*http.Request) error {
		if getHostname(.URL.Host) != getHostname([0].URL.Host) {
			return errors.New("different host name is not allowed")
		}
		return nil
	}
}

// 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{}{}
	}

	return func( *http.Request,  []*http.Request) error {
		 := getHostname(.URL.Host)
		if ,  := []; ! {
			return fmt.Errorf("redirect host [%s] is not allowed", )
		}
		return nil
	}
}

// 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{}{}
	}

	return func( *http.Request,  []*http.Request) error {
		 := getDomain(.URL.Host)
		if ,  := []; ! {
			return fmt.Errorf("redirect domain [%s] is not allowed", )
		}
		return nil
	}
}

func getHostname( string) ( string) {
	if strings.Index(, ":") > 0 {
		, _, _ = net.SplitHostPort()
	}
	 = strings.ToLower()
	return
}

func getDomain( string) string {
	 = getHostname()
	 := strings.Split(, ".")
	if len() < 3 {
		return 
	}
	 = [1:]
	return strings.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 {
	return func( *http.Request,  []*http.Request) error {
		for ,  := range  {
			if len(.Header.Values()) > 0 {
				continue
			}
			 := [0].Header.Values()
			for ,  := range  {
				.Header.Add(, )
			}
		}
		return nil
	}
}

// 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 {
	return func( *http.Request,  []*http.Request) error {
		if len() == 0 {
			return nil
		}
		// Only strip headers when redirecting to a different domain.
		if getDomain(.URL.Host) == getDomain([0].URL.Host) {
			return nil
		}
		for ,  := range  {
			.Header.Del()
		}
		return nil
	}
}