Skip to content

Commit 76a23bf

Browse files
authored
fix enabling compression by trimming whitespaces in accept encoding header (#6952)
1 parent 7525e98 commit 76a23bf

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

internal/transport/transport.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"fmt"
2929
"io"
3030
"net"
31+
"strings"
3132
"sync"
3233
"sync/atomic"
3334
"time"
@@ -362,8 +363,12 @@ func (s *Stream) SendCompress() string {
362363

363364
// ClientAdvertisedCompressors returns the compressor names advertised by the
364365
// client via grpc-accept-encoding header.
365-
func (s *Stream) ClientAdvertisedCompressors() string {
366-
return s.clientAdvertisedCompressors
366+
func (s *Stream) ClientAdvertisedCompressors() []string {
367+
values := strings.Split(s.clientAdvertisedCompressors, ",")
368+
for i, v := range values {
369+
values[i] = strings.TrimSpace(v)
370+
}
371+
return values
367372
}
368373

369374
// Done returns a channel which is closed when it receives the final status

server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,7 +2115,7 @@ func ClientSupportedCompressors(ctx context.Context) ([]string, error) {
21152115
return nil, fmt.Errorf("failed to fetch the stream from the given context %v", ctx)
21162116
}
21172117

2118-
return strings.Split(stream.ClientAdvertisedCompressors(), ","), nil
2118+
return stream.ClientAdvertisedCompressors(), nil
21192119
}
21202120

21212121
// SetTrailer sets the trailer metadata that will be sent when an RPC returns.
@@ -2155,7 +2155,7 @@ func (c *channelzServer) ChannelzMetric() *channelz.ServerInternalMetric {
21552155

21562156
// validateSendCompressor returns an error when given compressor name cannot be
21572157
// handled by the server or the client based on the advertised compressors.
2158-
func validateSendCompressor(name, clientCompressors string) error {
2158+
func validateSendCompressor(name string, clientCompressors []string) error {
21592159
if name == encoding.Identity {
21602160
return nil
21612161
}
@@ -2164,7 +2164,7 @@ func validateSendCompressor(name, clientCompressors string) error {
21642164
return fmt.Errorf("compressor not registered %q", name)
21652165
}
21662166

2167-
for _, c := range strings.Split(clientCompressors, ",") {
2167+
for _, c := range clientCompressors {
21682168
if c == name {
21692169
return nil // found match
21702170
}

test/compressor_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,13 @@ func (s) TestClientSupportedCompressors(t *testing.T) {
566566
),
567567
want: []string{"gzip"},
568568
},
569+
{
570+
desc: "With additional grpc-accept-encoding header with spaces between values",
571+
ctx: metadata.AppendToOutgoingContext(ctx,
572+
"grpc-accept-encoding", "identity, deflate",
573+
),
574+
want: []string{"gzip", "identity", "deflate"},
575+
},
569576
} {
570577
t.Run(tt.desc, func(t *testing.T) {
571578
ss := &stubserver.StubServer{

0 commit comments

Comments
 (0)