refactor: move base http3 transport into netxlite (#412)

This diff is part of https://github.com/ooni/probe/issues/1505.

You will notice that I have not adapted all the (great) tests we had
previously. They should live at another layer, and namely the one that
deals with performing measurements.

When I'm refactoring such a layer I'll ensure those tests that I have
not adapted here are reintroduced into the tree.
This commit is contained in:
Simone Basso
2021-06-30 15:19:10 +02:00
committed by GitHub
parent 527e1a0707
commit 4dc2907472
6 changed files with 87 additions and 196 deletions
+54
View File
@@ -0,0 +1,54 @@
package netxlite
import (
"context"
"crypto/tls"
"net/http"
"github.com/lucas-clemente/quic-go"
"github.com/lucas-clemente/quic-go/http3"
)
// http3Dialer adapts a QUICContextDialer to work with
// an http3.RoundTripper. This is necessary because the
// http3.RoundTripper does not support DialContext.
type http3Dialer struct {
Dialer QUICContextDialer
}
// dial is like QUICContextDialer.DialContext but without context.
func (d *http3Dialer) dial(network, address string, tlsConfig *tls.Config,
quicConfig *quic.Config) (quic.EarlySession, error) {
return d.Dialer.DialContext(
context.Background(), network, address, tlsConfig, quicConfig)
}
// http3Transport is an HTTPTransport using the http3 protocol.
type http3Transport struct {
child *http3.RoundTripper
}
var _ HTTPTransport = &http3Transport{}
// RoundTrip implements HTTPTransport.RoundTrip.
func (txp *http3Transport) RoundTrip(req *http.Request) (*http.Response, error) {
return txp.child.RoundTrip(req)
}
// CloseIdleConnections implements HTTPTransport.CloseIdleConnections.
func (txp *http3Transport) CloseIdleConnections() {
txp.child.Close()
}
// NewHTTP3Transport creates a new HTTPTransport using http3. The
// dialer argument MUST NOT be nil. If the tlsConfig argument is nil,
// then the code will use the default TLS configuration.
func NewHTTP3Transport(
dialer QUICContextDialer, tlsConfig *tls.Config) HTTPTransport {
return &http3Transport{
child: &http3.RoundTripper{
Dial: (&http3Dialer{dialer}).dial,
TLSClientConfig: tlsConfig,
},
}
}
+25
View File
@@ -0,0 +1,25 @@
package netxlite
import (
"crypto/tls"
"net"
"net/http"
"testing"
)
func TestHTTP3TransportWorks(t *testing.T) {
d := &QUICDialerResolver{
Dialer: &QUICDialerQUICGo{
QUICListener: &QUICListenerStdlib{},
},
Resolver: &net.Resolver{},
}
txp := NewHTTP3Transport(d, &tls.Config{})
client := &http.Client{Transport: txp}
resp, err := client.Get("https://www.google.com/robots.txt")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
txp.CloseIdleConnections()
}