|
3 | 3 | require 'rack/utils'
|
4 | 4 |
|
5 | 5 | class JRuby::Rack::ErrorApp
|
6 |
| - # Rack::ShowStatus catches all empty responses and replaces them |
7 |
| - # with a site explaining the error. |
8 |
| - # |
9 |
| - # Additional details can be put into <tt>rack.showstatus.detail</tt> |
10 |
| - # and will be shown as HTML. If such details exist, the error page |
11 |
| - # is always rendered, even if the reply was not empty. |
12 | 6 |
|
| 7 | + # catches empty responses and replaces them with a site explaining the error. |
| 8 | + # |
| 9 | + # @note kindly adapted from on Rack::ShowStatus |
13 | 10 | class ShowStatus
|
| 11 | + |
14 | 12 | def initialize(app)
|
15 | 13 | @app = app
|
16 | 14 | @template = ERB.new(TEMPLATE)
|
17 | 15 | end
|
18 | 16 |
|
19 | 17 | def call(env)
|
20 | 18 | status, headers, body = @app.call(env)
|
21 |
| - headers = Utils::HeaderHash.new(headers) |
22 |
| - empty = headers[CONTENT_LENGTH].to_i <= 0 |
| 19 | + headers = ::Rack::Utils::HeaderHash.new(headers) |
| 20 | + empty = headers['Content-Length'].to_i <= 0 |
23 | 21 |
|
24 | 22 | # client or server error, or explicit message
|
25 | 23 | if (status.to_i >= 400 && empty) || env["rack.showstatus.detail"]
|
26 |
| - # This double assignment is to prevent an "unused variable" warning on |
27 |
| - # Ruby 1.9.3. Yes, it is dumb, but I don't like Ruby yelling at me. |
28 |
| - req = req = Rack::Request.new(env) |
29 |
| - |
30 |
| - message = Rack::Utils::HTTP_STATUS_CODES[status.to_i] || status.to_s |
31 |
| - |
32 |
| - # This double assignment is to prevent an "unused variable" warning on |
33 |
| - # Ruby 1.9.3. Yes, it is dumb, but I don't like Ruby yelling at me. |
| 24 | + # required erb template variables (captured with binding) : |
| 25 | + req = req = ::Rack::Request.new(env) |
| 26 | + message = ::Rack::Utils::HTTP_STATUS_CODES[status.to_i] || status.to_s |
34 | 27 | detail = detail = env["rack.showstatus.detail"] || message
|
35 | 28 |
|
36 | 29 | body = @template.result(binding)
|
37 |
| - size = Rack::Utils.bytesize(body) |
38 |
| - [status, headers.merge(CONTENT_TYPE => "text/html", CONTENT_LENGTH => size.to_s), [body]] |
| 30 | + size = ::Rack::Utils.bytesize(body) |
| 31 | + [status, headers.merge('Content-Type' => "text/html", 'Content-Length' => size.to_s), [body]] |
39 | 32 | else
|
40 | 33 | [status, headers, body]
|
41 | 34 | end
|
42 | 35 | end
|
43 | 36 |
|
44 |
| - def h(obj) # :nodoc: |
| 37 | + # @private |
| 38 | + def h(obj) |
45 | 39 | case obj
|
46 | 40 | when String
|
47 |
| - Utils.escape_html(obj) |
| 41 | + ::Rack::Utils.escape_html(obj) |
48 | 42 | else
|
49 |
| - Utils.escape_html(obj.inspect) |
| 43 | + ::Rack::Utils.escape_html(obj.inspect) |
50 | 44 | end
|
51 | 45 | end
|
52 | 46 |
|
@@ -101,7 +95,7 @@ def h(obj) # :nodoc:
|
101 | 95 |
|
102 | 96 | <div id="explanation">
|
103 | 97 | <p>
|
104 |
| - You're seeing this error because you use <code>Rack::ShowStatus</code>. |
| 98 | + You're seeing this error because you use <code>JRuby::Rack::ErrorApp::ShowStatus</code>. |
105 | 99 | </p>
|
106 | 100 | </div>
|
107 | 101 | </body>
|
|
0 commit comments