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
@@ -26,15 +26,17 @@ class Roda
26
26
  end
27
27
 
28
28
  def self.configure(app, &block)
29
- app.send(:define_method, :_common_log_request?, &block)
30
- app.send(:private, :_common_log_request?)
31
- app.send(:alias_method, :_common_log_request?, :_common_log_request?)
29
+ app.class_exec do
30
+ define_method(:_common_log_request?, &block)
31
+ private :_common_log_request?
32
+ alias _common_log_request? _common_log_request?
33
+ end
32
34
  end
33
35
 
34
36
  module InstanceMethods
35
37
  private
36
38
 
37
- # Log request/response information in common log format to logger.
39
+ # Do common logging only if configured to do so.
38
40
  def _roda_after_90__common_logger(result)
39
41
  super if result && _common_log_request?(result)
40
42
  end
@@ -4,14 +4,12 @@ require 'delegate'
4
4
 
5
5
  class Roda
6
6
  module RodaPlugins
7
- # The flash plugin adds a +flash+ instance method to Roda,
8
- # for typical web application flash handling, where values
9
- # set in the current flash hash are available in the next
10
- # request.
7
+ # The flash plugin adds a +flash+ instance method, for typical web
8
+ # application flash handling, where values set in the current flash
9
+ # hash are available in the next request loading the flash.
11
10
  #
12
- # With the example below, if a POST request is submitted,
13
- # it will redirect and the resulting GET request will
14
- # return 'b'.
11
+ # With the example below, if a +POST+ request is submitted, it will
12
+ # redirect and the resulting +GET+ request will return <tt>'b'</tt>.
15
13
  #
16
14
  # plugin :flash
17
15
  #
@@ -23,7 +21,7 @@ class Roda
23
21
  #
24
22
  # r.post do
25
23
  # flash['a'] = 'b'
26
- # r.redirect('')
24
+ # r.redirect
27
25
  # end
28
26
  # end
29
27
  # end
@@ -35,11 +33,16 @@ class Roda
35
33
  # flash.now['a'] = 'b'
36
34
  # flash['a'] # = >'b'
37
35
  # end
36
+ #
37
+ # The flash is saved in the session, so it has the same restrictions
38
+ # as other data saved in the session. If the session does not support
39
+ # symbols (e.g. when using the sessions plugin), you should not use
40
+ # symbols as flash keys.
38
41
  module Flash
39
42
  SCOPE_INSTANCE_VARIABLES = [:@_flash].freeze
40
43
 
41
- # Simple flash hash, where assiging to the hash updates the flash
42
- # used in the following request.
44
+ # Hash-like flash, where assigning to the object updates the data
45
+ # accessed in the following request.
43
46
  class FlashHash < DelegateClass(Hash)
44
47
  # The flash hash for the next request. This
45
48
  # is what gets written to by #[]=.
@@ -48,8 +51,8 @@ class Roda
48
51
  # The flash hash for the current request
49
52
  alias now __getobj__
50
53
 
51
- # Setup the next hash when initializing, and handle treat nil
52
- # as a new empty hash.
54
+ # Setup the next hash when initializing, and treat nil
55
+ # argument as a new empty hash.
53
56
  def initialize(hash={})
54
57
  super(hash||{})
55
58
  @next = {}
@@ -93,7 +96,7 @@ class Roda
93
96
  # Access the flash hash for the current request, loading
94
97
  # it from the session if it is not already loaded.
95
98
  def flash
96
- # :_flash to support transparent upgrades from previous key
99
+ # RODA4: Remove fallback to checking :_flash
97
100
  @_flash ||= FlashHash.new(session['_flash'] || (session['_flash'] = session.delete(:_flash)))
98
101
  end
99
102
 
@@ -0,0 +1,24 @@
1
+ # frozen-string-literal: true
2
+
3
+ #
4
+ class Roda
5
+ module RodaPlugins
6
+ # The freeze_request_response plugin freezes the RodaRequest and
7
+ # RodaResponse classes when freezing the Roda class. This will
8
+ # become the default behavior in Roda 4.
9
+ module FreezeRequestResponse
10
+ module ClassMethods
11
+ # Freeze the RodaRequest and RodaResponse classes when freezing the Roda class.
12
+ def freeze
13
+ super
14
+ # RODA4: make this behavior the default
15
+ self::RodaRequest.freeze
16
+ self::RodaResponse.freeze
17
+ self
18
+ end
19
+ end
20
+ end
21
+
22
+ register_plugin(:freeze_request_response, FreezeRequestResponse)
23
+ end
24
+ end
@@ -33,9 +33,11 @@ class Roda
33
33
  end
34
34
 
35
35
  module ClassMethods
36
+ RodaPlugins.opt_attr_reader(self, :hash_branch_view_subdir_methods)
37
+
36
38
  # Freeze the hash_branch_view_subdir metadata when freezing the app.
37
39
  def freeze
38
- opts[:hash_branch_view_subdir_methods].freeze.each_value(&:freeze)
40
+ hash_branch_view_subdir_methods.freeze.each_value(&:freeze)
39
41
  super
40
42
  end
41
43
 
@@ -43,8 +45,8 @@ class Roda
43
45
  def inherited(subclass)
44
46
  super
45
47
 
46
- h = subclass.opts[:hash_branch_view_subdir_methods]
47
- opts[:hash_branch_view_subdir_methods].each do |namespace, routes|
48
+ h = subclass.hash_branch_view_subdir_methods
49
+ hash_branch_view_subdir_methods.each do |namespace, routes|
48
50
  h[namespace] = routes.dup
49
51
  end
50
52
  end
@@ -53,7 +55,7 @@ class Roda
53
55
  # by modifying the generated method to append the view subdirectory before
54
56
  # dispatching to the original block.
55
57
  def hash_branch(namespace='', segment, &block)
56
- meths = opts[:hash_branch_view_subdir_methods][namespace] ||= {}
58
+ meths = hash_branch_view_subdir_methods[namespace] ||= {}
57
59
 
58
60
  if block
59
61
  meth = meths[segment] = define_roda_method(meths[segment] || "_hash_branch_view_subdir_#{namespace}_#{segment}", 1, &convert_route_block(block))
@@ -88,9 +88,11 @@ class Roda
88
88
  end
89
89
 
90
90
  module ClassMethods
91
+ RodaPlugins.opt_attr_reader(self, :hash_branches)
92
+
91
93
  # Freeze the hash_branches metadata when freezing the app.
92
94
  def freeze
93
- opts[:hash_branches].freeze.each_value(&:freeze)
95
+ hash_branches.freeze.each_value(&:freeze)
94
96
  super
95
97
  end
96
98
 
@@ -98,8 +100,8 @@ class Roda
98
100
  def inherited(subclass)
99
101
  super
100
102
 
101
- h = subclass.opts[:hash_branches]
102
- opts[:hash_branches].each do |namespace, routes|
103
+ h = subclass.hash_branches
104
+ hash_branches.each do |namespace, routes|
103
105
  h[namespace] = routes.dup
104
106
  end
105
107
  end
@@ -108,7 +110,7 @@ class Roda
108
110
  # a block, removes the existing branch handler if it exists.
109
111
  def hash_branch(namespace='', segment, &block)
110
112
  segment = "/#{segment}"
111
- routes = opts[:hash_branches][namespace] ||= {}
113
+ routes = hash_branches[namespace] ||= {}
112
114
  if block
113
115
  routes[segment] = define_roda_method(routes[segment] || "hash_branch_#{namespace}_#{segment}", 1, &convert_route_block(block))
114
116
  elsif meth = routes.delete(segment)
@@ -125,7 +127,7 @@ class Roda
125
127
 
126
128
  return unless rp.getbyte(0) == 47 # "/"
127
129
 
128
- if routes = roda_class.opts[:hash_branches][namespace]
130
+ if routes = roda_class.hash_branches[namespace]
129
131
  if segment_end = rp.index('/', 1)
130
132
  if meth = routes[rp[0, segment_end]]
131
133
  always do
@@ -80,9 +80,11 @@ class Roda
80
80
  end
81
81
 
82
82
  module ClassMethods
