3 ms·
Use a standard URI parser to break this problem into smaller parts. Let a modern URI library worry about arcane details like spaces, fragments, userinfo, IPv6 h
by mdavidn 12y ago
Use a standard URI parser to break this problem into smaller parts. Let a modern URI library worry about arcane details like spaces, fragments, userinfo, IPv6 hosts, etc.
uri = URI.parse(target).normalize
uri.absolute? or raise 'URI not absolute'
%w[ http https ftp ].include?(uri.scheme) or raise 'Unsupported URI scheme'
# Etc
- droope 12y agoIt'd be interesting to look at URI.parse's source code.
- mdavidn 12y agoIt does match a regular expression internally. https://github.com/ruby/ruby/blob/trunk/lib/uri/rfc2396_parser.rb https://github.com/ruby/ruby/blob/trunk/lib/uri/rfc2396_pars...
- mathias 12y agoThat was not an option in this case, as the goal is to validate URLs entered as user input and blacklist certain URL constructs even though they’re technically valid.
- mdavidn 12y agoSo check if uri.host matches your blacklist.