roda 3.108.0 → 3.110.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/lib/roda/cache.rb +0 -2
  3. data/lib/roda/opts.rb +22 -0
  4. data/lib/roda/plugins/Integer_matcher_max.rb +1 -1
  5. data/lib/roda/plugins/_add_script_name.rb +14 -0
  6. data/lib/roda/plugins/_json.rb +22 -0
  7. data/lib/roda/plugins/_symbol_class_matchers.rb +8 -10
  8. data/lib/roda/plugins/additional_render_engines.rb +5 -1
  9. data/lib/roda/plugins/additional_view_directories.rb +7 -3
  10. data/lib/roda/plugins/assets.rb +179 -151
  11. data/lib/roda/plugins/assets_preloading.rb +26 -10
  12. data/lib/roda/plugins/autoload_hash_branches.rb +9 -3
  13. data/lib/roda/plugins/autoload_named_routes.rb +9 -3
  14. data/lib/roda/plugins/backtracking_array.rb +2 -2
  15. data/lib/roda/plugins/branch_locals.rb +4 -4
  16. data/lib/roda/plugins/break.rb +2 -2
  17. data/lib/roda/plugins/caching.rb +27 -17
  18. data/lib/roda/plugins/capture_erb.rb +19 -12
  19. data/lib/roda/plugins/chunked.rb +42 -34
  20. data/lib/roda/plugins/class_level_routing.rb +10 -5
  21. data/lib/roda/plugins/class_matchers.rb +14 -7
  22. data/lib/roda/plugins/common_logger.rb +27 -5
  23. data/lib/roda/plugins/conditional_sessions.rb +4 -4
  24. data/lib/roda/plugins/content_for.rb +12 -7
  25. data/lib/roda/plugins/content_security_policy.rb +17 -14
  26. data/lib/roda/plugins/cookie_flags.rb +17 -7
  27. data/lib/roda/plugins/cookies.rb +12 -6
  28. data/lib/roda/plugins/csrf.rb +2 -1
  29. data/lib/roda/plugins/custom_block_results.rb +11 -10
  30. data/lib/roda/plugins/custom_matchers.rb +4 -2
  31. data/lib/roda/plugins/default_headers.rb +4 -7
  32. data/lib/roda/plugins/default_status.rb +1 -0
  33. data/lib/roda/plugins/delay_build.rb +1 -1
  34. data/lib/roda/plugins/delegate.rb +7 -6
  35. data/lib/roda/plugins/direct_call.rb +5 -1
  36. data/lib/roda/plugins/disallow_file_uploads.rb +2 -3
  37. data/lib/roda/plugins/drop_body.rb +3 -6
  38. data/lib/roda/plugins/each_part.rb +23 -29
  39. data/lib/roda/plugins/early_hints.rb +4 -4
  40. data/lib/roda/plugins/empty_root.rb +5 -5
  41. data/lib/roda/plugins/environments.rb +11 -15
  42. data/lib/roda/plugins/erb_h.rb +4 -4
  43. data/lib/roda/plugins/error_email.rb +23 -20
  44. data/lib/roda/plugins/error_handler.rb +28 -34
  45. data/lib/roda/plugins/error_mail.rb +31 -16
  46. data/lib/roda/plugins/exception_page.rb +41 -35
  47. data/lib/roda/plugins/filter_common_logger.rb +6 -4
  48. data/lib/roda/plugins/flash.rb +16 -13
  49. data/lib/roda/plugins/freeze_request_response.rb +24 -0
  50. data/lib/roda/plugins/hash_branch_view_subdir.rb +6 -4
  51. data/lib/roda/plugins/hash_branches.rb +7 -5
  52. data/lib/roda/plugins/hash_paths.rb +7 -5
  53. data/lib/roda/plugins/hash_public.rb +11 -7
  54. data/lib/roda/plugins/hash_public_cache.rb +9 -7
  55. data/lib/roda/plugins/hash_routes.rb +6 -4
  56. data/lib/roda/plugins/heartbeat.rb +6 -2
  57. data/lib/roda/plugins/hmac_paths.rb +9 -3
  58. data/lib/roda/plugins/hooks.rb +13 -10
  59. data/lib/roda/plugins/host_authorization.rb +7 -2
  60. data/lib/roda/plugins/host_routing.rb +7 -2
  61. data/lib/roda/plugins/invalid_request_body.rb +20 -4
  62. data/lib/roda/plugins/ip_from_header.rb +5 -1
  63. data/lib/roda/plugins/json.rb +10 -8
  64. data/lib/roda/plugins/json_parser.rb +26 -12
  65. data/lib/roda/plugins/mail_processor.rb +9 -6
  66. data/lib/roda/plugins/mailer.rb +5 -3
  67. data/lib/roda/plugins/match_affix.rb +6 -1
  68. data/lib/roda/plugins/match_hook_args.rb +7 -5
  69. data/lib/roda/plugins/middleware.rb +13 -7
  70. data/lib/roda/plugins/module_include.rb +1 -1
  71. data/lib/roda/plugins/multi_public.rb +6 -2
  72. data/lib/roda/plugins/multi_route.rb +1 -1
  73. data/lib/roda/plugins/multi_run.rb +13 -17
  74. data/lib/roda/plugins/named_routes.rb +8 -6
  75. data/lib/roda/plugins/named_templates.rb +5 -3
  76. data/lib/roda/plugins/path.rb +16 -14
  77. data/lib/roda/plugins/path_rewriter.rb +8 -5
  78. data/lib/roda/plugins/permissions_policy.rb +21 -19
  79. data/lib/roda/plugins/public.rb +22 -10
  80. data/lib/roda/plugins/recheck_precompiled_assets.rb +30 -8
  81. data/lib/roda/plugins/redirect_http_to_https.rb +6 -3
  82. data/lib/roda/plugins/render.rb +37 -49
  83. data/lib/roda/plugins/render_coverage.rb +6 -3
  84. data/lib/roda/plugins/render_locals.rb +13 -7
  85. data/lib/roda/plugins/request_aref.rb +1 -1
  86. data/lib/roda/plugins/response_content_type.rb +7 -3
  87. data/lib/roda/plugins/route_csrf.rb +7 -2
  88. data/lib/roda/plugins/run_append_slash.rb +5 -1
  89. data/lib/roda/plugins/sec_fetch_site_csrf.rb +6 -2
  90. data/lib/roda/plugins/sessions.rb +164 -139
  91. data/lib/roda/plugins/shape_friendly.rb +3 -1
  92. data/lib/roda/plugins/sinatra_helpers.rb +7 -3
  93. data/lib/roda/plugins/status_handler.rb +5 -3
  94. data/lib/roda/plugins/strip_path_prefix.rb +4 -2
  95. data/lib/roda/plugins/symbol_matchers.rb +11 -6
  96. data/lib/roda/plugins/symbol_views.rb +1 -1
  97. data/lib/roda/plugins/timestamp_public.rb +8 -4
  98. data/lib/roda/plugins/type_routing.rb +9 -5
  99. data/lib/roda/plugins/typecast_params.rb +2 -2
  100. data/lib/roda/plugins/typecast_params_sized_integers.rb +1 -1
  101. data/lib/roda/plugins/unescape_path.rb +22 -4
  102. data/lib/roda/plugins/url_escape.rb +2 -14
  103. data/lib/roda/plugins.rb +34 -0
  104. data/lib/roda/request.rb +8 -7
  105. data/lib/roda/response.rb +9 -7
  106. data/lib/roda/version.rb +1 -1
  107. data/lib/roda.rb +37 -23
  108. metadata +7 -3
