i) lower case metedata key names; ii) make binary encoding consistent with other impls.
This commit is contained in:
@ -61,12 +61,12 @@ func isASCII(s string) bool {
|
|||||||
// Transmitting binary headers violates HTTP/2 spec.
|
// Transmitting binary headers violates HTTP/2 spec.
|
||||||
// TODO(zhaoq): Maybe check if k is ASCII also.
|
// TODO(zhaoq): Maybe check if k is ASCII also.
|
||||||
func encodeKeyValue(k, v string) (string, string) {
|
func encodeKeyValue(k, v string) (string, string) {
|
||||||
if isASCII(v) {
|
k = strings.ToLower(k)
|
||||||
return k, v
|
if strings.HasSuffix(k, binHdrSuffix) {
|
||||||
|
val := base64.StdEncoding.EncodeToString([]byte(v))
|
||||||
|
v = string(val)
|
||||||
}
|
}
|
||||||
key := strings.ToLower(k + binHdrSuffix)
|
return k, v
|
||||||
val := base64.StdEncoding.EncodeToString([]byte(v))
|
|
||||||
return key, string(val)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeKeyValue returns the original key and value corresponding to the
|
// DecodeKeyValue returns the original key and value corresponding to the
|
||||||
@ -75,12 +75,11 @@ func DecodeKeyValue(k, v string) (string, string, error) {
|
|||||||
if !strings.HasSuffix(k, binHdrSuffix) {
|
if !strings.HasSuffix(k, binHdrSuffix) {
|
||||||
return k, v, nil
|
return k, v, nil
|
||||||
}
|
}
|
||||||
key := k[:len(k)-len(binHdrSuffix)]
|
|
||||||
val, err := base64.StdEncoding.DecodeString(v)
|
val, err := base64.StdEncoding.DecodeString(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
return key, string(val), nil
|
return k, string(val), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MD is a mapping from metadata keys to values. Users should use the following
|
// MD is a mapping from metadata keys to values. Users should use the following
|
||||||
|
@ -40,6 +40,27 @@ import (
|
|||||||
|
|
||||||
const binaryValue = string(128)
|
const binaryValue = string(128)
|
||||||
|
|
||||||
|
func TestEncodeKeyValue(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
// input
|
||||||
|
kin string
|
||||||
|
vin string
|
||||||
|
// output
|
||||||
|
kout string
|
||||||
|
vout string
|
||||||
|
}{
|
||||||
|
{"key", "abc", "key", "abc"},
|
||||||
|
{"KEY", "abc", "key", "abc"},
|
||||||
|
{"key-bin", "abc", "key-bin", "YWJj"},
|
||||||
|
{"key-bin", binaryValue, "key-bin", "woA="},
|
||||||
|
} {
|
||||||
|
k, v := encodeKeyValue(test.kin, test.vin)
|
||||||
|
if k != test.kout || !reflect.DeepEqual(v, test.vout) {
|
||||||
|
t.Fatalf("encodeKeyValue(%q, %q) = %q, %q, want %q, %q", test.kin, test.vin, k, v, test.kout, test.vout)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDecodeKeyValue(t *testing.T) {
|
func TestDecodeKeyValue(t *testing.T) {
|
||||||
for _, test := range []struct {
|
for _, test := range []struct {
|
||||||
// input
|
// input
|
||||||
@ -51,8 +72,8 @@ func TestDecodeKeyValue(t *testing.T) {
|
|||||||
err error
|
err error
|
||||||
}{
|
}{
|
||||||
{"a", "abc", "a", "abc", nil},
|
{"a", "abc", "a", "abc", nil},
|
||||||
{"key-bin", "Zm9vAGJhcg==", "key", "foo\x00bar", nil},
|
{"key-bin", "Zm9vAGJhcg==", "key-bin", "foo\x00bar", nil},
|
||||||
{"key-bin", "woA=", "key", binaryValue, nil},
|
{"key-bin", "woA=", "key-bin", binaryValue, nil},
|
||||||
} {
|
} {
|
||||||
k, v, err := DecodeKeyValue(test.kin, test.vin)
|
k, v, err := DecodeKeyValue(test.kin, test.vin)
|
||||||
if k != test.kout || !reflect.DeepEqual(v, test.vout) || !reflect.DeepEqual(err, test.err) {
|
if k != test.kout || !reflect.DeepEqual(v, test.vout) || !reflect.DeepEqual(err, test.err) {
|
||||||
@ -69,9 +90,9 @@ func TestPairsMD(t *testing.T) {
|
|||||||
md MD
|
md MD
|
||||||
}{
|
}{
|
||||||
{[]string{}, MD{}},
|
{[]string{}, MD{}},
|
||||||
{[]string{"k1", "v1", "k2", binaryValue}, New(map[string]string{
|
{[]string{"k1", "v1", "k2-bin", binaryValue}, New(map[string]string{
|
||||||
"k1": "v1",
|
"k1": "v1",
|
||||||
"k2-bin": "woA=",
|
"k2-bin": binaryValue,
|
||||||
})},
|
})},
|
||||||
} {
|
} {
|
||||||
md := Pairs(test.kv...)
|
md := Pairs(test.kv...)
|
||||||
|
Reference in New Issue
Block a user