Class: Utopia::ContentLength

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/content_length.rb

Overview

A faster implementation of Rack::ContentLength which doesn’t rewrite body, but does expect it to either be an Array or an object that responds to #bytesize.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ContentLength

Returns a new instance of ContentLength.



26
27
28
# File 'lib/utopia/content_length.rb', line 26

def initialize(app)
	@app = app
end

Instance Method Details

#call(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/utopia/content_length.rb', line 34

def call(env)
	response = @app.call(env)
	
	unless response[2].empty? or response[1].include?(Rack::CONTENT_LENGTH)
		if content_length = self.content_length_of(response[2])
			response[1][Rack::CONTENT_LENGTH] = content_length
		end
	end
	
	return response
end

#content_length_of(body) ⇒ Object



30
31
32
# File 'lib/utopia/content_length.rb', line 30

def content_length_of(body)
	return body.map(&:bytesize).reduce(0, :+)
end