diff --git a/transformer/internal/amphtml/urls.go b/transformer/internal/amphtml/urls.go
index 5782c2ef5..25155e2ac 100644
--- a/transformer/internal/amphtml/urls.go
+++ b/transformer/internal/amphtml/urls.go
@@ -231,6 +231,7 @@ func ToCacheURLSubdomain(originHost string) string {
if err != nil {
return fallbackCacheURLSubdomain(originHost)
}
+
var sb strings.Builder
for _, rune := range unicode {
switch rune {
@@ -242,12 +243,36 @@ func ToCacheURLSubdomain(originHost string) string {
sb.WriteRune(rune)
}
}
- if result, err := p.ToASCII(sb.String()); err == nil && strings.ContainsRune(sb.String(), '-') {
+
+ utf8 := sb.String()
+ result, err := p.ToASCII(utf8)
+ if err == nil && isValidCurls(result) {
return result
}
+
+ // If there was an error due to the hyphen being in positions 3 and 4, try
+ // to create a human readable version for CURLS label v2. Since err does not
+ // tell us the specific error, check the hyphens manually.
+ if utf8[2] == '-' && utf8[3] == '-' {
+ var sb2 strings.Builder
+ sb2.WriteString("0-")
+ sb2.WriteString(utf8)
+ sb2.WriteString("-0")
+
+ utf8 = sb2.String()
+ result, err = p.ToASCII(utf8)
+ if err == nil && isValidCurls(result) {
+ return result
+ }
+ }
+
return fallbackCacheURLSubdomain(originHost)
}
+func isValidCurls(result string) bool {
+ return strings.ContainsRune(result, '-')
+}
+
func fallbackCacheURLSubdomain(originHost string) string {
sha := sha256.New()
diff --git a/transformer/internal/amphtml/urls_test.go b/transformer/internal/amphtml/urls_test.go
index 9950d4d52..49f89c778 100644
--- a/transformer/internal/amphtml/urls_test.go
+++ b/transformer/internal/amphtml/urls_test.go
@@ -366,12 +366,12 @@ func TestToCacheURLDomain(t *testing.T) {
{
desc: "R-LDH #2",
input: "in-trouble.com",
- expected: "j7pweznglei73fva3bo6oidjt74j3hx4tfyncjsdwud7r7cci4va",
+ expected: "0-in--trouble-com-0",
},
{
desc: "R-LDH #3",
input: "a--problem.com",
- expected: "a47psvede4jpgjom2kzmuhop74zzmdpjzasoctyoqqaxbkdbsyiq",
+ expected: "0-a----problem-com-0",
},
{
desc: "Transition mapping per UTS #46",
diff --git a/transformer/transformer_test.go b/transformer/transformer_test.go
index 32ff55527..b59c623cc 100644
--- a/transformer/transformer_test.go
+++ b/transformer/transformer_test.go
@@ -6,7 +6,7 @@ import (
"strings"
"testing"
- "github.com/golang/protobuf/proto"
+ "google3/net/proto2/go/proto"
rpb "github.com/ampproject/amppackager/transformer/request"
"github.com/ampproject/amppackager/transformer/transformers"
"github.com/google/go-cmp/cmp"