From 41e1716d10d748361bc2ef35a6995126f175a182 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 15 Oct 2024 01:05:22 -0700 Subject: [PATCH] add punycode normalization for URL --- pkg/util/url/url.go | 6 ++++++ pkg/util/url/url_test.go | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/pkg/util/url/url.go b/pkg/util/url/url.go index fb1cb30..a8edad1 100644 --- a/pkg/util/url/url.go +++ b/pkg/util/url/url.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/readium/go-toolkit/pkg/internal/extensions" + "golang.org/x/net/idna" ) /* @@ -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)} } diff --git a/pkg/util/url/url_test.go b/pkg/util/url/url_test.go index bd8be2a..1ed2c20 100644 --- a/pkg/util/url/url_test.go +++ b/pkg/util/url/url_test.go @@ -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())