Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions lib/uri/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,18 @@ def initialize(scheme,

if arg_check
self.scheme = scheme
self.userinfo = userinfo
self.hostname = host
self.port = port
self.userinfo = userinfo
self.path = path
self.query = query
self.opaque = opaque
self.fragment = fragment
else
self.set_scheme(scheme)
self.set_userinfo(userinfo)
self.set_host(host)
self.set_port(port)
self.set_userinfo(userinfo)
self.set_path(path)
self.query = query
self.set_opaque(opaque)
Expand Down Expand Up @@ -511,7 +511,7 @@ def set_userinfo(user, password = nil)
user, password = split_userinfo(user)
end
@user = user
@password = password if password
@password = password

[@user, @password]
end
Expand All @@ -522,7 +522,7 @@ def set_userinfo(user, password = nil)
# See also URI::Generic.user=.
#
def set_user(v)
set_userinfo(v, @password)
set_userinfo(v, nil)
v
end
protected :set_user
Expand Down Expand Up @@ -574,6 +574,12 @@ def password
@password
end

# Returns the authority info (array of user, password, host and
# port), if any is set. Or returns +nil+.
def authority
return @user, @password, @host, @port if @user || @password || @host || @port
end

# Returns the user component after URI decoding.
def decoded_user
URI.decode_uri_component(@user) if @user
Expand Down Expand Up @@ -615,6 +621,13 @@ def set_host(v)
end
protected :set_host

# Protected setter for the authority info (+user+, +password+, +host+
# and +port+). If +port+ is +nil+, +default_port+ will be set.
#
protected def set_authority(user, password, host, port = nil)
@user, @password, @host, @port = user, password, host, port || self.default_port
end

#
# == Args
#
Expand All @@ -639,6 +652,7 @@ def set_host(v)
def host=(v)
check_host(v)
set_host(v)
set_userinfo(nil)
v
end

Expand Down Expand Up @@ -729,6 +743,7 @@ def set_port(v)
def port=(v)
check_port(v)
set_port(v)
set_userinfo(nil)
port
end

Expand Down Expand Up @@ -1121,7 +1136,7 @@ def merge(oth)

base = self.dup

authority = rel.userinfo || rel.host || rel.port
authority = rel.authority

# RFC2396, Section 5.2, 2)
if (rel.path.nil? || rel.path.empty?) && !authority && !rel.query
Expand All @@ -1134,9 +1149,7 @@ def merge(oth)

# RFC2396, Section 5.2, 4)
if authority
base.set_userinfo(rel.userinfo)
base.set_host(rel.host)
base.set_port(rel.port || base.default_port)
base.set_authority(*authority)
base.set_path(rel.path)
elsif base.path && rel.path
base.set_path(merge_path(base.path, rel.path))
Expand Down
2 changes: 1 addition & 1 deletion lib/uri/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module URI
# :stopdoc:
VERSION_CODE = '010003'.freeze
VERSION_CODE = '010004'.freeze
VERSION = VERSION_CODE.scan(/../).collect{|n| n.to_i}.join('.').freeze
# :startdoc:
end
40 changes: 21 additions & 19 deletions spec/ruby/library/uri/set_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@
it "conforms to the MatzRuby tests" do
uri = URI.parse('http://foo:bar@baz')
(uri.user = 'oof').should == 'oof'
uri.to_s.should == 'http://oof:bar@baz'
(uri.password = 'rab').should == 'rab'
uri.to_s.should == 'http://oof:rab@baz'
(uri.userinfo = 'foo').should == 'foo'
uri.to_s.should == 'http://foo:rab@baz'
(uri.userinfo = ['foo', 'bar']).should == ['foo', 'bar']
uri.to_s.should == 'http://foo:bar@baz'
(uri.userinfo = ['foo']).should == ['foo']
uri.to_s.should == 'http://foo:bar@baz'
(uri.host = 'zab').should == 'zab'
uri.to_s.should == 'http://foo:bar@zab'
(uri.port = 8080).should == 8080
uri.to_s.should == 'http://foo:bar@zab:8080'
(uri.path = '/').should == '/'
uri.to_s.should == 'http://foo:bar@zab:8080/'
(uri.query = 'a=1').should == 'a=1'
uri.to_s.should == 'http://foo:bar@zab:8080/?a=1'
(uri.fragment = 'b123').should == 'b123'
uri.to_s.should == 'http://foo:bar@zab:8080/?a=1#b123'
version_is(URI::VERSION, "1.0.4") do
uri.to_s.should == 'http://oof@baz'
(uri.password = 'rab').should == 'rab'
uri.to_s.should == 'http://oof:rab@baz'
(uri.userinfo = 'foo').should == 'foo'
uri.to_s.should == 'http://foo@baz'
(uri.userinfo = ['foo', 'bar']).should == ['foo', 'bar']
uri.to_s.should == 'http://foo:bar@baz'
(uri.userinfo = ['foo']).should == ['foo']
uri.to_s.should == 'http://foo@baz'
(uri.host = 'zab').should == 'zab'
uri.to_s.should == 'http://zab'
(uri.port = 8080).should == 8080
uri.to_s.should == 'http://zab:8080'
(uri.path = '/').should == '/'
uri.to_s.should == 'http://zab:8080/'
(uri.query = 'a=1').should == 'a=1'
uri.to_s.should == 'http://zab:8080/?a=1'
(uri.fragment = 'b123').should == 'b123'
uri.to_s.should == 'http://zab:8080/?a=1#b123'
end

uri = URI.parse('http://example.com')
-> { uri.password = 'bar' }.should raise_error(URI::InvalidURIError)
Expand Down
15 changes: 10 additions & 5 deletions test/uri/test_generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ def test_merge_authority
u0 = URI.parse('http://new.example.org/path')
u1 = u.merge('//new.example.org/path')
assert_equal(u0, u1)
u0 = URI.parse('http://[email protected]')
u1 = u.merge('//[email protected]')
assert_equal(u0, u1)
end

def test_route
Expand Down Expand Up @@ -748,17 +751,18 @@ def test_join
def test_set_component
uri = URI.parse('http://foo:bar@baz')
assert_equal('oof', uri.user = 'oof')
assert_equal('http://oof:bar@baz', uri.to_s)
assert_equal('http://oof@baz', uri.to_s)
assert_equal('rab', uri.password = 'rab')
assert_equal('http://oof:rab@baz', uri.to_s)
assert_equal('foo', uri.userinfo = 'foo')
assert_equal('http://foo:rab@baz', uri.to_s)
assert_equal('http://foo@baz', uri.to_s)
assert_equal(['foo', 'bar'], uri.userinfo = ['foo', 'bar'])
assert_equal('http://foo:bar@baz', uri.to_s)
assert_equal(['foo'], uri.userinfo = ['foo'])
assert_equal('http://foo:bar@baz', uri.to_s)
assert_equal('http://foo@baz', uri.to_s)
assert_equal('zab', uri.host = 'zab')
assert_equal('http://foo:bar@zab', uri.to_s)
assert_equal('http://zab', uri.to_s)
uri.userinfo = ['foo', 'bar']
uri.port = ""
assert_nil(uri.port)
uri.port = "80"
Expand All @@ -768,7 +772,8 @@ def test_set_component
uri.port = " 080 "
assert_equal(80, uri.port)
assert_equal(8080, uri.port = 8080)
assert_equal('http://foo:bar@zab:8080', uri.to_s)
assert_equal('http://zab:8080', uri.to_s)
uri = URI.parse('http://foo:bar@zab:8080')
assert_equal('/', uri.path = '/')
assert_equal('http://foo:bar@zab:8080/', uri.to_s)
assert_equal('a=1', uri.query = 'a=1')
Expand Down
Loading