-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
transport.go
70 lines (57 loc) · 1.63 KB
/
transport.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
package ratus
import (
"net/http"
"net/url"
)
// maxIdleConnsPerHost is the maximum number of idle (keep-alive) connections.
const maxIdleConnsPerHost = 1024
// transport wraps around http.DefaultTransport to rewrite origins and set HTTP
// headers for all outgoing requests.
type transport struct {
scheme string
host string
headers map[string]string
roundTripper http.RoundTripper
}
// newTransport creates a custom transport instance.
func newTransport(origin string, headers map[string]string) (*transport, error) {
// Parse the origin string to extract URL components for rewriting.
u, err := url.Parse(origin)
if err != nil {
return nil, err
}
// Inherit settings from http.DefaultTransport by cloning it.
t := http.DefaultTransport.(*http.Transport).Clone()
// Remove limits of maximum number of connections.
t.MaxIdleConns = 0
t.MaxConnsPerHost = 0
t.MaxIdleConnsPerHost = maxIdleConnsPerHost
return &transport{
scheme: u.Scheme,
host: u.Host,
headers: headers,
roundTripper: t,
}, nil
}
// RoundTrip implements the http.RoundTripper interface.
func (t *transport) RoundTrip(r *http.Request) (*http.Response, error) {
// Rewrite request URL components.
if t.scheme != "" {
r.URL.Scheme = t.scheme
}
if t.host != "" {
r.URL.Host = t.host
}
// Set common header fields.
for k, v := range t.headers {
if _, ok := r.Header[k]; !ok {
r.Header.Set(k, v)
}
}
// Set User-Agent if it is not present.
if _, ok := r.Header["User-Agent"]; !ok {
r.Header.Set("User-Agent", "Ratus-Client")
}
// Execute the modified HTTP transaction.
return t.roundTripper.RoundTrip(r)
}