From 82dd3d19f5213d3e9dd6fc5488b4c362191786a3 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Wed, 3 Apr 2024 11:22:58 -0700 Subject: [PATCH] Rename well-known resource --- p2p/http/example_test.go | 6 +++--- p2p/http/libp2phttp.go | 13 +++++++------ p2p/http/libp2phttp_test.go | 6 +++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/p2p/http/example_test.go b/p2p/http/example_test.go index 719badeac6..a9992d3482 100644 --- a/p2p/http/example_test.go +++ b/p2p/http/example_test.go @@ -335,11 +335,11 @@ func ExampleWellKnownHandler() { } defer listener.Close() - // Serve `.well-known/libp2p`. Note, this is handled automatically if you use the libp2phttp.Host. + // Serve the well-known resource. Note, this is handled automatically if you use the libp2phttp.Host. go http.Serve(listener, &h) - // Get the `.well-known/libp2p` resource - resp, err := http.Get("http://" + listener.Addr().String() + "/.well-known/libp2p") + // Get the well-known resource + resp, err := http.Get("http://" + listener.Addr().String() + libp2phttp.WellKnownProtocols) if err != nil { log.Fatal(err) } diff --git a/p2p/http/libp2phttp.go b/p2p/http/libp2phttp.go index fbd8a64f4e..21b18890b2 100644 --- a/p2p/http/libp2phttp.go +++ b/p2p/http/libp2phttp.go @@ -30,6 +30,7 @@ import ( var log = logging.Logger("libp2phttp") const ProtocolIDForMultistreamSelect = "/http/1.1" +const WellKnownProtocols = "/.well-known/libp2p/protocols" const peerMetadataLimit = 8 << 10 // 8KB const peerMetadataLRUSize = 256 // How many different peer's metadata to keep in our LRU cache @@ -41,7 +42,7 @@ type ProtocolMeta struct { type PeerMeta map[protocol.ID]ProtocolMeta -// WellKnownHandler is an http.Handler that serves the .well-known/libp2p resource +// WellKnownHandler is an http.Handler that serves the well-known resource type WellKnownHandler struct { wellknownMapMu sync.Mutex wellKnownMapping PeerMeta @@ -137,7 +138,7 @@ type Host struct { // `http.Transport` on first use. DefaultClientRoundTripper *http.Transport - // WellKnownHandler is the http handler for the `.well-known/libp2p` + // WellKnownHandler is the http handler for the well-known // resource. It is responsible for sharing this node's protocol metadata // with other nodes. Users only care about this if they set their own // ServeMux with pre-existing routes. By default, new protocols are added @@ -270,7 +271,7 @@ func (h *Host) Serve() error { } h.serveMuxInit() - h.ServeMux.Handle("/.well-known/libp2p", &h.WellKnownHandler) + h.ServeMux.Handle(WellKnownProtocols, &h.WellKnownHandler) h.httpTransportInit() @@ -352,7 +353,7 @@ func (h *Host) Close() error { } // SetHTTPHandler sets the HTTP handler for a given protocol. Automatically -// manages the .well-known/libp2p mapping. +// manages the well-known resource mapping. // http.StripPrefix is called on the handler, so the handler will be unaware of // its prefix path. func (h *Host) SetHTTPHandler(p protocol.ID, handler http.Handler) { @@ -360,7 +361,7 @@ func (h *Host) SetHTTPHandler(p protocol.ID, handler http.Handler) { } // SetHTTPHandlerAtPath sets the HTTP handler for a given protocol using the -// given path. Automatically manages the .well-known/libp2p mapping. +// given path. Automatically manages the well-known resource mapping. // http.StripPrefix is called on the handler, so the handler will be unaware of // its prefix path. func (h *Host) SetHTTPHandlerAtPath(p protocol.ID, path string, handler http.Handler) { @@ -743,7 +744,7 @@ func (h *Host) getAndStorePeerMetadata(roundtripper http.RoundTripper, server pe return meta, nil } - req, err := http.NewRequest("GET", "/.well-known/libp2p", nil) + req, err := http.NewRequest("GET", WellKnownProtocols, nil) if err != nil { return nil, err } diff --git a/p2p/http/libp2phttp_test.go b/p2p/http/libp2phttp_test.go index 57dd0cc8e2..c5616feadd 100644 --- a/p2p/http/libp2phttp_test.go +++ b/p2p/http/libp2phttp_test.go @@ -205,7 +205,7 @@ func TestRoundTrippers(t *testing.T) { }) } - // Read the .well-known/libp2p resource + // Read the well-known resource wk, err := rt.(libp2phttp.PeerMetadataGetter).GetPeerMetadata() require.NoError(t, err) @@ -219,7 +219,7 @@ func TestRoundTrippers(t *testing.T) { func TestPlainOldHTTPServer(t *testing.T) { mux := http.NewServeMux() wk := libp2phttp.WellKnownHandler{} - mux.Handle("/.well-known/libp2p", &wk) + mux.Handle(libp2phttp.WellKnownProtocols, &wk) mux.Handle("/ping/", httpping.Ping{}) wk.AddProtocolMeta(httpping.PingProtocolID, libp2phttp.ProtocolMeta{Path: "/ping/"}) @@ -270,7 +270,7 @@ func TestPlainOldHTTPServer(t *testing.T) { }, getWellKnown: func(t *testing.T) (libp2phttp.PeerMeta, error) { client := http.Client{} - resp, err := client.Get("http://" + l.Addr().String() + "/.well-known/libp2p") + resp, err := client.Get("http://" + l.Addr().String() + libp2phttp.WellKnownProtocols) require.NoError(t, err) b, err := io.ReadAll(resp.Body)