Skip to content

Commit

Permalink
fix enabling compression by trimming whitespaces in accept encoding h…
Browse files Browse the repository at this point in the history
…eader (#6952)
  • Loading branch information
sercand committed Feb 20, 2024
1 parent 7525e98 commit 76a23bf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
9 changes: 7 additions & 2 deletions internal/transport/transport.go
Expand Up @@ -28,6 +28,7 @@ import (
"fmt"
"io"
"net"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -362,8 +363,12 @@ func (s *Stream) SendCompress() string {

// ClientAdvertisedCompressors returns the compressor names advertised by the
// client via grpc-accept-encoding header.
func (s *Stream) ClientAdvertisedCompressors() string {
return s.clientAdvertisedCompressors
func (s *Stream) ClientAdvertisedCompressors() []string {
values := strings.Split(s.clientAdvertisedCompressors, ",")
for i, v := range values {
values[i] = strings.TrimSpace(v)
}
return values
}

// Done returns a channel which is closed when it receives the final status
Expand Down
6 changes: 3 additions & 3 deletions server.go
Expand Up @@ -2115,7 +2115,7 @@ func ClientSupportedCompressors(ctx context.Context) ([]string, error) {
return nil, fmt.Errorf("failed to fetch the stream from the given context %v", ctx)
}

return strings.Split(stream.ClientAdvertisedCompressors(), ","), nil
return stream.ClientAdvertisedCompressors(), nil
}

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

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

for _, c := range strings.Split(clientCompressors, ",") {
for _, c := range clientCompressors {
if c == name {
return nil // found match
}
Expand Down
7 changes: 7 additions & 0 deletions test/compressor_test.go
Expand Up @@ -566,6 +566,13 @@ func (s) TestClientSupportedCompressors(t *testing.T) {
),
want: []string{"gzip"},
},
{
desc: "With additional grpc-accept-encoding header with spaces between values",
ctx: metadata.AppendToOutgoingContext(ctx,
"grpc-accept-encoding", "identity, deflate",
),
want: []string{"gzip", "identity", "deflate"},
},
} {
t.Run(tt.desc, func(t *testing.T) {
ss := &stubserver.StubServer{
Expand Down

0 comments on commit 76a23bf

Please sign in to comment.