From: Rob Biedenharn Date: 2008-01-24T22:23:35+09:00 Subject: Re: Creating a canonicalized url On Jan 24, 2008, at 7:14 AM, Dan Cuddeford wrote: > Hello there guys, > > I'm trying to track down an easy way to canonicalize a URL from with > ruby. I've been looking around for this but all I can find are some > procedure hacks sure as # canonicalize the url > if ($url -notmatch "^[a-z]+://") { $url = "http://$url" } > > which isn't going to take into account everything according to RFC > 2396 > > * Remove all leading and trailing dots > * Replace consecutive dots with a single dot. > * If the hostname can be parsed as an IP address, it should be > normalized to 4 dot-separated decimal values. The client should handle > any legal IP address encoding, including octal, hex, and fewer than 4 > components. > * Lowercase the whole string. > > > # The sequences "/../" and "/./" in the path should be resolved, by > replacing "/./" with "/", and removing "/../" along with the preceding > path component. > # Runs of consecutive slashes should be replaced with a single slash > character. > > So is there a method out there for this? I'd start looking at URI, in particular, URI#parse. $ fri URI#parse ------------------------------------------------------------- URI::parse URI::parse(uri) ------------------------------------------------------------------------ Synopsis URI::parse(uri_str) Args +uri_str+: String with URI. Description Creates one of the URI's subclasses instance from the string. Raises URI::InvalidURIError Raised if URI given is not a correct one. Usage require 'uri' uri = URI.parse("http://www.ruby-lang.org/") p uri # => # p uri.scheme # => "http" p uri.host # => "www.ruby-lang.org" As for the "Lowercase the whole string" part, only the domain is required to be case-insensitive. It is possible for the underlying web server to ignore case when finding a path, but the URI is not necessarily a reference to the same resource if the case is altered. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com