# 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 rack.showstatus.detail
# and will be shown as HTML. If such details exist, the error page
# is always rendered, even if the reply was not empty.
class ShowStatus
def initialize(app)
@app = app
@template = ERB.new(TEMPLATE)
end
def call(env)
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.
# Yes, it is dumb, but I don't like Ruby yelling at me.
req = req = Rack::Request.new(env)
message = Rack::Utils::HTTP_STATUS_CODES[status.to_i] || status.to_s
# 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
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
Utils.escape_html(obj)
else
Utils.escape_html(obj.inspect)
end
end
# :stopdoc:
# adapted from Django
# Copyright (c) Django Software Foundation and individual contributors.
# Used under the modified BSD license:
# http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5
TEMPLATE = <<'HTML'
<%=h message %> at <%=h req.script_name + req.path_info %>
<%=h message %> (<%= status.to_i %>)
Request Method:
<%=h req.request_method %>
Request URL:
<%=h req.url %>
<%=h detail %>
You're seeing this error because you use Rack::ShowStatus.