Skip to content
Browse files

Deflater sets Content-Length header [#9 state:resolved]

  • Loading branch information...
1 parent b5bf66f commit 2f79f55c314ea6026ffc3350f261ad3e902566be @josh josh committed
Showing with 88 additions and 76 deletions.
  1. +65 −70 lib/rack/deflater.rb
  2. +23 −6 test/spec_rack_deflater.rb
View
135 lib/rack/deflater.rb
@@ -4,85 +4,80 @@
require 'rack/utils'
module Rack
-
-class Deflater
- def initialize(app)
- @app = app
- end
-
- def call(env)
- status, headers, body = @app.call(env)
- headers = Utils::HeaderHash.new(headers)
-
- # 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/
- return [status, headers, body]
- end
-
- request = Request.new(env)
-
- encoding = Utils.select_best_encoding(%w(gzip deflate identity),
- request.accept_encoding)
-
- # Set the Vary HTTP header.
- vary = headers["Vary"].to_s.split(",").map { |v| v.strip }
- unless vary.include?("*") || vary.include?("Accept-Encoding")
- headers["Vary"] = vary.push("Accept-Encoding").join(",")
+ class Deflater
+ def initialize(app)
+ @app = app
end
- case encoding
- when "gzip"
- mtime = if headers.key?("Last-Modified")
- Time.httpdate(headers["Last-Modified"])
- else
- Time.now
- end
- [status,
- headers.merge("Content-Encoding" => "gzip"),
- self.class.gzip(body, mtime)]
- when "deflate"
- [status,
- headers.merge("Content-Encoding" => "deflate"),
- self.class.deflate(body)]
- when "identity"
- [status, headers, body]
- when nil
- message = ["An acceptable encoding for the requested resource #{request.fullpath} could not be found."]
- [406, {"Content-Type" => "text/plain"}, message]
+ def call(env)
+ status, headers, body = @app.call(env)
+ headers = Utils::HeaderHash.new(headers)
+
+ # 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/
+ return [status, headers, body]
+ end
+
+ request = Request.new(env)
+
+ encoding = Utils.select_best_encoding(%w(gzip deflate identity),
+ request.accept_encoding)
+
+ # Set the Vary HTTP header.
+ vary = headers["Vary"].to_s.split(",").map { |v| v.strip }
+ unless vary.include?("*") || vary.include?("Accept-Encoding")
+ headers["Vary"] = vary.push("Accept-Encoding").join(",")
+ end
+
+ case encoding
+ when "gzip"
+ mtime = headers.key?("Last-Modified") ?
+ Time.httpdate(headers["Last-Modified"]) : Time.now
+ body = self.class.gzip(body, mtime)
+ headers = headers.merge("Content-Encoding" => "gzip", "Content-Length" => body.length.to_s)
+ [status, headers, [body]]
+ when "deflate"
+ body = self.class.deflate(body)
+ headers = headers.merge("Content-Encoding" => "deflate", "Content-Length" => body.length.to_s)
+ [status, headers, [body]]
+ when "identity"
+ [status, headers, body]
+ when nil
+ message = ["An acceptable encoding for the requested resource #{request.fullpath} could not be found."]
+ [406, {"Content-Type" => "text/plain", "Content-Length" => message[0].length.to_s}, message]
+ end
end
- end
- def self.gzip(body, mtime)
- io = StringIO.new
- gzip = Zlib::GzipWriter.new(io)
- gzip.mtime = mtime
+ def self.gzip(body, mtime)
+ io = StringIO.new
+ gzip = Zlib::GzipWriter.new(io)
+ gzip.mtime = mtime
- # TODO: Add streaming
- body.each { |part| gzip << part }
+ # TODO: Add streaming
+ body.each { |part| gzip << part }
- gzip.close
- return io.string
- end
+ gzip.close
+ return io.string
+ end
- DEFLATE_ARGS = [
- Zlib::DEFAULT_COMPRESSION,
- # drop the zlib header which causes both Safari and IE to choke
- -Zlib::MAX_WBITS,
- Zlib::DEF_MEM_LEVEL,
- Zlib::DEFAULT_STRATEGY
- ]
+ DEFLATE_ARGS = [
+ Zlib::DEFAULT_COMPRESSION,
+ # drop the zlib header which causes both Safari and IE to choke
+ -Zlib::MAX_WBITS,
+ Zlib::DEF_MEM_LEVEL,
+ Zlib::DEFAULT_STRATEGY
+ ]
- # Loosely based on Mongrel's Deflate handler
- def self.deflate(body)
- deflater = Zlib::Deflate.new(*DEFLATE_ARGS)
+ # Loosely based on Mongrel's Deflate handler
+ def self.deflate(body)
+ deflater = Zlib::Deflate.new(*DEFLATE_ARGS)
- # TODO: Add streaming
- body.each { |part| deflater << part }
+ # TODO: Add streaming
+ body.each { |part| deflater << part }
- return deflater.finish
+ return deflater.finish
+ end
end
end
-
-end
View
29 test/spec_rack_deflater.rb
@@ -21,7 +21,11 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response = build_response(200, body, "deflate")
response[0].should.equal(200)
- response[1].should.equal({ "Content-Encoding" => "deflate", "Vary" => "Accept-Encoding" })
+ response[1].should.equal({
+ "Content-Encoding" => "deflate",
+ "Content-Length" => "8",
+ "Vary" => "Accept-Encoding"
+ })
response[2].to_s.should.equal("K\313\317OJ,\002\000")
end
@@ -30,7 +34,11 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response = build_response(200, "Hello world!", "deflate")
response[0].should.equal(200)
- response[1].should.equal({ "Content-Encoding" => "deflate", "Vary" => "Accept-Encoding" })
+ response[1].should.equal({
+ "Content-Encoding" => "deflate",
+ "Content-Length" => "14",
+ "Vary" => "Accept-Encoding"
+ })
response[2].to_s.should.equal("\363H\315\311\311W(\317/\312IQ\004\000")
end
@@ -41,7 +49,11 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response = build_response(200, body, "gzip")
response[0].should.equal(200)
- response[1].should.equal({ "Content-Encoding" => "gzip", "Vary" => "Accept-Encoding" })
+ response[1].should.equal({
+ "Content-Encoding" => "gzip",
+ "Content-Length" => "26",
+ "Vary" => "Accept-Encoding",
+ })
io = StringIO.new(response[2].to_s)
gz = Zlib::GzipReader.new(io)
@@ -68,12 +80,12 @@ class << body; def each; yield("foo"); yield("bar"); end; end
specify "should handle the lack of an acceptable encoding" do
response1 = build_response(200, "Hello world!", "identity;q=0", "PATH_INFO" => "/")
response1[0].should.equal(406)
- response1[1].should.equal({"Content-Type" => "text/plain"})
+ response1[1].should.equal({"Content-Type" => "text/plain", "Content-Length" => "71"})
response1[2].should.equal(["An acceptable encoding for the requested resource / could not be found."])
response2 = build_response(200, "Hello world!", "identity;q=0", "SCRIPT_NAME" => "/foo", "PATH_INFO" => "/bar")
response2[0].should.equal(406)
- response2[1].should.equal({"Content-Type" => "text/plain"})
+ response2[1].should.equal({"Content-Type" => "text/plain", "Content-Length" => "78"})
response2[2].should.equal(["An acceptable encoding for the requested resource /foo/bar could not be found."])
end
@@ -85,7 +97,12 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response = Rack::Deflater.new(app).call(request)
response[0].should.equal(200)
- response[1].should.equal({ "Content-Encoding" => "gzip", "Vary" => "Accept-Encoding", "Last-Modified" => last_modified })
+ response[1].should.equal({
+ "Content-Encoding" => "gzip",
+ "Content-Length" => "32",
+ "Vary" => "Accept-Encoding",
+ "Last-Modified" => last_modified
+ })
io = StringIO.new(response[2].to_s)
gz = Zlib::GzipReader.new(io)

0 comments on commit 2f79f55

Please sign in to comment.
Something went wrong with that request. Please try again.