patch to make deflater do nothing if content-encoding is already set

4 views
Skip to first unread message

mmb

unread,
Jul 28, 2009, 11:02:13 PM7/28/09
to Rack Development
This will handle content encoding being set by another middleware or
"use Rack::Deflater" being called twice.

From b186a5a3497fe9003a431394958ef3d87786a5c1 Mon Sep 17 00:00:00 2001
From: Matthew M. Boedicker <matt...@boedicker.org>
Date: Tue, 28 Jul 2009 22:41:37 -0400
Subject: [PATCH] leave content encoding and content as is when there
is already a content encoding

---
lib/rack/deflater.rb | 3 ++-
test/spec_rack_deflater.rb | 10 ++++++++++
2 files changed, 12 insertions(+), 1 deletions(-)

diff --git a/lib/rack/deflater.rb b/lib/rack/deflater.rb
index 14137a9..109df06 100644
--- a/lib/rack/deflater.rb
+++ b/lib/rack/deflater.rb
@@ -16,7 +16,8 @@ module Rack
# 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']
return [status, headers, body]
end

diff --git a/test/spec_rack_deflater.rb b/test/spec_rack_deflater.rb
index c9bb318..dd63769 100644
--- a/test/spec_rack_deflater.rb
+++ b/test/spec_rack_deflater.rb
@@ -124,4 +124,14 @@ context "Rack::Deflater" do
response[1].should.not.include "Content-Encoding"
response[2].join.should.equal("Hello World!")
end
+
+ specify "should leave content and encoding as is when there is
already a content-encoding" do
+ app = lambda { |env| [200, {'Content-Encoding' => 'something'},
['Hello World!']] }
+ request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" =>
"gzip")
+ response = Rack::Deflater.new(app).call(request)
+
+ response[1].should.include("Content-Encoding")
+ response[1]['Content-Encoding'].should.equal("something")
+ response[2].join.should.equal("Hello World!")
+ end
end
--
1.6.0.4
Reply all
Reply to author
Forward
0 new messages