padrino-core 0.18.6 → 0.18.7

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 (33) hide show
  1. checksums.yaml +4 -4
  2. data/lib/padrino-core/application/authenticity_token.rb +1 -1
  3. data/lib/padrino-core/application/routing.rb +27 -13
  4. data/lib/padrino-core/filter.rb +2 -2
  5. data/lib/padrino-core/loader.rb +1 -1
  6. data/lib/padrino-core/logger.rb +6 -1
  7. data/lib/padrino-core/path_router/compiler.rb +21 -3
  8. data/lib/padrino-core/path_router/matcher.rb +3 -0
  9. data/lib/padrino-core/path_router/route.rb +1 -1
  10. data/lib/padrino-core/path_router.rb +3 -3
  11. data/lib/padrino-core/reloader/storage.rb +55 -22
  12. data/lib/padrino-core/reloader.rb +62 -21
  13. data/lib/padrino-core/version.rb +1 -1
  14. data/test/fixtures/apps/concerned/models/branded_item.rb +5 -0
  15. data/test/fixtures/apps/concerned/models/mixins/branded.rb +13 -0
  16. data/test/fixtures/apps/excluded_constants/app.rb +7 -0
  17. data/test/fixtures/apps/excluded_constants/helpers/excluded_constants_helpers.rb +7 -0
  18. data/test/fixtures/apps/excluded_constants/support/excluded_constants_settings.rb +8 -0
  19. data/test/fixtures/apps/loaded_excluded/app.rb +7 -0
  20. data/test/fixtures/apps/loaded_excluded/helpers/loaded_excluded_helpers.rb +7 -0
  21. data/test/fixtures/apps/loaded_excluded/support/loaded_excluded_classes.rb +15 -0
  22. data/test/fixtures/dependencies/dbadmin/child.rb +2 -0
  23. data/test/fixtures/dependencies/dbadmin/parent.rb +2 -0
  24. data/test/test_csrf_protection.rb +13 -0
  25. data/test/test_dependencies.rb +16 -0
  26. data/test/test_filters.rb +48 -0
  27. data/test/test_logger.rb +39 -0
  28. data/test/test_reloader_external.rb +9 -0
  29. data/test/test_reloader_simple.rb +281 -0
  30. data/test/test_reloader_storage.rb +101 -0
  31. data/test/test_reloader_system.rb +86 -0
  32. data/test/test_routing.rb +247 -2
  33. metadata +13 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 37ece5f8ac3fea18d83aa9c9dcd392b3c3c584bd314197cc4a1afe41b1ce4ea6
4
- data.tar.gz: 693070c79a2bdd601a59977d14388254ad845795653a6c4a1fa1327bd6d7a10a
3
+ metadata.gz: 9b5b8184c5fd07158687b3ee195f5d7c23052dd64fe5be9b19a3abeca6484144
4
+ data.tar.gz: 70086f0b6fe227c80a98c1ba6838c5d86d09dec514ab5db32b98e0db8aed8c1e
5
5
  SHA512:
6
- metadata.gz: '01139ddfbe043ec2e177f4bdd9fa44ba010df56f467ddf7a56b187a60de8b2978c8df271807232b220054d32af7963ffa2a1d573bfb5078f7e454b43911071d8'
7
- data.tar.gz: bd34c2503675242fd859c47e6e73421b97b404ae1617aa1dca5d25a64ca1ea10d377b6de29b444ef94c8fd823e8c27b3cd7d9eb23de1a546943ff0c5ce53d35f
6
+ metadata.gz: c3acd5bc7d41c6445224ccb44f71d0d3ad739d22f2b5cdad4bc7efb4d6c206551a9ee6289efc41c43f4d2ea410069799c054916e2b7188f796924475c16133b4
7
+ data.tar.gz: 9603d7794a74800e082573f4919c0b1e4385169728614ea3052cc870c2723521d0d3360044708e26dbf03d7f0d0c393ba99629fba3d6aa5b3d07e15ff36d4415
@@ -36,7 +36,7 @@ module Padrino
36
36
  private
37
37
 
38
38
  def excepted?(form)
39
- @except.any? { |path| path.is_a?(Regexp) ? path.match?(form) : path == form }
39
+ @except.any? { |path| path.is_a?(Regexp) ? Routing.path_match?(path, form) : path == form }
40
40
  end
41
41
  end
