Merge pull request #783 from menghanl/comma_in_metadata
Allow comma in metadata Binary-Header value
This commit is contained in:
@ -60,15 +60,21 @@ func encodeKeyValue(k, v string) (string, string) {
|
|||||||
|
|
||||||
// DecodeKeyValue returns the original key and value corresponding to the
|
// DecodeKeyValue returns the original key and value corresponding to the
|
||||||
// encoded data in k, v.
|
// encoded data in k, v.
|
||||||
|
// If k is a binary header and v contains comma, v is split on comma before decoded,
|
||||||
|
// and the decoded v will be joined with comma before returned.
|
||||||
func DecodeKeyValue(k, v string) (string, string, error) {
|
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
|
||||||
}
|
}
|
||||||
val, err := base64.StdEncoding.DecodeString(v)
|
vvs := strings.Split(v, ",")
|
||||||
|
for i, vv := range vvs {
|
||||||
|
val, err := base64.StdEncoding.DecodeString(vv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
return k, string(val), nil
|
vvs[i] = string(val)
|
||||||
|
}
|
||||||
|
return k, strings.Join(vvs, ","), 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
|
||||||
|
@ -74,6 +74,8 @@ func TestDecodeKeyValue(t *testing.T) {
|
|||||||
{"a", "abc", "a", "abc", nil},
|
{"a", "abc", "a", "abc", nil},
|
||||||
{"key-bin", "Zm9vAGJhcg==", "key-bin", "foo\x00bar", nil},
|
{"key-bin", "Zm9vAGJhcg==", "key-bin", "foo\x00bar", nil},
|
||||||
{"key-bin", "woA=", "key-bin", binaryValue, nil},
|
{"key-bin", "woA=", "key-bin", binaryValue, nil},
|
||||||
|
{"a", "abc,efg", "a", "abc,efg", nil},
|
||||||
|
{"key-bin", "Zm9vAGJhcg==,Zm9vAGJhcg==", "key-bin", "foo\x00bar,foo\x00bar", 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) {
|
||||||
|
Reference in New Issue
Block a user