Skip to content

Commit

Permalink
Use URI::DEFAULT_PARSER rather than instantiate a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Jun 29, 2020
1 parent cf18812 commit 12f3f11
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/routing/mapper.rb
Expand Up @@ -1990,7 +1990,7 @@ def add_route(action, controller, options, _path, to, via, formatted, anchor, op
name_for_action(options.delete(:as), action)
end

path = Mapping.normalize_path URI.parser.escape(path), formatted
path = Mapping.normalize_path URI::DEFAULT_PARSER.escape(path), formatted
ast = Journey::Parser.parse path

mapping = Mapping.build(@scope, @set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, options)
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/routing/route_set.rb
Expand Up @@ -865,7 +865,7 @@ def recognize_path_with_request(req, path, extras, raise_on_missing: true)
params.each do |key, value|
if value.is_a?(String)
value = value.dup.force_encoding(Encoding::BINARY)
params[key] = URI.parser.unescape(value)
params[key] = URI::DEFAULT_PARSER.unescape(value)
end
end
req.path_parameters = params
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/parameter_encoding_test.rb
Expand Up @@ -44,7 +44,7 @@ class ParameterEncodingTest < ActionController::TestCase
end

test "does not raise an error when passed a param declared as ASCII-8BIT that contains invalid bytes" do
get :test_bar, params: { "bar" => URI.parser.escape("bar\xE2baz".b) }
get :test_bar, params: { "bar" => URI::DEFAULT_PARSER.escape("bar\xE2baz".b) }

assert_response :success
assert_equal "ASCII-8BIT", @response.body
Expand Down
4 changes: 2 additions & 2 deletions actionpack/test/controller/routing_test.rb
Expand Up @@ -2092,11 +2092,11 @@ def test_extras
end

def test_unicode_path
assert_equal({ controller: "news", action: "index" }, @routes.recognize_path(URI.parser.escape("こんにちは/世界"), method: :get))
assert_equal({ controller: "news", action: "index" }, @routes.recognize_path(URI::DEFAULT_PARSER.escape("こんにちは/世界"), method: :get))
end

def test_downcased_unicode_path
assert_equal({ controller: "news", action: "index" }, @routes.recognize_path(URI.parser.escape("こんにちは/世界").downcase, method: :get))
assert_equal({ controller: "news", action: "index" }, @routes.recognize_path(URI::DEFAULT_PARSER.escape("こんにちは/世界").downcase, method: :get))
end

private
Expand Down
4 changes: 2 additions & 2 deletions actionview/lib/action_view/helpers/url_helper.rb
Expand Up @@ -549,14 +549,14 @@ def current_page?(options, check_parameters: false)
return false unless request.get? || request.head?

check_parameters ||= options.is_a?(Hash) && options.delete(:check_parameters)
url_string = URI.parser.unescape(url_for(options)).force_encoding(Encoding::BINARY)
url_string = URI::DEFAULT_PARSER.unescape(url_for(options)).force_encoding(Encoding::BINARY)

# We ignore any extra parameters in the request_uri if the
# submitted URL doesn't have any either. This lets the function
# work with things like ?order=asc
# the behaviour can be disabled with check_parameters: true
request_uri = url_string.index("?") || check_parameters ? request.fullpath : request.path
request_uri = URI.parser.unescape(request_uri).force_encoding(Encoding::BINARY)
request_uri = URI::DEFAULT_PARSER.unescape(request_uri).force_encoding(Encoding::BINARY)

if url_string.start_with?("/") && url_string != "/"
url_string.chomp!("/")
Expand Down
5 changes: 5 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,8 @@
* `URI.parser` is deprecated and will be removed in Rails 6.2. Use
`URI::DEFAULT_PARSER` instead.

*Jean Boussier*

* `require_dependency` has been documented to be _obsolete_ in `:zeitwerk`
mode. The method is not deprecated as such (yet), but applications are
encouraged to not use it.
Expand Down
6 changes: 5 additions & 1 deletion activesupport/lib/active_support/core_ext/uri.rb
Expand Up @@ -19,7 +19,11 @@ def unescape(str, escaped = /%[a-fA-F\d]{2}/)
module URI
class << self
def parser
@parser ||= URI::Parser.new
ActiveSupport::Deprecation.warn(<<-MSG.squish)
URI.parser is deprecated and will be removed in Rails 6.2.
Use `URI::DEFAULT_PARSER` instead.
MSG
URI::DEFAULT_PARSER
end
end
end
2 changes: 1 addition & 1 deletion activesupport/test/core_ext/uri_ext_test.rb
Expand Up @@ -8,7 +8,7 @@ class URIExtTest < ActiveSupport::TestCase
def test_uri_decode_handle_multibyte
str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese.

parser = URI.parser
parser = URI::DEFAULT_PARSER
assert_equal str + str, parser.unescape(str + parser.escape(str).encode(Encoding::UTF_8))
end
end
2 changes: 1 addition & 1 deletion railties/lib/rails/info_controller.rb
Expand Up @@ -20,7 +20,7 @@ def properties

def routes
if path = params[:path]
path = URI.parser.escape path
path = URI::DEFAULT_PARSER.escape path
normalized_path = with_leading_slash path
render json: {
exact: match_route { |it| it.match normalized_path },
Expand Down
2 changes: 1 addition & 1 deletion railties/test/application/assets_test.rb
Expand Up @@ -325,7 +325,7 @@ class User < ActiveRecord::Base; raise 'should not be reached'; end
# Load app env
app "development"

get "/assets/#{URI.parser.escape(asset_path)}"
get "/assets/#{URI::DEFAULT_PARSER.escape(asset_path)}"
assert_match "not an image really", last_response.body
assert_file_exists("#{app_path}/public/assets/#{asset_path}")
end
Expand Down

0 comments on commit 12f3f11

Please sign in to comment.