hanami-router 2.3.0 → 2.3.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5bc038728052f6719d7a3c7ecfbfbb02a5b5d730ab95bafdb1a8415602faa861
4
- data.tar.gz: 961ad5a29c273a42526261c5305d41dc21530c16baf20ed01c106ab7c8b635ff
3
+ metadata.gz: 1f02aa739470dd4d899daf0b24a799e136b289cae1a29a0dcffe9a23ff058194
4
+ data.tar.gz: f03429f9ad782f1f231a9c0dee4d99ad976be9dfe2c628259b989b9f4a46d660
5
5
  SHA512:
6
- metadata.gz: 723502c7e0e4dd4beba5de8a1c3e77dd8e912103f833dc709f7c8f001702a54b93128222768721b4391ce4ee3e1a7d951046bdcb67132b406d6582159394bdd6
7
- data.tar.gz: 646f78f7b642bffc4ff8be39e6c7fff011ade92ee8154651d66aafe1b803de0e4901151a01a962af6832c3ace23d4f27167186cdac26de1a29cc3453f02b2ca2
6
+ metadata.gz: 75a2139a9afd3d62267181914bfdfbdffe54c5db6ac8fc8ab7a1079cd215655d878c7328fb43ebcb2424af4a923ab7e26fed4ec9feed84c4d70ba3f7bfc02948
7
+ data.tar.gz: c6271704409a32bab1e7ecc45f7c670d3dbb60484722d4a609f6ceec38ee977346ade97055f866d2d2b679899b290aa00ebaeab743f87476acaba69263875dcf
data/CHANGELOG.md CHANGED
@@ -1,6 +1,60 @@
1
1
  # Hanami::Router
2
2
 
