From 23bc261aa50903114324adc4d1ff4f567036b5b1 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Mon, 21 Aug 2023 13:10:03 -0700 Subject: [PATCH] Rename HTTPHost to Host --- p2p/http/example_test.go | 46 ++++++++++++++++++------------------- p2p/http/libp2phttp.go | 38 +++++++++++++++--------------- p2p/http/libp2phttp_test.go | 36 ++++++++++++++--------------- 3 files changed, 60 insertions(+), 60 deletions(-) diff --git a/p2p/http/example_test.go b/p2p/http/example_test.go index 54d791f87c..48b2c28a35 100644 --- a/p2p/http/example_test.go +++ b/p2p/http/example_test.go @@ -14,8 +14,8 @@ import ( ma "github.com/multiformats/go-multiaddr" ) -func ExampleHTTPHost_withAStockGoHTTPClient() { - server := libp2phttp.HTTPHost{ +func ExampleHost_withAStockGoHTTPClient() { + server := libp2phttp.Host{ ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/0/http")}, } @@ -56,12 +56,12 @@ func ExampleHTTPHost_withAStockGoHTTPClient() { // Output: Hello HTTP } -func ExampleHTTPHost_listenOnHTTPTransportAndStreams() { +func ExampleHost_listenOnHTTPTransportAndStreams() { serverStreamHost, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/50124/quic-v1")) if err != nil { log.Fatal(err) } - server := libp2phttp.HTTPHost{ + server := libp2phttp.Host{ ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50124/http")}, StreamHost: serverStreamHost, @@ -73,13 +73,13 @@ func ExampleHTTPHost_listenOnHTTPTransportAndStreams() { // Output: Server listening on: [/ip4/127.0.0.1/udp/50124/quic-v1 /ip4/127.0.0.1/tcp/50124/http] } -func ExampleHTTPHost_overLibp2pStreams() { +func ExampleHost_overLibp2pStreams() { serverStreamHost, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/0/quic-v1")) if err != nil { log.Fatal(err) } - server := libp2phttp.HTTPHost{ + server := libp2phttp.Host{ StreamHost: serverStreamHost, } @@ -96,7 +96,7 @@ func ExampleHTTPHost_overLibp2pStreams() { log.Fatal(err) } - client := libp2phttp.HTTPHost{StreamHost: clientStreamHost} + client := libp2phttp.Host{StreamHost: clientStreamHost} // Make an HTTP request using the Go standard library, but over libp2p // streams. If the server were listening on an HTTP transport, this could @@ -119,8 +119,8 @@ func ExampleHTTPHost_overLibp2pStreams() { // Output: Hello HTTP } -func ExampleHTTPHost_Serve() { - server := libp2phttp.HTTPHost{ +func ExampleHost_Serve() { + server := libp2phttp.Host{ ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50221/http")}, } @@ -133,8 +133,8 @@ func ExampleHTTPHost_Serve() { // Output: [/ip4/127.0.0.1/tcp/50221/http] } -func ExampleHTTPHost_SetHTTPHandler() { - server := libp2phttp.HTTPHost{ +func ExampleHost_SetHTTPHandler() { + server := libp2phttp.Host{ ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50222/http")}, } @@ -167,8 +167,8 @@ func ExampleHTTPHost_SetHTTPHandler() { // Output: Hello World } -func ExampleHTTPHost_SetHTTPHandlerAtPath() { - server := libp2phttp.HTTPHost{ +func ExampleHost_SetHTTPHandlerAtPath() { + server := libp2phttp.Host{ ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50224/http")}, } @@ -201,11 +201,11 @@ func ExampleHTTPHost_SetHTTPHandlerAtPath() { // Output: Hello World } -func ExampleHTTPHost_NamespacedClient() { - var client libp2phttp.HTTPHost +func ExampleHost_NamespacedClient() { + var client libp2phttp.Host // Create the server - server := libp2phttp.HTTPHost{ + server := libp2phttp.Host{ ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50221/http")}, } @@ -239,11 +239,11 @@ func ExampleHTTPHost_NamespacedClient() { // Output: Hello World } -func ExampleHTTPHost_NamespaceRoundTripper() { - var client libp2phttp.HTTPHost +func ExampleHost_NamespaceRoundTripper() { + var client libp2phttp.Host // Create the server - server := libp2phttp.HTTPHost{ + server := libp2phttp.Host{ ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50223/http")}, } @@ -283,11 +283,11 @@ func ExampleHTTPHost_NamespaceRoundTripper() { // Output: Hello World } -func ExampleHTTPHost_NewRoundTripper() { - var client libp2phttp.HTTPHost +func ExampleHost_NewRoundTripper() { + var client libp2phttp.Host // Create the server - server := libp2phttp.HTTPHost{ + server := libp2phttp.Host{ ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50225/http")}, } @@ -333,7 +333,7 @@ func ExampleWellKnownHandler() { } defer listener.Close() - // Serve `.well-known/libp2p`. Note, this is handled automatically if you use the HTTPHost. + // Serve `.well-known/libp2p`. Note, this is handled automatically if you use the libp2phttp.Host. go http.Serve(listener, &h) // Get the `.well-known/libp2p` resource diff --git a/p2p/http/libp2phttp.go b/p2p/http/libp2phttp.go index 5cb65b409e..2f5bfabb0a 100644 --- a/p2p/http/libp2phttp.go +++ b/p2p/http/libp2phttp.go @@ -98,13 +98,13 @@ func (h *WellKnownHandler) RemoveProtocolMapping(p protocol.ID) { h.wellknownMapMu.Unlock() } -// HTTPHost is a libp2p host for request/responses with HTTP semantics. This is -// in contrast to a stream-oriented host like the host.Host interface. Its -// zero-value (&HTTPHost{}) is usable. Do not copy by value. +// Host is a libp2p host for request/responses with HTTP semantics. This is +// in contrast to a stream-oriented host like the core host.Host interface. Its +// zero-value (&Host{}) is usable. Do not copy by value. // See examples for usage. // // Warning, this is experimental. The API will likely change. -type HTTPHost struct { +type Host struct { // StreamHost is a stream based libp2p host used to do HTTP over libp2p streams. May be nil StreamHost host.Host // ListenAddrs are the requested addresses to listen on. Multiaddrs must be @@ -152,7 +152,7 @@ func newPeerMetadataCache() *lru.Cache[peer.ID, PeerMeta] { return peerMetadata } -func (h *HTTPHost) Addrs() []ma.Multiaddr { +func (h *Host) Addrs() []ma.Multiaddr { h.createHTTPTransport.Do(func() { h.httpTransport = newHTTPTransport() }) @@ -161,7 +161,7 @@ func (h *HTTPHost) Addrs() []ma.Multiaddr { } // ID returns the peer ID of the underlying stream host, or the zero value if there is no stream host. -func (h *HTTPHost) PeerID() peer.ID { +func (h *Host) PeerID() peer.ID { if h.StreamHost != nil { return h.StreamHost.ID() } @@ -172,7 +172,7 @@ var ErrNoListeners = errors.New("nothing to listen on") // Serve starts the HTTP transport listeners. Always returns a non-nil error. // If there are no listeners, returns ErrNoListeners. -func (h *HTTPHost) Serve() error { +func (h *Host) Serve() error { // assert that each addr contains a /http component for _, addr := range h.ListenAddrs { _, isHTTP := normalizeHTTPMultiaddr(addr) @@ -312,7 +312,7 @@ func (h *HTTPHost) Serve() error { return err } -func (h *HTTPHost) Close() error { +func (h *Host) Close() error { h.createHTTPTransport.Do(func() { h.httpTransport = newHTTPTransport() }) @@ -324,7 +324,7 @@ func (h *HTTPHost) Close() error { // manages the .well-known/libp2p mapping. // http.StripPrefix is called on the handler, so the handler will be unaware of // its prefix path. -func (h *HTTPHost) SetHTTPHandler(p protocol.ID, handler http.Handler) { +func (h *Host) SetHTTPHandler(p protocol.ID, handler http.Handler) { h.SetHTTPHandlerAtPath(p, string(p), handler) } @@ -332,7 +332,7 @@ func (h *HTTPHost) SetHTTPHandler(p protocol.ID, handler http.Handler) { // given path. Automatically manages the .well-known/libp2p mapping. // http.StripPrefix is called on the handler, so the handler will be unaware of // its prefix path. -func (h *HTTPHost) SetHTTPHandlerAtPath(p protocol.ID, path string, handler http.Handler) { +func (h *Host) SetHTTPHandlerAtPath(p protocol.ID, path string, handler http.Handler) { if path == "" || path[len(path)-1] != '/' { // We are nesting this handler under this path, so it should end with a slash. path += "/" @@ -349,7 +349,7 @@ type PeerMetadataGetter interface { type streamRoundTripper struct { server peer.ID h host.Host - httpHost *HTTPHost + httpHost *Host } type streamReadCloser struct { @@ -395,7 +395,7 @@ func (rt *streamRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) type roundTripperForSpecificServer struct { http.RoundTripper ownRoundtripper bool - httpHost *HTTPHost + httpHost *Host server peer.ID targetServerAddr string sni string @@ -480,7 +480,7 @@ func (rt *namespacedRoundTripper) RoundTrip(r *http.Request) (*http.Response, er } // NamespaceRoundTripper returns an http.RoundTripper that are scoped to the given protocol on the given server. -func (h *HTTPHost) NamespaceRoundTripper(roundtripper http.RoundTripper, p protocol.ID, server peer.ID) (*namespacedRoundTripper, error) { +func (h *Host) NamespaceRoundTripper(roundtripper http.RoundTripper, p protocol.ID, server peer.ID) (*namespacedRoundTripper, error) { protos, err := h.getAndStorePeerMetadata(roundtripper, server) if err != nil { return &namespacedRoundTripper{}, err @@ -514,7 +514,7 @@ func (h *HTTPHost) NamespaceRoundTripper(roundtripper http.RoundTripper, p proto // creating many namespaced clients, consider creating a round tripper directly // and namespacing the roundripper yourself, then creating clients from the // namespace round tripper. -func (h *HTTPHost) NamespacedClient(p protocol.ID, server peer.AddrInfo, opts ...RoundTripperOption) (http.Client, error) { +func (h *Host) NamespacedClient(p protocol.ID, server peer.AddrInfo, opts ...RoundTripperOption) (http.Client, error) { rt, err := h.NewRoundTripper(server, opts...) if err != nil { return http.Client{}, err @@ -531,7 +531,7 @@ func (h *HTTPHost) NamespacedClient(p protocol.ID, server peer.AddrInfo, opts .. // NewRoundTripper returns an http.RoundTripper that can fulfill and HTTP // request to the given server. It may use an HTTP transport or a stream based // transport. It is valid to pass an empty server.ID. -func (h *HTTPHost) NewRoundTripper(server peer.AddrInfo, opts ...RoundTripperOption) (http.RoundTripper, error) { +func (h *Host) NewRoundTripper(server peer.AddrInfo, opts ...RoundTripperOption) (http.RoundTripper, error) { options := roundTripperOpts{} for _, o := range opts { options = o(options) @@ -678,7 +678,7 @@ func normalizeHTTPMultiaddr(addr ma.Multiaddr) (ma.Multiaddr, bool) { // ProtocolPathPrefix looks up the protocol path in the well-known mapping and // returns it. Will only store the peer's protocol mapping if the server ID is // provided. -func (h *HTTPHost) getAndStorePeerMetadata(roundtripper http.RoundTripper, server peer.ID) (PeerMeta, error) { +func (h *Host) getAndStorePeerMetadata(roundtripper http.RoundTripper, server peer.ID) (PeerMeta, error) { if h.peerMetadata == nil { h.peerMetadata = newPeerMetadataCache() } @@ -730,7 +730,7 @@ func (h *HTTPHost) getAndStorePeerMetadata(roundtripper http.RoundTripper, serve // AddPeerMetadata adds a peer's protocol metadata to the http host. Useful if // you have out-of-band knowledge of a peer's protocol mapping. -func (h *HTTPHost) AddPeerMetadata(server peer.ID, meta PeerMeta) { +func (h *Host) AddPeerMetadata(server peer.ID, meta PeerMeta) { if h.peerMetadata == nil { h.peerMetadata = newPeerMetadataCache() } @@ -738,7 +738,7 @@ func (h *HTTPHost) AddPeerMetadata(server peer.ID, meta PeerMeta) { } // GetPeerMetadata gets a peer's cached protocol metadata from the http host. -func (h *HTTPHost) GetPeerMetadata(server peer.ID) (PeerMeta, bool) { +func (h *Host) GetPeerMetadata(server peer.ID) (PeerMeta, bool) { if h.peerMetadata == nil { return nil, false } @@ -746,7 +746,7 @@ func (h *HTTPHost) GetPeerMetadata(server peer.ID) (PeerMeta, bool) { } // RemovePeerMetadata removes a peer's protocol metadata from the http host -func (h *HTTPHost) RemovePeerMetadata(server peer.ID, meta PeerMeta) { +func (h *Host) RemovePeerMetadata(server peer.ID, meta PeerMeta) { if h.peerMetadata == nil { return } diff --git a/p2p/http/libp2phttp_test.go b/p2p/http/libp2phttp_test.go index 2e171c0acc..16d7bd8a16 100644 --- a/p2p/http/libp2phttp_test.go +++ b/p2p/http/libp2phttp_test.go @@ -34,7 +34,7 @@ func TestHTTPOverStreams(t *testing.T) { ) require.NoError(t, err) - httpHost := libp2phttp.HTTPHost{StreamHost: serverHost} + httpHost := libp2phttp.Host{StreamHost: serverHost} httpHost.SetHTTPHandler("/hello", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello")) @@ -52,7 +52,7 @@ func TestHTTPOverStreams(t *testing.T) { Addrs: serverHost.Addrs(), }) - clientRT, err := (&libp2phttp.HTTPHost{StreamHost: clientHost}).NewRoundTripper(peer.AddrInfo{ID: serverHost.ID()}) + clientRT, err := (&libp2phttp.Host{StreamHost: clientHost}).NewRoundTripper(peer.AddrInfo{ID: serverHost.ID()}) require.NoError(t, err) client := &http.Client{Transport: clientRT} @@ -73,7 +73,7 @@ func TestRoundTrippers(t *testing.T) { ) require.NoError(t, err) - httpHost := libp2phttp.HTTPHost{ + httpHost := libp2phttp.Host{ ServeInsecureHTTP: true, StreamHost: serverHost, ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/0/http")}, @@ -92,12 +92,12 @@ func TestRoundTrippers(t *testing.T) { testCases := []struct { name string - setupRoundTripper func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.HTTPHost) http.RoundTripper + setupRoundTripper func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.Host) http.RoundTripper expectStreamRoundTripper bool }{ { name: "HTTP preferred", - setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.HTTPHost) http.RoundTripper { + setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.Host) http.RoundTripper { rt, err := clientHTTPHost.NewRoundTripper(peer.AddrInfo{ ID: serverHost.ID(), Addrs: serverMultiaddrs, @@ -108,7 +108,7 @@ func TestRoundTrippers(t *testing.T) { }, { name: "HTTP first", - setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.HTTPHost) http.RoundTripper { + setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.Host) http.RoundTripper { rt, err := clientHTTPHost.NewRoundTripper(peer.AddrInfo{ ID: serverHost.ID(), Addrs: []ma.Multiaddr{serverHTTPAddr, serverHost.Addrs()[0]}, @@ -119,7 +119,7 @@ func TestRoundTrippers(t *testing.T) { }, { name: "No HTTP transport", - setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.HTTPHost) http.RoundTripper { + setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.Host) http.RoundTripper { rt, err := clientHTTPHost.NewRoundTripper(peer.AddrInfo{ ID: serverHost.ID(), Addrs: []ma.Multiaddr{serverHost.Addrs()[0]}, @@ -131,7 +131,7 @@ func TestRoundTrippers(t *testing.T) { }, { name: "Stream transport first", - setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.HTTPHost) http.RoundTripper { + setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.Host) http.RoundTripper { rt, err := clientHTTPHost.NewRoundTripper(peer.AddrInfo{ ID: serverHost.ID(), Addrs: []ma.Multiaddr{serverHost.Addrs()[0], serverHTTPAddr}, @@ -143,7 +143,7 @@ func TestRoundTrippers(t *testing.T) { }, { name: "Existing stream transport connection", - setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.HTTPHost) http.RoundTripper { + setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.Host) http.RoundTripper { clientStreamHost.Connect(context.Background(), peer.AddrInfo{ ID: serverHost.ID(), Addrs: serverHost.Addrs(), @@ -166,7 +166,7 @@ func TestRoundTrippers(t *testing.T) { require.NoError(t, err) defer clientStreamHost.Close() - clientHttpHost := &libp2phttp.HTTPHost{StreamHost: clientStreamHost} + clientHttpHost := &libp2phttp.Host{StreamHost: clientStreamHost} rt := tc.setupRoundTripper(t, clientStreamHost, clientHttpHost) if tc.expectStreamRoundTripper { @@ -184,7 +184,7 @@ func TestRoundTrippers(t *testing.T) { var resp *http.Response var err error if tc { - var h libp2phttp.HTTPHost + var h libp2phttp.Host require.NoError(t, err) nrt, err := h.NamespaceRoundTripper(rt, "/hello", serverHost.ID()) require.NoError(t, err) @@ -243,7 +243,7 @@ func TestPlainOldHTTPServer(t *testing.T) { { name: "using libp2phttp", do: func(t *testing.T, request *http.Request) (*http.Response, error) { - var clientHttpHost libp2phttp.HTTPHost + var clientHttpHost libp2phttp.Host rt, err := clientHttpHost.NewRoundTripper(peer.AddrInfo{Addrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/" + serverAddrParts[1] + "/http")}}) require.NoError(t, err) @@ -251,7 +251,7 @@ func TestPlainOldHTTPServer(t *testing.T) { return client.Do(request) }, getWellKnown: func(t *testing.T) (libp2phttp.PeerMeta, error) { - var clientHttpHost libp2phttp.HTTPHost + var clientHttpHost libp2phttp.Host rt, err := clientHttpHost.NewRoundTripper(peer.AddrInfo{Addrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/" + serverAddrParts[1] + "/http")}}) require.NoError(t, err) return rt.(libp2phttp.PeerMetadataGetter).GetPeerMetadata() @@ -308,8 +308,8 @@ func TestPlainOldHTTPServer(t *testing.T) { } } -func TestHTTPHostZeroValue(t *testing.T) { - server := libp2phttp.HTTPHost{ +func TestHostZeroValue(t *testing.T) { + server := libp2phttp.Host{ ServeInsecureHTTP: true, ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/0/http")}, } @@ -319,7 +319,7 @@ func TestHTTPHostZeroValue(t *testing.T) { }() defer server.Close() - c := libp2phttp.HTTPHost{} + c := libp2phttp.Host{} client, err := c.NamespacedClient("/hello", peer.AddrInfo{Addrs: server.Addrs()}) require.NoError(t, err) resp, err := client.Get("/") @@ -332,7 +332,7 @@ func TestHTTPHostZeroValue(t *testing.T) { } func TestHTTPS(t *testing.T) { - server := libp2phttp.HTTPHost{ + server := libp2phttp.Host{ TLSConfig: selfSignedTLSConfig(t), ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/0/https")}, } @@ -344,7 +344,7 @@ func TestHTTPS(t *testing.T) { clientTransport := http.DefaultTransport.(*http.Transport).Clone() clientTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} - client := libp2phttp.HTTPHost{ + client := libp2phttp.Host{ DefaultClientRoundTripper: clientTransport, } httpClient, err := client.NamespacedClient(httpping.PingProtocolID, peer.AddrInfo{Addrs: server.Addrs()})