Skip to content

Commit

Permalink
cleanup(netxlite): add file for quirky behavior required by netx
Browse files Browse the repository at this point in the history
There are a bunch of quirks in the codebase that are there because the
./legacy/netx package assumes them. So, in light of the fact that I'm
going to modify how we construct HTTP clients, I feel a bit safer if we
move what I should not touch in a different place.

Part of ooni/probe#2531
  • Loading branch information
bassosimone committed Sep 14, 2023
1 parent 873b4e1 commit f71b4a3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
18 changes: 1 addition & 17 deletions internal/netxlite/http.go → internal/netxlite/httplegacy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package netxlite

//
// HTTP/1.1 and HTTP2 code
// Legacy HTTP code and behavior assumed by ./legacy/netx 😅
//

import (
Expand Down Expand Up @@ -91,17 +91,6 @@ func newOOHTTPBaseTransport(dialer model.Dialer, tlsDialer model.TLSDialer) mode
}
}

// WrapHTTPTransport creates an HTTPTransport using the given logger
// and guarantees that returned errors are wrapped.
//
// This is a low level factory. Consider not using it directly.
func WrapHTTPTransport(logger model.DebugLogger, txp model.HTTPTransport) model.HTTPTransport {
return &httpTransportLogger{
HTTPTransport: &httpTransportErrWrapper{txp},
Logger: logger,
}
}

// NewHTTPTransportStdlib creates a new HTTPTransport using
// the stdlib for DNS resolutions and TLS.
//
Expand Down Expand Up @@ -139,8 +128,3 @@ func NewHTTPClientWithResolver(logger model.Logger, reso model.Resolver) model.H
func NewHTTPClient(txp model.HTTPTransport) model.HTTPClient {
return WrapHTTPClient(&http.Client{Transport: txp})
}

// WrapHTTPClient wraps an HTTP client to add error wrapping capabilities.
func WrapHTTPClient(clnt model.HTTPClient) model.HTTPClient {
return &httpClientErrWrapper{clnt}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,3 @@ func TestNewHTTPClientWithResolver(t *testing.T) {
t.Fatal("invalid resolver")
}
}

func TestWrapHTTPClient(t *testing.T) {
origClient := &http.Client{}
wrapped := WrapHTTPClient(origClient)
errWrapper := wrapped.(*httpClientErrWrapper)
innerClient := errWrapper.HTTPClient.(*http.Client)
if innerClient != origClient {
t.Fatal("not the inner client we expected")
}
}
27 changes: 27 additions & 0 deletions internal/netxlite/httpwrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package netxlite

//
// Wrappers for already constructed types.
//

import (
"github.com/ooni/probe-cli/v3/internal/model"
)

// WrapHTTPTransport creates an HTTPTransport using the given logger
// and guarantees that returned errors are wrapped.
//
// This is a low level factory. Consider not using it directly.
func WrapHTTPTransport(logger model.DebugLogger, txp model.HTTPTransport) model.HTTPTransport {
return &httpTransportLogger{
HTTPTransport: &httpTransportErrWrapper{txp},
Logger: logger,
}
}

// WrapHTTPClient wraps an HTTP client to add error wrapping capabilities.
//
// This is a low level factory. Consider not using it directly.
func WrapHTTPClient(clnt model.HTTPClient) model.HTTPClient {
return &httpClientErrWrapper{clnt}
}
16 changes: 16 additions & 0 deletions internal/netxlite/httpwrap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package netxlite

import (
"net/http"
"testing"
)

func TestWrapHTTPClient(t *testing.T) {
origClient := &http.Client{}
wrapped := WrapHTTPClient(origClient)
errWrapper := wrapped.(*httpClientErrWrapper)
innerClient := errWrapper.HTTPClient.(*http.Client)
if innerClient != origClient {
t.Fatal("not the inner client we expected")
}
}

0 comments on commit f71b4a3

Please sign in to comment.