Skip to content

Commit

Permalink
Use unpadded base64 encoding for binary metadata headers; handle padd…
Browse files Browse the repository at this point in the history
…ed or unpadded input (#1209)
  • Loading branch information
dfawley committed Apr 28, 2017
1 parent b610ffd commit 4d1604c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 6 additions & 2 deletions transport/http_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,15 @@ func (d *decodeState) status() *status.Status {
const binHdrSuffix = "-bin"

func encodeBinHeader(v []byte) string {
return base64.StdEncoding.EncodeToString(v)
return base64.RawStdEncoding.EncodeToString(v)
}

func decodeBinHeader(v string) ([]byte, error) {
return base64.StdEncoding.DecodeString(v)
if len(v)%4 == 0 {
// Input was padded, or padding was not necessary.
return base64.StdEncoding.DecodeString(v)
}
return base64.RawStdEncoding.DecodeString(v)
}

func encodeMetadataHeader(k, v string) string {
Expand Down
3 changes: 2 additions & 1 deletion transport/http_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestEncodeMetadataHeader(t *testing.T) {
{"key", "abc", "abc"},
{"KEY", "abc", "abc"},
{"key-bin", "abc", "YWJj"},
{"key-bin", binaryValue, "woA="},
{"key-bin", binaryValue, "woA"},
} {
v := encodeMetadataHeader(test.kin, test.vin)
if !reflect.DeepEqual(v, test.vout) {
Expand All @@ -178,6 +178,7 @@ func TestDecodeMetadataHeader(t *testing.T) {
}{
{"a", "abc", "abc", nil},
{"key-bin", "Zm9vAGJhcg==", "foo\x00bar", nil},
{"key-bin", "Zm9vAGJhcg", "foo\x00bar", nil},
{"key-bin", "woA=", binaryValue, nil},
{"a", "abc,efg", "abc,efg", nil},
} {
Expand Down

0 comments on commit 4d1604c

Please sign in to comment.