-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
option.go
93 lines (82 loc) · 1.81 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package httploader
import (
"crypto/tls"
"net/http"
"strings"
)
type Option func(h *HTTPLoader)
func WithTransport(transport http.RoundTripper) Option {
return func(h *HTTPLoader) {
if transport != nil {
h.Transport = transport
}
}
}
func WithInsecureSkipVerifyTransport(enabled bool) Option {
return func(h *HTTPLoader) {
if enabled {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
h.Transport = transport
}
}
}
func WithForwardHeaders(headers ...string) Option {
return func(h *HTTPLoader) {
for _, raw := range headers {
splits := strings.Split(raw, ",")
for _, header := range splits {
header = strings.TrimSpace(header)
if len(header) > 0 {
h.ForwardHeaders = append(h.ForwardHeaders, header)
}
}
}
}
}
func WithForwardAllHeaders(enabled bool) Option {
return func(h *HTTPLoader) {
if enabled {
h.ForwardHeaders = []string{"*"}
}
}
}
func WithOverrideHeader(name, value string) Option {
return func(h *HTTPLoader) {
h.OverrideHeaders[name] = value
}
}
func WithAllowedSources(hosts ...string) Option {
return func(h *HTTPLoader) {
for _, raw := range hosts {
splits := strings.Split(raw, ",")
for _, host := range splits {
host = strings.TrimSpace(host)
if len(host) > 0 {
h.AllowedSources = append(h.AllowedSources, host)
}
}
}
}
}
func WithMaxAllowedSize(maxAllowedSize int) Option {
return func(h *HTTPLoader) {
if maxAllowedSize > 0 {
h.MaxAllowedSize = maxAllowedSize
}
}
}
func WithUserAgent(userAgent string) Option {
return func(h *HTTPLoader) {
if userAgent != "" {
h.UserAgent = userAgent
}
}
}
func WithDefaultScheme(scheme string) Option {
return func(h *HTTPLoader) {
if scheme != "" {
h.DefaultScheme = scheme
}
}
}