Skip to content

Commit

Permalink
Use multiaddr http-path protocol (#206)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
gammazero authored Jul 9, 2024
1 parent 3199a65 commit 0b9fd7c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion dagsync/ipnisync/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ipnisync_test
import (
"context"
"crypto/rand"
"fmt"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -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())

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ github.com/multiformats/go-base36 v0.2.0 h1:lFsAbNOGeKtuKozrtBsAkSVhv1p9D0/qedU9
github.com/multiformats/go-base36 v0.2.0/go.mod h1:qvnKE++v+2MWCfePClUEjE78Z7P2a1UV0xHgWc0hkp4=
github.com/multiformats/go-multiaddr v0.1.1/go.mod h1:aMKBKNEYmzmDmxfX88/vz+J5IU55txyt0p4aiWVohjo=
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=
Expand Down
19 changes: 13 additions & 6 deletions maurl/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
Expand Down Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0b9fd7c

Please sign in to comment.