Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libp2phttp: Rename well-known resource #2757

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions p2p/http/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
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"
Copy link
Contributor

@gammazero gammazero May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoPolo Changing this from "/.well-known/libp2p" to "/.well-known/libp2p/protocols" will break the ability to communicate with existing servers deployed without upgrading them first. Since these servers are running in places not under our control, they will not be immediately upgradable.

This means that libp2phttp needs to support both the old and the new values, or revert to using the old value.

Since there are already deployments using the old value, I suggest changing this to:

const WellKnownProtocols = "/.well-known/libp2p"

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
Loading