class Utopia::ContentLength

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.

Public Class Methods

new(app) click to toggle source
# File lib/utopia/content_length.rb, line 28
def initialize(app)
        @app = app
end

Public Instance Methods

call(env) click to toggle source
# 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) click to toggle source
# 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