lib/rack/show_status.rb in rack-2.2.10 vs lib/rack/show_status.rb in rack-3.0.0.beta1
- old
+ new
@@ -1,9 +1,14 @@
# frozen_string_literal: true
require 'erb'
+require_relative 'constants'
+require_relative 'utils'
+require_relative 'request'
+require_relative 'body_proxy'
+
module Rack
# Rack::ShowStatus catches all empty responses and replaces them
# with a site explaining the error.
#
# Additional details can be put into <tt>rack.showstatus.detail</tt>
@@ -15,12 +20,11 @@
@app = app
@template = ERB.new(TEMPLATE)
end
def call(env)
- status, headers, body = @app.call(env)
- headers = Utils::HeaderHash[headers]
+ status, headers, body = response = @app.call(env)
empty = headers[CONTENT_LENGTH].to_i <= 0
# client or server error, or explicit message
if (status.to_i >= 400 && empty) || env[RACK_SHOWSTATUS_DETAIL]
# This double assignment is to prevent an "unused variable" warning.
@@ -31,15 +35,21 @@
# This double assignment is to prevent an "unused variable" warning.
# Yes, it is dumb, but I don't like Ruby yelling at me.
detail = detail = env[RACK_SHOWSTATUS_DETAIL] || message
- body = @template.result(binding)
- size = body.bytesize
- [status, headers.merge(CONTENT_TYPE => "text/html", CONTENT_LENGTH => size.to_s), [body]]
- else
- [status, headers, body]
+ html = @template.result(binding)
+ size = html.bytesize
+
+ response[2] = Rack::BodyProxy.new([html]) do
+ body.close if body.respond_to?(:close)
+ end
+
+ headers[CONTENT_TYPE] = "text/html"
+ headers[CONTENT_LENGTH] = size.to_s
end
+
+ response
end
def h(obj) # :nodoc:
case obj
when String