Class: Isomorfeus::Transport::CompressorRack
- Inherits:
-
Object
- Object
- Isomorfeus::Transport::CompressorRack
- Defined in:
- lib/isomorfeus/transport/compressor_rack.rb
Defined Under Namespace
Classes: BrotliStream, GzipStream
Constant Summary collapse
- C_ENCODINGS =
%w[br gzip identity].freeze
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ CompressorRack
constructor
A new instance of CompressorRack.
Constructor Details
#initialize(app, options = {}) ⇒ CompressorRack
Returns a new instance of CompressorRack.
8 9 10 |
# File 'lib/isomorfeus/transport/compressor_rack.rb', line 8 def initialize(app, = {}) @app = app end |
Instance Method Details
#call(env) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/isomorfeus/transport/compressor_rack.rb', line 12 def call(env) status, headers, body = @app.call(env) headers = Rack::Utils::HeaderHash[headers] unless should_deflate?(env, status, headers, body) return [status, headers, body] end request = Rack::Request.new(env) encoding = Rack::Utils.select_best_encoding(C_ENCODINGS, request.accept_encoding) vary = headers["Vary"].to_s.split(",").map(&:strip) unless vary.include?("*") || vary.include?("Accept-Encoding") headers["Vary"] = vary.push("Accept-Encoding").join(",") end case encoding when "br" headers['Content-Encoding'] = "br" headers.delete(Rack::CONTENT_LENGTH) [status, headers, BrotliStream.new(body, @sync)] when "gzip" headers['Content-Encoding'] = "gzip" headers.delete(Rack::CONTENT_LENGTH) mtime = headers["Last-Modified"] mtime = Time.httpdate(mtime).to_i if mtime [status, headers, GzipStream.new(body, mtime, @sync)] when "identity" [status, headers, body] when nil = "An acceptable encoding for the requested resource #{request.fullpath} could not be found." bp = Rack::BodyProxy.new([]) { body.close if body.respond_to?(:close) } [406, { CONTENT_TYPE => "text/plain", CONTENT_LENGTH => .length.to_s }, bp] end end |