Skip to content

Commit

Permalink
AMP Cache Origin changes as described in ampproject/amphtml#26205
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 298721329
  • Loading branch information
Googler authored and Chris Papazian committed May 19, 2020
1 parent 3010911 commit 97885ef
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
27 changes: 26 additions & 1 deletion transformer/internal/amphtml/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions transformer/internal/amphtml/urls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion transformer/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 97885ef

Please sign in to comment.