Skip to content

Commit e7aee1f

Browse files
committed
Fix ContentEncoding handling of nil body.
1 parent a93f779 commit e7aee1f

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/protocol/http/content_encoding.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ def call(request)
4646
# https://lists.w3.org/Archives/Public/ietf-http-wg/2014JanMar/1179.html
4747
return response if response.partial?
4848

49+
body = response.body
50+
51+
# If there is no response body, there is nothing to encode:
52+
return response if body.nil? or body.empty?
53+
4954
# Ensure that caches are aware we are varying the response based on the accept-encoding request header:
5055
response.headers.add("vary", "accept-encoding")
5156

52-
# TODO use http-accept and sort by priority
53-
if !response.body.empty? and accept_encoding = request.headers["accept-encoding"]
57+
if accept_encoding = request.headers["accept-encoding"]
5458
if content_type = response.headers["content-type"] and @content_types =~ content_type
55-
body = response.body
56-
5759
accept_encoding.each do |name|
5860
if wrapper = @wrappers[name]
5961
response.headers["content-encoding"] = name

test/protocol/http/content_encoding.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,23 @@
7979
expect(response.read).to be == "Hello World!"
8080
end
8181
end
82+
83+
with "nil body" do
84+
let(:app) do
85+
app = ->(request){
86+
Protocol::HTTP::Response[200, Protocol::HTTP::Headers["content-type" => "text/plain"], nil]
87+
}
88+
end
89+
90+
let(:client) {subject.new(app)}
91+
92+
it "does not compress response" do
93+
response = client.get("/index", {"accept-encoding" => "gzip"})
94+
95+
expect(response).to be(:success?)
96+
expect(response.headers).not.to have_keys("content-encoding")
97+
98+
expect(response.read).to be == nil
99+
end
100+
end
82101
end

0 commit comments

Comments
 (0)