Skip to content

Commit cfccf91

Browse files
committed
Fix.
1 parent 0543f93 commit cfccf91

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

lib/protocol/http/accept_encoding.rb

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/protocol/http/accept_encoding.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@
117117
# Mock upstream server that returns brotli-encoded content
118118
upstream_delegate = ->(request) {
119119
# Simulate a server responding with brotli encoding
120-
# when the request has accept-encoding: gzip
121-
expect(request.headers["accept-encoding"]).to be == ["gzip"]
122-
123120
Protocol::HTTP::Response[200,
124121
Protocol::HTTP::Headers[
125122
"content-type" => "text/html",

0 commit comments

Comments
 (0)