83
+ RodaPlugins.opt_attr_reader(self, :hash_paths)
84
+
83
85
  # Freeze the hash_paths metadata when freezing the app.
84
86
  def freeze
85
- opts[:hash_paths].freeze.each_value(&:freeze)
87
+ hash_paths.freeze.each_value(&:freeze)
86
88
  super
87
89
  end
88
90
 
@@ -90,8 +92,8 @@ class Roda
90
92
  def inherited(subclass)
91
93
  super
92
94
 
93
- h = subclass.opts[:hash_paths]
94
- opts[:hash_paths].each do |namespace, routes|
95
+ h = subclass.hash_paths
96
+ hash_paths.each do |namespace, routes|
95
97
  h[namespace] = routes.dup
96
98
  end
97
99
  end
@@ -102,7 +104,7 @@ class Roda
102
104
  # there is one. If called without a block, removes the existing
103
105
  # path handler if it exists.
104
106
  def hash_path(namespace='', path, &block)
105
- routes = opts[:hash_paths][namespace] ||= {}
107
+ routes = hash_paths[namespace] ||= {}
106
108
  if block
107
109
  routes[path] = define_roda_method(routes[path] || "hash_path_#{namespace}_#{path}", 1, &convert_route_block(block))
108
110
  elsif meth = routes.delete(path)
@@ -115,7 +117,7 @@ class Roda
115
117
  # Checks the matching hash_path namespace for a branch matching the
116
118
  # remaining path, and dispatch to that block if there is one.
117
119
  def hash_paths(namespace=matched_path)
118
- if (routes = roda_class.opts[:hash_paths][namespace]) && (meth = routes[@remaining_path])
120
+ if (routes = roda_class.hash_paths[namespace]) && (meth = routes[@remaining_path])
119
121
  always do
120
122
  @remaining_path = ''
121
123
  scope.send(meth, self)
@@ -78,16 +78,20 @@ class Roda
78
78
  end
79
79
 
80
80
  module ClassMethods
81
+ RodaPlugins.opt_attr_reader(self, :hash_public_prefix)
82
+ RodaPlugins.opt_attr_reader(self, :hash_public_length)
83
+ RodaPlugins.opt_attr_reader(self, :hash_public_mutex)
84
+ RodaPlugins.opt_attr_reader(self, :hash_public_cache)
85
+
81
86
  # The digest for the given file to use in hash_path.
82
87
  def hash_path_digest(file)
83
- opts = self.opts
84
- cache = opts[:hash_public_cache]
85
- mutex = opts[:hash_public_mutex]
88
+ cache = hash_public_cache
89
+ mutex = hash_public_mutex
86
90
  unless digest = mutex.synchronize{cache[file]}
87
- digest = Digest::SHA256.file(File.join(opts[:public_root], file)).base64digest
91
+ digest = Digest::SHA256.file(File.join(public_root, file)).base64digest
88
92
  digest.chomp!("=")
89
93
  digest.tr!("+/", "-_")
90
- if length = opts[:hash_public_length]
94
+ if length = hash_public_length
91
95
  digest = digest[0, length]
92
96
  end
93
97
  digest.freeze
@@ -102,7 +106,7 @@ class Roda
102
106
  # This does not check the file is inside the directory for performance
103
107
  # reasons, so this should not be called with untrusted input.
104
108
  def hash_path(file)
105
- "/#{opts[:hash_public_prefix]}/#{self.class.hash_path_digest(file)}/#{file}"
109
+ "/#{self.class.hash_public_prefix}/#{self.class.hash_path_digest(file)}/#{file}"
106
110
  end
107
111
  end
108
112
 
@@ -112,7 +116,7 @@ class Roda
112
116
  # a string segment for the content hash, and this is a GET request.
113
117
  def hash_public
114
118
  if is_get?
115
- on roda_class.opts[:hash_public_prefix], String do |_|
119
+ on roda_class.hash_public_prefix, String do |_|
116
120
  public
117
121
  end
118
122
  end
@@ -1,6 +1,5 @@
1
1
  # frozen-string-literal: true
2
2
 
