Skip to content
Open
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
59 changes: 44 additions & 15 deletions lib/uri/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ def password

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

Expand Down Expand Up @@ -624,7 +624,7 @@ def set_host(v)
# 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)
protected def set_authority_components(user, password, host, port = nil)
@user, @password, @host, @port = user, password, host, port || self.default_port
end

Expand Down Expand Up @@ -747,6 +747,45 @@ def port=(v)
port
end

#
# == Description
#
# Returns the authority of the URI, as defined in
# https://www.rfc-editor.org/rfc/rfc3986#section-3.2.
#
# authority = [ userinfo "@" ] host [ ":" port ]
#
# Returns an empty string if no authority is present.
#
# == Usage
#
# require 'uri'
#
# URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').authority
# #=> "www.example.com"
# URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').authority
# #=> "www.example.com:8000"
# URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').authority
# #=> "www.example.com"
# URI::HTTP.build(host: 'www.example.com', userinfo: 'user:password').authority
# #=> "user:[email protected]"
#
def authority
str = ''.dup
if self.userinfo
str << self.userinfo
str << '@'
end
if @host
str << @host
end
if @port && @port != self.default_port
str << ':'
str << @port.to_s
end
str
end

def check_registry(v) # :nodoc:
raise InvalidURIError, "cannot set registry"
end
Expand Down Expand Up @@ -1136,7 +1175,7 @@ def merge(oth)

base = self.dup

authority = rel.authority
authority = rel.authority_components

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

# RFC2396, Section 5.2, 4)
if authority
base.set_authority(*authority)
base.set_authority_components(*authority)
base.set_path(rel.path)
elsif base.path && rel.path
base.set_path(merge_path(base.path, rel.path))
Expand Down Expand Up @@ -1365,17 +1404,7 @@ def to_s
if @host || %w[file postgres].include?(@scheme)
str << '//'
end
if self.userinfo
str << self.userinfo
str << '@'
end
if @host
str << @host
end
if @port && @port != self.default_port
str << ':'
str << @port.to_s
end
str << self.authority
if (@host || @port) && [email protected]? && [email protected]_with?('/')
str << '/'
end
Expand Down
28 changes: 6 additions & 22 deletions lib/uri/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,6 @@ def request_uri
url.start_with?(?/.freeze) ? url : ?/ + url
end

#
# == Description
#
# Returns the authority for an HTTP uri, as defined in
# https://www.rfc-editor.org/rfc/rfc3986#section-3.2.
#
#
# Example:
#
# URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').authority #=> "www.example.com"
# URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').authority #=> "www.example.com:8000"
# URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').authority #=> "www.example.com"
#
def authority
if port == default_port
host
else
"#{host}:#{port}"
end
end

#
# == Description
#
Expand All @@ -127,9 +106,14 @@ def authority
# URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').origin #=> "http://www.example.com:8000"
# URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').origin #=> "http://www.example.com"
# URI::HTTPS.build(host: 'www.example.com', path: '/foo/bar').origin #=> "https://www.example.com"
# URI::HTTPS.build(host: 'www.example.com', userinfo: 'user:password').origin #=> "https://www.example.com"
#
def origin
"#{scheme}://#{authority}"
str = "#{scheme}://#{host}"
if port != default_port
str << ":#{port}"
end
str
end
end

Expand Down
3 changes: 2 additions & 1 deletion test/uri/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ def test_authority
assert_equal('a.b.c', URI.parse('http://a.b.c/').authority)
assert_equal('a.b.c:8081', URI.parse('http://a.b.c:8081/').authority)
assert_equal('a.b.c', URI.parse('http://a.b.c:80/').authority)
assert_equal('[email protected]', URI.parse('https://[email protected]/').authority)
end


def test_origin
assert_equal('http://a.b.c', URI.parse('http://a.b.c/').origin)
assert_equal('http://a.b.c:8081', URI.parse('http://a.b.c:8081/').origin)
assert_equal('http://a.b.c', URI.parse('http://a.b.c:80/').origin)
assert_equal('https://a.b.c', URI.parse('https://a.b.c/').origin)
assert_equal('https://a.b.c', URI.parse('https://[email protected]/').origin)
end
end