Skip to content

Commit d7334c4

Browse files
sercanddfawley
authored andcommitted
fix enabling compression by trimming whitespaces in accept encoding header (#6952)
1 parent d076e14 commit d7334c4

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

internal/transport/transport.go

+7-2
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -2120,7 +2120,7 @@ func ClientSupportedCompressors(ctx context.Context) ([]string, error) {
21202120
return nil, fmt.Errorf("failed to fetch the stream from the given context %v", ctx)
21212121
}
21222122

2123-
return strings.Split(stream.ClientAdvertisedCompressors(), ","), nil
2123+
return stream.ClientAdvertisedCompressors(), nil
21242124
}
21252125

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

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

2172-
for _, c := range strings.Split(clientCompressors, ",") {
2172+
for _, c := range clientCompressors {
21732173
if c == name {
21742174
return nil // found match
21752175
}

test/compressor_test.go

+7
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)