Skip to content

Commit

Permalink
add punycode normalization for URL
Browse files Browse the repository at this point in the history
  • Loading branch information
chocolatkey committed Oct 15, 2024
1 parent e8791be commit 41e1716
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/util/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/readium/go-toolkit/pkg/internal/extensions"
"golang.org/x/net/idna"
)

/*
Expand Down Expand Up @@ -302,6 +303,11 @@ func (u AbsoluteURL) Normalize() URL {
}

u.url.Scheme = SchemeFromString(u.url.Scheme).String()
asciiHost, err := idna.ToASCII(u.url.Host)
if err == nil {
u.url.Host = asciiHost
}

return AbsoluteURL{url: u.url, scheme: Scheme(u.url.Scheme)}
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/util/url/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ func TestNormalize(t *testing.T) {
u, _ := URLFromString("HTTP://example.com/foo")
assert.Equal(t, "http://example.com/foo", u.Normalize().String())

// Host becomes punycode equivalent.
u, _ = URLFromString("http://도메인.com/foo")
assert.Equal(t, "http://xn--hq1bm8jm9l.com/foo", u.Normalize().String())

// Percent encoding of path is normalized.
u, _ = URLFromString("HTTP://example.com/c'est%20valide")
assert.Equal(t, "http://example.com/c'est%20valide", u.Normalize().String())
Expand Down

0 comments on commit 41e1716

Please sign in to comment.