From af18cf588a2fed68edd17e00e30169b4aa5a056c Mon Sep 17 00:00:00 2001 From: Trevor Bramble Date: Sun, 26 Jul 2015 22:51:25 -0700 Subject: [PATCH 01/16] Raise supported/expected dependency versions Drop Travis testing for Ruby versions lower than 2.2 (or equivalent). --- .travis.yml | 7 ------- Gemfile | 6 +++--- lib/sinatra/version.rb | 2 +- sinatra.gemspec | 6 +++--- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5d667d16c6..ed5c379edd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,11 +2,6 @@ language: ruby rvm: - - 1.8.7 - - 1.9.2 - - 1.9.3 - - 2.0.0 - - 2.1 - 2.2 - rbx-2 - jruby @@ -17,8 +12,6 @@ sudo: false matrix: include: - - { rvm: 1.8.7, env: tilt=master } - - { rvm: 1.9.3, env: tilt=master } - { rvm: 2.2, env: rack=master } - { rvm: 2.2, env: tilt=master } allow_failures: diff --git a/Gemfile b/Gemfile index 4df6d3c5d8..e14834b982 100644 --- a/Gemfile +++ b/Gemfile @@ -33,7 +33,7 @@ if RUBY_ENGINE == 'jruby' gem 'trinidad' end -if RUBY_ENGINE == "ruby" and RUBY_VERSION > '1.9.2' +if RUBY_ENGINE == "ruby" gem 'less', '~> 2.0' gem 'therubyracer' gem 'redcarpet' @@ -71,6 +71,6 @@ if RUBY_ENGINE == "rbx" gem 'rubysl-test-unit' end -platforms :ruby_18, :jruby do - gem 'json' unless RUBY_VERSION > '1.9' # is there a jruby but 1.8 only selector? +platforms :jruby do + gem 'json' end diff --git a/lib/sinatra/version.rb b/lib/sinatra/version.rb index 0554f20001..805144a101 100644 --- a/lib/sinatra/version.rb +++ b/lib/sinatra/version.rb @@ -1,3 +1,3 @@ module Sinatra - VERSION = '1.4.6' + VERSION = '2.0.0' end diff --git a/sinatra.gemspec b/sinatra.gemspec index 3309eebb8e..e1ff916927 100644 --- a/sinatra.gemspec +++ b/sinatra.gemspec @@ -13,7 +13,7 @@ Gem::Specification.new 'sinatra', Sinatra::VERSION do |s| s.extra_rdoc_files = s.files.select { |p| p =~ /^README/ } << 'LICENSE' s.rdoc_options = %w[--line-numbers --inline-source --title Sinatra --main README.rdoc --encoding=UTF-8] - s.add_dependency 'rack', '~> 1.5', '>= 1.5.4', '< 1.6' - s.add_dependency 'tilt', '>= 1.3', '< 3' - s.add_dependency 'rack-protection', '~> 1.4' + s.add_dependency 'rack', '~> 1.6' + s.add_dependency 'tilt', '~> 2.0' + s.add_dependency 'rack-protection', '~> 1.5' end From 103c2c9bd6dcfd995ab8cfa15dbf7e271d199fbc Mon Sep 17 00:00:00 2001 From: Trevor Bramble Date: Sun, 26 Jul 2015 23:54:51 -0700 Subject: [PATCH 02/16] Require JRuby 9k, allow > Rack 1.6 --- .travis.yml | 2 +- sinatra.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index ed5c379edd..5ead55427a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ language: ruby rvm: - 2.2 - rbx-2 - - jruby + - jruby-9.0.0.0.pre1 - jruby-head - ruby-head diff --git a/sinatra.gemspec b/sinatra.gemspec index e1ff916927..0f42cd9830 100644 --- a/sinatra.gemspec +++ b/sinatra.gemspec @@ -13,7 +13,7 @@ Gem::Specification.new 'sinatra', Sinatra::VERSION do |s| s.extra_rdoc_files = s.files.select { |p| p =~ /^README/ } << 'LICENSE' s.rdoc_options = %w[--line-numbers --inline-source --title Sinatra --main README.rdoc --encoding=UTF-8] - s.add_dependency 'rack', '~> 1.6' + s.add_dependency 'rack', '>= 1.6' s.add_dependency 'tilt', '~> 2.0' s.add_dependency 'rack-protection', '~> 1.5' end From 2c408347896ae427d92a0584ee2b8d48cbb6dc47 Mon Sep 17 00:00:00 2001 From: Trevor Bramble Date: Tue, 28 Jul 2015 19:33:33 -0700 Subject: [PATCH 03/16] jruby 9k is a real thing now --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5ead55427a..39176ac077 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,10 +3,10 @@ language: ruby rvm: - 2.2 - - rbx-2 - - jruby-9.0.0.0.pre1 - - jruby-head + - jruby-9.0.0.0 - ruby-head + - jruby-head + - rbx-2 sudo: false @@ -17,6 +17,6 @@ matrix: allow_failures: - env: rack=master - env: tilt=master - - rvm: rbx-2 - rvm: ruby-head - rvm: jruby-head + - rvm: rbx-2 From f62011c528a68e09a07f379607bd500a314977d5 Mon Sep 17 00:00:00 2001 From: Trevor Bramble Date: Sat, 5 Sep 2015 01:55:42 -0700 Subject: [PATCH 04/16] Set minimum Ruby version, update README, add "-alpha" version designation --- .travis.yml | 7 ++++--- README.md | 29 +++++------------------------ lib/sinatra/version.rb | 2 +- sinatra.gemspec | 2 ++ 4 files changed, 12 insertions(+), 28 deletions(-) diff --git a/.travis.yml b/.travis.yml index 39176ac077..9b394cc2dd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,11 +2,12 @@ language: ruby rvm: - - 2.2 - - jruby-9.0.0.0 + - 2.2.2 + - 2.2.3 - ruby-head - - jruby-head - rbx-2 + - jruby-9.0.0.0 + - jruby-head sudo: false diff --git a/README.md b/README.md index 72bdeba7db..478be15dfc 100644 --- a/README.md +++ b/README.md @@ -2871,31 +2871,10 @@ thin --threaded start The following Ruby versions are officially supported:
-
Ruby 1.8.7
+
Ruby 2.2.2+
- 1.8.7 is fully supported, however, if nothing is keeping you from it, we - recommend upgrading or switching to JRuby or Rubinius. Support for 1.8.7 - will not be dropped before Sinatra 2.0. Ruby 1.8.6 is no longer supported. -
- -
Ruby 1.9.2
-
- 1.9.2 is fully supported. Do not use 1.9.2p0, as it is known to cause - segmentation faults when running Sinatra. Official support will continue - at least until the release of Sinatra 1.5. -
- -
Ruby 1.9.3
-
- 1.9.3 is fully supported and recommended. Please note that switching to 1.9.3 - from an earlier version will invalidate all sessions. 1.9.3 will be supported - until the release of Sinatra 2.0. -
- -
Ruby 2.x
-
- 2.x is fully supported and recommended. There are currently no plans to drop - official support for it. + 2.2.2 is fully supported and recommended. There are currently no plans to + drop official support for it.
Rubinius
@@ -2912,6 +2891,8 @@ The following Ruby versions are officially supported:
+Versions of Ruby prior to 2.2.2 are no longer supported as of Sinatra 2.0. + We also keep an eye on upcoming Ruby versions. The following Ruby implementations are not officially supported but still are diff --git a/lib/sinatra/version.rb b/lib/sinatra/version.rb index 805144a101..0e48ab1194 100644 --- a/lib/sinatra/version.rb +++ b/lib/sinatra/version.rb @@ -1,3 +1,3 @@ module Sinatra - VERSION = '2.0.0' + VERSION = '2.0.0-alpha' end diff --git a/sinatra.gemspec b/sinatra.gemspec index 0f42cd9830..d2ddebf194 100644 --- a/sinatra.gemspec +++ b/sinatra.gemspec @@ -13,6 +13,8 @@ Gem::Specification.new 'sinatra', Sinatra::VERSION do |s| s.extra_rdoc_files = s.files.select { |p| p =~ /^README/ } << 'LICENSE' s.rdoc_options = %w[--line-numbers --inline-source --title Sinatra --main README.rdoc --encoding=UTF-8] + s.required_ruby_version = '>= 2.2.2' + s.add_dependency 'rack', '>= 1.6' s.add_dependency 'tilt', '~> 2.0' s.add_dependency 'rack-protection', '~> 1.5' From bebfac5131aa58eff6bb10f7a69db15d801d29ea Mon Sep 17 00:00:00 2001 From: Trevor Bramble Date: Sat, 5 Sep 2015 02:21:46 -0700 Subject: [PATCH 05/16] Remove tests for pre-1.9 Rubies --- test/routing_test.rb | 71 +++++++++++--------------------------------- 1 file changed, 17 insertions(+), 54 deletions(-) diff --git a/test/routing_test.rb b/test/routing_test.rb index 7496da3012..936de717e8 100644 --- a/test/routing_test.rb +++ b/test/routing_test.rb @@ -275,8 +275,7 @@ class RoutingTest < Minitest::Test assert_equal "foo=;bar=", body end - it "supports named captures like %r{/hello/(?[^/?#]+)} on Ruby >= 1.9" do - next if RUBY_VERSION < '1.9' + it "supports named captures like %r{/hello/(?[^/?#]+)}" do mock_app { get Regexp.new('/hello/(?[^/?#]+)') do "Hello #{params['person']}" @@ -286,8 +285,7 @@ class RoutingTest < Minitest::Test assert_equal 'Hello Frank', body end - it "supports optional named captures like %r{/page(?.[^/?#]+)?} on Ruby >= 1.9" do - next if RUBY_VERSION < '1.9' + it "supports optional named captures like %r{/page(?.[^/?#]+)?}" do mock_app { get Regexp.new('/page(?.[^/?#]+)?') do "format=#{params[:format]}" @@ -1299,59 +1297,24 @@ def authorize(username, password) assert_equal "hey", body end - # NOTE Block params behaves differently under 1.8 and 1.9. Under 1.8, block - # param arity is lax: declaring a mismatched number of block params results - # in a warning. Under 1.9, block param arity is strict: mismatched block - # arity raises an ArgumentError. - - if RUBY_VERSION >= '1.9' - - it 'raises an ArgumentError with block param arity 1 and no values' do - mock_app { - get '/foo' do |foo| - 'quux' - end - } - - assert_raises(ArgumentError) { get '/foo' } - end - - it 'raises an ArgumentError with block param arity 1 and too many values' do - mock_app { - get '/:foo/:bar/:baz' do |foo| - 'quux' - end - } - - assert_raises(ArgumentError) { get '/a/b/c' } - end - - else - - it 'does not raise an ArgumentError with block param arity 1 and no values' do - mock_app { - get '/foo' do |foo| - 'quux' - end - } - - silence_warnings { get '/foo' } - assert ok? - assert_equal 'quux', body - end + it 'raises an ArgumentError with block param arity 1 and no values' do + mock_app { + get '/foo' do |foo| + 'quux' + end + } - it 'does not raise an ArgumentError with block param arity 1 and too many values' do - mock_app { - get '/:foo/:bar/:baz' do |foo| - 'quux' - end - } + assert_raises(ArgumentError) { get '/foo' } + end - silence_warnings { get '/a/b/c' } - assert ok? - assert_equal 'quux', body - end + it 'raises an ArgumentError with block param arity 1 and too many values' do + mock_app { + get '/:foo/:bar/:baz' do |foo| + 'quux' + end + } + assert_raises(ArgumentError) { get '/a/b/c' } end it "matches routes defined in superclasses" do From 6e3ccf5a83836aff68daa70a49ba1dd4eb86a881 Mon Sep 17 00:00:00 2001 From: Trevor Bramble Date: Sat, 5 Sep 2015 13:46:17 -0700 Subject: [PATCH 06/16] Drop minimum Ruby version to 2.2.0 Put the baseline at 2.2.0 because Ruby is now using SemVer and any 2.2.x release should still be compatible. --- README.md | 8 ++++---- sinatra.gemspec | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 478be15dfc..2f1790ebc8 100644 --- a/README.md +++ b/README.md @@ -2871,9 +2871,9 @@ thin --threaded start The following Ruby versions are officially supported:
-
Ruby 2.2.2+
+
Ruby 2.2
- 2.2.2 is fully supported and recommended. There are currently no plans to + 2.2 is fully supported and recommended. There are currently no plans to drop official support for it.
@@ -2916,7 +2916,7 @@ implementation. If you run MacRuby, you should `gem install control_tower`. Sinatra currently doesn't run on Cardinal, SmallRuby, BlueRuby or any -Ruby version prior to 1.8.7. +Ruby version prior to 2.2. ## The Bleeding Edge @@ -2946,7 +2946,7 @@ Then, in your project directory, create a `Gemfile`: ```ruby source 'https://rubygems.org' -gem 'sinatra', :github => "sinatra/sinatra" +gem 'sinatra', :github => 'sinatra/sinatra' # other dependencies gem 'haml' # for instance, if you use haml diff --git a/sinatra.gemspec b/sinatra.gemspec index d2ddebf194..e5459f98c1 100644 --- a/sinatra.gemspec +++ b/sinatra.gemspec @@ -13,7 +13,7 @@ Gem::Specification.new 'sinatra', Sinatra::VERSION do |s| s.extra_rdoc_files = s.files.select { |p| p =~ /^README/ } << 'LICENSE' s.rdoc_options = %w[--line-numbers --inline-source --title Sinatra --main README.rdoc --encoding=UTF-8] - s.required_ruby_version = '>= 2.2.2' + s.required_ruby_version = '>= 2.2.0' s.add_dependency 'rack', '>= 1.6' s.add_dependency 'tilt', '~> 2.0' From 06df8bfc345279b4f6e736f2fe2c2845a4d0ca19 Mon Sep 17 00:00:00 2001 From: Kashyap Date: Sat, 12 Sep 2015 11:23:23 +0530 Subject: [PATCH 07/16] Remove 1.8 specific handling in `time_for` Ruby 1.8 didn't support `to_time` method on a Time object, and so there was custom conversion of Date, DateTime objects to a Time object. This is no more a problem. --- lib/sinatra/base.rb | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 12b9ae7930..31ae406f6d 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -592,22 +592,12 @@ def not_found? # Generates a Time object from the given value. # Used by #expires and #last_modified. def time_for(value) - if value.respond_to? :to_time - value.to_time - elsif value.is_a? Time - value - elsif value.respond_to? :new_offset - # DateTime#to_time does the same on 1.9 - d = value.new_offset 0 - t = Time.utc d.year, d.mon, d.mday, d.hour, d.min, d.sec + d.sec_fraction - t.getlocal - elsif value.respond_to? :mday - # Date#to_time does the same on 1.9 - Time.local(value.year, value.mon, value.mday) - elsif value.is_a? Numeric + if value.is_a? Numeric Time.at value - else + elsif value.respond_to? :to_s Time.parse value.to_s + else + value.to_time end rescue ArgumentError => boom raise boom From 5f1981d9c957911378414e306dc4761bbdd9aa40 Mon Sep 17 00:00:00 2001 From: Kashyap Date: Sat, 12 Sep 2015 13:02:10 +0530 Subject: [PATCH 08/16] Use `singleton_class` in lieu of class << self --- lib/sinatra/base.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 31ae406f6d..d2404574d1 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -1523,8 +1523,7 @@ def setup_traps # Dynamically defines a method on settings. def define_singleton(name, content = Proc.new) - # replace with call to singleton_class once we're 1.9 only - (class << self; self; end).class_eval do + singleton_class.class_eval do undef_method(name) if method_defined? name String === content ? class_eval("def #{name}() #{content}; end") : define_method(name, &content) end From d02125e6d50a043d063b0cd29d0a300398cf63eb Mon Sep 17 00:00:00 2001 From: Kashyap Date: Sat, 12 Sep 2015 13:07:42 +0530 Subject: [PATCH 09/16] Remove check for availability of URI::Parser Related Commit: 4a02a757fde29ba21d241c838c8114f64a393fae In the above commit, the URI parser instance was switched based on the availability of the `Parser` module because Ruby 1.9.2 and above deprecated the usage of URI::decode in favour of URI::Parser#unescape. This change removes this switch since this is not required anymore. --- lib/sinatra/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index d2404574d1..c091c5b3d7 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -867,7 +867,7 @@ class Base include Helpers include Templates - URI_INSTANCE = URI.const_defined?(:Parser) ? URI::Parser.new : URI + URI_INSTANCE = URI::Parser.new attr_accessor :app, :env, :request, :response, :params attr_reader :template_cache From 14251720e17ea43e95f4c7fb480d715fa8d6af4b Mon Sep 17 00:00:00 2001 From: Kashyap Date: Sat, 12 Sep 2015 13:18:55 +0530 Subject: [PATCH 10/16] Revert special char in heredoc fix specific to 1.9.2 This reverts commit 3c99033fcc34d6a231466e01c6f60fb4be9e8bfb. It was made to avoid a syntax error on Ruby 1.9.2. A tick in a heredoc now doesn't raise an error under Ruby 2.2.0. --- lib/sinatra/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index c091c5b3d7..26034aa4e7 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -1925,7 +1925,7 @@ class #{self.class} -

Sinatra doesn’t know this ditty.

+

Sinatra doesn’t know this ditty.

Try this: From 6aea9f804e1c01b7b9f5f4038f70c4cd2f27e6f3 Mon Sep 17 00:00:00 2001 From: Trevor Bramble Date: Sat, 12 Sep 2015 21:13:54 -0700 Subject: [PATCH 11/16] Raise Rack version requirement to 2.x --- .travis.yml | 5 ----- Gemfile | 17 ++++------------- lib/sinatra/base.rb | 2 +- lib/sinatra/show_exceptions.rb | 4 ++-- sinatra.gemspec | 2 +- test/helper.rb | 9 +++------ 6 files changed, 11 insertions(+), 28 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9b394cc2dd..b87d786e77 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,12 +12,7 @@ rvm: sudo: false matrix: - include: - - { rvm: 2.2, env: rack=master } - - { rvm: 2.2, env: tilt=master } allow_failures: - - env: rack=master - - env: tilt=master - rvm: ruby-head - rvm: jruby-head - rvm: rbx-2 diff --git a/Gemfile b/Gemfile index e14834b982..2e79ead2d6 100644 --- a/Gemfile +++ b/Gemfile @@ -11,22 +11,10 @@ source 'https://rubygems.org' unless ENV['QUICK'] gemspec gem 'rake' +gem 'rack', github: 'rack/rack' gem 'rack-test', '>= 0.6.2' gem "minitest", "~> 5.0" -# Allows stuff like `tilt=1.2.2 bundle install` or `tilt=master ...`. -# Used by the CI. -repos = {'tilt' => "rtomayko/tilt", 'rack' => "rack/rack"} - -%w[tilt rack].each do |lib| - dep = case ENV[lib] - when 'stable', nil then nil - when /(\d+\.)+\d+/ then "~> " + ENV[lib].sub("#{lib}-", '') - else {:github => repos[lib], :branch => dep} - end - gem lib, dep -end - if RUBY_ENGINE == 'jruby' gem 'nokogiri', '!= 1.5.0' gem 'jruby-openssl' @@ -42,6 +30,9 @@ if RUBY_ENGINE == "ruby" gem 'rdiscount' gem 'RedCloth' gem 'puma' + #TODO: remove explicit require once net-http-server does it + #(apparently it was shipped w/ stdlib in Rubies < 2.2.2) + gem 'gserver' gem 'net-http-server' gem 'yajl-ruby' gem 'nokogiri' diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 12b9ae7930..0e3501a7e4 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -150,7 +150,7 @@ def finish if calculate_content_length? # if some other code has already set Content-Length, don't muck with it # currently, this would be the static file-handler - headers["Content-Length"] = body.inject(0) { |l, p| l + Rack::Utils.bytesize(p) }.to_s + headers["Content-Length"] = body.inject(0) { |l, p| l + p.bytesize }.to_s end [status.to_i, headers, result] diff --git a/lib/sinatra/show_exceptions.rb b/lib/sinatra/show_exceptions.rb index 6a39c5beb8..9aca060cf8 100644 --- a/lib/sinatra/show_exceptions.rb +++ b/lib/sinatra/show_exceptions.rb @@ -1,4 +1,4 @@ -require 'rack/showexceptions' +require 'rack/show_exceptions' module Sinatra # Sinatra::ShowExceptions catches all exceptions raised from the app it @@ -40,7 +40,7 @@ def call(env) 500, { "Content-Type" => content_type, - "Content-Length" => Rack::Utils.bytesize(body.join).to_s + "Content-Length" => body.join.bytesize.to_s }, body ] diff --git a/sinatra.gemspec b/sinatra.gemspec index e5459f98c1..c6eaed7dde 100644 --- a/sinatra.gemspec +++ b/sinatra.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new 'sinatra', Sinatra::VERSION do |s| s.required_ruby_version = '>= 2.2.0' - s.add_dependency 'rack', '>= 1.6' + s.add_dependency 'rack', '~> 2.0.0.alpha' s.add_dependency 'tilt', '~> 2.0' s.add_dependency 'rack-protection', '~> 1.5' end diff --git a/test/helper.rb b/test/helper.rb index 1e1e665b77..49ce119aea 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -3,12 +3,9 @@ RUBY_ENGINE = 'ruby' unless defined? RUBY_ENGINE -begin - require 'rack' -rescue LoadError - require 'rubygems' - require 'rack' -end +require 'bundler' +require 'bundler/setup' +require 'rack' testdir = File.dirname(__FILE__) $LOAD_PATH.unshift testdir unless $LOAD_PATH.include?(testdir) From fad3619e96c1ceea98b1c394e32bf11a43e1e360 Mon Sep 17 00:00:00 2001 From: Trevor Bramble Date: Mon, 14 Sep 2015 22:15:02 -0700 Subject: [PATCH 12/16] Updated ShowExceptions notification, Rack::File usage --- lib/sinatra/base.rb | 5 ++--- test/settings_test.rb | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 0e3501a7e4..edcdfdc3f4 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -362,9 +362,8 @@ def send_file(path, opts = {}) last_modified opts[:last_modified] if opts[:last_modified] - file = Rack::File.new nil - file.path = path - result = file.serving env + file = Rack::File.new(settings.public_folder) + result = file.call(env) result[1].each { |k,v| headers[k] ||= v } headers['Content-Length'] = result[1]['Content-Length'] opts[:status] &&= Integer(opts[:status]) diff --git a/test/settings_test.rb b/test/settings_test.rb index 7ec03c681b..1b135f56d3 100644 --- a/test/settings_test.rb +++ b/test/settings_test.rb @@ -244,7 +244,7 @@ def foo=(value) get '/' assert_equal 500, status assert body.include?("StandardError") - assert body.include?("show_exceptions setting") + assert body.include?("Rack::ShowExceptions") end it 'does not override app-specified error handling when set to :after_handler' do From 855208a2936b27da68588ba547b3fb70c3e48268 Mon Sep 17 00:00:00 2001 From: Trevor Bramble Date: Wed, 14 Oct 2015 22:18:59 -0700 Subject: [PATCH 13/16] Removed intermediary `path` var in send_file tests When later referenced, `path` would have a value of the current file, not the "uploaded" test file. Also, it looked useless? --- test/helpers_test.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/test/helpers_test.rb b/test/helpers_test.rb index f56fe24ce3..38f645f822 100644 --- a/test/helpers_test.rb +++ b/test/helpers_test.rb @@ -778,10 +778,9 @@ def teardown end def send_file_app(opts={}) - path = @file mock_app { get '/file.txt' do - send_file path, opts + send_file @file, opts end } end @@ -883,11 +882,10 @@ def send_file_app(opts={}) end it "does not override Content-Type if already set and no explicit type is given" do - path = @file mock_app do get('/') do content_type :png - send_file path + send_file @file end end get '/' @@ -895,11 +893,10 @@ def send_file_app(opts={}) end it "does override Content-Type even if already set, if explicit type is given" do - path = @file mock_app do get('/') do content_type :png - send_file path, :type => :gif + send_file @file, :type => :gif end end get '/' @@ -907,10 +904,9 @@ def send_file_app(opts={}) end it 'can have :status option as a string' do - path = @file mock_app do post '/' do - send_file path, :status => '422' + send_file @file, :status => '422' end end post '/' From a4dc2ea93c61aa59155b26b90daf7b29ca382e0c Mon Sep 17 00:00:00 2001 From: Kashyap Date: Thu, 29 Oct 2015 18:11:31 +0530 Subject: [PATCH 14/16] Revert 855208a2936b27da68588ba547b3fb70c3e48268. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This causes conflicts with the way the instance variable `@file` in the test file works with the scope of `mock_app`. Without assigning the `@file` instance variable to a temporary variable—in this case `path`—we won't be able to access the instance var inside the mock_app > get context. --- test/helpers_test.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/helpers_test.rb b/test/helpers_test.rb index 38f645f822..f56fe24ce3 100644 --- a/test/helpers_test.rb +++ b/test/helpers_test.rb @@ -778,9 +778,10 @@ def teardown end def send_file_app(opts={}) + path = @file mock_app { get '/file.txt' do - send_file @file, opts + send_file path, opts end } end @@ -882,10 +883,11 @@ def send_file_app(opts={}) end it "does not override Content-Type if already set and no explicit type is given" do + path = @file mock_app do get('/') do content_type :png - send_file @file + send_file path end end get '/' @@ -893,10 +895,11 @@ def send_file_app(opts={}) end it "does override Content-Type even if already set, if explicit type is given" do + path = @file mock_app do get('/') do content_type :png - send_file @file, :type => :gif + send_file path, :type => :gif end end get '/' @@ -904,9 +907,10 @@ def send_file_app(opts={}) end it 'can have :status option as a string' do + path = @file mock_app do post '/' do - send_file @file, :status => '422' + send_file path, :status => '422' end end post '/' From 1a9642bb00d0744754015abd46a9143a0dbe57e5 Mon Sep 17 00:00:00 2001 From: Kashyap Date: Thu, 29 Oct 2015 21:24:49 +0530 Subject: [PATCH 15/16] Use Rack::File#serving method The API of `Rack::File#serving` has been changed to take two arguments: `request` and `path` of the file. `Rack::File` would then interpret the file path based on the root path supplied to it's `#new` method, and the `path` value passed into the `#serving` method. This change also sets the root path to the app root path. This reverts the earlier change to use `Rack::File#call` method because it appends the route path fragment to the root path provided to `Rack::File#new` and considers that as the file path for the static file. Also, it doesn't work with POST requests of static files, which Sinatra does. So we can't quite use it without modification of the method. --- lib/sinatra/base.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index bf93408af4..3ab0bc07d9 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -362,8 +362,9 @@ def send_file(path, opts = {}) last_modified opts[:last_modified] if opts[:last_modified] - file = Rack::File.new(settings.public_folder) - result = file.call(env) + file = Rack::File.new(File.dirname(settings.app_file)) + result = file.serving(request, path) + result[1].each { |k,v| headers[k] ||= v } headers['Content-Length'] = result[1]['Content-Length'] opts[:status] &&= Integer(opts[:status]) From 1dfae3d87e236785cade93b7b2d1adbd4b450eaa Mon Sep 17 00:00:00 2001 From: Kashyap Date: Sun, 1 Nov 2015 09:43:44 +0530 Subject: [PATCH 16/16] Rack::File#serving now returns Rack::File::Iterator Background: ========== In the `body` Sinatra helper, we DON'T delete the 'Content-Length' header when: 1. the request is a HEAD request OR 2. If the "value" is a `Rack::File` type OR 3. If the "value" is a `Stream` where "value" is the argument passed to the `body` helper method. (Refer https://github.com/sinatra/sinatra/issues/739 and https://github.com/sinatra/sinatra/pull/770) Post Rack 2.0, the `Rack::File#serving` method now returns a response array with the second argument of type `Rack::File::Iterator`. This change breaks this conditional and deletes the `Content-Length` header. This changes fixes this problem. --- lib/sinatra/base.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 3ab0bc07d9..bd76580b2e 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -239,7 +239,11 @@ def body(value = nil, &block) def block.each; yield(call) end response.body = block elsif value - headers.delete 'Content-Length' unless request.head? || value.is_a?(Rack::File) || value.is_a?(Stream) + # Rack 2.0 returns a Rack::File::Iterator here instead of + # Rack::File as it was in the previous API. + unless request.head? || value.is_a?(Rack::File::Iterator) || value.is_a?(Stream) + headers.delete 'Content-Length' + end response.body = value else response.body @@ -368,7 +372,7 @@ def send_file(path, opts = {}) result[1].each { |k,v| headers[k] ||= v } headers['Content-Length'] = result[1]['Content-Length'] opts[:status] &&= Integer(opts[:status]) - halt opts[:status] || result[0], result[2] + halt (opts[:status] || result[0]), result[2] rescue Errno::ENOENT not_found end @@ -1054,6 +1058,7 @@ def indifferent_hash # Run the block with 'throw :halt' support and apply result to the response. def invoke res = catch(:halt) { yield } + res = [res] if Fixnum === res or String === res if Array === res and Fixnum === res.first res = res.dup