From 1d57fd9f981d8dc85c2018a9089455a80f4d5b30 Mon Sep 17 00:00:00 2001 From: gammazero <11790789+gammazero@users.noreply.github.com> Date: Mon, 8 Jul 2024 18:20:29 -0700 Subject: [PATCH] Use multiaddr http-path protocol Now that http-path is an official par of multiaddr, use it to identify the path portion of an http multiaddr. --- dagsync/ipnisync/publisher_test.go | 3 ++- go.mod | 2 +- go.sum | 2 ++ maurl/convert.go | 19 +++++++++++++------ 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/dagsync/ipnisync/publisher_test.go b/dagsync/ipnisync/publisher_test.go index 93cf38a..41f240d 100644 --- a/dagsync/ipnisync/publisher_test.go +++ b/dagsync/ipnisync/publisher_test.go @@ -3,6 +3,7 @@ package ipnisync_test import ( "context" "crypto/rand" + "fmt" "io" "net" "net/http" @@ -280,7 +281,7 @@ func TestNewPublisherForListener(t *testing.T) { pathPart := strings.TrimLeft(handlerPath, "/") expectedMaddr := "/ip4/192.168.200.1/tcp/8080/http" if pathPart != "" { - expectedMaddr += "/httpath/" + url.PathEscape(pathPart) + expectedMaddr += fmt.Sprint("/", multiaddr.ProtocolWithCode(multiaddr.P_HTTP_PATH).Name, "/", url.PathEscape(pathPart)) } req.Equal(expectedMaddr, maddr.String()) diff --git a/go.mod b/go.mod index b9a187f..4eb75f3 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/libp2p/go-libp2p-pubsub v0.11.0 github.com/libp2p/go-msgio v0.3.0 github.com/mr-tron/base58 v1.2.0 - github.com/multiformats/go-multiaddr v0.12.4 + github.com/multiformats/go-multiaddr v0.13.0 github.com/multiformats/go-multicodec v0.9.0 github.com/multiformats/go-multihash v0.2.3 github.com/multiformats/go-varint v0.0.7 diff --git a/go.sum b/go.sum index def0754..8adeabf 100644 --- a/go.sum +++ b/go.sum @@ -233,6 +233,8 @@ github.com/multiformats/go-multiaddr v0.1.1/go.mod h1:aMKBKNEYmzmDmxfX88/vz+J5IU github.com/multiformats/go-multiaddr v0.2.0/go.mod h1:0nO36NvPpyV4QzvTLi/lafl2y95ncPj0vFwVF6k6wJ4= github.com/multiformats/go-multiaddr v0.12.4 h1:rrKqpY9h+n80EwhhC/kkcunCZZ7URIF8yN1WEUt2Hvc= github.com/multiformats/go-multiaddr v0.12.4/go.mod h1:sBXrNzucqkFJhvKOiwwLyqamGa/P5EIXNPLovyhQCII= +github.com/multiformats/go-multiaddr v0.13.0 h1:BCBzs61E3AGHcYYTv8dqRH43ZfyrqM8RXVPT8t13tLQ= +github.com/multiformats/go-multiaddr v0.13.0/go.mod h1:sBXrNzucqkFJhvKOiwwLyqamGa/P5EIXNPLovyhQCII= github.com/multiformats/go-multiaddr-dns v0.3.1 h1:QgQgR+LQVt3NPTjbrLLpsaT2ufAA2y0Mkk+QRVJbW3A= github.com/multiformats/go-multiaddr-dns v0.3.1/go.mod h1:G/245BRQ6FJGmryJCrOuTdB37AMA5AMOVuO6NY3JwTk= github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/eQsuaL3/CWe167E= diff --git a/maurl/convert.go b/maurl/convert.go index 8975256..7ccbd3f 100644 --- a/maurl/convert.go +++ b/maurl/convert.go @@ -10,7 +10,7 @@ import ( manet "github.com/multiformats/go-multiaddr/net" ) -// register an 'httpath' component: +// register old 'httpath' component: var transcodePath = multiaddr.NewTranscoderFromFunctions(pathStB, pathBtS, pathVal) func pathVal(b []byte) error { @@ -29,10 +29,13 @@ func pathBtS(b []byte) (string, error) { } func init() { - _ = multiaddr.AddProtocol(protoHTTPath) + _ = multiaddr.AddProtocol(oldProtoHTTPath) } -var protoHTTPath = multiaddr.Protocol{ +// oldProtoHTTPath was used before "http-path" was part of multiaddr. +// +// TODO: remove when all providers have upgraded to IPNI with support for multiaddr.P_HTTP_PPATH +var oldProtoHTTPath = multiaddr.Protocol{ Name: "httpath", Code: 0x300200, VCode: multiaddr.CodeToVarint(0x300200), @@ -85,7 +88,11 @@ func ToURL(ma multiaddr.Multiaddr) (*url.URL, error) { } path := "" - if pb, ok := pm[protoHTTPath.Code]; ok { + pb, ok := pm[multiaddr.P_HTTP_PATH] + if !ok { + pb, ok = pm[oldProtoHTTPath.Code] + } + if ok { path, err = url.PathUnescape(pb) if err != nil { path = "" @@ -138,11 +145,11 @@ func FromURL(u *url.URL) (multiaddr.Multiaddr, error) { joint := multiaddr.Join(*addr, http) if u.Path != "" { - httpath, err := multiaddr.NewComponent(protoHTTPath.Name, url.PathEscape(u.Path)) + httppath, err := multiaddr.NewComponent(multiaddr.ProtocolWithCode(multiaddr.P_HTTP_PATH).Name, url.PathEscape(u.Path)) if err != nil { return nil, err } - joint = multiaddr.Join(joint, httpath) + joint = multiaddr.Join(joint, httppath) } return joint, nil