Skip to content

Commit

Permalink
URL/Data URI: space must be encoded as %20 not + as per RFC3986
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Apr 5, 2022
1 parent 8a4cf11 commit 7ecf472
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
14 changes: 5 additions & 9 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,11 @@ func EncodeURL(b []byte, table [256]bool) []byte {
for i := 0; i < len(b); i++ {
c := b[i]
if table[c] {
if c == ' ' {
b[i] = '+'
} else {
b = append(b, 0, 0)
copy(b[i+3:], b[i+1:])
b[i+0] = '%'
b[i+1] = "0123456789ABCDEF"[c>>4]
b[i+2] = "0123456789ABCDEF"[c&15]
}
b = append(b, 0, 0)
copy(b[i+3:], b[i+1:])
b[i+0] = '%'
b[i+1] = "0123456789ABCDEF"[c>>4]
b[i+2] = "0123456789ABCDEF"[c&15]
}
}
return b
Expand Down
5 changes: 2 additions & 3 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func TestEncodeURL(t *testing.T) {
{"AZaz09-_.!~*'()", "AZaz09-_.!~*'()"},
{"<>", "%3C%3E"},
{"\u2318", "%E2%8C%98"},
{"a b", "a+b"},
{"a b", "a%20b"},
}
for _, tt := range urlTests {
t.Run(tt.url, func(t *testing.T) {
Expand All @@ -279,8 +279,7 @@ func TestEncodeDataURI(t *testing.T) {
url string
expected string
}{
{"a b", "a+b"},
{`<svg xmlns="http://www.w3.org/2000/svg"></svg>`, `%3Csvg+xmlns=%22http://www.w3.org/2000/svg%22%3E%3C/svg%3E`},
{`<svg xmlns="http://www.w3.org/2000/svg"></svg>`, `%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%3E%3C/svg%3E`},
}
for _, tt := range urlTests {
t.Run(tt.url, func(t *testing.T) {
Expand Down

0 comments on commit 7ecf472

Please sign in to comment.