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.



28
29
30
# File 'lib/utopia/content_length.rb', line 28

def initialize(app)
	@app = app
end

Instance Method Details

#call(env) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/utopia/content_length.rb', line 38

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



32
33
34
35
36
# File 'lib/utopia/content_length.rb', line 32

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