Repository: libp2p/go-libp2p
Pull request #2763 (2024-04-10 13:51:01)

fix: revert gorilla/websocket from 1.5.1 to 1.5.0
fix https://github.com/libp2p/go-libp2p/issues/2762




Patch:
diff --git a/go.mod b/go.mod
index 2c231af2..74dd50cb 100644
--- a/go.mod
+++ b/go.mod
@@ -10,7 +10,7 @@ require (
 	github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0
 	github.com/flynn/noise v1.1.0
 	github.com/google/gopacket v1.1.19
-	github.com/gorilla/websocket v1.5.1
+	github.com/gorilla/websocket v1.5.0
 	github.com/hashicorp/golang-lru/arc/v2 v2.0.5
 	github.com/hashicorp/golang-lru/v2 v2.0.5
 	github.com/ipfs/go-cid v0.4.1
diff --git a/go.sum b/go.sum
index 5d1648e6..155ca30f 100644
--- a/go.sum
+++ b/go.sum
@@ -133,8 +133,8 @@ github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
 github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY=
 github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg=
 github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
-github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
-github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
+github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
+github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
 github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
 github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
 github.com/hashicorp/golang-lru/arc/v2 v2.0.5 h1:l2zaLDubNhW4XO3LnliVj0GXO3+/CGNJAg1dcN2Fpfw=
diff --git a/p2p/http/libp2phttp.go b/p2p/http/libp2phttp.go
index fbd8a64f..21b18890 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
 	}


Test patch:
diff --git a/p2p/http/example_test.go b/p2p/http/example_test.go
index 2e90f8cc..ca87fe40 100644
--- a/p2p/http/example_test.go
+++ b/p2p/http/example_test.go
@@ -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)
 	}
diff --git a/p2p/http/libp2phttp_test.go b/p2p/http/libp2phttp_test.go
index 57dd0cc8..c5616fea 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)