42
42
  end
@@ -105,16 +105,31 @@ module Padrino
105
105
  [route_path, decode_path(path), decode_path(route_path)].map(&:scrub).uniq
106
106
  end
107
107
 
108
+ ##
109
+ # Whether +pattern+ (a Regexp, String or anything with ===) matches +path+, a
110
+ # decoded form of a request path. A Regexp fixed to an encoding the path can't be
111
+ # read in (/n with non-ASCII bytes) doesn't match it, as in Sinatra.
112
+ #
113
+ # @api private
114
+ def path_match?(pattern, path)
115
+ pattern === path
116
+ rescue Encoding::CompatibilityError
117
+ false
118
+ end
119
+
108
120
  ##
109
121
  # Whether a provides condition for +types+ accepts +format+, a Symbol
110
- # decoded as route params receive it.
122
+ # decoded as route params receive it. :any accepts only the name of a
123
+ # file extension with a registered MIME type, which the route answers with.
111
124
  #
112
125
  # @example
113
126
  # Padrino::Routing.provided_format?(%i[html json], :json) # => true
127
+ # Padrino::Routing.provided_format?([:any], :nosuchext) # => false
128
+ # Padrino::Routing.provided_format?([:any], :'text/plain') # => false
114
129
  #
115
130
  # @api private
116
131
  def provided_format?(types, format)
117
- !([:any, format] & types).empty?
132
+ types.include?(format) || (types.include?(:any) && !::Rack::Mime.mime_type(".#{format}", nil).nil?)
118
133
  end
119
134
 
120
135
  ##
@@ -422,10 +437,10 @@ module Padrino
422
437
  # Recognize a given path.
423
438
  #
424
439
  # @param [String] path
425
- # Path+Query to parse
440
+ # The path to recognize, percent-encoded or not, without a query string.
426
441
  #
427
- # @return [Symbol, Hash]
428
- # Returns controller and query params.
442
+ # @return [Array(Symbol, Hash), nil]
443
+ # Returns controller and query params, or nil when no route matches the path.
429
444
  #
430
445
  # @example Giving a controller like:
431
446
  # controller :foo do
@@ -441,8 +456,7 @@ module Padrino
441
456
  # # => [:foo_bar, { id: :mine }]
442
457
  #
443
458
  def recognize_path(path)
444
- responses = @router.recognize_path(path)
445
- [responses[0], responses[1]]
459
+ @router.recognize_path(path)
446
460
  end
447
461
 
448
462
  ##
@@ -871,7 +885,7 @@ module Padrino
871
885
  def provides_format(*types)
872
886
  mime_types = types.map { |type| mime_type(CONTENT_TYPE_ALIASES[type] || type) }
873
887
  condition do
874
- return provides_format?(types, params[:format].to_sym) if params[:format]
888
+ return provides_format?(types, params[:format].to_s.scrub.to_sym) if params[:format]
875
889
 
876
890
  accepts = request.accept.map(&:to_str)
877
891
  # Per rfc2616-sec14:
@@ -882,7 +896,7 @@ module Padrino
882
896
 
883
897
  accepts = accepts.empty? ? mime_types.slice(0, 1) : (accepts & mime_types)
884
898
 
885
- type = accepts.first && mime_symbol(accepts.first)
899
+ type = accepts.first && (mime_symbol(accepts.first) || accepts.first)
886
900
  type ||= catch_all && types.first
887
901
 
888
902
  accept_format = CONTENT_TYPE_ALIASES[type] || type
@@ -909,7 +923,7 @@ module Padrino
909
923
  mime_types = types.map { |type| mime_type(CONTENT_TYPE_ALIASES[type] || type) }
910
924
  condition do
911
925
  halt 406 unless mime_types.include?(request.media_type)
912
- content_type(mime_symbol(request.media_type))
926
+ content_type(mime_symbol(request.media_type) || request.media_type)
913
927
  end
914
928
  end
915
929
 
@@ -1063,8 +1077,7 @@ module Padrino
1063
1077
  private
1064
1078
 
1065
1079
  def provides_any?(formats)
1066
- accepted_format = formats.first
1067
- type = accepted_format ? mime_symbol(accepted_format) : :html
1080
+ type = formats.filter_map { |format| mime_symbol(format) || (:html if ::Rack::Mime.match?('text/html', format)) }.first || :html
1068
1081
  content_type(CONTENT_TYPE_ALIASES[type] || type)
