Skip to content

Commit 0d40a1b

Browse files
committed
re-invent missing Rack::Utils.bytesize (2.0 will work) + prefix ::Rack constant!
1 parent 0c1f3c7 commit 0d40a1b

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def serve(code, path, env)
8989
body = env['REQUEST_METHOD'] == 'HEAD' ? [] : FileBody.new(path, size = File.size?(path))
9090
response = [ code, headers, body ]
9191

92-
size ||= ::Rack::Utils.bytesize(File.read(path)) if defined?(::Rack::Utils.bytesize)
92+
size ||= Utils.bytesize(File.read(path))
9393

9494
response[1]['Content-Length'] = size.to_s if size
9595
response
@@ -155,13 +155,21 @@ def expand_path(path, root = self.root)
155155

156156
begin
157157
require 'rack/utils'
158+
Utils = ::Rack::Utils
159+
160+
if ''.respond_to?(:bytesize) # Ruby >= 1.9
161+
def Utils.bytesize(string); string.bytesize end
162+
else
163+
def Utils.bytesize(string); string.size end
164+
end unless defined? Utils.bytesize
165+
158166
require 'rack/mime'
159167
rescue LoadError; end
160168

161-
if defined? Rack::Utils.best_q_match
169+
if defined? Utils.best_q_match
162170

163171
def accepts_html?(env)
164-
Rack::Utils.best_q_match(env['HTTP_ACCEPT'], %w[text/html])
172+
Utils.best_q_match(env['HTTP_ACCEPT'], %w[text/html])
165173
rescue
166174
http_accept?(env, 'text/html')
167175
end

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ def initialize(app)
1515

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

2121
detail = env['rack.showstatus.detail']
2222
# client or server error, or explicit message
2323
if (status.to_i >= 400 && empty) || detail
2424
# required erb template variables (captured with binding) :
2525
request = req = ::Rack::Request.new(env); request && req # avoid un-used warning
26-
message = ::Rack::Utils::HTTP_STATUS_CODES[status.to_i] || status.to_s
26+
message = Utils::HTTP_STATUS_CODES[status.to_i] || status.to_s
2727
detail = detail.nil? ? message : detail # false for no detail
2828

2929
body = @template.result(binding)
30-
size = ::Rack::Utils.bytesize(body)
30+
size = Utils.bytesize(body)
3131
[status, headers.merge('Content-Type' => "text/html", 'Content-Length' => size.to_s), [body]]
3232
else
3333
[status, headers, body]
@@ -38,9 +38,9 @@ def call(env)
3838
def h(obj)
3939
case obj
4040
when String
41-
::Rack::Utils.escape_html(obj)
41+
Utils.escape_html(obj)
4242
else
43-
::Rack::Utils.escape_html(obj.inspect)
43+
Utils.escape_html(obj.inspect)
4444
end
4545
end
4646

0 commit comments

Comments
 (0)