1// Copyright (c) 2015-2019 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
2// resty source code and usage is governed by a MIT style
3// license that can be found in the LICENSE file.
4
5package resty
6
7import (
8	"errors"
9	"fmt"
10	"net"
11	"net/http"
12	"strings"
13)
14
15type (
16	// RedirectPolicy to regulate the redirects in the resty client.
17	// Objects implementing the RedirectPolicy interface can be registered as
18	//
19	// Apply function should return nil to continue the redirect jounery, otherwise
20	// return error to stop the redirect.
21	RedirectPolicy interface {
22		Apply(req *http.Request, via []*http.Request) error
23	}
24
25	// The RedirectPolicyFunc type is an adapter to allow the use of ordinary functions as RedirectPolicy.
26	// If f is a function with the appropriate signature, RedirectPolicyFunc(f) is a RedirectPolicy object that calls f.
27	RedirectPolicyFunc func(*http.Request, []*http.Request) error
28)
29
30// Apply calls f(req, via).
31func (f RedirectPolicyFunc) Apply(req *http.Request, via []*http.Request) error {
32	return f(req, via)
33}
34
35// NoRedirectPolicy is used to disable redirects in the HTTP client
36// 		resty.SetRedirectPolicy(NoRedirectPolicy())
37func NoRedirectPolicy() RedirectPolicy {
38	return RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
39		return errors.New("auto redirect is disabled")
40	})
41}
42
43// FlexibleRedirectPolicy is convenient method to create No of redirect policy for HTTP client.
44// 		resty.SetRedirectPolicy(FlexibleRedirectPolicy(20))
45func FlexibleRedirectPolicy(noOfRedirect int) RedirectPolicy {
46	return RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
47		if len(via) >= noOfRedirect {
48			return fmt.Errorf("stopped after %d redirects", noOfRedirect)
49		}
50
51		checkHostAndAddHeaders(req, via[0])
52
53		return nil
54	})
55}
56
57// DomainCheckRedirectPolicy is convenient method to define domain name redirect rule in resty client.
58// Redirect is allowed for only mentioned host in the policy.
59// 		resty.SetRedirectPolicy(DomainCheckRedirectPolicy("host1.com", "host2.org", "host3.net"))
60func DomainCheckRedirectPolicy(hostnames ...string) RedirectPolicy {
61	hosts := make(map[string]bool)
62	for _, h := range hostnames {
63		hosts[strings.ToLower(h)] = true
64	}
65
66	fn := RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
67		if ok := hosts[getHostname(req.URL.Host)]; !ok {
68			return errors.New("redirect is not allowed as per DomainCheckRedirectPolicy")
69		}
70
71		return nil
72	})
73
74	return fn
75}
76
77func getHostname(host string) (hostname string) {
78	if strings.Index(host, ":") > 0 {
79		host, _, _ = net.SplitHostPort(host)
80	}
81	hostname = strings.ToLower(host)
82	return
83}
84
85// By default Golang will not redirect request headers
86// after go throughing various discussion comments from thread
87// https://github.com/golang/go/issues/4800
88// go-resty will add all the headers during a redirect for the same host
89func checkHostAndAddHeaders(cur *http.Request, pre *http.Request) {
90	curHostname := getHostname(cur.URL.Host)
91	preHostname := getHostname(pre.URL.Host)
92	if strings.EqualFold(curHostname, preHostname) {
93		for key, val := range pre.Header {
94			cur.Header[key] = val
95		}
96	} else { // only library User-Agent header is added
97		cur.Header.Set(hdrUserAgentKey, fmt.Sprintf(hdrUserAgentValue, Version))
98	}
99}
100