@@ -19,20 +19,31 @@ class Roda
19
19
  # plugin :common_logger, $stdout
20
20
  # plugin :common_logger, Logger.new('filename')
21
21
  # plugin :common_logger, Logger.new('filename'), method: :debug
22
+ # plugin :common_logger, use_request_ip: true
23
+ #
24
+ # Keyword Arguments:
25
+ # +method+ :: Specifies the method to call on the logger object when logging.
26
+ # +use_request_ip+ :: Use +request.ip+ to get the IP address, instead of the default
27
+ # of checking +HTTP_X_FORWARDED_FOR+ and then +REMOTE_ADDR+. This
28
+ # will become the default behavior starting in Roda 4.
22
29
  module CommonLogger
23
30
  SCOPE_INSTANCE_VARIABLES = [:@_request_timer].freeze
24
31
 
25
32
  MUTATE_LINE = RUBY_VERSION < '2.3' || RUBY_VERSION >= '3'
26
33
  private_constant :MUTATE_LINE
27
34
 
28
- def self.configure(app, logger=nil, opts=OPTS)
29
- app.opts[:common_logger] = logger || app.opts[:common_logger] || $stderr
30
- app.opts[:common_logger_meth] = app.opts[:common_logger].method(opts.fetch(:method){logger.respond_to?(:write) ? :write : :<<})
35
+ def self.configure(app, logger=nil, opts=(no_positional_opts = true; OPTS), method: opts[:method], use_request_ip: false, **kw)
36
+ RodaPlugins.check_keywords("the common_logger plugin", no_positional_opts, opts, kw)
37
+
38
+ common_logger = app.opts[:common_logger] = logger || app.opts[:common_logger] || $stderr # RODA4: drop setting of option, no longer used
39
+ app.opts[:common_logger_meth] = common_logger.method(method || (common_logger.respond_to?(:write) ? :write : :<<))
40
+ app.opts[:common_logger_use_request_ip] = use_request_ip # RODA4: Deefault to true
31
41
  end
