Skip to content

Commit

Permalink
Fix compatiblity with newer uri gem
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Sep 6, 2024
1 parent 0487291 commit 384bf45
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
16 changes: 4 additions & 12 deletions lib/sprockets/legacy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,10 @@ def matches_filter(filters, logical_path, filename)
end
end

# URI.unescape is deprecated on 1.9. We need to use URI::Parser
# if its available.
if defined? URI::DEFAULT_PARSER
def unescape(str)
str = URI::DEFAULT_PARSER.unescape(str)
str.force_encoding(Encoding.default_internal) if Encoding.default_internal
str
end
else
def unescape(str)
URI.unescape(str)
end
def unescape(str)
URIUtils::URI_PARSER.unescape(str)
str.force_encoding(Encoding.default_internal) if Encoding.default_internal
str
end
end

Expand Down
10 changes: 6 additions & 4 deletions lib/sprockets/uri_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module Sprockets
module URIUtils
extend self

URI_PARSER = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::RFC2396_Parser.new

# Internal: Parse URI into component parts.
#
# uri - String uri
Expand All @@ -44,7 +46,7 @@ def join_uri(scheme, userinfo, host, port, registry, path, opaque, query, fragme
def split_file_uri(uri)
scheme, _, host, _, _, path, _, query, _ = URI.split(uri)

path = URI::Generic::DEFAULT_PARSER.unescape(path)
path = URI_PARSER.unescape(path)
path.force_encoding(Encoding::UTF_8)

# Hack for parsing Windows "file:///C:/Users/IEUser" paths
Expand All @@ -63,7 +65,7 @@ def join_file_uri(scheme, host, path, query)
str = "#{scheme}://"
str << host if host
path = "/#{path}" unless path.start_with?("/")
str << URI::Generic::DEFAULT_PARSER.escape(path)
str << URI_PARSER.escape(path)
str << "?#{query}" if query
str
end
Expand Down Expand Up @@ -162,7 +164,7 @@ def encode_uri_query_params(params)
when Integer
query << "#{key}=#{value}"
when String, Symbol
query << "#{key}=#{URI::Generic::DEFAULT_PARSER.escape(value.to_s)}"
query << "#{key}=#{URI_PARSER.escape(value.to_s)}"
when TrueClass
query << "#{key}"
when FalseClass, NilClass
Expand All @@ -182,7 +184,7 @@ def encode_uri_query_params(params)
def parse_uri_query_params(query)
query.to_s.split('&').reduce({}) do |h, p|
k, v = p.split('=', 2)
v = URI::Generic::DEFAULT_PARSER.unescape(v) if v
v = URI_PARSER.unescape(v) if v
h[k.to_sym] = v || true
h
end
Expand Down

0 comments on commit 384bf45

Please sign in to comment.