Sha256: 343fa360ad6d27bdb3452779bef44b71a462ddb74491d30d1453ecb79e128818

Contents?: true

Size: 796 Bytes

Versions: 9

Compression:

Stored size: 796 Bytes

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2016-2022, by Samuel Williams.

require_relative 'middleware'

module Utopia
	# 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.
	class ContentLength
		def initialize(app)
			@app = app
		end
		
		def content_length_of(body)
			if body.respond_to?(:map)
				return body.map(&:bytesize).reduce(0, :+)
			end
		end
		
		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
	end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
utopia-2.25.0 lib/utopia/content_length.rb
utopia-2.24.4 lib/utopia/content_length.rb
utopia-2.24.3 lib/utopia/content_length.rb
utopia-2.24.1 lib/utopia/content_length.rb
utopia-2.23.0 lib/utopia/content_length.rb
utopia-2.22.2 lib/utopia/content_length.rb
utopia-2.22.1 lib/utopia/content_length.rb
utopia-2.22.0 lib/utopia/content_length.rb
utopia-2.21.0 lib/utopia/content_length.rb