32
42
 
33
43
  if RUBY_VERSION >= '2.1'
34
44
  # A timer object for calculating elapsed time.
35
45
  def self.start_timer
46
+ # RODA4.1: Remove
36
47
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
37
48
  end
38
49
  else
@@ -43,6 +54,11 @@ class Roda
43
54
  # simplecov:enable
44
55
  end
45
56
 
57
+ module ClassMethods
58
+ RodaPlugins.opt_attr_reader(self, :common_logger_meth)
59
+ RodaPlugins.opt_attr_reader(self, :common_logger_use_request_ip, name: :common_logger_use_request_ip?)
60
+ end
61
+
46
62
  module InstanceMethods
47
63
  private
48
64
 
@@ -58,7 +74,13 @@ class Roda
58
74
 
59
75
  env = @_request.env
60
76
 
61
- line = "#{env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-"} - #{env["REMOTE_USER"] || "-"} [#{Time.now.strftime("%d/%b/%Y:%H:%M:%S %z")}] \"#{env["REQUEST_METHOD"]} #{env["SCRIPT_NAME"]}#{env["PATH_INFO"]}#{"?#{env["QUERY_STRING"]}" if ((qs = env["QUERY_STRING"]) && !qs.empty?)} #{@_request.http_version}\" #{status} #{((length = headers[RodaResponseHeaders::CONTENT_LENGTH]) && (length unless length == '0')) || '-'} #{elapsed_time} "
77
+ ip = if self.class.common_logger_use_request_ip?
78
+ @_request.ip
79
+ else
80
+ env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"]
81
+ end
82
+
83
+ line = "#{ip || "-"} - #{env["REMOTE_USER"] || "-"} [#{Time.now.strftime("%d/%b/%Y:%H:%M:%S %z")}] \"#{env["REQUEST_METHOD"]} #{env["SCRIPT_NAME"]}#{env["PATH_INFO"]}#{"?#{env["QUERY_STRING"]}" if ((qs = env["QUERY_STRING"]) && !qs.empty?)} #{@_request.http_version}\" #{status} #{((length = headers[RodaResponseHeaders::CONTENT_LENGTH]) && (length unless length == '0')) || '-'} #{elapsed_time} "
62
84
  if MUTATE_LINE
63
85
  line.gsub!(/[^[:print:]]/){|c| sprintf("\\x%x", c.ord)}
64
86
  # simplecov:disable
@@ -67,7 +89,7 @@ class Roda
67
89
  # simplecov:enable
68
90
  end
69
91
  line[-1] = "\n"
70
- opts[:common_logger_meth].call(line)
92
+ self.class.common_logger_meth.call(line)
71
93
  end
72
94
 
73
95
  # Create timer instance used for timing
@@ -8,22 +8,22 @@ class Roda
8
8
  # use in applications that want to use sessions for some requests,
9
9
  # and want to be sure that sessions are not used for other requests.
10
10
  # For example, if you want to make sure that sessions are not used for
11
- # requests with paths starting with /static, you could do:
11
+ # requests with paths starting with +/static+, you could do:
12
12
  #
13
13
  # plugin :conditional_sessions, secret: ENV["SECRET"] do
14
14
  # !path_info.start_with?('/static')
15
15
  # end
16
16
  #
17
- # The the request session, session_created_at, and session_updated_at methods
17
+ # Then the request +session+, +session_created_at+, and +session_updated_at+ methods
18
18
  # raise a RodaError exception when sessions are not allowed. The request
19
- # persist_session and route scope clear_session methods do nothing when
19
+ # +persist_session+ and route scope +clear_session+ methods do nothing when
20
20
  # sessions are not allowed.
21
21
  module ConditionalSessions
22
22
  # Pass all options to the sessions block, and use the block to define
23
23
  # a request method for whether sessions are allowed.
24
24
  def self.load_dependencies(app, opts=OPTS, &block)
25
25
  app.plugin :sessions, opts
26
- app::RodaRequest.class_eval do
26
+ app::RodaRequest.class_exec do
27
27
  define_method(:use_sessions?, &block)
28
28
  alias use_sessions? use_sessions?
29
29
  end
@@ -11,7 +11,7 @@ class Roda
11
11
  # of the normal content pane.
12
12
  #
13
13
  # In the template in which you want to store content, call
