Permalink
Browse files

Added excluding certain paths from deflating if they match :exclude r…

…egexp option.
  • Loading branch information...
1 parent 7f24707 commit 366012821f860901ba28526e614d1b1e63c32ebe @jakubpawlowicz committed Nov 14, 2012
Showing with 36 additions and 3 deletions.
  1. +10 −3 lib/rack/deflater.rb
  2. +26 −0 test/spec_deflater.rb
View
@@ -14,7 +14,7 @@ def call(env)
status, headers, body = @app.call(env)
headers = Utils::HeaderHash.new(headers)
- unless should_deflate?(status, headers, body)
+ unless should_deflate?(env, status, headers, body)
return [status, headers, body]
end
@@ -100,7 +100,7 @@ def each
private
- def should_deflate?(status, headers, body)
+ def should_deflate?(env, status, headers, body)
# Skip compressing empty entity body responses and responses with
# no-transform set.
if Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
@@ -109,12 +109,19 @@ def should_deflate?(status, headers, body)
return false
end
- # Skip if request body is too short
+ # Skip if response body is too short
if @options[:min_content_length] &&
@options[:min_content_length] > headers['Content-Length'].to_i
return false
end
+ # Skip if paths :exclude is provided and evaluates to true
+ if @options[:exclude] &&
+ @options[:exclude].kind_of?(Regexp) &&
+ @options[:exclude].match(env['PATH_INFO'])
+ return false
+ end
+
# Skip if :skip_if lambda is provided and it evaluates to true
if @options[:skip_if] && @options[:skip_if].call(status, headers, body)
return false
View
@@ -231,6 +231,32 @@ class << body; def each; yield("foo"); yield("bar"); end; end
gz.close
end
+ should "skip processing if path matches :exclude" do
+ body = 'Hello World!'
+ app = lambda do |env|
+ [200, {'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }, [body]]
+ end
+ request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
+ response = deflater(app, { exclude: /^\/$/ }).call(request)
+
+ response[0].should.equal(200)
+ Enumerator.new(response[2]).to_a.join.should.equal("Hello World!")
+ end
+
+ should "gzip response if path do not match :exclude" do
+ body = 'Hello World!'
+ app = lambda do |env|
+ [200, {'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }, [body]]
+ end
+ request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
+ response = deflater(app, { exclude: /^sth$/ }).call(request)
+
+ response[0].should.equal(200)
+ gz = Zlib::GzipReader.new(StringIO.new(Enumerator.new(response[2]).to_a.join('')))
+ gz.read.should.equal(body)
+ gz.close
+ end
+
should "do nothing if :skip_if lambda evaluates to true" do
body = 'Hello World!'
app = lambda do |env|

0 comments on commit 3660128

Please sign in to comment.