aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/vendor/rack-1.0/rack/deflater.rb
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2009-02-07 00:08:28 -0600
committerJoshua Peek <josh@joshpeek.com>2009-02-07 00:08:28 -0600
commit2277fbedbea930fb8ce38ab7eb133de6fcc4a2d6 (patch)
tree784f2dfcd066eaa17a89cc435ec4de44393799c8 /actionpack/lib/action_controller/vendor/rack-1.0/rack/deflater.rb
parent24f2e676f700b8a387c6f4c27acf172658cd7863 (diff)
downloadrails-2277fbedbea930fb8ce38ab7eb133de6fcc4a2d6.tar.gz
rails-2277fbedbea930fb8ce38ab7eb133de6fcc4a2d6.tar.bz2
rails-2277fbedbea930fb8ce38ab7eb133de6fcc4a2d6.zip
Temporarily bundle Rack 1.0 prerelease for testing
Diffstat (limited to 'actionpack/lib/action_controller/vendor/rack-1.0/rack/deflater.rb')
-rw-r--r--actionpack/lib/action_controller/vendor/rack-1.0/rack/deflater.rb83
1 files changed, 83 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/vendor/rack-1.0/rack/deflater.rb b/actionpack/lib/action_controller/vendor/rack-1.0/rack/deflater.rb
new file mode 100644
index 0000000000..32f9a7ec29
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/rack-1.0/rack/deflater.rb
@@ -0,0 +1,83 @@
+require "zlib"
+require "stringio"
+require "time" # for Time.httpdate
+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(",")
+ 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
+
+ def self.gzip(body, mtime)
+ io = StringIO.new
+ gzip = Zlib::GzipWriter.new(io)
+ gzip.mtime = mtime
+
+ # TODO: Add streaming
+ body.each { |part| gzip << part }
+
+ 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
+ ]
+
+ # 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 }
+
+ return deflater.finish
+ end
+ end
+end