1069
1082
  end
1070
1083
 
@@ -1078,7 +1091,8 @@ module Padrino
1078
1091
  end
1079
1092
 
1080
1093
  def mime_symbol(media_type)
1081
- ::Rack::Mime::MIME_TYPES.key(media_type).sub(/\./, '').to_sym
1094
+ extension = ::Rack::Mime::MIME_TYPES.key(media_type)
1095
+ extension&.sub(/\./, '')&.to_sym
1082
1096
  end
1083
1097
 
1084
1098
  def filter!(type, base = settings)
@@ -59,9 +59,9 @@ module Padrino
59
59
  # gives for it, matches. An :except filter is skipped only if every one of them matches
60
60
  # exactly, and none has a newline. +info+ is the path_entry of path_info, when the caller has it.
61
61
  def match_path?(argument, request, settings, paths, info = nil)
62
- return paths.all? { |path| !path.include?("\n") && argument === path } unless @mode
62
+ return paths.all? { |path| !path.include?("\n") && Routing.path_match?(argument, path) } unless @mode
63
63
 
64
- filter_paths(request, settings, paths, info || path_entry(request)).any? { |form| argument === form }
64
+ filter_paths(request, settings, paths, info || path_entry(request)).any? { |form| Routing.path_match?(argument, form) }
65
65
  end
66
66
 
67
67
  # +paths+ and the forms route_forms gives for each, once per request for each route run and
@@ -154,7 +154,7 @@ module Padrino
154
154
  files.delete(file)
155
155
  loaded = true
156
156
  rescue NameError, LoadError => error
157
- raise if Reloader.exclude.any? { |path| file.start_with?(path) } || options[:cyclic] == false
157
+ raise if Reloader.send(:path_excluded?, file) || options[:cyclic] == false
158
158
  logger.devel "Cyclic dependency reload for #{error.class}: #{error.message}"
159
159
  rescue Exception => fatal
160
160
  break
@@ -358,8 +358,13 @@ module Padrino
358
358
  }
359
359
  Config.merge!(PADRINO_LOGGER) if PADRINO_LOGGER
360
360
 
361
+ # Built when this file loads, so threads that log for the first time at once build each
362
+ # class's mutex one at a time and share it.
363
+ MUTEX_INIT = Mutex.new # @private
364
+ private_constant :MUTEX_INIT
365
+
361
366
  def self.mutex
362
- @_mutex ||= Mutex.new
367
+ (@_mutex if defined?(@_mutex)) || MUTEX_INIT.synchronize { @_mutex ||= Mutex.new }
363
368
  end
364
369
 
365
370
  def self.logger
@@ -20,7 +20,7 @@ module Padrino
20
20
  return if compiled?
21
21
  @routes.each_with_index do |route, index|
22
22
  route.index = index
