sinatra 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sinatra might be problematic. Click here for more details.

data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.1.0
1
+ 2.2.0
data/lib/sinatra/base.rb CHANGED
@@ -78,6 +78,8 @@ module Sinatra
78
78
  super
79
79
  rescue Rack::Utils::ParameterTypeError, Rack::Utils::InvalidParameterError => e
80
80
  raise BadRequest, "Invalid query parameters: #{Rack::Utils.escape_html(e.message)}"
81
+ rescue EOFError => e
82
+ raise BadRequest, "Invalid multipart/form-data: #{Rack::Utils.escape_html(e.message)}"
81
83
  end
82
84
 
83
85
  class AcceptEntry
@@ -188,7 +190,7 @@ module Sinatra
188
190
  headers["Content-Length"] = body.map(&:bytesize).reduce(0, :+).to_s
189
191
  end
190
192
 
191
- [status.to_i, headers, result]
193
+ [status, headers, result]
192
194
  end
193
195
 
194
196
  private
@@ -198,11 +200,11 @@ module Sinatra
198
200
  end
199
201
 
200
202
  def drop_content_info?
201
- status.to_i / 100 == 1 or drop_body?
203
+ informational? or drop_body?
202
204
  end
203
205
 
204
206
  def drop_body?
205
- DROP_BODY_RESPONSES.include?(status.to_i)
207
+ DROP_BODY_RESPONSES.include?(status)
206
208
  end
207
209
  end
208
210
 
@@ -869,12 +871,12 @@ module Sinatra
869
871
 
870
872
  def compile_template(engine, data, options, views)
871
873
  eat_errors = options.delete :eat_errors
872
- template_cache.fetch engine, data, options, views do
873
- template = Tilt[engine]
874
- raise "Template engine not found: #{engine}" if template.nil?
874
+ template = Tilt[engine]
875
+ raise "Template engine not found: #{engine}" if template.nil?
875
876
 
876
- case data
877
- when Symbol
877
+ case data
878
+ when Symbol
879
+ template_cache.fetch engine, data, options, views do
878
880
  body, path, line = settings.templates[data]
879
881
  if body
880
882
  body = body.call if body.respond_to?(:call)
@@ -892,17 +894,24 @@ module Sinatra
892
894
  throw :layout_missing if eat_errors and not found
893
895
  template.new(path, 1, options)
894
896
  end
895
- when Proc, String
896
- body = data.is_a?(String) ? Proc.new { data } : data
897
- caller = settings.caller_locations.first
898
- path = options[:path] || caller[0]
899
- line = options[:line] || caller[1]
900
- template.new(path, line.to_i, options, &body)
901
- else
902
- raise ArgumentError, "Sorry, don't know how to render #{data.inspect}."
903
897
  end
898
+ when Proc
899
+ compile_block_template(template, options, &data)
900
+ when String
901
+ template_cache.fetch engine, data, options, views do
902
+ compile_block_template(template, options) { data }
903
+ end
904
+ else
905
+ raise ArgumentError, "Sorry, don't know how to render #{data.inspect}."
904
906
  end
905
907
  end
908
+
909
+ def compile_block_template(template, options, &body)
910
+ caller = settings.caller_locations.first
911
+ path = options[:path] || caller[0]
912
+ line = options[:line] || caller[1]
913
+ template.new(path, line.to_i, options, &body)
914
+ end
906
915
  end
907
916
 
908
917
  # Base class for all Sinatra applications and middleware.
@@ -916,7 +925,7 @@ module Sinatra
916
925
  attr_accessor :app, :env, :request, :response, :params
917
926
  attr_reader :template_cache
918
927
 
919
- def initialize(app = nil)
928
+ def initialize(app = nil, **kwargs)
920
929
  super()
921
930
  @app = app
922
931
  @template_cache = Tilt::Cache.new
@@ -934,6 +943,7 @@ module Sinatra
934
943
  @params = IndifferentHash.new
935
944
  @request = Request.new(env)
936
945
  @response = Response.new
946
+ @pinned_response = nil
937
947
  template_cache.clear if settings.reload_templates
938
948
 
939
949
  invoke { dispatch! }
@@ -994,11 +1004,11 @@ module Sinatra
994
1004
 
995
1005
  # Run filters defined on the class and all superclasses.
996
1006
  # Accepts an optional block to call after each filter is applied.
997
- def filter!(type, base = settings)
998
- filter! type, base.superclass if base.superclass.respond_to?(:filters)
1007
+ def filter!(type, base = settings, &block)
1008
+ filter!(type, base.superclass, &block) if base.superclass.respond_to?(:filters)
999
1009
  base.filters[type].each do |args|
1000
1010
  result = process_route(*args)
1001
- yield result if block_given?
1011
+ block.call(result) if block_given?
1002
1012
  end
1003
1013
  end
1004
1014
 