14
- # content_for with a block:
14
+ # +content_for+ with a block:
15
15
  #
16
16
  # <% content_for :foo do %>
17
17
  # Some content here.
@@ -27,11 +27,11 @@ class Roda
27
27
  # <% content_for :foo, "Some content" %>
28
28
  #
29
29
  # In the template in which you want to retrieve content,
30
- # call content_for without the block or argument:
30
+ # call +content_for+ without the block or argument:
31
31
  #
32
32
  # <%= content_for :foo %>
33
33
  #
34
- # Note that when storing content by calling content_for
34
+ # Note that when storing content by calling +content_for+
35
35
  # with a block and embedding template code, the return
36
36
  # value of the block is used as the content (after being
37
37
  # converted to a string). This can cause issues in some
@@ -51,7 +51,7 @@ class Roda
51
51
  # If content_for is used multiple times with the same key,
52
52
  # by default, the last call will append previous calls.
53
53
  # If you want to overwrite the previous content, pass the
54
- # <tt>append: false</tt> option when loading the plugin:
54
+ # <tt>append: false</tt> argument when loading the plugin:
55
55
  #
56
56
  # plugin :content_for, append: false
57
57
  module ContentFor
@@ -65,8 +65,13 @@ class Roda
65
65
 
66
66
  # Configure whether to append or overwrite if content_for
67
67
  # is called multiple times with the same key.
68
- def self.configure(app, opts = OPTS)
69
- app.opts[:append_content_for] = opts.fetch(:append, true)
68
+ def self.configure(app, opts=(no_positional_opts = true; OPTS), append: opts.fetch(:append, true), **kw)
69
+ RodaPlugins.check_keywords("the content_for plugin", no_positional_opts, opts, kw)
70
+ app.opts[:append_content_for] = append
71
+ end
72
+
73
+ module ClassMethods
74
+ RodaPlugins.opt_attr_reader(self, :append_content_for, name: :append_content_for?)
70
75
  end
71
76
 
72
77
  module InstanceMethods
@@ -75,7 +80,7 @@ class Roda
75
80
  # stored content with the given key, or return nil if there
76
81
  # is no content stored with that key.
77
82
  def content_for(key, value=nil, &block)
78
- append = opts[:append_content_for]
83
+ append = self.class.append_content_for?
79
84
 
80
85
  if block || value
81
86
  if block
@@ -41,7 +41,7 @@ class Roda
41
41
  # end
42
42
  #
43
43
  # The following methods are available for configuring the content security policy,
44
- # which specify the setting (substituting _ with -):
44
+ # which specify the setting (substituting +_+ with +-+):
45
45
  #
46
46
  # * base_uri
47
47
  # * child_src
@@ -92,7 +92,7 @@ class Roda
92
92
  # content_security_policy.get_script_src
93
93
  # # => [:self, :unsafe_eval, 'example.com', [:nonce, 'foobarbaz']]
94
94
  #
95
- # The clear method can be used to remove all settings from the policy. Empty policies
95
+ # The +clear+ method can be used to remove all settings from the policy. Empty policies
96
96
  # do not set any headers. You can use +response.skip_content_security_policy!+ to skip
97
97
  # setting a policy. This is faster than calling +content_security_policy.clear+, since
98
98
  # it does not duplicate the default policy.
@@ -115,7 +115,7 @@ class Roda
115
115
 
116
116
  # Represents a content security policy.
117
117
  class Policy
118
- '
118
+ %w'
119
119
  base-uri
120
120
  child-src
121
121
  connect-src
@@ -136,7 +136,7 @@ class Roda
136
136
  script-src
137
137
  style-src
138
138
  worker-src
139
- '.split.each(&:freeze).each do |setting|
139
+ '.each(&:freeze).each do |setting|
140
140
  meth = setting.tr('-', '_').freeze
141
141
 
142
142
  # Setting method name sets the setting value, or removes it if no args are given.
@@ -153,9 +153,12 @@ class Roda
153
153
  # are given.
154
154
  define_method("add_#{meth}") do |*args|
155
155
  unless args.empty?
156
- @opts[setting] ||= EMPTY_ARRAY
157
- @opts[setting] += args
158
- @opts[setting].freeze
156
+ if @opts[setting]
157
+ @opts[setting] += args
158
+ @opts[setting].freeze
159
+ else
160
+ @opts[setting] = args.freeze
161
+ end
159
162
  end
160
163
  nil
161
164
  end
@@ -187,16 +190,13 @@ class Roda
187
190
  end
188
191
  end
189
192
 
190
- def initialize
191
- clear
192
- end
193
-
194
193
  # Clear all settings, useful to remove any inherited settings.
