Skip to content

Commit

Permalink
Skip deflating in Rack::Deflater if Content-Length is 0
Browse files Browse the repository at this point in the history
Fixes usage with Rack::Sendfile.

Fixes #1142
  • Loading branch information
jeremyevans authored and rafaelfranca committed Jan 16, 2020
1 parent e758a11 commit 75d178c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rack/deflater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ def should_deflate?(env, status, headers, body)
# Skip if @condition lambda is given and evaluates to false
return false if @condition && !@condition.call(env, status, headers, body)

# No point in compressing empty body, also handles usage with
# Rack::Sendfile.
return false if headers[CONTENT_LENGTH] == '0'

true
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/spec_deflater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,15 @@ class << app_body; def each; yield('foo'); yield('bar'); end; end
verify(200, 'Hello World!', { 'gzip' => nil }, options)
end

it "not deflate if content-length is 0" do
options = {
'response_headers' => {
'Content-Length' => '0'
},
}
verify(200, '', { 'gzip' => nil }, options)
end

it "deflate response if :if lambda evaluates to true" do
options = {
'deflater_options' => {
Expand Down

0 comments on commit 75d178c

Please sign in to comment.