Skip to content

Commit

Permalink
Expand comment on DefaultClientRoundTripper
Browse files Browse the repository at this point in the history
And don't use the http.DefaultRoundTripper and cast
  • Loading branch information
MarcoPolo committed Aug 23, 2023
1 parent 7e37919 commit 8f7f71f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
16 changes: 8 additions & 8 deletions p2p/http/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func ExampleHost_withAStockGoHTTPClient() {
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
InsecureAllowHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/0/http")},
}

Expand Down Expand Up @@ -62,7 +62,7 @@ func ExampleHost_listenOnHTTPTransportAndStreams() {
log.Fatal(err)
}
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
InsecureAllowHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50124/http")},
StreamHost: serverStreamHost,
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func ExampleHost_overLibp2pStreams() {

func ExampleHost_Serve() {
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
InsecureAllowHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50221/http")},
}

Expand All @@ -135,7 +135,7 @@ func ExampleHost_Serve() {

func ExampleHost_SetHTTPHandler() {
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
InsecureAllowHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50222/http")},
}

Expand Down Expand Up @@ -169,7 +169,7 @@ func ExampleHost_SetHTTPHandler() {

func ExampleHost_SetHTTPHandlerAtPath() {
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
InsecureAllowHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50224/http")},
}

Expand Down Expand Up @@ -206,7 +206,7 @@ func ExampleHost_NamespacedClient() {

// Create the server
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
InsecureAllowHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50221/http")},
}

Expand Down Expand Up @@ -244,7 +244,7 @@ func ExampleHost_NamespaceRoundTripper() {

// Create the server
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
InsecureAllowHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50223/http")},
}

Expand Down Expand Up @@ -288,7 +288,7 @@ func ExampleHost_NewRoundTripper() {

// Create the server
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
InsecureAllowHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50225/http")},
}

Expand Down
26 changes: 18 additions & 8 deletions p2p/http/libp2phttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,20 @@ type Host struct {
ListenAddrs []ma.Multiaddr
// TLSConfig is the TLS config for the server to use
TLSConfig *tls.Config
// ServeInsecureHTTP indicates if the server should serve unencrypted HTTP requests over TCP.
ServeInsecureHTTP bool
// InsecureAllowHTTP indicates if the server is allowed to serve unencrypted
// HTTP requests over TCP.
InsecureAllowHTTP bool
// ServeMux is the http.ServeMux used by the server to serve requests. The
// zero value is a new serve mux. Users may manually add handlers to this
// mux instead of using `SetHTTPHandler`, but if they do, they should also
// update the WellKnownHandler's protocol mapping.
ServeMux http.ServeMux

// DefaultClientRoundTripper is the default http.RoundTripper for clients
// DefaultClientRoundTripper is the default http.RoundTripper for clients to
// use when making requests over an HTTP transport. This must be an
// `*http.Transport` type so that the transport can be cloned and the
// `TLSClientConfig` field can be configured. If unset, it will create a new
// `http.Transport` on first use.
DefaultClientRoundTripper *http.Transport

// WellKnownHandler is the http handler for the `.well-known/libp2p`
Expand All @@ -133,7 +138,10 @@ type Host struct {
peerMetadata *lru.Cache[peer.ID, PeerMeta]
// createHTTPTransport is used to lazily create the httpTransport in a thread-safe way.
createHTTPTransport sync.Once
httpTransport *httpTransport
// createDefaultClientRoundTripper is used to lazily create the default
// client round tripper in a thread-safe way.
createDefaultClientRoundTripper sync.Once
httpTransport *httpTransport
}

type httpTransport struct {
Expand Down Expand Up @@ -277,7 +285,7 @@ func (h *Host) Serve() error {
errCh <- srv.ServeTLS(l, "", "")
}()
h.httpTransport.listenAddrs = append(h.httpTransport.listenAddrs, listenAddr)
} else if h.ServeInsecureHTTP {
} else if h.InsecureAllowHTTP {
go func() {
errCh <- http.Serve(l, &h.ServeMux)
}()
Expand Down Expand Up @@ -579,10 +587,12 @@ func (h *Host) NewRoundTripper(server peer.AddrInfo, opts ...RoundTripperOption)
scheme = "https"
}

h.createDefaultClientRoundTripper.Do(func() {
if h.DefaultClientRoundTripper == nil {
h.DefaultClientRoundTripper = &http.Transport{}
}
})
rt := h.DefaultClientRoundTripper
if rt == nil {
rt = http.DefaultTransport.(*http.Transport)
}
ownRoundtripper := false
if parsed.sni != parsed.host {
// We have a different host and SNI (e.g. using an IP address but specifying a SNI)
Expand Down
4 changes: 2 additions & 2 deletions p2p/http/libp2phttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestRoundTrippers(t *testing.T) {
require.NoError(t, err)

httpHost := libp2phttp.Host{
ServeInsecureHTTP: true,
InsecureAllowHTTP: true,
StreamHost: serverHost,
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/0/http")},
}
Expand Down Expand Up @@ -310,7 +310,7 @@ func TestPlainOldHTTPServer(t *testing.T) {

func TestHostZeroValue(t *testing.T) {
server := libp2phttp.Host{
ServeInsecureHTTP: true,
InsecureAllowHTTP: true,
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/0/http")},
}
server.SetHTTPHandler("/hello", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello")) }))
Expand Down

0 comments on commit 8f7f71f

Please sign in to comment.