From 8271d80f14cbbb0edf985870311006a941df138b Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Tue, 5 May 2020 00:58:46 +0300 Subject: [PATCH 1/4] Rack::Backstage. Remove unnecessary contition The condition was needed to support old Ruby versions. As far as Rack::Contrib supports Ruby from v2.2 all the versions have String#bytesize method --- lib/rack/contrib/backstage.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rack/contrib/backstage.rb b/lib/rack/contrib/backstage.rb index 7ca99960..86d61cde 100644 --- a/lib/rack/contrib/backstage.rb +++ b/lib/rack/contrib/backstage.rb @@ -12,7 +12,7 @@ def initialize(app, path) def call(env) if File.exists?(@file) content = File.read(@file) - length = "".respond_to?(:bytesize) ? content.bytesize.to_s : content.size.to_s + length = content.bytesize.to_s [503, {'Content-Type' => 'text/html', 'Content-Length' => length}, [content]] else @app.call(env) From 537b7cd35549801c90072181b27d5a7a761b5481 Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Tue, 5 May 2020 01:03:38 +0300 Subject: [PATCH 2/4] Rack::JSONP. Remove excessive to_ary method call and refactor a little --- lib/rack/contrib/jsonp.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rack/contrib/jsonp.rb b/lib/rack/contrib/jsonp.rb index cb66132d..f0800972 100644 --- a/lib/rack/contrib/jsonp.rb +++ b/lib/rack/contrib/jsonp.rb @@ -55,7 +55,7 @@ def call(env) # Set new Content-Length, if it was set before we mutated the response body if headers['Content-Length'] - length = response.to_ary.inject(0) { |len, part| len + part.bytesize } + length = response.map(&:bytesize).reduce(0, :+) headers['Content-Length'] = length.to_s end end From d638d0efa7d1ad8fcae8383753be388d8e499451 Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Tue, 5 May 2020 01:05:48 +0300 Subject: [PATCH 3/4] Rack::MailExcpetion, Rack::NestedParams. Make rewind method call unconditional . `'rack.input'` should always support `rewind` method --- lib/rack/contrib/mailexceptions.rb | 2 +- lib/rack/contrib/nested_params.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rack/contrib/mailexceptions.rb b/lib/rack/contrib/mailexceptions.rb index 69bb5417..b72e7bfc 100644 --- a/lib/rack/contrib/mailexceptions.rb +++ b/lib/rack/contrib/mailexceptions.rb @@ -106,7 +106,7 @@ def send_notification(exception, env) def extract_body(env) if io = env['rack.input'] - io.rewind if io.respond_to?(:rewind) + io.rewind io.read end end diff --git a/lib/rack/contrib/nested_params.rb b/lib/rack/contrib/nested_params.rb index 2aa48a1a..d2644f13 100644 --- a/lib/rack/contrib/nested_params.rb +++ b/lib/rack/contrib/nested_params.rb @@ -26,7 +26,7 @@ def call(env) post_body = env[POST_BODY] env[FORM_INPUT] = post_body env[FORM_HASH] = Rack::Utils.parse_nested_query(post_body.read) - post_body.rewind if post_body.respond_to?(:rewind) + post_body.rewind end @app.call(env) end From 9fb9e3f3fd7ec163a2741493e5d99a7d7d12c61d Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Tue, 5 May 2020 01:08:06 +0300 Subject: [PATCH 4/4] Rack::Signals. Close original body --- lib/rack/contrib/signals.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/rack/contrib/signals.rb b/lib/rack/contrib/signals.rb index b3330deb..d61daf8e 100644 --- a/lib/rack/contrib/signals.rb +++ b/lib/rack/contrib/signals.rb @@ -26,6 +26,10 @@ def each(&block) @body.each(&block) @callback.call end + + def close + @body.close if @body.respond_to?(:close) + end end def initialize(app, &block)