23
- route.regexp = /\A(?<_#{index}>#{route.matcher.to_regexp})\z/
23
+ route.regexp = /\A(?:#{route.matcher.to_regexp})\z/
24
24
  end
25
25
  index_by_prefix!
26
26
  @compiled = true
@@ -69,6 +69,17 @@ module Padrino
69
69
  rotation { |offset| match?(offset, pattern) }
70
70
  end
71
71
 
72
+ ##
73
+ # Finds the first route that matches PATH_INFO, and returns it with its params, read from
74
+ # the path as the route matched it. Returns nil when no route matches.
75
+ #
76
+ def recognize_path(path_info)
77
+ compile! unless compiled?
78
+ pattern = decode_pattern(path_info)
79
+ route = match?(0, pattern)
80
+ [route, route.params_for(pattern, {})] if route
81
+ end
82
+
72
83
  private
73
84
 
74
85
  ##
@@ -92,13 +103,20 @@ module Padrino
92
103
  route = routes[candidates[at]]
93
104
  regexp = route.regexp
94
105
  return route if regexp.match?(path) || (trimmed && regexp.match?(trimmed))
106
+ rescue Encoding::CompatibilityError
107
+ # A Regexp fixed to another encoding (/n with non-ASCII bytes) matches no path it can't
108
+ # read, as in Sinatra (see Routing.path_match?). The same holds in the loop below.
95
109
  end
96
110
  return
97
111
  end
98
112
  while (route = routes[offset])
99
113
  regexp = route.regexp
100
- # A route added after compiling has no regexp and matches nothing.
101
- return route if regexp && (regexp.match?(path) || (trimmed && regexp.match?(trimmed)))
114
+ begin
115
+ # A route added after compiling has no regexp and matches nothing.
116
+ return route if regexp && (regexp.match?(path) || (trimmed && regexp.match?(trimmed)))
117
+ rescue Encoding::CompatibilityError
118
+ nil
119
+ end
102
120
  offset += 1
103
121
  end
104
122
  end
@@ -26,6 +26,9 @@ module Padrino
26
26
  elsif pattern != '/' && pattern.end_with?('/')
27
27
  whole_regexp.match(pattern[0..-2])
28
28
  end
29
+ rescue Encoding::CompatibilityError
30
+ # A Regexp fixed to another encoding doesn't match (see Routing.path_match?).
31
+ nil
29
32
  end
30
33
 
31
34
  # The handler, anchored to the whole path as PathRouter::Compiler matches routes
@@ -37,7 +37,7 @@ module Padrino
37
37
  ##
38
38
  # The accessors will be used in other classes
39
39
  #
40
- attr_accessor :action, :cache, :cache_key, :cache_expires,
40
+ attr_accessor :action, :cache, :cache_key, :cache_expires, :cache_lookup,
41
41
  :parent, :use_layout, :controller, :host, :user_agent, :path_for_generation, :default_values,
42
42
  :provided_formats
43
43
 
@@ -79,12 +79,12 @@ module Padrino
79
79
  end
80
80
 
81
81
  ##
82
- # Recognizes route and expanded params from a path.
82
+ # Recognizes route and expanded params from a path, or returns nil when no route matches it.
83
83
  #
84
84
  def recognize_path(path_info)
85
85
  prepare! unless prepared?
86
- route = @engine.find_by_pattern(path_info).first
87
- [route.name, route.params_for(path_info, {})]
86
+ route, params = @engine.recognize_path(path_info)
87
+ [route.name, params] if route
88
88
  end
89
89
 
90
90
  ##
@@ -23,8 +23,8 @@ module Padrino
23
23
  reset_concerns(name)
24
24
  @old_entries ||= {}
25
25
  @old_entries[name] = {
26
- constants: object_classes,
27
- features: old_features = Set.new($LOADED_FEATURES.dup)
26
+ constants: all_classes,
27
+ features: old_features = $LOADED_FEATURES.dup
28
28
  }
29
29
  features = file && file[:features] || []
30
30
  features.each { |feature| Reloader.safe_load(feature, force: true) }
@@ -34,7 +34,7 @@ module Padrino
34
34
  def commit(name)
35
35
  entry = {
36
36
  constants: new_classes(@old_entries[name][:constants]),
37
- features: Set.new($LOADED_FEATURES) - @old_entries[name][:features] - [name]
37
+ features: Set.new($LOADED_FEATURES - @old_entries[name][:features] - [name])
38
38
  }
39
39
  files[name] = entry
40
40
  @old_entries.delete(name)
@@ -68,40 +68,73 @@ module Padrino
68
68
 
69
69
  ObjectSpace.each_object(::Module) do |mod|
70
70
  # Module#=== doesn't call mod, which can be a DeprecatedConstantProxy that warns on any call.
71
- next unless ::ActiveSupport::Concern === mod && mod.name
72
- next unless Object.const_source_location(mod.name)&.first == file
73
- Reloader.reset_concern(mod) if Object.const_get(mod.name).equal?(mod)
71
+ next unless ::ActiveSupport::Concern === mod && (name = Reloader.send(:constant_name, mod))
72
+ next unless Object.const_source_location(name)&.first == file
73
+ Reloader.reset_concern(mod) if Object.const_get(name).equal?(mod)
74
74
  rescue NameError
75
75
  next
76
76
  end
77
77
  end
78
78
 
79
79
  ##
80
- # Returns all the classes in the object space.
80
+ # Returns the classes in the object space (without singleton classes when it walks the class tree).
81
81
  #
82
- def object_classes
83
- klasses = Set.new
84
-
85
- ObjectSpace.each_object(::Class).each do |klass|
86
- if block_given?
87
- if (filtered_class = yield(klass))
88
- klasses << filtered_class
89
- end
90
- else
91
- klasses << klass
82
+ def object_classes(&block)
83
+ klasses = all_classes
84
+ klasses = klasses.filter_map(&block) if block
85
+ Set.new(klasses)
86
+ end
87
+
88
+ ##
89
+ # Follows a method past the versions that a library prepended or wrote in Ruby, to the
90
+ # one Class defines natively, or nil when there is none.
91
+ #
92
+ def native_class_method(method)
93
+ method = method.super_method while method && (method.owner != ::Class || method.source_location)
94
+ method
95
+ end
96
+
97
+ # Ruby's own Class#subclasses (Ruby 3.1+), so a version that ActiveSupport prepends to hide
98
+ # reloaded classes is bypassed. CRuby only: other engines keep the heap scan until their
99
+ # subclass lists are known to be complete.
100
+ SUBCLASSES = if RUBY_ENGINE == 'ruby' && ::Class.method_defined?(:subclasses)
101
+ native_class_method(::Class.instance_method(:subclasses))
102
+ end
103
+ private_constant :SUBCLASSES
104
+
105
+ ##
106
+ # Scans the heap for classes, where the class tree walk isn't used.
107
+ #
108
+ def heap_classes
109
+ ObjectSpace.each_object(::Class).to_a
110
+ end
111
+
112
+ if SUBCLASSES
113
+ ##
114
+ # Walks the class tree down from BasicObject, which is several times faster than
115
+ # scanning the heap. It leaves out singleton classes, which have no constant to remove.
116
+ #
117
+ def all_classes
118
+ classes = [::BasicObject]
119
+ index = 0
120
+ while (klass = classes[index])
121
+ classes.concat(SUBCLASSES.bind_call(klass))
122
+ index += 1
92
123
  end
124
+ classes
93
125
  end
94
-
95
- klasses
126
+ else
127
+ alias all_classes heap_classes
96
128
  end
97
129
 
98
130
  ##
99
131
  # Returns a list of object space classes that are not included in "snapshot".
100
132
  #
101
133
  def new_classes(snapshot)
102
- object_classes do |klass|
103
- snapshot.include?(klass) ? nil : klass
104
- end
134
+ classes = all_classes
135
+ # The snapshot keeps its classes alive, so the same count means no class is new.
136
+ return Set.new if classes.size == snapshot.size
137
+ Set.new(classes - snapshot.to_a)
105
138
  end
106
139
  end
107
140
  end
@@ -142,13 +142,14 @@ module Padrino
142
142
  # Removes the specified class and constant.
143
143
  #
144
144
  def remove_constant(const)
145
+ name = constant_name(const) || return
145
146
  return if constant_excluded?(const)
146
- base, _, object = const.to_s.rpartition('::')
147
+ base, _, object = name.rpartition('::')
147
148
  base = base.empty? ? Object : Inflections.constantize(base)
148
149
  return if base.autoload?(object, false) || !base.const_defined?(object, false)
149
150
  return unless base.const_get(object, false).equal?(const)
150
151
  base.send :remove_const, object
151
- logger.devel "Removed constant #{const} from #{base}"
152
+ logger.devel "Removed constant #{name} from #{constant_name(base)}"
152
153
  rescue NameError
153
154
  # no const to remove
154
155
  end
@@ -162,7 +163,7 @@ module Padrino
162
163
  %i[@_included_block @_prepended_block].each do |ivar|
163
164
  concern.remove_instance_variable(ivar) if concern.instance_variable_defined?(ivar)
164
165
  end
165
- logger.devel "Reset concern #{concern}"
166
+ logger.devel "Reset concern #{constant_name(concern)}"
166
167
  end
167
168
 
168
169
  ##
@@ -291,7 +292,7 @@ module Padrino
291
292
  def rotation
292
293
  files_for_rotation.each do |file|
293
294
  file = File.expand_path(file)
294
- next if Reloader.exclude.any? { |base| file.start_with?(base) } || !File.file?(file)
295
+ next if path_excluded?(file) || !File.file?(file)
295
296
  yield file
296
297
  end
297
298
  nil
@@ -317,7 +318,21 @@ module Padrino
317
318
  # Tells if a feature should be excluded from Reloader tracking.
318
319
  #
319
320
  def feature_excluded?(file)
320
- !file.start_with?(Padrino.root) || exclude.any? { |excluded_path| file.start_with?(excluded_path) }
321
+ !path_inside?(file, Padrino.root) || path_excluded?(file)
322
+ end
323
+
324
+ ##
325
+ # Tells if the file is in one of the excluded paths, or is one of them.
326
+ #
327
+ def path_excluded?(file)
328
+ exclude.any? { |excluded_path| path_inside?(file, excluded_path) }
329
+ end
330
+
331
+ ##
332
+ # Tells if the file is the path itself or inside it, so /app-gem isn't inside /app.
333
+ #
334
+ def path_inside?(file, path)
335
+ file == path || file.start_with?(File.join(path, ''))
321
336
  end
322
337
 
323
338
  ##
@@ -333,34 +348,60 @@ module Padrino
333
348
  end
334
349
 
335
350
  ##
336
- # Tells if a constant is defined only outside of Padrino project path.
337
- # If a constant has any methods defined inside of the project path it's
338
- # considered internal and will be included in further testing.
339
- # A constant without methods (an exception class, say) is judged by
340
- # where its name was defined instead.
351
+ # Tells if a constant belongs to code the reloader doesn't track, so it's kept on reload.
352
+ # Tracked code is in Padrino.root, outside the excluded paths or in an excluded file
353
+ # that was run with +load+ (which runs it again, so its classes are removed).
354
+ #
355
+ # A constant whose name was first defined in untracked code is kept, even when tracked
356
+ # files add methods to it: the untracked file isn't run again, so a removed class would
357
+ # come back without its methods. A tracked file that reopens it runs again on reload,
358
+ # so its edited and added methods apply, but the methods it deletes or renames stay
359
+ # until the app restarts. Ruby records only the first definition of a name, so a class
360
+ # that a tracked file defines before the untracked file that owns it counts as tracked,
361
+ # and a name first defined by eval without a file name, or from a relative path, counts
362
+ # as untracked wherever it runs. A kept class keeps its superclass, so a project file that
363
+ # gives it one that was reloaded raises a superclass mismatch until the app restarts.
364
+ #
365
+ # Otherwise a constant with a method that has no Ruby source (Struct and Data members,
366
+ # an alias of a built-in method) is removed, unless its name has no Ruby source line
367
+ # (C or Java code defined it).
368
+ # Any other constant is kept only when it has methods and all of them were defined in
369
+ # untracked code.
341
370
  #
342
371
  def external_constant?(const)
372
+ file, line = constant_location(const)
373
+ return true if file && untracked_source?(file)
343
374
  sources = object_sources(const)
344
- sources = [constant_source(const)].compact if sources.empty?
345
- # consider methodless constants of unknown origin not external
375
+ # C extensions define their constants at line 0 of the library file.
376
+ return line.to_i < 1 if sources.delete?(nil)
377
+ # consider methodless constants named in tracked code, or of unknown origin, not external
346
378
  return false if sources.empty?
347
- sources.none? { |source| source.start_with?(Padrino.root) }
379
+ sources.all? { |source| untracked_source?(source) }
348
380
  end
349
381
 
350
382
  ##
351
- # Returns the file in which the constant's name was defined, if known.
383
+ # Tells if a file holds code the reloader doesn't track: it's outside Padrino.root,
384
+ # or in an excluded path and was required (a file run with +load+ runs again).
352
385
  #
353
- def constant_source(const)
386
+ def untracked_source?(file)
387
+ !path_inside?(file, Padrino.root) || (path_excluded?(file) && $LOADED_FEATURES.include?(file))
388
+ end
389
+
390
+ ##
391
+ # Returns the file and line where the constant's name was defined, if known.
392
+ #
393
+ def constant_location(const)
354
394
  base, _, object = constant_name(const).to_s.rpartition('::')
355
395
  return if object.empty?
356
396
  base = base.empty? ? Object : Inflections.constantize(base)
357
- base.const_source_location(object)&.first
358
- rescue NameError
397
+ base.const_source_location(object)
398
+ rescue NameError, ArgumentError # JRuby's Java packages raise ArgumentError
359
399
  nil
360
400
  end
361
401
 
362
402
  ##
363
- # Gets all the sources in which target class is defined.
403
+ # Gets all the sources in which target class is defined, with nil for
404
+ # a method that has no Ruby source.
364
405
  #
365
406
  # Note: Method#source_location is for Ruby 1.9.3+ only.
366
407
  #
@@ -370,14 +411,14 @@ module Padrino
370
411
  next unless method_name.is_a?(Symbol)
371
412
 
372
413
  method_object = target.method(method_name)
373
- sources << method_object.source_location.first if method_object.owner == target.singleton_class
414
+ sources << method_object.source_location&.first if method_object.owner == target.singleton_class
374
415
  end
375
416
 
376
417
  target.instance_methods.each do |method_name|
377
418
  next unless method_name.is_a?(Symbol)
378
419
 
379
420
  method_object = target.instance_method(method_name)
380
- sources << method_object.source_location.first if method_object.owner == target
421
+ sources << method_object.source_location&.first if method_object.owner == target
381
422
  end
382
423
  sources
383
424
  end
@@ -395,7 +436,7 @@ module Padrino
395
436
  #
396
437
  def reloadable_apps
397
438
  Padrino.mounted_apps.select do |app|
398
- next unless app.app_file.start_with?(Padrino.root)
439
+ next unless path_inside?(app.app_file, Padrino.root)
399
440
  app.app_obj.respond_to?(:reload) && app.app_obj.reload?
400
441
  end
401
442
  end
@@ -6,7 +6,7 @@
6
6
  #
7
7
  module Padrino
8
8
  # The version constant for the current version of Padrino.
9
- VERSION = '0.18.6' unless defined?(Padrino::VERSION)
9
+ VERSION = '0.18.7' unless defined?(Padrino::VERSION)
10
10
 
11
11
  #
12
12
  # The current Padrino version.
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class BrandedItem
4
+ include Branded
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Branded
4
+ extend ActiveSupport::Concern
5
+
6
+ def self.name
7
+ 'Brand'
8
+ end
9
+
10
+ included do
11
+ define_method(:brand) { 'b1' }
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ PADRINO_ROOT = __dir__ unless defined? PADRINO_ROOT
2
+
3
+ class SystemExcludedConstantsDemo < Padrino::Application
4
+ set :reload, true
5
+ end
6
+
7
+ Padrino.load!
@@ -0,0 +1,7 @@
1
+ require_relative '../support/excluded_constants_settings'
2
+
3
+ class ExcludedConstantsAppSettings
4
+ def self.value
5
+ 1
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ class ExcludedConstantsSettings
2
+ def self.value
3
+ 1
4
+ end
5
+ end
6
+
7
+ class ExcludedConstantsError < StandardError
8
+ end
@@ -0,0 +1,7 @@
1
+ PADRINO_ROOT = __dir__ unless defined? PADRINO_ROOT
2
+
3
+ class SystemLoadedExcludedDemo < Padrino::Application
4
+ set :reload, true
5
+ end
6
+
7
+ Padrino.load!
@@ -0,0 +1,7 @@
1
+ class LoadedExcludedBase
2
+ def self.value
3
+ 1
4
+ end
5
+ end
6
+
7
+ load File.expand_path('../support/loaded_excluded_classes.rb', __dir__)
@@ -0,0 +1,15 @@
1
+ class LoadedExcludedThing < LoadedExcludedBase
2
+ def self.other_value
3
+ 2
4
+ end
5
+ end
6
+
7
+ class LoadedExcludedPoint < Struct.new(:x, :y)
8
+ def sum
9
+ x + y
10
+ end
11
+ end
12
+
13
+ class LoadedExcludedLabel
14
+ alias label inspect
15
+ end
@@ -0,0 +1,2 @@
1
+ class DbadminChild < DbadminParent
2
+ end
@@ -0,0 +1,2 @@
1
+ class DbadminParent
2
+ end
@@ -200,6 +200,19 @@ describe 'Application' do
200
200
  assert_empty seen
201
201
  end
202
202
 
203
+ it 'should stay on for a non-ASCII path a Regexp fixed to another encoding can not read' do
204
+ mock_app do
205
+ enable :sessions
206
+ set :protect_from_csrf, except: [%r{\A/caf\xC3\xA9}n, %r{\A/api/}]
207
+ post('/:name') { 'name' }
208
+ post('/api/:id') { 'api' }
209
+ end
210
+ post '/caf%C3%A9'
211
+ assert_equal 403, status
212
+ post '/api/x'
213
+ assert_equal 200, status
214
+ end
215
+
203
216
  it 'should not raise without a path' do
204
217
  env = Rack::MockRequest.env_for('/')
205
218
  env.delete('PATH_INFO')