feat(netxlite): introduce wrapping constructors (#507)
This diff has been extracted from https://github.com/ooni/probe-cli/pull/506. In it, we introduce wrapping constructors for types and we update the docs. These new constructures are used by the code in https://github.com/ooni/probe-cli/pull/506. In itself, this work is part of https://github.com/ooni/probe/issues/1733.
This commit is contained in:
parent
985c1ba761
commit
741a8bc4c2
3 changed files with 45 additions and 25 deletions
|
|
@ -82,11 +82,15 @@ func (txp *httpTransportConnectionsCloser) CloseIdleConnections() {
|
|||
txp.TLSDialer.CloseIdleConnections()
|
||||
}
|
||||
|
||||
// NewHTTPTransport creates a new HTTP transport using the given
|
||||
// NewHTTPTransport combines NewOOHTTPBaseTransport and
|
||||
// WrapHTTPTransport to construct a new HTTPTransport.
|
||||
func NewHTTPTransport(logger Logger, dialer Dialer, tlsDialer TLSDialer) HTTPTransport {
|
||||
return WrapHTTPTransport(logger, NewOOHTTPBaseTransport(dialer, tlsDialer))
|
||||
}
|
||||
|
||||
// NewOOHTTPBaseTransport creates a new HTTP transport using the given
|
||||
// dialer and TLS dialer to create connections.
|
||||
//
|
||||
// The returned transport will use the given Logger for logging.
|
||||
//
|
||||
// The returned transport will gracefully handle TLS connections
|
||||
// created using gitlab.com/yawning/utls.git.
|
||||
//
|
||||
|
|
@ -106,10 +110,7 @@ func (txp *httpTransportConnectionsCloser) CloseIdleConnections() {
|
|||
// necessary to perform sane measurements with tracing. We will be
|
||||
// able to possibly relax this requirement after we change the
|
||||
// way in which we perform measurements.
|
||||
//
|
||||
// The returned transport will set a default user agent if the
|
||||
// request has not already set a user agent.
|
||||
func NewHTTPTransport(logger Logger, dialer Dialer, tlsDialer TLSDialer) HTTPTransport {
|
||||
func NewOOHTTPBaseTransport(dialer Dialer, tlsDialer TLSDialer) HTTPTransport {
|
||||
// Using oohttp to support any TLS library.
|
||||
txp := oohttp.DefaultTransport.(*oohttp.Transport).Clone()
|
||||
|
||||
|
|
@ -138,16 +139,24 @@ func NewHTTPTransport(logger Logger, dialer Dialer, tlsDialer TLSDialer) HTTPTra
|
|||
// upon us when we are using TLS parroting).
|
||||
txp.ForceAttemptHTTP2 = true
|
||||
|
||||
// Ensure we correctly forward CloseIdleConnections and compose
|
||||
// with a logging transport thus enabling logging.
|
||||
// Ensure we correctly forward CloseIdleConnections.
|
||||
return &httpTransportConnectionsCloser{
|
||||
HTTPTransport: &oohttp.StdlibTransport{Transport: txp},
|
||||
Dialer: dialer,
|
||||
TLSDialer: tlsDialer,
|
||||
}
|
||||
}
|
||||
|
||||
// WrapHTTPTransport creates a new HTTP transport using
|
||||
// the given logger for logging.
|
||||
//
|
||||
// The returned transport will set a default user agent if the
|
||||
// request has not already set a user agent.
|
||||
func WrapHTTPTransport(logger Logger, txp HTTPTransport) HTTPTransport {
|
||||
return &httpUserAgentTransport{
|
||||
HTTPTransport: &httpTransportLogger{
|
||||
HTTPTransport: &httpTransportConnectionsCloser{
|
||||
HTTPTransport: &oohttp.StdlibTransport{Transport: txp},
|
||||
Dialer: dialer,
|
||||
TLSDialer: tlsDialer,
|
||||
},
|
||||
Logger: logger,
|
||||
HTTPTransport: txp,
|
||||
Logger: logger,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue