diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index 6fd0f7c..3071c45 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -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 @@ -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 @@ -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:password@www.example.com" + # + 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 @@ -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 @@ -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)) @@ -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) && !@path.empty? && !@path.start_with?('/') str << '/' end diff --git a/lib/uri/http.rb b/lib/uri/http.rb index 3c41cd4..076c689 100644 --- a/lib/uri/http.rb +++ b/lib/uri/http.rb @@ -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 # @@ -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 diff --git a/test/uri/test_http.rb b/test/uri/test_http.rb index 8816d20..78912fd 100644 --- a/test/uri/test_http.rb +++ b/test/uri/test_http.rb @@ -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('userinfo@a.b.c', URI.parse('https://userinfo@a.b.c/').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://userinfo@a.b.c/').origin) end end