/
transports.go
64 lines (54 loc) · 1.83 KB
/
transports.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package httpx
import "net/http"
// WrapTransportWithHeader wraps a http.Transport to always use the values from the given header.
func WrapTransportWithHeader(parent http.RoundTripper, h http.Header) *TransportWithHeader {
return &TransportWithHeader{
RoundTripper: parent,
h: h,
}
}
// NewTransportWithHeader returns a new http.Transport that always uses the values from the given header.
func NewTransportWithHeader(h http.Header) *TransportWithHeader {
return &TransportWithHeader{
RoundTripper: http.DefaultTransport,
h: h,
}
}
// TransportWithHeader is an http.RoundTripper that always uses the values from the given header.
type TransportWithHeader struct {
http.RoundTripper
h http.Header
}
// RoundTrip implements http.RoundTripper.
func (ct *TransportWithHeader) RoundTrip(req *http.Request) (*http.Response, error) {
for k := range ct.h {
req.Header.Set(k, ct.h.Get(k))
}
return ct.RoundTripper.RoundTrip(req)
}
// NewTransportWithHost returns a new http.Transport that always uses the given host.
func NewTransportWithHost(host string) *TransportWithHost {
return &TransportWithHost{
RoundTripper: http.DefaultTransport,
host: host,
}
}
// WrapRoundTripperWithHost wraps a http.RoundTripper that always uses the given host.
func WrapRoundTripperWithHost(parent http.RoundTripper, host string) *TransportWithHost {
return &TransportWithHost{
RoundTripper: parent,
host: host,
}
}
// TransportWithHost is an http.RoundTripper that always uses the given host.
type TransportWithHost struct {
http.RoundTripper
host string
}
// RoundTrip implements http.RoundTripper.
func (ct *TransportWithHost) RoundTrip(req *http.Request) (*http.Response, error) {
req.Host = ct.host
return ct.RoundTripper.RoundTrip(req)
}