Skip to content

Commit

Permalink
Merge pull request rack#341 from rykov/master
Browse files Browse the repository at this point in the history
Prevent Rack::Deflater from encoding pre-encoded responses
  • Loading branch information
raggi committed Mar 17, 2012
2 parents d404218 + 717cdb5 commit 52c80c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/rack/deflater.rb
Expand Up @@ -16,7 +16,8 @@ def call(env)
# Skip compressing empty entity body responses and responses with
# no-transform set.
if Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
headers['Cache-Control'].to_s =~ /\bno-transform\b/
headers['Cache-Control'].to_s =~ /\bno-transform\b/ ||
(headers['Content-Encoding'] && headers['Content-Encoding'] !~ /\bidentity\b/)
return [status, headers, body]
end

Expand Down
20 changes: 20 additions & 0 deletions test/spec_deflater.rb
Expand Up @@ -166,4 +166,24 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response[1].should.not.include "Content-Encoding"
response[2].join.should.equal("Hello World!")
end

should "do nothing when Content-Encoding already present" do
app = lambda { |env| [200, {'Content-Encoding' => 'gzip'}, ['Hello World!']] }
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
response = Rack::Deflater.new(app).call(request)

response[0].should.equal(200)
response[2].join.should.equal("Hello World!")
end

should "deflate when Content-Encoding is identity" do
app = lambda { |env| [200, {'Content-Encoding' => 'identity'}, ['Hello World!']] }
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "deflate")
response = Rack::Deflater.new(app).call(request)

response[0].should.equal(200)
buf = ''
response[2].each { |part| buf << part }
inflate(buf).should.equal("Hello World!")
end
end

0 comments on commit 52c80c7

Please sign in to comment.