195
194
  def clear
196
195
  @opts = {}
197
196
  @report_only = nil
198
197
  @header_value = nil
199
198
  end
199
+ alias initialize clear
200
200
 
201
201
  # Do not allow future modifications to any settings.
202
202
  def freeze
@@ -276,7 +276,6 @@ class Roda
276
276
  end
277
277
  end
278
278
 
279
-
280
279
  # Yield the current Content Security Policy to the block.
281
280
  def self.configure(app)
282
281
  policy = app.opts[:content_security_policy] = if policy = app.opts[:content_security_policy]
@@ -289,6 +288,10 @@ class Roda
289
288
  policy.freeze
290
289
  end
291
290
 
291
+ module ClassMethods
292
+ RodaPlugins.opt_attr_reader(self, :content_security_policy)
293
+ end
294
+
292
295
  module InstanceMethods
293
296
  # If a block is given, yield the current content security policy. Returns the
294
297
  # current content security policy.
@@ -308,7 +311,7 @@ class Roda
308
311
 
309
312
  # The current content security policy to be used for this response.
310
313
  def content_security_policy
311
- @content_security_policy ||= roda_class.opts[:content_security_policy].dup
314
+ @content_security_policy ||= roda_class.content_security_policy.dup
312
315
  end
313
316
 
314
317
  # Do not set a content security policy header for this response.
@@ -322,7 +325,7 @@ class Roda
322
325
  def set_default_headers
323
326
  super
324
327
  unless @skip_content_security_policy
325
- (@content_security_policy || roda_class.opts[:content_security_policy]).set_header(headers)
328
+ (@content_security_policy || roda_class.content_security_policy).set_header(headers)
326
329
  end
327
330
  end
328
331
  end
@@ -60,8 +60,12 @@ class Roda
60
60
  class Error < RodaError
61
61
  end
62
62
 
63
+ Config = Struct.new(*DEFAULTS.keys, :same_site_string)
64
+ private_constant :Config
65
+
63
66
  def self.configure(app, opts=OPTS)
64
- previous = app.opts[:cookie_flags] || DEFAULTS
67
+ RodaPlugins.check_supported_opts("the cookie_flags plugin", opts, DEFAULTS.keys)
68
+ previous = app.cookie_flags || DEFAULTS
65
69
  opts = app.opts[:cookie_flags] = previous.merge(opts)
66
70
 
67
71
  case opts[:same_site]
@@ -72,20 +76,26 @@ class Roda
72
76
  end
73
77
 
74
78
  opts.freeze
79
+ app.opts[:cookie_flags_config] = Config.new(*opts.values_at(*Config.members)).freeze
80
+ end
81
+
82
+ module ClassMethods
83
+ RodaPlugins.opt_attr_reader(self, :cookie_flags)
84
+ RodaPlugins.opt_attr_reader(self, :cookie_flags_config)
75
85
  end
76
86
 
77
87
  module InstanceMethods
78
88
  private
79
89
 
80
90
  def _handle_cookie_flags_array(cookies)
81
- opts = self.class.opts[:cookie_flags]
82
- needs_secure = opts[:secure]
83
- needs_httponly = opts[:httponly]
84
- if needs_same_site = opts[:same_site]
85
- same_site_string = opts[:same_site_string]
91
+ opts = self.class.cookie_flags_config
92
+ needs_secure = opts.secure
93
+ needs_httponly = opts.httponly
94
+ if needs_same_site = opts.same_site
95
+ same_site_string = opts.same_site_string
86
96
  same_site_regexp = /;\s*samesite\s*=\s*(\S+)\s*(?:\z|;)/i
87
97
  end
88
- action = opts[:action]
98
+ action = opts.action
89
99
 
90
100
  cookies.map do |cookie|
91
101
  if needs_secure
@@ -19,20 +19,26 @@ class Roda
19
19
  # plugin :cookies, domain: 'example.com', path: '/api'
20
20
  module Cookies
21
21
  # Allow setting default cookie options when loading the cookies plugin.
22
- def self.configure(app, opts={})
23
- app.opts[:cookies_opts] = (app.opts[:cookies_opts]||{}).merge(opts).freeze
22
+ def self.configure(app, opts=OPTS)
23
+ app.opts[:cookies_opts] = (app.cookies_opts||OPTS).merge(opts).freeze
24
+ end
25
+
26
+ module ClassMethods
27
+ RodaPlugins.opt_attr_reader(self, :cookies_opts)
24
28
  end
25
29
 
26
30
  module ResponseMethods
27
- # Modify the headers to include a Set-Cookie value that
31
+ # Modify the headers to include a <tt>Set-Cookie</tt> value that
28
32
  # deletes the cookie. A value hash can be provided to
