Module: SafeURI
- Defined in:
- lib/safe_uri.rb,
lib/safe_uri/version.rb,
lib/safe_uri/open-uri.rb,
lib/safe_uri/exceptions.rb
Defined Under Namespace
Classes: SafeURIERROR, UnsupportedScheme
Constant Summary collapse
- VERSION =
'0.1.0'
Class Method Summary collapse
-
.open(url, *args, **options) ⇒ Object
This occurs as open-uri delegates its #open method to Kernel#open, while overriding Kernel#open.
- .parse_url(url) ⇒ Object
Class Method Details
.open(url, *args, **options) ⇒ Object
This occurs as open-uri delegates its #open method to Kernel#open, while overriding Kernel#open. #open below provides safer alternative to URI.open equivalent. raises exception when pipe charactor is specified (usually Errno::ENOENT)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/safe_uri/open-uri.rb', line 8 def self.open(url, *args, **) parsed_url = parse_url(url) if parsed_url.scheme if parsed_url.respond_to?(:open) parse_url(url).open(*args, **) else raise UnsupportedScheme.new, "scheme #{parsed_url.scheme} is not supported by open-uri" end else # avoid falling back to Kernel.open File.open(url, *args, **) end end |
.parse_url(url) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/safe_uri/open-uri.rb', line 23 def self.parse_url(url) # To percent-encode multi-byte charactors (that might be included as part of given url) # SafeURI relies on Addressable::URI#normalize that can handle proper encoding # while avoiding double-encoding when percent-encoded charactors already exists. normalized_url = Addressable::URI.parse(url).normalize.to_s URI.parse(normalized_url) end |