@@ -1006,7 +1016,7 @@ module Sinatra
1006
1016
  def route!(base = settings, pass_block = nil)
1007
1017
  if routes = base.routes[@request.request_method]
1008
1018
  routes.each do |pattern, conditions, block|
1009
- @response.delete_header('Content-Type') unless @pinned_response
1019
+ response.delete_header('Content-Type') unless @pinned_response
1010
1020
 
1011
1021
  returned_pass_block = process_route(pattern, conditions) do |*args|
1012
1022
  env['sinatra.route'] = "#{@request.request_method} #{pattern}"
@@ -1089,6 +1099,7 @@ module Sinatra
1089
1099
  return unless valid_path?(path)
1090
1100
 
1091
1101
  path = File.expand_path(path)
1102
+ return unless path.start_with?(File.expand_path(public_dir) + '/')
1092
1103
  return unless File.file?(path)
1093
1104
 
1094
1105
  env['sinatra.static_file'] = path
@@ -1124,7 +1135,7 @@ module Sinatra
1124
1135
  invoke do
1125
1136
  static! if settings.static? && (request.get? || request.head?)
1126
1137
  filter! :before do
1127
- @pinned_response = !@response['Content-Type'].nil?
1138
+ @pinned_response = !response['Content-Type'].nil?
1128
1139
  end
1129
1140
  route!
1130
1141
  end
@@ -1145,7 +1156,7 @@ module Sinatra
1145
1156
  end
1146
1157
  @env['sinatra.error'] = boom
1147
1158
 
1148
- if boom.respond_to? :http_status
1159
+ if boom.respond_to? :http_status and boom.http_status.between? 400, 599
1149
1160
  status(boom.http_status)
1150
1161
  elsif settings.use_code? and boom.respond_to? :code and boom.code.between? 400, 599
1151
1162
  status(boom.code)
@@ -1153,8 +1164,6 @@ module Sinatra
1153
1164
  status(500)
1154
1165
  end
1155
1166
 
1156
- status(500) unless status.between? 400, 599
1157
-
1158
1167
  if server_error?
1159
1168
  dump_errors! boom if settings.dump_errors?
1160
1169
  raise boom if settings.show_exceptions? and settings.show_exceptions != :after_handler
@@ -1168,7 +1177,7 @@ module Sinatra
1168
1177
 
1169
1178
  if not_found? || bad_request?
1170
1179
  if boom.message && boom.message != boom.class.name
1171
- body boom.message
1180
+ body Rack::Utils.escape_html(boom.message)
1172
1181
  else
1173
1182
  content_type 'text/html'
1174
1183
  body '<h1>' + (not_found? ? 'Not Found' : 'Bad Request') + '</h1>'
@@ -1442,7 +1451,7 @@ module Sinatra
1442
1451
  # in `extensions` available to the handlers and templates
1443
1452
  def helpers(*extensions, &block)
1444
1453
  class_eval(&block) if block_given?
1445
- prepend(*extensions) if extensions.any?
1454
+ include(*extensions) if extensions.any?
1446
1455
  end
1447
1456
 
1448
1457
  # Register an extension. Alternatively take a block from which an
@@ -1471,6 +1480,7 @@ module Sinatra
1471
1480
  @prototype = nil
1472
1481
  @middleware << [middleware, args, block]
1473
1482
  end
1483
+ ruby2_keywords(:use) if respond_to?(:ruby2_keywords, true)
1474
1484
 
1475
1485
  # Stop the self-hosted server if running.
1476
1486
  def quit!
@@ -1490,7 +1500,7 @@ module Sinatra
1490
1500
  def run!(options = {}, &block)
1491
1501
  return if running?
1492
1502
  set options
1493
- handler = detect_rack_handler
1503
+ handler = Rack::Handler.pick(server)
1494
1504
  handler_name = handler.name.gsub(/.*::/, '')
1495
1505
  server_settings = settings.respond_to?(:server_settings) ? settings.server_settings : {}
1496
1506
  server_settings.merge!(:Port => port, :Host => bind)
@@ -1523,8 +1533,8 @@ module Sinatra
1523
1533
  # Create a new instance of the class fronted by its middleware
1524
1534
  # pipeline. The object is guaranteed to respond to #call but may not be
1525
1535
  # an instance of the class new was called on.
1526
- def new(*args, &bk)
1527
- instance = new!(*args, &bk)
1536
+ def new(*args, **kwargs, &bk)
1537
+ instance = new!(*args, **kwargs, &bk)
1528
1538
  Wrapper.new(build(instance).to_app, instance)
1529
1539
  end
1530
1540
 
@@ -1744,17 +1754,6 @@ module Sinatra
1744
1754
  builder.use session_store, options
1745
1755
  end
1746
1756
 
