Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 66 lines (51 sloc) 1.548 kB
a31ee95 @qerub Implemented Rack::Deflater
qerub authored
1 require "zlib"
cabc20d added gzip support to Rack::Deflater
Ben authored
2 require "stringio"
a31ee95 @qerub Implemented Rack::Deflater
qerub authored
3
4 module Rack
5
6 class Deflater
7 def initialize(app)
8 @app = app
9 end
10
11 def call(env)
12 status, headers, body = @app.call(env)
13
a2157d0 @qerub deflater.rb - Removed unnecessary require "time" and whitespace
qerub authored
14 request = Request.new(env)
6967688 @qerub deflater.rb - Added an error message for the 406 Not Acceptable case
qerub authored
15
cabc20d added gzip support to Rack::Deflater
Ben authored
16 encoding = Utils.select_best_encoding(%w(gzip deflate identity), request.accept_encoding)
a31ee95 @qerub Implemented Rack::Deflater
qerub authored
17
18 case encoding
cabc20d added gzip support to Rack::Deflater
Ben authored
19 when "gzip"
d2d51ff @spicyj added mtime for Deflater.gzip and fixed gzip spec
spicyj authored
20 mtime = headers["Last-Modified"] || Time.now
21 [status, headers.merge("Content-Encoding" => "gzip"), self.class.gzip(body, mtime)]
a31ee95 @qerub Implemented Rack::Deflater
qerub authored
22 when "deflate"
23 [status, headers.merge("Content-Encoding" => "deflate"), self.class.deflate(body)]
24 when "identity"
25 [status, headers, body]
26 when nil
6967688 @qerub deflater.rb - Added an error message for the 406 Not Acceptable case
qerub authored
27 message = "An acceptable encoding for the requested resource #{request.fullpath} could not be found."
28 [406, {"Content-Type" => "text/plain"}, message]
a31ee95 @qerub Implemented Rack::Deflater
qerub authored
29 end
30 end
31
a2157d0 @qerub deflater.rb - Removed unnecessary require "time" and whitespace
qerub authored
32 def self.gzip(body, mtime = Time.now)
cabc20d added gzip support to Rack::Deflater
Ben authored
33 io = StringIO.new
34 gzip = Zlib::GzipWriter.new(io)
d2d51ff @spicyj added mtime for Deflater.gzip and fixed gzip spec
spicyj authored
35 gzip.mtime = mtime
cabc20d added gzip support to Rack::Deflater
Ben authored
36
37 # TODO: Add streaming
38 # TODO: Consider all part types
39 body.each { |part| gzip << part }
40
41 gzip.close
42 return io.string
43 end
44
f3b542a @qerub deflater.rb - Move out the Zlib::Deflate constructor arguments to a c…
qerub authored
45 DEFLATE_ARGS = [
46 Zlib::DEFAULT_COMPRESSION,
47 # drop the zlib header which causes both Safari and IE to choke
48 -Zlib::MAX_WBITS,
49 Zlib::DEF_MEM_LEVEL,
50 Zlib::DEFAULT_STRATEGY
51 ]
52
a31ee95 @qerub Implemented Rack::Deflater
qerub authored
53 # Loosely based on Mongrel's Deflate handler
54 def self.deflate(body)
f3b542a @qerub deflater.rb - Move out the Zlib::Deflate constructor arguments to a c…
qerub authored
55 deflater = Zlib::Deflate.new(*DEFLATE_ARGS)
a31ee95 @qerub Implemented Rack::Deflater
qerub authored
56
57 # TODO: Add streaming
58 # TODO: Consider all part types
59 body.each { |part| deflater << part }
60
61 return deflater.finish
62 end
63 end
64
65 end
Something went wrong with that request. Please try again.