Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Jun 12, 2024
1 parent e0315b9 commit d31f376
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
19 changes: 18 additions & 1 deletion multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ var good = []string{
"/ip4/127.0.0.1/tcp/127/wss",
"/ip4/127.0.0.1/tcp/127/webrtc-direct",
"/ip4/127.0.0.1/tcp/127/webrtc",
"/http-path/tmp%2Fbar",
"/http-path/tmp%2Fbar%2Fbaz",
"/http-path/foo",
"/ip4/127.0.0.1/tcp/0/p2p/12D3KooWCryG7Mon9orvQxcS1rYZjotPgpwoJNHHKcLLfE4Hf5mV/http-path/foo",
"/ip4/127.0.0.1/tcp/443/tls/sni/example.com/http/http-path/foo",
}

func TestConstructSucceeds(t *testing.T) {
Expand Down Expand Up @@ -927,16 +932,28 @@ func TestDNS(t *testing.T) {

func TestHTTPPath(t *testing.T) {
t.Run("bad addr", func(t *testing.T) {
badAddr := "/http-path/thisIsMissingAfullBytes%f"
badAddr := "/http-path/thisIsMissingAfullByte%f"
_, err := NewMultiaddr(badAddr)
require.Error(t, err)
})

t.Run("only reads the http-path part", func(t *testing.T) {
addr := "/http-path/tmp%2Fbar/p2p-circuit" // The http-path only reference the part immediately after it. It does not include the rest of the multiaddr (like the /path component sometimes does)
m, err := NewMultiaddr(addr)
require.NoError(t, err)
m.ValueForProtocol(P_HTTP_PATH)
v, err := m.ValueForProtocol(P_HTTP_PATH)
require.NoError(t, err)
require.Equal(t, "tmp%2Fbar", v)
})

t.Run("round trip", func(t *testing.T) {
cases := []string{
"/http-path/tmp%2Fbar",
"/http-path/tmp%2Fbar%2Fbaz",
"/http-path/foo",
"/ip4/127.0.0.1/tcp/0/p2p/12D3KooWCryG7Mon9orvQxcS1rYZjotPgpwoJNHHKcLLfE4Hf5mV/http-path/foo",
"/ip4/127.0.0.1/tcp/443/tls/sni/example.com/http/http-path/foo",
}
for _, c := range cases {
m, err := NewMultiaddr(c)
Expand Down
12 changes: 12 additions & 0 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,25 @@ var TranscoderHTTPPath = NewTranscoderFromFunctions(httpPathStB, httpPathBtS, va

func httpPathStB(s string) ([]byte, error) {
unescaped, err := url.QueryUnescape(s)
if err != nil {
return nil, err
}
if len(unescaped) == 0 {
return nil, fmt.Errorf("empty http path is not allowed")

Check warning on line 467 in transcoders.go

View check run for this annotation

Codecov / codecov/patch

transcoders.go#L467

Added line #L467 was not covered by tests
}
return []byte(unescaped), err
}

func httpPathBtS(b []byte) (string, error) {
if len(b) == 0 {
return "", fmt.Errorf("empty http path is not allowed")

Check warning on line 474 in transcoders.go

View check run for this annotation

Codecov / codecov/patch

transcoders.go#L474

Added line #L474 was not covered by tests
}
return url.QueryEscape(string(b)), nil
}

func validateHTTPPath(b []byte) error {
if len(b) == 0 {
return fmt.Errorf("empty http path is not allowed")

Check warning on line 481 in transcoders.go

View check run for this annotation

Codecov / codecov/patch

transcoders.go#L481

Added line #L481 was not covered by tests
}
return nil // We can represent any byte slice when we escape it.
}

0 comments on commit d31f376

Please sign in to comment.