29
33
  # override the default one used to delete the cookie.
30
34
  # Example:
31
35
  #
32
36
  # response.delete_cookie('foo')
33
37
  # response.delete_cookie('foo', domain: 'example.org')
34
- def delete_cookie(key, value = {})
35
- ::Rack::Utils.delete_cookie_header!(@headers, key, roda_class.opts[:cookies_opts].merge(value))
38
+ def delete_cookie(key, value = OPTS)
39
+ opts = roda_class.cookies_opts
40
+ value = value.empty? ? opts : opts.merge(value)
41
+ ::Rack::Utils.delete_cookie_header!(@headers, key, value)
36
42
  end
37
43
 
38
44
  # Set the cookie with the given key in the headers.
@@ -41,7 +47,7 @@ class Roda
41
47
  # response.set_cookie('foo', value: 'bar', domain: 'example.org')
42
48
  def set_cookie(key, value)
43
49
  value = { :value=>value } unless value.respond_to?(:keys)
44
- ::Rack::Utils.set_cookie_header!(@headers, key, roda_class.opts[:cookies_opts].merge(value))
50
+ ::Rack::Utils.set_cookie_header!(@headers, key, roda_class.cookies_opts.merge(value))
45
51
  end
46
52
  end
47
53
  end
@@ -1,11 +1,12 @@
1
1
  # frozen-string-literal: true
2
2
 
3
- # RODA4: Remove plugin
4
3
 
5
4
  require 'rack/csrf'
6
5
 
7
6
  class Roda
8
7
  module RodaPlugins
8
+ warn("The csrf plugin is deprecated and will be removed in Roda 4. Switch to the route_csrf or sec_fetch_site_csrf plugin, or use the Rack::Csrf middleware directly.", uplevel: 1)
9
+
9
10
  # This plugin is no longer recommended for use, it exists only for
10
11
  # backwards compatibility. Consider using the route_csrf plugin
11
12
  # instead, as that provides stronger CSRF protection.
@@ -5,12 +5,11 @@ class Roda
5
5
  module RodaPlugins
6
6
  # The custom_block_results plugin allows you to specify handling
7
7
  # for different block results. By default, Roda only supports
8
- # nil, false, and string block results, but using this plugin,
8
+ # +nil+, +false+, and string block results, but using this plugin,
9
9
  # you can support other block results.
10
10
  #
11
- # For example, if you wanted to support returning Integer
12
- # block results, and have them set the response status code,
13
- # you could do:
11
+ # For example, if you wanted to support Integer block results, and
12
+ # have the integer set the response status code, you could do:
14
13
  #
15
14
  # plugin :custom_block_results
16
15
  #
@@ -23,12 +22,12 @@ class Roda
23
22
  # end
24
23
  #
25
24
  # The expected use case for this is to customize behavior by
26
- # class, but matching uses ===, so it is possible to use non-class
27
- # objects that respond to === appropriately.
25
+ # class, but matching uses <tt>===</tt>, so it is possible to use non-class
26
+ # objects that respond to <tt>===</tt> appropriately.
28
27
  #
29
28
  # Note that custom block result handling only occurs if the types
30
29
  # are not handled by Roda itself. You cannot use this to modify
31
- # the handling of nil, false, or string results. Additionally,
30
+ # the handling of +nil+, +false+, or string results. Additionally,
32
31
  # if the response body has already been written to before the the
33
32
  # route block exits, then the result of the block is ignored,
34
33
  # and the related +handle_block_result+ block will not be called
@@ -44,16 +43,18 @@ class Roda
44
43
  end
45
44
 
46
45
  module ClassMethods
46
+ RodaPlugins.opt_attr_reader(self, :custom_block_results)
47
+
47
48
  # Freeze the configured custom block results when freezing the app.
48
49
  def freeze
49
- opts[:custom_block_results].freeze
50
+ custom_block_results.freeze
50
51
  super
51
52
  end
52
53
 
53
54
  # Specify a block that will be called when an instance of klass
54
55
  # is returned as a block result. The block defines a method.
55
56
  def handle_block_result(klass, &block)
56
- opts[:custom_block_results][klass] = define_roda_method(opts[:custom_block_results][klass] || "custom_block_result_#{klass}", 1, &block)
57
+ custom_block_results[klass] = define_roda_method(custom_block_results[klass] || "custom_block_result_#{klass}", 1, &block)
57
58
  end
58
59
  end
59
60
 
@@ -63,7 +64,7 @@ class Roda
63
64
  # Try each configured custom block result, and call the related method
64
65
  # to get the block result.
