|
@@ -1,5 +1,6 @@
|
|
|
require 'jsonpath'
|
|
|
require 'cgi'
|
|
|
+require 'addressable/uri'
|
|
|
|
|
|
module Utils
|
|
|
def self.unindent(s)
|
|
@@ -25,11 +26,29 @@ module Utils
|
|
|
begin
|
|
|
URI(uri)
|
|
|
rescue URI::Error
|
|
|
- URI(uri.to_s.gsub(/[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]+/) { |unsafe|
|
|
|
- unsafe.bytes.each_with_object(String.new) { |uc, s|
|
|
|
- s << sprintf('%%%02X', uc)
|
|
|
- }
|
|
|
- }.force_encoding(Encoding::US_ASCII))
|
|
|
+ begin
|
|
|
+ URI(uri.to_s.gsub(/[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]+/) { |unsafe|
|
|
|
+ unsafe.bytes.each_with_object(String.new) { |uc, s|
|
|
|
+ s << sprintf('%%%02X', uc)
|
|
|
+ }
|
|
|
+ }.force_encoding(Encoding::US_ASCII))
|
|
|
+ rescue URI::Error => e
|
|
|
+ begin
|
|
|
+ auri = Addressable::URI.parse(uri.to_s)
|
|
|
+ rescue
|
|
|
+ # Do not leak Addressable::URI::InvalidURIError which
|
|
|
+ # callers might not expect.
|
|
|
+ raise e
|
|
|
+ else
|
|
|
+ # Addressable::URI#normalize! modifies the query and
|
|
|
+ # fragment components beyond escaping unsafe characters, so
|
|
|
+ # avoid using it. Otherwise `?a[]=%2F` would be normalized
|
|
|
+ # as `?a%5B%5D=/`, for example.
|
|
|
+ auri.site = auri.normalized_site
|
|
|
+ auri.path = auri.normalized_path
|
|
|
+ URI(auri.to_s)
|
|
|
+ end
|
|
|
+ end
|
|
|
end
|
|
|
end
|
|
|
|