Skip to content

Commit

Permalink
Rename well-known resource (#2757)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo authored Apr 15, 2024
1 parent a2da8e0 commit 46ef49e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
6 changes: 3 additions & 3 deletions p2p/http/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,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)
}
Expand Down
13 changes: 7 additions & 6 deletions p2p/http/libp2phttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -352,15 +353,15 @@ 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) {
h.SetHTTPHandlerAtPath(p, string(p), 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) {
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions p2p/http/libp2phttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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/"})
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 46ef49e

Please sign in to comment.