Skip to content

Commit

Permalink
Fixes issues with relative paths. Closes #7.
Browse files Browse the repository at this point in the history
  • Loading branch information
sporkmonger committed Apr 4, 2010
1 parent 48e0b24 commit ee4c453
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ def self.heuristic_parse(uri, hints={})
if parsed.path.include?(".")
new_host = parsed.path[/^([^\/]+\.[^\/]*)/, 1]
if new_host
new_path = parsed.path.gsub(
Regexp.new("^" + Regexp.escape(new_host)), "")
parsed.host = new_host
parsed.path = new_path
parsed.scheme = hints[:scheme] unless parsed.scheme
parsed.defer_validation do
new_path = parsed.path.gsub(
Regexp.new("^" + Regexp.escape(new_host)), "")
parsed.host = new_host
parsed.path = new_path
parsed.scheme = hints[:scheme] unless parsed.scheme
end
end
end
return parsed
Expand Down Expand Up @@ -1105,8 +1107,13 @@ def path
# @return [String] The path component, normalized.
def normalized_path
@normalized_path ||= (begin
if self.scheme == nil && self.path != nil && self.path != "" &&
self.path =~ /^(?!\/)[^\/:]*:.*$/
# Relative paths with colons in the first segment are ambiguous.
self.path.sub!(":", "%2F")
end
# String#split(delimeter, -1) uses the more strict splitting behavior
# found in Python.
# found by default in Python.
result = (self.path.strip.split("/", -1).map do |segment|
Addressable::URI.normalize_component(
segment,
Expand Down Expand Up @@ -2036,6 +2043,11 @@ def validate
raise InvalidURIError, "Hostname not supplied: '#{self.to_s}'"
end
end
if self.path != nil && self.path != "" && self.path[0..0] != "/" &&
self.authority != nil
raise InvalidURIError,
"Cannot have a relative path with an authority set: '#{self.to_s}'"
end
return nil
end

Expand Down
47 changes: 47 additions & 0 deletions spec/addressable/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3865,3 +3865,50 @@ def to_str
@uri.query.should == "a=a&b[c]&b[d]=d"
end
end

describe Addressable::URI, "when assigning path values" do
before do
@uri = Addressable::URI.new
end

it "should correctly assign paths containing colons" do
@uri.path = "acct:bob@sporkmonger.com"
Addressable::URI.parse(@uri.normalize.to_str).path.should == @uri.path
@uri.normalize.to_str.should == "acct%2Fbob@sporkmonger.com"
end

it "should correctly assign paths containing colons" do
@uri.path = "/acct:bob@sporkmonger.com"
@uri.authority = "example.com"
@uri.normalize.to_str.should == "//example.com/acct:bob@sporkmonger.com"
end

it "should correctly assign paths containing colons" do
@uri.path = "acct:bob@sporkmonger.com"
@uri.scheme = "something"
@uri.normalize.to_str.should == "something:acct:bob@sporkmonger.com"
end

it "should not allow relative paths to be assigned on absolute URIs" do
(lambda do
@uri.scheme = "http"
@uri.host = "example.com"
@uri.path = "acct:bob@sporkmonger.com"
end).should raise_error(Addressable::URI::InvalidURIError)
end

it "should not allow relative paths to be assigned on absolute URIs" do
(lambda do
@uri.path = "acct:bob@sporkmonger.com"
@uri.scheme = "http"
@uri.host = "example.com"
end).should raise_error(Addressable::URI::InvalidURIError)
end

it "should not allow relative paths to be assigned on absolute URIs" do
(lambda do
@uri.path = "uuid:0b3ecf60-3f93-11df-a9c3-001f5bfffe12"
@uri.scheme = "urn"
end).should_not raise_error(Addressable::URI::InvalidURIError)
end
end

0 comments on commit ee4c453

Please sign in to comment.