Skip to content

Commit

Permalink
update URLMap Regexp usage for Ruby v3.3 (#3165)
Browse files Browse the repository at this point in the history
* update URLMap Regexp usage for Ruby v3.3

Ruby v3.3.0-preview1 doesn't support passing 3 arguments to
`Regexp.new`, so update `URLMap` to use a 2 argument version instead.

* Add unit test for PR 3165

With a `Gemfile` that excludes `rack`, have Puma use its own `URLMap`
class to handle the mapping of the route defined in `config.ru`. Without
the fix provided by PR 3165, this new test will time out.

* test_url_map.rb - run single

* test/url_map_test/config.ru - check whether Rack::URLMap is defined

Just in case...

---------

Co-authored-by: MSP-Greg <Greg.mpls@gmail.com>
  • Loading branch information
fallwith and MSP-Greg committed Jun 6, 2023
1 parent 763d1a1 commit 188f5da
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/puma/rack/urlmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def remap(map)
end

location = location.chomp('/')
match = Regexp.new("^#{Regexp.quote(location).gsub('/', '/+')}(.*)", nil, 'n')
match = Regexp.new("^#{Regexp.quote(location).gsub('/', '/+')}(.*)", Regexp::NOENCODING)

[host, location, match, app]
}.sort_by do |(host, location, _, _)|
Expand Down
24 changes: 24 additions & 0 deletions test/test_url_map.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require_relative "helper"
require_relative "helpers/integration"

class TestURLMap < TestIntegration

def teardown
return if skipped?
super
end

# make sure the mapping defined in url_map_test/config.ru works
def test_basic_url_mapping
skip_if :jruby
env = { "BUNDLE_GEMFILE" => "#{__dir__}/url_map_test/Gemfile" }
Dir.chdir("#{__dir__}/url_map_test") do
cli_server set_pumactl_args, env: env
end
connection = connect("/ok")
# Puma 6.2.2 and below will time out here with Ruby v3.3
# see https://github.com/puma/puma/pull/3165
body = read_body(connection, 1)
assert_equal("OK", body)
end
end
1 change: 1 addition & 0 deletions test/url_map_test/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gem 'puma', path: '../..'
9 changes: 9 additions & 0 deletions test/url_map_test/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
map "/ok" do
run ->(env) {
if Object.const_defined?(:Rack) && ::Rack.const_defined?(:URLMap)
[200, {}, ["::Rack::URLMap is loaded"]]
else
[200, {}, ["OK"]]
end
}
end

0 comments on commit 188f5da

Please sign in to comment.