All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jruby.rack.error_app.show_status.rb Maven / Gradle / Ivy

Go to download

A servlet bridge for (Ruby-based) Rack applications that allow them to run in Java Application servers using JRuby.

There is a newer version: 1.2.2
Show newest version
require 'erb'

class JRuby::Rack::ErrorApp

  # catches empty responses and replaces them with a site explaining the error.
  #
  # @note kindly adapted from on Rack::ShowStatus
  # @private internal API - likely won't exist in 1.2.x
  class ShowStatus

    def initialize(app)
      require 'rack/request'; require 'rack/utils'
      @app = app
      @template = ERB.new(TEMPLATE)
    end

    def call(env)
      status, headers, body = @app.call(env)
      headers = ::Rack::Utils::HeaderHash.new(headers)
      empty = headers['Content-Length'].to_i <= 0

      # client or server error, or explicit message
      if (status.to_i >= 400 && empty) || env["rack.showstatus.detail"]
        # required erb template variables (captured with binding) :
        req = req = ::Rack::Request.new(env)
        message = ::Rack::Utils::HTTP_STATUS_CODES[status.to_i] || status.to_s
        detail = detail = env["rack.showstatus.detail"] || message

        body = @template.result(binding)
        size = ::Rack::Utils.bytesize(body)
        [status, headers.merge('Content-Type' => "text/html", 'Content-Length' => size.to_s), [body]]
      else
        [status, headers, body]
      end
    end

    # @private
    def h(obj)
      case obj
      when String
        ::Rack::Utils.escape_html(obj)
      else
        ::Rack::Utils.escape_html(obj.inspect)
      end
    end

    # :stopdoc:

# adapted from Django 
# Copyright (c) 2005, the Lawrence Journal-World
# 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 JRuby::Rack::ErrorApp::ShowStatus.

HTML # :startdoc: end end




© 2015 - 2025 Weber Informatics LLC | Privacy Policy