Permalink
Browse files

Prevent Rack::Deflater from double-encoding

  • Loading branch information...
1 parent 0fbb575 commit 717cdb5ec9f5217d15b1817949a5ed641fb7b731 @rykov rykov committed Feb 18, 2012
Showing with 22 additions and 1 deletion.
  1. +2 −1 lib/rack/deflater.rb
  2. +20 −0 test/spec_deflater.rb
View
@@ -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
View
@@ -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 717cdb5

Please sign in to comment.