From 2f916039a2b20b6d6e704a3921bd43a79b81ac2d Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@ruby-lang.org>
Date: Thu, 26 Jun 2025 01:21:50 +0900
Subject: [PATCH] Add authority accessor

---
 lib/uri/generic.rb | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

--- a/lib/uri/generic.rb
+++ b/lib/uri/generic.rb
@@ -575,6 +575,13 @@ module URI
       @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
+
     #
     # Checks the host +v+ component for RFC2396 compliance
     # and against the URI::Parser Regexp for :HOST.
@@ -606,6 +613,13 @@ module URI
     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
     #
@@ -1114,7 +1128,7 @@ module URI
 
       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
@@ -1127,9 +1141,7 @@ module URI
 
       # 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))