65
66
  def unsupported_block_result(result)
66
- roda_class.opts[:custom_block_results].each do |klass, meth|
67
+ roda_class.custom_block_results.each do |klass, meth|
67
68
  if klass === result
68
69
  result = scope.send(meth, result)
69
70
 
@@ -58,8 +58,10 @@ class Roda
58
58
  end
59
59
 
60
60
  module ClassMethods
61
+ RodaPlugins.opt_attr_reader(self, :custom_matchers)
62
+
61
63
  def custom_matcher(match_class, &block)
62
- custom_matchers = Hash[opts[:custom_matchers]]
64
+ custom_matchers = Hash[self.custom_matchers]
63
65
  meth = custom_matchers[match_class] = custom_matchers[match_class] || :"_custom_matcher_#{match_class}"
64
66
  opts[:custom_matchers] = custom_matchers.freeze
65
67
  self::RodaRequest.send(:define_method, meth, &block)
@@ -72,7 +74,7 @@ class Roda
72
74
 
73
75
  # Try custom matchers before calling super
74
76
  def unsupported_matcher(matcher)
75
- roda_class.opts[:custom_matchers].each do |match_class, meth|
77
+ roda_class.custom_matchers.each do |match_class, meth|
76
78
  if match_class === matcher
77
79
  return send(meth, matcher)
78
80
  end
@@ -4,7 +4,7 @@
4
4
  class Roda
5
5
  module RodaPlugins
6
6
  # The default_headers plugin accepts a hash of headers,
7
- # and overrides the default_headers method in the
7
+ # and overrides the +default_headers+ method in the
8
8
  # response class to be a copy of the headers.
9
9
  #
10
10
  # Note that when using this module, you should not
@@ -22,20 +22,17 @@ class Roda
22
22
  # plugin :default_headers, 'Bar'=>'baz'
23
23
  module DefaultHeaders
24
24
  # Merge the given headers into the existing default headers, if any.
25
- def self.configure(app, headers={})
25
+ def self.configure(app, headers=OPTS)
26
26
  app.opts[:default_headers] = (app.default_headers || app::RodaResponse::DEFAULT_HEADERS).merge(headers).freeze
27
27
  end
28
28
 
29
29
  module ClassMethods
30
- # The default response headers to use for the current class.
31
- def default_headers
32
- opts[:default_headers]
33
- end
30
+ RodaPlugins.opt_attr_reader(self, :default_headers)
34
31
 
35
32
  # Optimize the response class set_default_headers method if it hasn't been
36
33
  # overridden and all default headers are strings.
37
34
  def freeze
38
- if (headers = opts[:default_headers]).all?{|k, v| k.is_a?(String) && v.is_a?(String)} &&
35
+ if (headers = default_headers).all?{|k, v| k.is_a?(String) && v.is_a?(String)} &&
39
36
  (self::RodaResponse.instance_method(:set_default_headers).owner == Base::ResponseMethods)
40
37
  self::RodaResponse.class_eval(<<-END, __FILE__, __LINE__+1)
41
38
  private
@@ -18,6 +18,7 @@ class Roda
18
18
  def self.configure(app, &block)
19
19
  raise RodaError, "default_status plugin requires a block" unless block
20
20
  if check_arity = app.opts.fetch(:check_arity, true)
21
+ # RODA4: Remove support for :check_arity, default to false behavior
21
22
  unless block.arity == 0
22
23
  if check_arity == :warn
23
24
  RodaPlugins.warn "Arity mismatch in block passed to plugin :default_status. Expected Arity 0, but arguments required for #{block.inspect}"
@@ -3,7 +3,7 @@
3
3
  #
4
4
  class Roda
5
5
  module RodaPlugins
6
- warn("The delay_build plugin no longer has an effect. It will be removed in Roda 4.")
6
+ warn("The delay_build plugin no longer has an effect. It will be removed in Roda 4.", uplevel: 1)
7
7
 
8
8
  module DelayBuild
9
9
  module ClassMethods
@@ -5,10 +5,10 @@ class Roda
5
5
  module RodaPlugins
6
6
  # The delegate plugin allows you to easily setup instance methods in
7
7
  # the scope of the route block that call methods on the related
8
- # request, response, or class which may offer a simpler API in some cases.
8
+ # request, response, or class, which may offer a simpler API in some cases.
9
9
  # Roda doesn't automatically setup such delegate methods because
10
- # it pollutes the application's method namespace, but this plugin
11
- # allows the user to do so.
10
+ # it pollutes the application's method namespace, and doing so should be
11
+ # the user's choice.
12
12
  #
13
13
  # Here's an example based on the README's initial example, using the
