erli
erli

Reputation: 408

Sanitizing URL strings

Say we have a string

url = "http://example.com/foo/baz/../../."

Obviously, we know from the Unix shell that ../../. essentially means to go up two directories. Hence, this URL is really going to http://example.com/. My question is, given these ../ characters in a string, how can we sanitize the URL string to point at the actual resource?

For example:

url = "http://example.com/foo/baz/../../hello.html"
url = process(url)
url = "http://example.com/hello.html"

Another:

url = "http://example.com/foo/baz/../."
url = process(url)
url = "http://example.com/foo/"

Keep in mind, the function still as to be able to take in normal URLs (ie. http://example.com) and return them as is if there is nothing to sanitize

Upvotes: 0

Views: 709

Answers (2)

Adobe
Adobe

Reputation: 13487

#!/usr/bin/env ruby

# ======
## defs:

def process(url)
  url_components = url.split('/')
  url_components2 = url_components.dup

  current_index = 0

  url_components.each do |component|

    if component == '..'
      url_components2.delete_at(current_index)
      url_components2.delete_at(current_index-1)
      current_index -= 1
    elsif
      component == '.'
      url_components2.delete_at(current_index)
    else
      current_index += 1
    end

  end

  url_resolved = url_components2.join('/')
  return url_resolved
end


# =======
## tests:

urls = [
  "http://example.com/foo/baz/../../.",
  "http://example.com/foo/baz/../../hello.html",
  "http://example.com/foo/baz/../."
]

urls.each do |url|
  print url, ' => '
  puts process(url)
end

Upvotes: 0

castwide
castwide

Reputation: 323

The addressable gem can do this.

require 'addressable'
Addressable::URI.parse("http://example.com/foo/baz/../../hello.html").normalize.to_s
#=> "http://example.com/hello.html"    

Upvotes: 1

Related Questions