1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Use URI::DEFAULT_PARSER rather than instantiate a new one

This commit is contained in:
Jean Boussier 2020-06-27 12:19:31 +02:00
parent cf1881237a
commit 12f3f11f61
10 changed files with 20 additions and 11 deletions

View file

@ -1990,7 +1990,7 @@ module ActionDispatch
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)

View file

@ -865,7 +865,7 @@ module ActionDispatch
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

View file

@ -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

View file

@ -2092,11 +2092,11 @@ class RackMountIntegrationTests < ActiveSupport::TestCase
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

View file

@ -549,14 +549,14 @@ module ActionView
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!("/")

View file

@ -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.

View file

@ -19,7 +19,11 @@ end
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

View file

@ -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

View file

@ -20,7 +20,7 @@ class Rails::InfoController < Rails::ApplicationController # :nodoc:
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 },

View file

@ -325,7 +325,7 @@ module ApplicationTests
# 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