3
- Rack compatible HTTP router for Ruby
3
+ Rack compatible HTTP router for Ruby.
4
+
5
+ ## [Unreleased]
6
+
7
+ ### Added
8
+
9
+ ### Changed
10
+
11
+ ### Deprecated
12
+
13
+ ### Removed
14
+
15
+ ### Fixed
16
+
17
+ ### Security
18
+
19
+ [Unreleased]: http://github.com/hanami/hanami-router/compare/v2.3.2...main
20
+
21
+ ## [2.3.2] - 2026-12-02
22
+
23
+ ### Fixed
24
+
25
+ - Fix a memory leak where the router kept every request path it received in memory. (@aaronmallen in #312)
26
+
27
+ ### Security
28
+
29
+ - Apply a scope's middleware to all of its paths when it contains a nested scope or slice with its own middleware. Previously, paths in the outer scope that weren't in the nested scope skipped the outer scope's middleware, such as authentication. (@parndt in GHSA-73j7-6jvw-2v7g)
30
+ - Stop concurrent requests to the same route from receiving each other's path params. Matching a route stored the captured params on the route itself, so a thread switch between matching and reading them could give one request another's params. (@parndt in GHSA-wgjv-fq27-94m7)
31
+
32
+ [2.3.2]: http://github.com/hanami/hanami-router/compare/v2.3.1...v2.3.2
33
+
34
+ ## [2.3.1] - 2025-12-17
35
+
36
+ ### Changed
37
+
38
+ - Require Rack version 2.2.16 or higher, which is the earliest version of Rack 2.x that will run on Ruby 4.0 as well as our currently supported Ruby versions (3.2, 3.3, 3.4). (@cllns in 2b15582)
39
+
40
+ ### Fixed
41
+
42
+ - Set correct Rack `"SCRIPT_NAME"` value for mounts under dynamic prefixes. (@timriley in #294).
43
+
44
+ For example, given a mount under `scope '/stations/:station_id'`, the "SCRIPT_NAME" will be e.g. `"/stations/42"` rather than `"/stations/:station_id"`.
45
+ - For a mount with a prefix, allow the Rack `"PATH_INFO"` value to remain an empty string when a request is made for that exact prefix only. (@timriley in #295)
46
+
47
+ Given the following:
48
+
49
+ ```ruby
50
+ mount ->(env) { [200, {}, [env["PATH_INFO"]] }, at: "/settings"
51
+ ```
52
+
53
+ When a request is made to `"/settings"`, the `"SCRIPT_NAME"` will be `"/settings"` and `"PATH_INFO"` will be `""`. This ensures that the Rack environment can be used to reconstruct the same path as used for the original request.
54
+
55
+ To ensure a mounted router instance can still route to its root, treat `""` the same as `"/"` for the purposes of matching routes.
56
+
57
+ [2.3.1]: http://github.com/hanami/hanami-router/compare/v2.3.0...v2.3.1
4
58
 
5
59
  ## v2.3.0 - 2025-11-12
6
60
 
@@ -17,18 +17,17 @@ Gem::Specification.new do |spec|
17
17
  spec.files = `git ls-files -- lib/* CHANGELOG.md LICENSE.md README.md hanami-router.gemspec`.split($/)
18
18
  spec.executables = []
19
19
  spec.require_paths = ["lib"]
20
- spec.metadata["rubygems_mfa_required"] = "true"
21
20
  spec.required_ruby_version = ">= 3.2"
22
21
 
23
- spec.add_dependency "rack", ">= 2.0"
22
+ spec.add_dependency "rack", ">= 2.2.16"
24
23
  spec.add_dependency "mustermann", "~> 3.0"
25
24
  spec.add_dependency "mustermann-contrib", "~> 3.0"
26
25
  spec.add_dependency "csv", "~> 3.3"
27
26
 
28
- spec.add_development_dependency "bundler", ">= 1.6", "< 3"
29
27
  spec.add_development_dependency "rake", "~> 13"
30
28
  spec.add_development_dependency "rack-test", "~> 2.0"
31
29
  spec.add_development_dependency "rspec", "~> 3.8"
30
+ spec.add_development_dependency "ostruct" # Remove once we drop support for Rack 2
32
31
 
33
32
  spec.add_development_dependency "rubocop", "~> 1.0"
34
33
  spec.add_development_dependency "rubocop-performance", "~> 1.0"
@@ -60,7 +60,7 @@ module Hanami
60
60
  parsers.each_with_object(registry) do |spec, memo|
61
61
  if spec.is_a?(Hash) && spec.size > 1
62
62
  spec.each do |key, value|
63
- build_parsers([key => [value]], memo)
63
+ build_parsers([{key => [value]}], memo)
64
64
  end
65
65
  else
66
66
  name, *media_types = Array(*spec).flatten(0)
@@ -59,7 +59,7 @@ module Hanami
59
59
  # raise Hanami::Middleware::BodyParser::BodyParsingError.new(exception.message)
60
60
  # end
61
61
  # end
62
- def parse(body, env = {}) # rubocop:disable Lint/UnusedMethodArgument
62
+ def parse(body, env = {})
63
63
  raise NoMethodError
64
64
  end
65
65
  end
@@ -38,16 +38,19 @@ module Hanami
38
38
  # @since 2.0.0
39
39
  def find(path)
40
40
  node = @root
41
+ app = @root.app
41
42
 
43
+ # Track the deepest scope with an app on the way down. When the descent stops early (a
44
+ # segment with no matching child under a node that has children), the request is still
45
+ # within that scope, so its middleware must apply.
42
46
  for_each_segment(path) do |segment|
47
+ node = node.get(segment)
43
48
  break unless node
44
49
 
45
- node = node.get(segment)
50
+ app = node.app if node.app?
46
51
  end
47
52
 
48
- return node.app if node&.app?
49
-
50
- @root.app || @app
53
+ app || @app
51
54
  end
52
55
 
53
56
  # @api private
@@ -9,24 +9,24 @@ module Hanami
9
9
  #
10
10
  # @api private
11
11
  # @since 2.2.0
12
- attr_reader :to, :params, :matcher
12
+ attr_reader :to, :matcher
13
13
 
14
14
  # @api private
15
15
  # @since 2.2.0
16
16
  def initialize(route, to, constraints)
17
17
  @to = to
18
- @params = nil
19
18
  @matcher = Mustermann.new(route, type: :rails, version: "5.0", capture: constraints)
20
19
  end
21
20
 
21
+ # Returns the captured params for the given path, or nil if the path doesn't match.
22
+ #
23
+ # Leaves are shared across concurrent requests, so the captures are returned rather than
24
+ # stored on the leaf.
25
+ #
22
26
  # @api private
23
27
  # @since 2.2.0
24
28
  def match(path)
25
- match = matcher.match(path)
26
-
27
- @params = match.named_captures if match
28
-
29
- match
29
+ matcher.match(path)&.named_captures
30
30
  end
31
31
  end
32
32
  end
@@ -14,9 +14,12 @@ module Hanami
14
14
  if @prefix.to_s == "/"
15
15
  env[::Rack::SCRIPT_NAME] = EMPTY_STRING
16
16
  else
17
- env[::Rack::SCRIPT_NAME] = env[::Rack::SCRIPT_NAME].to_s + @prefix.to_s
18
- env[::Rack::PATH_INFO] = env[::Rack::PATH_INFO].sub(@prefix.to_s, EMPTY_STRING)
19
- env[::Rack::PATH_INFO] = DEFAULT_PREFIX if env[::Rack::PATH_INFO] == EMPTY_STRING
17
+ # To set SCRIPT_NAME, use the actual matched portion of the path, not the prefix string
18
+ # itself. This is important for prefixes with dynamic segments like "/stations/:id". In
19
+ # this case, we want e.g. "/stations/42" as SCRIPT_NAME, not "/stations/:id".
20
+ matched_path = match.to_s
21
+ env[::Rack::SCRIPT_NAME] = env[::Rack::SCRIPT_NAME].to_s + matched_path
22
+ env[::Rack::PATH_INFO] = env[::Rack::PATH_INFO].sub(matched_path, EMPTY_STRING)
20
23
  end
21
24
 
22
25
  [@app, match.named_captures]
@@ -45,10 +45,17 @@ module Hanami
45
45
  @leaves << Leaf.new(route, to, constraints)
46
46
  end
47
47
 
48
+ # Returns the endpoint and captured params of the first leaf matching the given path, or nil.
49
+ #
48
50
  # @api private
49
51
  # @since 2.2.0
50
52
  def match(path)
51
- @leaves&.find { |leaf| leaf.match(path) }
53
+ @leaves&.each do |leaf|
54
+ params = leaf.match(path)
55
+ return [leaf.to, params] if params
56
+ end
57
+
58
+ nil
52
59
  end
53
60
 
54
61
  private
@@ -63,7 +63,7 @@ module Hanami
63
63
 
64
64
  # @api private
65
65
  # @since 2.0.0
66
- def initialize(http_method:, path:, to:, as: nil, constraints: {}, blk: nil) # rubocop:disable Metrics/ParameterLists
66
+ def initialize(http_method:, path:, to:, as: nil, constraints: {}, blk: nil)
67
67
  @http_method = http_method
68
68
  @path = path
69
69
  @to = to
@@ -17,7 +17,6 @@ module Hanami
17
17
  # @since 2.0.0
18
18
  def initialize
19
19
  @root = Node.new
20
- @segments_map = {}
21
20
  end
22
21
 
23
22
  # @api private
@@ -41,7 +40,7 @@ module Hanami
41
40
 
42
41
  return unless segments.all? { |segment| node = node.get(segment) }
43
42
 
44
- node.match(path)&.then { |found| [found.to, found.params] }
43
+ node.match(path)
45
44
  end
46
45
 
47
46
  private
@@ -54,12 +53,8 @@ module Hanami
54
53
  # @api private
55
54
  # @since 2.2.0
56
55
  def segments_from(path)
57
- if @segments_map[path]
58
- @segments_map[path]
59
- else
60
- _, *segments = path.split(SEGMENT_SEPARATOR)
61
- @segments_map[path] = segments
62
- end
56
+ _, *segments = path.split(SEGMENT_SEPARATOR)
57
+ segments
63
58
  end
64
59
  end
65
60
  end
@@ -8,6 +8,6 @@ module Hanami
8
8
  #
9
9
  # @since 0.1.0
10
10
  # @api public
11
- VERSION = "2.3.0"
11
+ VERSION = "2.3.2"
12
12
  end
13
13
  end
data/lib/hanami/router.rb CHANGED
@@ -642,7 +642,12 @@ module Hanami
642
642
  # @since 2.0.0
643
643
  # @api private
644
644
  def fixed(env)
645
- @fixed.dig(env[::Rack::REQUEST_METHOD], env[::Rack::PATH_INFO])
645
+ path_info = env[::Rack::PATH_INFO]
646
+ # Treat empty PATH_INFO as "/" for route matching. This allows root routes (defined as "/") to
647
+ # match the empty PATH_INFO that is set for requests to a mount without a trailing slash.
648
+ path_info = DEFAULT_PREFIX if path_info == EMPTY_STRING
649
+
650
+ @fixed.dig(env[::Rack::REQUEST_METHOD], path_info)
646
651
  end
647
652
 
648
653
  # @since 2.0.0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-router
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hanakai team
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: '2.0'
18
+ version: 2.2.16
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
- version: '2.0'
25
+ version: 2.2.16
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: mustermann
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -65,26 +65,6 @@ dependencies:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
67
  version: '3.3'
68
- - !ruby/object:Gem::Dependency
69
- name: bundler
70
- requirement: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - ">="
73
- - !ruby/object:Gem::Version
74
- version: '1.6'
75
- - - "<"
76
- - !ruby/object:Gem::Version
77
- version: '3'
78
- type: :development
79
- prerelease: false
80
- version_requirements: !ruby/object:Gem::Requirement
81
- requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- version: '1.6'
85
- - - "<"
86
- - !ruby/object:Gem::Version
87
- version: '3'
88
68
  - !ruby/object:Gem::Dependency
89
69
  name: rake
90
70
  requirement: !ruby/object:Gem::Requirement
@@ -127,6 +107,20 @@ dependencies:
127
107
  - - "~>"
128
108
  - !ruby/object:Gem::Version
129
109
  version: '3.8'
110
+ - !ruby/object:Gem::Dependency
111
+ name: ostruct
112
+ requirement: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ type: :development
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
130
124
  - !ruby/object:Gem::Dependency
131
125
  name: rubocop
132
126
  requirement: !ruby/object:Gem::Requirement
@@ -200,8 +194,7 @@ files:
200
194
  homepage: http://hanamirb.org
201
195
  licenses:
202
196
  - MIT
203
- metadata:
204
- rubygems_mfa_required: 'true'
197
+ metadata: {}
205
198
  rdoc_options: []
206
199
  require_paths:
207
200
  - lib