yahns Ruby server user/dev discussion
 help / color / Atom feed
From: Eric Wong <e@80x24.org>
To: yahns-public@yhbt.net
Subject: [PATCH] fix rack 2.x compatibility bugs
Date: Wed, 27 Jul 2016 00:00:15 +0000
Message-ID: <20160727000015.23694-1-e@80x24.org> (raw)

rack 2.x has some incompatible changes an deprecations; support
it but remain compatible with rack 1.x for the next few years.
---
 extras/try_gzip_static.rb     | 10 +++++++++-
 test/test_output_buffering.rb |  5 ++++-
 test/test_reopen_logs.rb      |  6 +++++-
 3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/extras/try_gzip_static.rb b/extras/try_gzip_static.rb
index d412589..4dd435a 100644
--- a/extras/try_gzip_static.rb
+++ b/extras/try_gzip_static.rb
@@ -49,7 +49,7 @@ def get_range(env, path, st)
     end
 
     size = st.size
-    ranges = Rack::Utils.byte_ranges(env, size)
+    ranges = byte_ranges(env, size)
     if ranges.nil? || ranges.length > 1
       [ 200 ] # serve the whole thing, possibly with static gzip \o/
     elsif ranges.empty?
@@ -221,4 +221,12 @@ def r(code, exc = nil, env = nil)
       [ code, h, [ msg ] ]
     end
   end
+
+  if Rack::Utils.respond_to?(:get_byte_ranges) # rack 2.0+
+    def byte_ranges(env, size)
+      Rack::Utils.get_byte_ranges(env['HTTP_RANGE'], size)
+    end
+  else # rack 1.x
+    def byte_ranges(env, size); Rack::Utils.byte_ranges(env, size); end
+  end
 end
diff --git a/test/test_output_buffering.rb b/test/test_output_buffering.rb
index da93b87..ba3e94a 100644
--- a/test/test_output_buffering.rb
+++ b/test/test_output_buffering.rb
@@ -126,7 +126,10 @@ def gplv3.each
           end
           size = gplv3.size
           len = size.to_s
-          ranges = Rack::Utils.byte_ranges(e, size)
+
+          ranges = Rack::Utils.respond_to?(:get_byte_ranges) ?
+                   Rack::Utils.get_byte_ranges(e['HTTP_RANGE'], size) :
+                   Rack::Utils.byte_ranges(e, size)
           status = 200
           h = { "Content-Type" => "text/plain", "Content-Length" => len }
           if ranges && ranges.size == 1
diff --git a/test/test_reopen_logs.rb b/test/test_reopen_logs.rb
index 2eb3e0d..2ad1306 100644
--- a/test/test_reopen_logs.rb
+++ b/test/test_reopen_logs.rb
@@ -2,7 +2,11 @@
 # License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
 # frozen_string_literal: true
 require_relative 'server_helper'
-require 'rack/commonlogger'
+require 'rack'
+# trigger autoload, since rack 1.x => 2.x renames:
+# 'rack/commonlogger' => 'rack/common_logger'
+# so we can't require directly
+Rack::CommonLogger.class
 
 class TestReopenLogs < Testcase
   ENV["N"].to_i > 1 and parallelize_me!
-- 
EW


                 reply index

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/yahns/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160727000015.23694-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=yahns-public@yhbt.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

yahns Ruby server user/dev discussion

Archives are clonable:
	git clone --mirror https://yhbt.net/yahns-public
	git clone --mirror http://ou63pmih66umazou.onion/yahns-public

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.lang.ruby.yahns
	nntp://ou63pmih66umazou.onion/inbox.comp.lang.ruby.yahns

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git