3
- require 'json'
4
3
  require 'find'
5
4
 
6
5
  class Roda
@@ -29,6 +28,7 @@ class Roda
29
28
  # load_hash_public_cache_file
30
29
  module HashPublicCache
31
30
  def self.load_dependencies(app, _cache_file, opts = OPTS)
31
+ app.plugin :_json
32
32
  app.plugin :hash_public, opts
33
33
  end
34
34
 
@@ -40,13 +40,15 @@ class Roda
40
40
  end
41
41
 
42
42
  module ClassMethods
43
+ RodaPlugins.opt_attr_reader(self, :hash_public_cache_file)
44
+
43
45
  # Load the hash public cache file, if it exists. This replaces the hash
44
46
  # public cache with the values from the file.
45
47
  def load_hash_public_cache_file
46
- file = opts[:hash_public_cache_file]
48
+ file = hash_public_cache_file
47
49
  return unless File.file?(file)
48
50
 
49
- cache = opts[:hash_public_cache] = (opts[:json_parser] || ::JSON.method(:parse)).call(::File.read(file))
51
+ cache = opts[:hash_public_cache] = json_parser.call(::File.read(file))
50
52
  cache.each_value(&:freeze)
51
53
  nil
52
54
  end
@@ -56,13 +58,13 @@ class Roda
56
58
  # If a block is given, it will only calculate the digest for the file
57
59
  # if the block returns truthy.
58
60
  def scan_hash_public_cache_dir
59
- cache = opts[:hash_public_cache]
61
+ cache = hash_public_cache
60
62
 
61
63
  # Public root doesn't have trailing slash even if given, as
62
64
  # File.expand_path removes it.
63
- root = opts[:public_root] + File::SEPARATOR
65
+ root = public_root + File::SEPARATOR
64
66
 
65
- Find.find(opts[:public_root]) do |file|
67
+ Find.find(public_root) do |file|
66
68
  if File.file?(file)
67
69
  file = file.sub(root, '')
68
70
  next if cache[file]
@@ -80,7 +82,7 @@ class Roda
80
82
 
81
83
  # Write the current hash public cache to the cache file.
82
84
  def dump_hash_public_cache_file
83
- File.write(opts[:hash_public_cache_file], (opts[:json_serializer] || :to_json.to_proc).call(opts[:hash_public_cache]))
85
+ File.write(hash_public_cache_file, json_serializer.call(hash_public_cache))
84
86
  nil
85
87
  end
86
88
  end
@@ -191,7 +191,7 @@ class Roda
191
191
  def dispatch_from(namespace='', branch, &block)
192
192
  ns = @namespace
193
193
  if block
194
- meth_hash = @roda.opts[:hash_routes_methods]
194
+ meth_hash = @roda.hash_routes_methods
195
195
  key = [:dispatch_from, namespace, branch].freeze
196
196
  meth = meth_hash[key] = @roda.define_roda_method(meth_hash[key] || "hash_routes_dispatch_from_#{namespace}_#{branch}", 1, &block)
197
197
  @roda.hash_branch(namespace, branch) do |r|
@@ -241,7 +241,7 @@ class Roda
241
241
  end
242
242
  end
243
243
 
244
- [:get, :post, :delete, :head, :options, :link, :patch, :put, :trace, :unlink].each do |meth|
244
+ %i[get post delete head options link patch put trace unlink query].each do |meth|
245
245
  define_method(meth) do |path, &block|
246
246
  verb(meth, path, &block)
247
247
  end
@@ -253,7 +253,7 @@ class Roda
253
253
  # Returns 404 for requests for the path with a different request method.
254
254
  def verb(verb, path, &block)
255
255
  path = path == true ? "" : "/#{path}"
256
- meth_hash = @roda.opts[:hash_routes_methods]
256
+ meth_hash = @roda.hash_routes_methods
257
257
  key = [@namespace, path].freeze
258
258
  meth = meth_hash[key] = @roda.define_roda_method(meth_hash[key] || "hash_routes_#{@namespace}_#{path}", 0, &block)
259
259
  @roda.hash_path(@namespace, path) do |r|