1747
- def detect_rack_handler
1748
- servers = Array(server)
1749
- servers.each do |server_name|
1750
- begin
1751
- return Rack::Handler.get(server_name.to_s)
1752
- rescue LoadError, NameError
1753
- end
1754
- end
1755
- fail "Server handler (#{servers.join(',')}) not found."
1756
- end
1757
-
1758
1757
  def inherited(subclass)
1759
1758
  subclass.reset!
1760
1759
  subclass.set :app_file, caller_files.first unless subclass.app_file?
@@ -1958,6 +1957,8 @@ module Sinatra
1958
1957
  return super(*args, &block) if respond_to? method_name
1959
1958
  Delegator.target.send(method_name, *args, &block)
1960
1959
  end
1960
+ # ensure keyword argument passing is compatible with ruby >= 2.7
1961
+ ruby2_keywords(method_name) if respond_to?(:ruby2_keywords, true)
1961
1962
  private method_name
1962
1963
  end
1963
1964
  end
@@ -180,6 +180,20 @@ module Sinatra
180
180
  end
181
181
  end
182
182
 
183
+ def select(*args, &block)
184
+ return to_enum(:select) unless block_given?
185
+ dup.tap { |hash| hash.select!(*args, &block) }
186
+ end
187
+
188
+ def reject(*args, &block)
189
+ return to_enum(:reject) unless block_given?
190
+ dup.tap { |hash| hash.reject!(*args, &block) }
191
+ end
192
+
193
+ def compact
194
+ dup.tap(&:compact!)
195
+ end if method_defined?(:compact) # Added in Ruby 2.4
196
+
183
197
  private
184
198
 
185
199
  def convert_key(key)
@@ -1,3 +1,3 @@
1
1
  module Sinatra
2
- VERSION = '2.1.0'
2
+ VERSION = '2.2.0'
3
3
  end
data/sinatra.gemspec CHANGED
@@ -19,9 +19,8 @@ Gem::Specification.new 'sinatra', version do |s|
19
19
  "SECURITY.md",
20
20
  "sinatra.gemspec",
21
21
  "VERSION"]
22
- s.test_files = s.files.select { |p| p =~ /^test\/.*_test.rb/ }
23
- s.extra_rdoc_files = s.files.select { |p| p =~ /^README/ } << 'LICENSE'
24
- s.rdoc_options = %w[--line-numbers --inline-source --title Sinatra --main README.rdoc --encoding=UTF-8]
22
+ s.extra_rdoc_files = %w[README.md LICENSE]
23
+ s.rdoc_options = %w[--line-numbers --title Sinatra --main README.rdoc --encoding=UTF-8]
25
24
 
26
25
  if s.respond_to?(:metadata)
27
26
  s.metadata = {
@@ -33,11 +32,6 @@ Gem::Specification.new 'sinatra', version do |s|
33
32
  'documentation_uri' => 'https://www.rubydoc.info/gems/sinatra'
34
33
  }
35
34
  else
36
- msg = "RubyGems 2.0 or newer is required to protect against public "\
37
- "gem pushes. You can update your rubygems version by running:\n\n"\
38
- "gem install rubygems-update\n"\
39
- "update_rubygems\n"\
40
- "gem update --system"
41
35
  raise <<-EOF
42
36
  RubyGems 2.0 or newer is required to protect against public gem pushes. You can update your rubygems version by running:
43
37
  gem install rubygems-update
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Mizerany
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2020-09-04 00:00:00.000000000 Z
14
+ date: 2022-02-15 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rack
@@ -47,14 +47,14 @@ dependencies:
47
47
  requirements:
48
48
  - - '='
49
49
  - !ruby/object:Gem::Version
50
- version: 2.1.0
50
+ version: 2.2.0
51
51
  type: :runtime
52
52
  prerelease: false
53
53
  version_requirements: !ruby/object:Gem::Requirement
54
54
  requirements:
55
55
  - - '='
56
56
  - !ruby/object:Gem::Version
57
- version: 2.1.0
57
+ version: 2.2.0
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: mustermann
60
60
  requirement: !ruby/object:Gem::Requirement
@@ -75,18 +75,7 @@ email: sinatrarb@googlegroups.com
75
75
  executables: []
76
76
  extensions: []
77
77
  extra_rdoc_files:
78
- - README.de.md
79
- - README.es.md
80
- - README.fr.md
81
- - README.hu.md
82
- - README.ja.md
83
- - README.ko.md
84
- - README.malayalam.md
85
78
  - README.md
86
- - README.pt-br.md
87
- - README.pt-pt.md
88
- - README.ru.md
89
- - README.zh.md
90
79
  - LICENSE
91
80
  files:
92
81
  - ".yardopts"
@@ -138,7 +127,6 @@ metadata:
138
127
  post_install_message:
139
128
  rdoc_options:
140
129
  - "--line-numbers"
141
- - "--inline-source"
142
130
  - "--title"
143
131
  - Sinatra
144
132
  - "--main"