lib/rack/contrib/csshttprequest.rb in rack-contrib-2.2.0 vs lib/rack/contrib/csshttprequest.rb in rack-contrib-2.3.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'csshttprequest' module Rack # A Rack middleware for providing CSSHTTPRequest responses. @@ -11,24 +13,26 @@ # Proxies the request to the application then encodes the response with # the CSSHTTPRequest encoder def call(env) status, headers, response = @app.call(env) + headers = Utils::HeaderHash.new(headers) + if chr_request?(env) - response = encode(response) - modify_headers!(headers, response) + encoded_response = encode(response) + modify_headers!(headers, encoded_response) + response = [encoded_response] end [status, headers, response] end def chr_request?(env) env['csshttprequest.chr'] ||= !(/\.chr$/.match(env['PATH_INFO'])).nil? || Rack::Request.new(env).params['_format'] == 'chr' end - def encode(response, assembled_body="") - response.each { |s| assembled_body << s.to_s } # call down the stack - return ::CSSHTTPRequest.encode(assembled_body) + def encode(body) + ::CSSHTTPRequest.encode(body.to_enum.to_a.join) end def modify_headers!(headers, encoded_response) headers['Content-Length'] = encoded_response.bytesize.to_s headers['Content-Type'] = 'text/css'