Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for http-path #246

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ func TestRoundTrip(t *testing.T) {
"/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g",
"/p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP",
"/p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP/unix/a/b/c",
"/http-path/tmp%2Fbar",
} {
ma, err := NewMultiaddr(s)
if err != nil {
Expand Down Expand Up @@ -923,3 +924,36 @@ func TestDNS(t *testing.T) {
t.Fatal("expected equality")
}
}

func TestHTTPPath(t *testing.T) {
t.Run("bad addr", func(t *testing.T) {
badAddr := "/http-path/thisIsMissingAfullBytes%f"
_, err := NewMultiaddr(badAddr)
MarcoPolo marked this conversation as resolved.
Show resolved Hide resolved
require.Error(t, err)
})

t.Run("round trip", func(t *testing.T) {
cases := []string{
"/http-path/tmp%2Fbar",
"/http-path/tmp%2Fbar%2Fbaz",
"/http-path/foo",
MarcoPolo marked this conversation as resolved.
Show resolved Hide resolved
}
for _, c := range cases {
m, err := NewMultiaddr(c)
require.NoError(t, err)
require.Equal(t, c, m.String())
}
})

t.Run("value for protocol", func(t *testing.T) {
m := StringCast("/http-path/tmp%2Fbar")
v, err := m.ValueForProtocol(P_HTTP_PATH)
require.NoError(t, err)
// This gives us the url escaped version
require.Equal(t, "tmp%2Fbar", v)

// If we want the raw unescaped version, we can use the component and read it
_, component := SplitLast(m)
require.Equal(t, "tmp/bar", string(component.RawValue()))
})
}
9 changes: 9 additions & 0 deletions protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
P_P2P = 421
P_IPFS = P_P2P // alias for backwards compatibility
P_HTTP = 480
P_HTTP_PATH = 481
P_HTTPS = 443 // deprecated alias for /tls/http
P_ONION = 444 // also for backwards compatibility
P_ONION3 = 445
Expand Down Expand Up @@ -206,6 +207,13 @@ var (
Code: P_HTTP,
VCode: CodeToVarint(P_HTTP),
}
protoHTTPPath = Protocol{
Name: "http-path",
Code: P_HTTP_PATH,
VCode: CodeToVarint(P_HTTP_PATH),
Size: LengthPrefixedVarSize,
Transcoder: TranscoderHTTPPath,
}
protoHTTPS = Protocol{
Name: "https",
Code: P_HTTPS,
Expand Down Expand Up @@ -301,6 +309,7 @@ func init() {
protoWEBTRANSPORT,
protoCERTHASH,
protoHTTP,
protoHTTPPath,
protoHTTPS,
protoP2P,
protoUNIX,
Expand Down
16 changes: 16 additions & 0 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"errors"
"fmt"
"net"
"net/url"
"strconv"
"strings"

Expand Down Expand Up @@ -454,3 +455,18 @@
_, err := mh.Decode(b)
return err
}

var TranscoderHTTPPath = NewTranscoderFromFunctions(httpPathStB, httpPathBtS, validateHTTPPath)

func httpPathStB(s string) ([]byte, error) {
unescaped, err := url.QueryUnescape(s)
return []byte(unescaped), err
}

func httpPathBtS(b []byte) (string, error) {
return url.QueryEscape(string(b)), nil
}

func validateHTTPPath(b []byte) error {
return nil // We can represent any byte slice when we escape it.

Check warning on line 471 in transcoders.go

View check run for this annotation

Codecov / codecov/patch

transcoders.go#L470-L471

Added lines #L470 - L471 were not covered by tests
}