Skip to content

Commit

Permalink
Try to preserve consecutive slashes in a relative path.
Browse files Browse the repository at this point in the history
This is a workaround for a bug of URI#+ that led to failure in
resolving a relative path containing double slash like
"/../http://.../".  GH sparklemotion#304
  • Loading branch information
knu committed May 21, 2013
1 parent a1ea119 commit 5aabc6e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

* Bug fix
* Ensure page URLs with whitespace in them are escaped #313 @pacop.
* Added a workaround for a bug of URI#+ that led to failure in resolving a relative path containing double slash like "/../http://.../". #304

=== 2.6.0

Expand Down
11 changes: 10 additions & 1 deletion lib/mechanize/http/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,16 @@ def resolve(uri, referer = current_page)
base = nil
end

uri = referer_uri + (base ? base.uri : referer_uri) + uri
base = referer_uri + (base ? base.uri : referer_uri)

# Workaround for URI's bug in that it squashes consecutive
# slashes. See #304.
if uri.path.match(%r{\A(.*?/)(?!/\.\.?(?!/))(/.*)\z}i)
uri = URI((base + $1).to_s + $2)
else
uri = base + uri
end

# Strip initial "/.." bits from the path
uri.path.sub!(/^(\/\.\.)+(?=\/)/, '')
end
Expand Down
9 changes: 9 additions & 0 deletions test/test_mechanize_http_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,15 @@ def test_resolve_parameters_query_append
assert_nil params
end

def test_resolve_slashes
page = Mechanize::Page.new URI('http://example/foo/'), nil, '', 200, @mech
uri = '/bar/http://example/test/'

resolved = @agent.resolve uri, page

assert_equal 'http://example/bar/http://example/test/', resolved.to_s
end

def test_response_authenticate
@agent.add_auth @uri, 'user', 'password'

Expand Down

0 comments on commit 5aabc6e

Please sign in to comment.