Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
69 lines
2.0 KiB
Go
69 lines
2.0 KiB
Go
// 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)
|
|
}
|
|
|
|
type TransportFunc func(*http.Request) (*http.Response, error)
|
|
|
|
func (f TransportFunc) RoundTrip(req *http.Request) (*http.Response, error) { return f(req) }
|