Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Added endpoint escaping in the transcoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Jul 8, 2019
1 parent c3621a5 commit 34d16ce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
11 changes: 8 additions & 3 deletions addrs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestConvertWebsocketMultiaddrToNetAddr(t *testing.T) {
}

func TestTranscoder(t *testing.T) {
bytes, err := WsTranscoder.StringToBytes("libp2pEndpoint")
bytes, err := WsTranscoder.StringToBytes("libp2p%20Endpoint")
if err != nil {
t.Fatal(err)
}
Expand All @@ -78,13 +78,18 @@ func TestTranscoder(t *testing.T) {
t.Fatal(err)
}

if str != "libp2pEndpoint" {
t.Fatalf("excepted \"libp2pEndpoint\" but got \"%s\"", str)
if str != "libp2p%20Endpoint" {
t.Fatalf("excepted \"libp2p%%20Endpoint\" but got \"%s\"", str)
}

// Now testing with a /
bytes, err = WsTranscoder.StringToBytes("libp2p/endpoint")
if bytes != nil || err == nil {
t.Fatalf("endpoint with a / shouldn't works, but here got : %s", hex.Dump(bytes))
}
// Now testing with a wrongly escaped endpoint
bytes, err = WsTranscoder.StringToBytes("libp2p%AGendpoint")
if bytes != nil || err == nil {
t.Fatalf("a wrongly escaped endpoint shouldn't works, but here got : %s", hex.Dump(bytes))
}
}
9 changes: 6 additions & 3 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ import (
manet "github.com/multiformats/go-multiaddr-net"
)

// WsEndpointTranscoder use the vanilla []byte(s)
// WsEndpointTranscoder use the vanilla []byte(s) and use url for escaping.
var WsTranscoder = ma.NewTranscoderFromFunctions(wsStB, wsBtS, nil)

func wsStB(s string) ([]byte, error) {
if strings.Contains(s, "/") {
return nil, fmt.Errorf("Endpoint must be under root, not %s", s)
}

s, err := url.PathUnescape(s)
if err != nil {
return nil, err
}
return []byte(s), nil
}
func wsBtS(b []byte) (string, error) {
return string(b), nil
return url.PathEscape(string(b)), nil
}

// WsProtocol is the multiaddr protocol definition for this transport.
Expand Down

0 comments on commit 34d16ce

Please sign in to comment.