@@ -265,9 +265,11 @@ class Roda
265
265
  end
266
266
 
267
267
  module ClassMethods
268
+ RodaPlugins.opt_attr_reader(self, :hash_routes_methods)
269
+
268
270
  # Freeze the hash_routes metadata when freezing the app.
269
271
  def freeze
270
- opts[:hash_routes_methods].freeze
272
+ hash_routes_methods.freeze
271
273
  super
272
274
  end
273
275
 
@@ -16,7 +16,11 @@ class Roda
16
16
  module Heartbeat
17
17
  # Set the heartbeat path to the given path.
18
18
  def self.configure(app, opts=OPTS)
19
- app.opts[:heartbeat_path] = (opts[:path] || app.opts[:heartbeat_path] || "/heartbeat").dup.freeze
19
+ app.opts[:heartbeat_path] = (opts[:path] || app.heartbeat_path || "/heartbeat").dup.freeze
20
+ end
21
+
22
+ module ClassMethods
23
+ RodaPlugins.opt_attr_reader(self, :heartbeat_path)
20
24
  end
21
25
 
22
26
  module InstanceMethods
@@ -24,7 +28,7 @@ class Roda
24
28
 
25
29
  # If the request is for a heartbeat path, return the heartbeat response.
26
30
  def _roda_before_20__heartbeat
27
- if env['PATH_INFO'] == opts[:heartbeat_path]
31
+ if env['PATH_INFO'] == self.class.heartbeat_path
28
32
  response = @_response
29
33
  response.status = 200
30
34
  response[RodaResponseHeaders::CONTENT_TYPE] = 'text/plain'
@@ -231,6 +231,12 @@ class Roda
231
231
  end
232
232
  end
233
233
 
234
+ module ClassMethods
235
+ RodaPlugins.opt_attr_reader(self, :hmac_paths_secret)
236
+ RodaPlugins.opt_attr_reader(self, :hmac_paths_old_secret)
237
+ RodaPlugins.opt_attr_reader(self, :hmac_paths_namespace_session_key)
238
+ end
239
+
234
240
  module InstanceMethods
235
241
  # Return a path with an HMAC. Designed to be used with r.hmac_path, to make sure
236
242
  # users can only request paths that they have been provided by the application
@@ -315,7 +321,7 @@ class Roda
315
321
  # using the secret given in the plugin, for the given root and options.
316
322
  # This always returns a hexidecimal string.
317
323
  def hmac_path_hmac_secret(root, opts=OPTS)
318
- secret = opts[:secret] || self.opts[:hmac_paths_secret]
324
+ secret = opts[:secret] || self.class.hmac_paths_secret
319
325
 
320
326
  if namespace = hmac_path_namespace(opts)
321
327
  secret = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, secret, namespace)
@@ -326,7 +332,7 @@ class Roda
326
332
 
327
333
  # The default namespace to use for hmac_path, if a :namespace option is not provided.
328
334
  def hmac_path_default_namespace
329
- if (key = opts[:hmac_paths_namespace_session_key]) && (value = session[key])
335
+ if (key = self.class.hmac_paths_namespace_session_key) && (value = session[key])
330
336
  value.to_s
331
337
  end
332
338
  end
@@ -395,7 +401,7 @@ class Roda
395
401
  def hmac_path_valid?(root, path, hmac, opts=OPTS)
396
402
  if Rack::Utils.secure_compare(scope.hmac_path_hmac(root, path, opts), hmac)
397
403
  true
398
- elsif old_secret = roda_class.opts[:hmac_paths_old_secret]
404
+ elsif old_secret = roda_class.hmac_paths_old_secret
399
405
  opts = opts.dup
400
406
  opts[:secret] = old_secret
401
407
  Rack::Utils.secure_compare(scope.hmac_path_hmac(root, path, opts), hmac)
@@ -39,21 +39,24 @@ class Roda
39
39
  end
40
40
 
41
41
  module ClassMethods
42
+ RodaPlugins.opt_attr_reader(self, :after_hooks)
43
+ RodaPlugins.opt_attr_reader(self, :before_hooks)
44
+
42
45
  # Freeze the array of hook methods when freezing the app.
