fix: do not percent encode character tilde (#2139)

This commit is contained in:
lyuxuan
2018-06-14 16:03:11 -07:00
committed by GitHub
parent 692f13ae39
commit d2f8e5a29d
2 changed files with 37 additions and 4 deletions

View File

@ -438,7 +438,7 @@ func decodeTimeout(s string) (time.Duration, error) {
const (
spaceByte = ' '
tildaByte = '~'
tildeByte = '~'
percentByte = '%'
)
@ -456,7 +456,7 @@ func encodeGrpcMessage(msg string) string {
lenMsg := len(msg)
for i := 0; i < lenMsg; i++ {
c := msg[i]
if !(c >= spaceByte && c < tildaByte && c != percentByte) {
if !(c >= spaceByte && c <= tildeByte && c != percentByte) {
return encodeGrpcMessageUnchecked(msg)
}
}
@ -478,7 +478,7 @@ func encodeGrpcMessageUnchecked(msg string) string {
// utf8.RuneError.
//
// fmt.Sprintf("%%%02X", utf8.RuneError) gives "%FFFD".
if b >= spaceByte && b < tildaByte && b != percentByte {
if b >= spaceByte && b <= tildeByte && b != percentByte {
buf.WriteByte(b)
} else {
buf.WriteString(fmt.Sprintf("%%%02X", b))

View File

@ -112,6 +112,23 @@ func TestEncodeGrpcMessage(t *testing.T) {
t.Errorf("encodeGrpcMessage(%q) = %q, want %q", tt.input, actual, tt.expected)
}
}
// make sure that all the visible ASCII chars except '%' are not percent encoded.
for i := ' '; i <= '~' && i != '%'; i++ {
output := encodeGrpcMessage(string(i))
if output != string(i) {
t.Errorf("encodeGrpcMessage(%v) = %v, want %v", string(i), output, string(i))
}
}
// make sure that all the invisible ASCII chars and '%' are percent encoded.
for i := rune(0); i == '%' || (i >= rune(0) && i < ' ') || (i > '~' && i <= rune(127)); i++ {
output := encodeGrpcMessage(string(i))
expected := fmt.Sprintf("%%%02X", i)
if output != expected {
t.Errorf("encodeGrpcMessage(%v) = %v, want %v", string(i), output, expected)
}
}
}
func TestDecodeGrpcMessage(t *testing.T) {
@ -129,7 +146,23 @@ func TestDecodeGrpcMessage(t *testing.T) {
} {
actual := decodeGrpcMessage(tt.input)
if tt.expected != actual {
t.Errorf("dncodeGrpcMessage(%q) = %q, want %q", tt.input, actual, tt.expected)
t.Errorf("decodeGrpcMessage(%q) = %q, want %q", tt.input, actual, tt.expected)
}
}
// make sure that all the visible ASCII chars except '%' are not percent decoded.
for i := ' '; i <= '~' && i != '%'; i++ {
output := decodeGrpcMessage(string(i))
if output != string(i) {
t.Errorf("decodeGrpcMessage(%v) = %v, want %v", string(i), output, string(i))
}
}
// make sure that all the invisible ASCII chars and '%' are percent decoded.
for i := rune(0); i == '%' || (i >= rune(0) && i < ' ') || (i > '~' && i <= rune(127)); i++ {
output := decodeGrpcMessage(fmt.Sprintf("%%%02X", i))
if output != string(i) {
t.Errorf("decodeGrpcMessage(%v) = %v, want %v", fmt.Sprintf("%%%02X", i), output, string(i))
}
}
}