Skip to content

Commit

Permalink
added parsing for %2f and default to return passed in value when can'…
Browse files Browse the repository at this point in the history
…t be parsed
  • Loading branch information
stewartmckee committed Aug 23, 2010
1 parent bd7741d commit 78678f7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 21 deletions.
3 changes: 2 additions & 1 deletion README.textile
@@ -1,5 +1,5 @@

h1. Absolutize v0.0.9
h1. Absolutize v0.0.11

h2. Intro

Expand All @@ -23,6 +23,7 @@ Creates a new absolutize object based on a base_url
** :remove_anchors - removes any anchor tags (false)
** :force_escaping - forces escaping of urls if it hasn't been done already (true)
** :output_debug - puts debug info while processing the url (false)
** :raise_exceptions - raises exceptions when it can't parse a url, otherwise returns passed in value (false)

bq. absolutizer = Absolutize.new("http://www.vamosa.com/mainfolder/", :remove_anchors => true)

Expand Down
2 changes: 1 addition & 1 deletion absolutize.gemspec
@@ -1,6 +1,6 @@
spec = Gem::Specification.new do |s|
s.name = "absolutize"
s.version = "0.0.9"
s.version = "0.0.11"
s.author = "Stewart McKee"
s.email = "stewart.mckee@vamosa.com"
s.homepage = "http://github.com/vamosa/absolutize"
Expand Down
8 changes: 8 additions & 0 deletions changelog.txt
@@ -1,3 +1,11 @@
v0.0.11

added raise_exceptions option and defaulted to not raising exceptions, passed in value will be returned when cant be parsed unless option set to true

v0.0.10

fixed parsing for %2f to allow it to work as a / when joining url

v0.0.9

removed the removal of // in the uri path
Expand Down
34 changes: 21 additions & 13 deletions lib/absolutize.rb
Expand Up @@ -11,6 +11,8 @@ def initialize(base_url, options = {})
@options[:remove_anchors] = false if @options[:remove_anchors].nil?
@options[:force_escaping] = true if @options[:force_escaping].nil?
@options[:output_debug] = false if @options[:output_debug].nil?
@options[:raise_exceptions] = false if @options[:output_debug].nil?

end

def url(relative_url)
Expand All @@ -25,23 +27,29 @@ def url(relative_url)
begin
absolute_url = URI.join(@base_url, relative_url)
rescue URI::InvalidURIError => uri
puts "Unable to use URI.join attempting manually" if @options[:output_debug]
if @base_url =~ /\Ahttp/ and relative_url =~ /\A\//
puts "base url starts with http and relative_url is relative to root" if @options[:output_debug]
uri = URI.parse(@base_url)
if uri.port
absolute_url = URI.parse("#{uri.scheme}://#{uri.host}:#{uri.port}#{relative_url}")
begin
puts "Unable to use URI.join attempting manually" if @options[:output_debug]
if @base_url =~ /\Ahttp/ and relative_url =~ /\A\//
puts "base url starts with http and relative_url is relative to root" if @options[:output_debug]
uri = URI.parse(@base_url)
if uri.port
absolute_url = URI.parse("#{uri.scheme}://#{uri.host}:#{uri.port}#{relative_url}")
else
absolute_url = URI.parse("#{uri.scheme}://#{uri.host}#{relative_url}")
end
elsif relative_url =~ /\Ahttp/
#new url is absolute anyway
absolute_url = URI.parse(relative_url)
else
absolute_url = URI.parse("#{uri.scheme}://#{uri.host}#{relative_url}")
raise "Unable to absolutize #{@base_url} and #{relative_url}" if @options[:raise_exceptions]
absolute_url = relative_url
end
elsif relative_url =~ /\Ahttp/
#new url is absolute anyway
absolute_url = URI.parse(relative_url)
else
raise "Unable to absolutize #{@base_url} and #{relative_url}"
rescue URI::InvalidURIError => uri_2
# ok, givin it a fair go trying to get this url, raise an exception if the option is set otherwise give up and return original relative url
raise "Unable to absolutize #{@base_url} and #{relative_url}" if @options[:raise_exceptions]
absolute_url = relative_url
end
end

absolute_url
end
end
27 changes: 21 additions & 6 deletions spec/absolutize/absolutize_spec.rb
Expand Up @@ -49,6 +49,9 @@
it "should encode with pipes" do
@absolutize.url("/root%20folder/asdf.html?lang=en|us").to_s.should == "http://www.baseurl.com/root%20folder/asdf.html?lang=en%7Cus"
end
it "should not raise an exception with really bad url" do
@absolutize.url("http://.asdf.com/").to_s.should == "http://.asdf.com/"
end
end

describe "with remove_anchors true" do
Expand All @@ -65,15 +68,27 @@
@absolutize = Absolutize.new(@base_url, :force_escaping => false)
end
it "should not escape invalid characters" do
lambda { @absolutize.url("/root folder/asdf.html#anchor")}.should raise_error

ab = Absolutize.new("http://www.baseurl.com/top_folder/index.html", :force_escaping => false)
#this is actually wrong, but we have not forced escaping

#this example should be decoded first, but we are checking that when force_escaping is off it does not
ab.url("%2ftop_folder%2fsub_folder%2findex.html").to_s.should == "http://www.baseurl.com/top_folder/%2ftop_folder%2fsub_folder%2findex.html"
#should work fine
ab.url("/top_folder/sub_folder/index.html").to_s.should == "http://www.baseurl.com/top_folder/sub_folder/index.html"

end

end

describe "with raise_exceptions true" do
before(:each) do
@absolutize = Absolutize.new(@base_url, :raise_exceptions => true)
end
it "should raise an exception when absolutize can't fix a url" do
lambda { @absolutize.url("http://.asdf.com/root folder/asdf.html#anchor")}.should raise_error
end
it "should not raise an exception when a valid url is given" do
lambda { @absolutize.url("http://www.asdf.com/rootfolder/asdf.html#anchor")}.should_not raise_error
end

it "should not raise an exception when a fixable url is given" do
lambda { @absolutize.url("http://www.asdf.com/root folder[1]/asdf.html#anchor")}.should_not raise_error
end
end
end

0 comments on commit 78678f7

Please sign in to comment.