14
14
  # request_delegate method to simplify the DSL:
@@ -49,26 +49,27 @@ class Roda
49
49
  # end
50
50
  module Delegate
51
51
  module ClassMethods
52
- # Delegate the given methods to the class
52
+ # Delegate the given instance methods to the class
53
53
  def class_delegate(*meths)
54
54
  meths.each do |meth|
55
55
  define_method(meth){|*a, &block| self.class.public_send(meth, *a, &block)}
56
56
  end
57
57
  end
58
58
 
59
- # Delegate the given methods to the request
59
+ # Delegate the given instance methods to the request
60
60
  def request_delegate(*meths)
61
61
  meths.each do |meth|
62
62
  define_method(meth){|*a, &block| @_request.public_send(meth, *a, &block)}
63
63
  end
64
64
  end
65
65
 
66
- # Delegate the given methods to the response
66
+ # Delegate the given instance methods to the response
67
67
  def response_delegate(*meths)
68
68
  meths.each do |meth|
69
69
  define_method(meth){|*a, &block| @_response.public_send(meth, *a, &block)}
70
70
  end
71
71
  end
72
+ # RODA4: Switch the above calls to ...
72
73
  end
73
74
  end
74
75
 
@@ -7,6 +7,11 @@ class Roda
7
7
  # (app.call will still call the middleware).
8
8
  # This can be used as an optimization, as the Roda class itself can be used
9
9
  # as the callable, which is faster than using a lambda.
10
+ #
11
+ # This plugin is used automatically by Roda when freezing an app, if Roda
12
+ # determines that doing so is safe (where there is no middleware). However,
13
+ # it can be used manually for unfrozen apps, or in cases where middleware
14
+ # is used.
10
15
  module DirectCall
11
16
  def self.configure(app)
12
17
  app.send(:build_rack_app)
@@ -35,4 +40,3 @@ class Roda
35
40
  register_plugin(:direct_call, DirectCall)
36
41
  end
37
42
  end
38
-
@@ -12,7 +12,7 @@ class Roda
12
12
  #
13
13
  # plugin :disallow_file_uploads
14
14
  #
15
- # This plugin is only supported on Rack 1.6+. This plugin does not technically
15
+ # This plugin is supported on Rack 1.6+. This plugin does not technically
16
16
  # block users from uploading files, it only blocks the parsing of request bodies containing
17
17
  # multipart file uploads. So if you do not call +r.POST+ (or something that calls it such as
18
18
  # +r.params+), then Roda will not attempt to parse the request body, and an exception will not
@@ -24,7 +24,7 @@ class Roda
24
24
  NO_TEMPFILE = lambda{|_,_| raise Error, "Support for uploading files has been disabled"}
25
25
 
26
26
  module RequestMethods
27
- # HTML escape the input and return the escaped version.
27
+ # Raise an error if there is an attempt to use a tempfile.
28
28
  def initialize(_, env)
29
29
  env['rack.multipart.tempfile_factory'] = NO_TEMPFILE
30
30
  super
@@ -35,4 +35,3 @@ class Roda
35
35
  register_plugin(:disallow_file_uploads, DisallowFileUploads)
36
36
  end
37
37
  end
38
-
@@ -4,9 +4,9 @@
4
4
  class Roda
5
5
  module RodaPlugins
6
6
  # The drop_body plugin automatically drops the body and
7
- # Content-Type/Content-Length headers from the response if
7
+ # +Content-Type+/+Content-Length+ headers from the response if
8
8
  # the response status indicates that the response should
9
- # not include a body (response statuses 100, 101, 102, 204,
9
+ # not include a body (response statuses 100-199, 204,
10
10
  # and 304). For response status 205, the body and Content-Type
11
11
  # headers are dropped, but the Content-length header is set to
12
12
  # '0' instead of being dropped.
@@ -15,16 +15,13 @@ class Roda
15
15
  DROP_BODY_STATUSES = [100, 101, 102, 204, 205, 304].freeze
16
16
  RodaPlugins.deprecate_constant(self, :DROP_BODY_STATUSES)
17
17
 
18
- DROP_BODY_RANGE = 100..199
19
- private_constant :DROP_BODY_RANGE
20
-
21
18
  # If the response status indicates a body should not be
22
19
  # returned, use an empty body and remove the Content-Length
23
20
  # and Content-Type headers.
24
21
  def finish
25
22
  r = super
26
23
  case r[0]
27
- when DROP_BODY_RANGE, 204, 304
24
+ when 100..199, 204, 304
28
25
  r[2] = EMPTY_ARRAY
29
26
  h = r[1]
30
27
  h.delete(RodaResponseHeaders::CONTENT_LENGTH)