@@ -21,6 +21,7 @@ class AcceptEncoding < Middleware
2121 # The default wrappers to use for decoding content.
2222 DEFAULT_WRAPPERS = {
2323 "gzip" => Body ::Inflate . method ( :for ) ,
24+ "identity" => -> ( body ) { body } , # Identity means no encoding
2425
2526 # There is no point including this:
2627 # 'identity' => ->(body){body},
@@ -46,15 +47,32 @@ def call(request)
4647
4748 response = super
4849
49- if body = response . body and !body . empty? and content_encoding = response . headers . delete ( CONTENT_ENCODING )
50- # We want to unwrap all encodings
51- content_encoding . reverse_each do |name |
52- if wrapper = @wrappers [ name ]
53- body = wrapper . call ( body )
50+ if body = response . body and !body . empty?
51+ if content_encoding = response . headers [ CONTENT_ENCODING ]
52+ # Process encodings from the end (last applied first)
53+ # Remove encodings as we successfully decode them
54+ while name = content_encoding . last
55+ # Look up wrapper with case-insensitive matching
56+ wrapper = @wrappers [ name ] || @wrappers [ name . downcase ]
57+
58+ if wrapper
59+ body = wrapper . call ( body )
60+ # Remove the encoding we just processed:
61+ content_encoding . pop
62+ else
63+ # Unknown encoding - stop processing here:
64+ break
65+ end
66+ end
67+
68+ # Update the response body:
69+ response . body = body
70+
71+ # Remove the content-encoding header if we decoded all encodings:
72+ if content_encoding . empty?
73+ response . headers . delete ( CONTENT_ENCODING )
5474 end
5575 end
56-
57- response . body = body
5876 end
5977
6078 return response
0 commit comments