Skip to content

Commit dae7ca7

Browse files
committed
make back-ported ShowStatus compatible and use it instead of Rack::ShowStatus
see #190 for details
1 parent c0b925e commit dae7ca7

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

src/main/java/org/jruby/rack/DefaultRackApplicationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public IRubyObject createErrorApplicationObject(final Ruby runtime) {
189189
}
190190
if (errorApp == null) {
191191
errorApp = "require 'jruby/rack/error_app' \n" +
192-
"use Rack::ShowStatus \n" +
192+
"use JRuby::Rack::ErrorApp::ShowStatus \n" +
193193
"run JRuby::Rack::ErrorApp.new";
194194
}
195195
runtime.evalScriptlet("load 'jruby/rack/boot/rack.rb'");

src/main/ruby/jruby/rack/error_app.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module JRuby
99
module Rack
1010
class ErrorApp
1111

12+
autoload :ShowStatus, 'jruby/rack/error_app/show_status'
13+
1214
EXCEPTION = org.jruby.rack.RackEnvironment::EXCEPTION
1315

1416
DEFAULT_RESPONSE_CODE = 500

src/main/ruby/jruby/rack/error_app/show_status.rb

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,44 @@
33
require 'rack/utils'
44

55
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.
126

7+
# catches empty responses and replaces them with a site explaining the error.
8+
#
9+
# @note kindly adapted from on Rack::ShowStatus
1310
class ShowStatus
11+
1412
def initialize(app)
1513
@app = app
1614
@template = ERB.new(TEMPLATE)
1715
end
1816

1917
def call(env)
2018
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
2321

2422
# client or server error, or explicit message
2523
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
3427
detail = detail = env["rack.showstatus.detail"] || message
3528

3629
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]]
3932
else
4033
[status, headers, body]
4134
end
4235
end
4336

44-
def h(obj) # :nodoc:
37+
# @private
38+
def h(obj)
4539
case obj
4640
when String
47-
Utils.escape_html(obj)
41+
::Rack::Utils.escape_html(obj)
4842
else
49-
Utils.escape_html(obj.inspect)
43+
::Rack::Utils.escape_html(obj.inspect)
5044
end
5145
end
5246

@@ -101,7 +95,7 @@ def h(obj) # :nodoc:
10195
10296
<div id="explanation">
10397
<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>.
10599
</p>
106100
</div>
107101
</body>

src/spec/ruby/rack/application_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ def newRuntime() # use the current runtime instead of creating new
269269
#expect( rack_app ).to be_a Rack::Handler::Servlet
270270
expect( rack_app.class.name ).to eql 'Rack::Handler::Servlet'
271271
app = rack_app.instance_variable_get('@app')
272-
expect( app ).to be_a Rack::ShowStatus
273-
#expect( app.class.name ).to eql 'Rack::ShowStatus'
272+
expect( app ).to be_a JRuby::Rack::ErrorApp::ShowStatus
273+
#expect( app.class.name ).to eql 'JRuby::Rack::ErrorApp::ShowStatus'
274274
error_app = app.instance_variable_get('@app')
275275
expect( error_app ).to be_a JRuby::Rack::ErrorApp
276276
#expect( error_app.class.name ).to eql 'JRuby::Rack::ErrorApp'

0 commit comments

Comments
 (0)