@@ -44,7 +44,7 @@ import (
4444)
4545
4646func init () {
47- internal .AcceptedCompressionNames = acceptedCompressionNames
47+ internal .AcceptCompressors = AcceptCompressors
4848}
4949
5050// Compressor defines the interface gRPC uses to compress a message.
@@ -167,23 +167,22 @@ type callInfo struct {
167167 maxRetryRPCBufferSize int
168168 onFinish []func (err error )
169169 authority string
170- acceptedResponseCompressors * acceptedCompressionConfig
170+ acceptedResponseCompressors [] string
171171}
172172
173- type acceptedCompressionConfig struct {
174- headerValue string
175- allowed map [string ]struct {}
176- }
177-
178- func (cfg * acceptedCompressionConfig ) allows (name string ) bool {
179- if cfg == nil {
173+ func acceptedCompressorAllows (allowed []string , name string ) bool {
174+ if allowed == nil {
180175 return true
181176 }
182177 if name == "" || name == encoding .Identity {
183178 return true
184179 }
185- _ , ok := cfg .allowed [name ]
186- return ok
180+ for _ , a := range allowed {
181+ if a == name {
182+ return true
183+ }
184+ }
185+ return false
187186}
188187
189188func defaultCallInfo () * callInfo {
@@ -193,14 +192,12 @@ func defaultCallInfo() *callInfo {
193192 }
194193}
195194
196- func newAcceptedCompressionConfig (names []string ) (* acceptedCompressionConfig , error ) {
197- cfg := & acceptedCompressionConfig {
198- allowed : make (map [string ]struct {}, len (names )),
199- }
195+ func newAcceptedCompressionConfig (names []string ) ([]string , error ) {
200196 if len (names ) == 0 {
201- return cfg , nil
197+ return nil , nil
202198 }
203- var ordered []string
199+ var allowed []string
200+ seen := make (map [string ]struct {}, len (names ))
204201 for _ , name := range names {
205202 name = strings .TrimSpace (name )
206203 if name == "" || name == encoding .Identity {
@@ -209,17 +206,13 @@ func newAcceptedCompressionConfig(names []string) (*acceptedCompressionConfig, e
209206 if ! grpcutil .IsCompressorNameRegistered (name ) {
210207 return nil , status .Errorf (codes .InvalidArgument , "grpc: compressor %q is not registered" , name )
211208 }
212- if _ , dup := cfg . allowed [name ]; dup {
209+ if _ , dup := seen [name ]; dup {
213210 continue
214211 }
215- cfg . allowed [name ] = struct {}{}
216- ordered = append (ordered , name )
212+ seen [name ] = struct {}{}
213+ allowed = append (allowed , name )
217214 }
218- if len (ordered ) == 0 {
219- return nil , status .Error (codes .InvalidArgument , "grpc: no valid compressor names provided" )
220- }
221- cfg .headerValue = strings .Join (ordered , "," )
222- return cfg , nil
215+ return allowed , nil
223216}
224217
225218// CallOption configures a Call before it starts or extracts information from
@@ -523,25 +516,25 @@ func (o CompressorCallOption) before(c *callInfo) error {
523516}
524517func (o CompressorCallOption ) after (* callInfo , * csAttempt ) {}
525518
526- func acceptedCompressionNames (names ... string ) CallOption {
519+ func AcceptCompressors (names ... string ) CallOption {
527520 cp := append ([]string (nil ), names ... )
528- return acceptedCompressionNamesCallOption {names : cp }
521+ return AcceptCompressorsCallOption {names : cp }
529522}
530523
531- type acceptedCompressionNamesCallOption struct {
524+ type AcceptCompressorsCallOption struct {
532525 names []string
533526}
534527
535- func (o acceptedCompressionNamesCallOption ) before (c * callInfo ) error {
536- cfg , err := newAcceptedCompressionConfig (o .names )
528+ func (o AcceptCompressorsCallOption ) before (c * callInfo ) error {
529+ allowed , err := newAcceptedCompressionConfig (o .names )
537530 if err != nil {
538531 return err
539532 }
540- c .acceptedResponseCompressors = cfg
533+ c .acceptedResponseCompressors = allowed
541534 return nil
542535}
543536
544- func (acceptedCompressionNamesCallOption ) after (* callInfo , * csAttempt ) {}
537+ func (AcceptCompressorsCallOption ) after (* callInfo , * csAttempt ) {}
545538
546539// CallContentSubtype returns a CallOption that will set the content-subtype
547540// for a call. For example, if content-subtype is "json", the Content-Type over
@@ -893,7 +886,7 @@ func outPayload(client bool, msg any, dataLength, payloadLength int, t time.Time
893886 }
894887}
895888
896- func checkRecvPayload (pf payloadFormat , recvCompress string , haveCompressor bool , isServer bool , acceptedCfg * acceptedCompressionConfig ) * status.Status {
889+ func checkRecvPayload (pf payloadFormat , recvCompress string , haveCompressor bool , isServer bool ) * status.Status {
897890 switch pf {
898891 case compressionNone :
899892 case compressionMade :
@@ -906,9 +899,6 @@ func checkRecvPayload(pf payloadFormat, recvCompress string, haveCompressor bool
906899 }
907900 return status .Newf (codes .Internal , "grpc: Decompressor is not installed for grpc-encoding %q" , recvCompress )
908901 }
909- if ! isServer && acceptedCfg != nil && ! acceptedCfg .allows (recvCompress ) {
910- return status .Newf (codes .FailedPrecondition , "grpc: peer compressed the response with %q which is not allowed by AcceptedCompressionNames" , recvCompress )
911- }
912902 default :
913903 return status .Newf (codes .Internal , "grpc: received unexpected payload format %d" , pf )
914904 }
@@ -932,16 +922,15 @@ func (p *payloadInfo) free() {
932922// the buffer is no longer needed.
933923// TODO: Refactor this function to reduce the number of arguments.
934924// See: https://google.github.io/styleguide/go/best-practices.html#function-argument-lists
935- func recvAndDecompress (p * parser , s recvCompressor , dc Decompressor , maxReceiveMessageSize int , payInfo * payloadInfo , compressor encoding.Compressor , isServer bool , acceptedCfg * acceptedCompressionConfig ,
936- ) (out mem.BufferSlice , err error ) {
925+ func recvAndDecompress (p * parser , s recvCompressor , dc Decompressor , maxReceiveMessageSize int , payInfo * payloadInfo , compressor encoding.Compressor , isServer bool ) (out mem.BufferSlice , err error ) {
937926 pf , compressed , err := p .recvMsg (maxReceiveMessageSize )
938927 if err != nil {
939928 return nil , err
940929 }
941930
942931 compressedLength := compressed .Len ()
943932
944- if st := checkRecvPayload (pf , s .RecvCompress (), compressor != nil || dc != nil , isServer , acceptedCfg ); st != nil {
933+ if st := checkRecvPayload (pf , s .RecvCompress (), compressor != nil || dc != nil , isServer ); st != nil {
945934 compressed .Free ()
946935 return nil , st .Err ()
947936 }
@@ -1016,8 +1005,8 @@ type recvCompressor interface {
10161005// For the two compressor parameters, both should not be set, but if they are,
10171006// dc takes precedence over compressor.
10181007// TODO(dfawley): wrap the old compressor/decompressor using the new API?
1019- func recv (p * parser , c baseCodec , s recvCompressor , dc Decompressor , m any , maxReceiveMessageSize int , payInfo * payloadInfo , compressor encoding.Compressor , isServer bool , acceptedCfg * acceptedCompressionConfig ) error {
1020- data , err := recvAndDecompress (p , s , dc , maxReceiveMessageSize , payInfo , compressor , isServer , acceptedCfg )
1008+ func recv (p * parser , c baseCodec , s recvCompressor , dc Decompressor , m any , maxReceiveMessageSize int , payInfo * payloadInfo , compressor encoding.Compressor , isServer bool ) error {
1009+ data , err := recvAndDecompress (p , s , dc , maxReceiveMessageSize , payInfo , compressor , isServer )
10211010 if err != nil {
10221011 return err
10231012 }
0 commit comments