43
46
  def freeze
44
- opts[:after_hooks].freeze
45
- opts[:before_hooks].freeze
47
+ after_hooks.freeze
48
+ before_hooks.freeze
46
49
 
47
50
  super
48
51
  end
49
52
 
50
53
  # Add an after hook.
51
54
  def after(&block)
52
- opts[:after_hooks] << define_roda_method("after_hook", 1, &block)
53
- if opts[:after_hooks].length == 1
54
- class_eval("alias _roda_after_80__hooks #{opts[:after_hooks].first}", __FILE__, __LINE__)
55
+ after_hooks << define_roda_method("after_hook", 1, &block)
56
+ if after_hooks.length == 1
57
+ class_eval("alias _roda_after_80__hooks #{after_hooks.first}", __FILE__, __LINE__)
55
58
  else
56
- class_eval("def _roda_after_80__hooks(res) #{opts[:after_hooks].map{|m| "#{m}(res)"}.join(';')} end", __FILE__, __LINE__)
59
+ class_eval("def _roda_after_80__hooks(res) #{after_hooks.map{|m| "#{m}(res)"}.join(';')} end", __FILE__, __LINE__)
57
60
  end
58
61
  private :_roda_after_80__hooks
59
62
  def_roda_after
@@ -62,11 +65,11 @@ class Roda
62
65
 
63
66
  # Add a before hook.
64
67
  def before(&block)
65
- opts[:before_hooks].unshift(define_roda_method("before_hook", 0, &block))
66
- if opts[:before_hooks].length == 1
67
- class_eval("alias _roda_before_10__hooks #{opts[:before_hooks].first}", __FILE__, __LINE__)
68
+ before_hooks.unshift(define_roda_method("before_hook", 0, &block))
69
+ if before_hooks.length == 1
70
+ class_eval("alias _roda_before_10__hooks #{before_hooks.first}", __FILE__, __LINE__)
68
71
  else
69
- class_eval("def _roda_before_10__hooks; #{opts[:before_hooks].join(';')} end", __FILE__, __LINE__)
72
+ class_eval("def _roda_before_10__hooks; #{before_hooks.join(';')} end", __FILE__, __LINE__)
70
73
  end
71
74
  private :_roda_before_10__hooks
72
75
  def_roda_before
@@ -101,6 +101,11 @@ class Roda
101
101
  end
102
102
  end
103
103
 
104
+ module ClassMethods
105
+ RodaPlugins.opt_attr_reader(self, :host_authorization_host)
106
+ RodaPlugins.opt_attr_reader(self, :host_authorization_check_forwarded, name: :host_authorization_check_forwarded?)
107
+ end
108
+
104
109
  module InstanceMethods
105
110
  # Check whether the host is authorized. If not authorized, return a response
106
111
  # immediately based on the plugin block.
@@ -108,7 +113,7 @@ class Roda
108
113
  r = @_request
109
114
  return if host_authorized?(_convert_host_for_authorization(r.env["HTTP_HOST"].to_s.dup))
110
115
 
111
- if opts[:host_authorization_check_forwarded] && (host = r.env["HTTP_X_FORWARDED_HOST"])
116
+ if self.class.host_authorization_check_forwarded? && (host = r.env["HTTP_X_FORWARDED_HOST"])
112
117
  if i = host.rindex(',')
113
118
  host = host[i+1, 10000000].to_s
114
119
  end
@@ -133,7 +138,7 @@ class Roda
133
138
  end
134
139
 
135
140
  # Whether the host given is one of the authorized hosts for this application.
136
- def host_authorized?(host, authorized_host = opts[:host_authorization_host])
141
+ def host_authorized?(host, authorized_host = self.class.host_authorization_host)
137
142
  case authorized_host
138
143
  when Array
139
144
  authorized_host.any?{|auth_host| host_authorized?(host, auth_host)}
@@ -176,6 +176,11 @@ class Roda
176
176
  end
177
177
  private_constant :DSL
178
178
 
179
+ module ClassMethods
180
+ RodaPlugins.opt_attr_reader(self, :host_routing_hash)
181
+ RodaPlugins.opt_attr_reader(self, :host_routing_default_host)
182
+ end
183
+
179
184
  module InstanceMethods
180
185
  # Handle case where plugin is used without providing a block to
181
186
  # +hosts.default+. This returns nil, ensuring that the hostname
@@ -207,9 +212,9 @@ class Roda
207
212
  host = ""
208
213
  end
209
214
 
210
- roda_class.opts[:host_routing_hash][host] ||
215
+ roda_class.host_routing_hash[host] ||
211
216
  scope._host_routing_default(host) ||
212
- roda_class.opts[:host_routing_default_host]
217
+ roda_class.host_routing_default_host
213
218
  end
214
219
  end
215
220
  end
@@ -37,13 +37,25 @@ class Roda
37
37
  # # To treat the exception raised as a submitted parameter
38
38
  # {body_error: exception}
39
39
  # end
40
+ #
41
+ # You can use the +rescue_classes+ argument when loading the plugin to only rescue
42
+ # specific exception classes:
43
+ #
44
+ # plugin :invalid_request_body, :empty_hash, rescue_classes: Rack::BadRequest
45
+ #
46
+ # However, this can be risky, because there have been many cases in the past where
47
+ # Rack can rescue classes such as +ArgumentError+ or +NoMethodError+ when parsing
48
+ # invalid request bodies. The default for +rescue_classes+ is +StandardError+.
40
49
  module InvalidRequestBody
41
50
  # Exception class raised for invalid request bodies.
42
51
  Error = Class.new(RodaError)
43
52
 
44
53
  # Set the action to use (:empty_400, :empty_hash, :raise) for invalid request bodies,
45
- # or use a block for custom behavior.
46
- def self.configure(app, action=nil, &block)
54
+ # or use a block for custom behavior. The rescue_classes argument is an exception
55
+ # class or array of exception classes to rescue.
56
+ def self.configure(app, action=nil, rescue_classes: StandardError, &block)
57
+ app.opts[:invalid_response_body_rescue_classes] = Array(rescue_classes).dup.freeze
58
+
47
59
  if action
48
60
  if block
49
61
  raise RodaError, "cannot provide both block and action when loading invalid_request_body plugin"
@@ -56,7 +68,7 @@ class Roda
56
68
 
57
69
  app::RodaRequest.send(:alias_method, :handle_invalid_request_body, method)
58
70
  elsif block
59
- app::RodaRequest.class_eval do
71
+ app::RodaRequest.class_exec do
60
72
  define_method(:handle_invalid_request_body, &block)
61
73
  alias handle_invalid_request_body handle_invalid_request_body
62
74
  end
@@ -67,6 +79,10 @@ class Roda
67
79
  app::RodaRequest.send(:private, :handle_invalid_request_body)
68
80
  end
69
81
 
82
+ module ClassMethods
83
+ RodaPlugins.opt_attr_reader(self, :invalid_response_body_rescue_classes)
84
+ end
85
+
70
86
  module RequestMethods
71
87
  # Handle invalid request bodies as configured if the default behavior
72
88
  # raises an exception.
@@ -74,7 +90,7 @@ class Roda
74
90
  super
75
91
  rescue RodaError
76
92
  raise
77
- rescue => e
93
+ rescue *roda_class.invalid_response_body_rescue_classes => e
78
94
  handle_invalid_request_body(e)
79
95
  end
80
96
 
@@ -20,11 +20,15 @@ class Roda
20
20
  app.opts[:ip_from_header_env_key] = "HTTP_#{header.upcase.tr('-', '_')}".freeze
21
21
  end
22
22
 
23
+ module ClassMethods
24
+ RodaPlugins.opt_attr_reader(self, :ip_from_header_env_key)
25
+ end
26
+
23
27
  module RequestMethods
24
28
  # Return the IP address continained in the configured header, if present.
25
29
  # Fallback to the default behavior if not present.
26
30
  def ip
27
- @env[roda_class.opts[:ip_from_header_env_key]] || super
31
+ @env[roda_class.ip_from_header_env_key] || super
28
32
  end
29
33
  end
30
34
  end