@@ -21,6 +21,7 @@ class AcceptEncoding < Middleware
21
21
# The default wrappers to use for decoding content.
22
22
DEFAULT_WRAPPERS = {
23
23
"gzip" => Body ::Inflate . method ( :for ) ,
24
+ "identity" => -> ( body ) { body } , # Identity means no encoding
24
25
25
26
# There is no point including this:
26
27
# 'identity' => ->(body){body},
@@ -46,15 +47,32 @@ def call(request)
46
47
47
48
response = super
48
49
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 )
54
74
end
55
75
end
56
-
57
- response . body = body
58
76
end
59
77
60
78
return response
0 commit comments