From: Daniel Leidert <dleidert@debian.org>
Date: Sat, 6 Nov 2021 04:29:37 +0100
Subject: Replace URI.encode by URI.encode_www_form_component for Ruby 3

Debian-Bug: https://bugs.debian.org/996344
---
 lib/openstack.rb                      |  4 ++--
 lib/openstack/compute/server.rb       | 32 ++++++++++++++++----------------
 lib/openstack/swift/connection.rb     | 10 +++++-----
 lib/openstack/swift/container.rb      | 12 ++++++------
 lib/openstack/swift/storage_object.rb | 14 +++++++-------
 5 files changed, 36 insertions(+), 36 deletions(-)

diff --git a/lib/openstack.rb b/lib/openstack.rb
index 37cb71e..e5f7e4a 100644
--- a/lib/openstack.rb
+++ b/lib/openstack.rb
@@ -80,8 +80,8 @@ module OpenStack
 
   def self.paginate(options = {})
     path_args = []
-    path_args.push(URI.encode("limit=#{options[:limit]}")) if options[:limit]
-    path_args.push(URI.encode("offset=#{options[:offset]}")) if options[:offset]
+    path_args.push(URI.encode_www_form_component("limit=#{options[:limit]}")) if options[:limit]
+    path_args.push(URI.encode_www_form_component("offset=#{options[:offset]}")) if options[:offset]
     path_args.join("&")
   end
 
diff --git a/lib/openstack/compute/server.rb b/lib/openstack/compute/server.rb
index 3e6a87d..d97f82e 100644
--- a/lib/openstack/compute/server.rb
+++ b/lib/openstack/compute/server.rb
@@ -49,7 +49,7 @@ module Compute
     #  >> server.refresh
     #  => true
     def populate(data=nil)
-      path = "/servers/#{URI.encode(@id.to_s)}"
+      path = "/servers/#{URI.encode_www_form_component(@id.to_s)}"
       if data.nil? then
           response = @compute.connection.req("GET", path)
           OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
@@ -80,7 +80,7 @@ module Compute
     #   => true
     def reboot(type="SOFT")
       data = JSON.generate(:reboot => {:type => type})
-      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       true
     end
@@ -105,7 +105,7 @@ module Compute
       data = JSON.generate(:suspend => nil)
       puts data
       pp "About to post ACTION"
-      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       true
     end
@@ -118,7 +118,7 @@ module Compute
     #   => true
     def start()
       data = JSON.generate(:resume => nil)
-      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       true
     end
@@ -135,7 +135,7 @@ module Compute
     #   => "MyServer"
     def update(options)
       data = JSON.generate(:server => options)
-      response = @compute.connection.csreq("PUT",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("PUT",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       # If we rename the instance, repopulate the object
       self.populate if options[:name]
@@ -149,7 +149,7 @@ module Compute
     #   >> server.delete!
     #   => true
     def delete!
-      response = @compute.connection.csreq("DELETE",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}",@svrmgmtport,@svrmgmtscheme)
+      response = @compute.connection.csreq("DELETE",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}",@svrmgmtport,@svrmgmtscheme)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       true
     end
@@ -205,7 +205,7 @@ module Compute
     #   =>
     def create_image(options)
       data = JSON.generate(:createImage => options)
-      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       image_id = response["Location"].split("/images/").last
       OpenStack::Compute::Image.new(@compute, image_id)
@@ -220,7 +220,7 @@ module Compute
     #   => true
     def resize!(flavorRef)
       data = JSON.generate(:resize => {:flavorRef => flavorRef})
-      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       self.populate
       true
@@ -235,7 +235,7 @@ module Compute
     def confirm_resize!
       # If the resize bug gets figured out, should put a check here to make sure that it's in the proper state for this.
       data = JSON.generate(:confirmResize => nil)
-      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       self.populate
       true
@@ -251,7 +251,7 @@ module Compute
     def revert_resize!
       # If the resize bug gets figured out, should put a check here to make sure that it's in the proper state for this.
       data = JSON.generate(:revertResize => nil)
-      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       self.populate
       true
@@ -283,7 +283,7 @@ module Compute
     #Return Hash with type and URL
     def get_console
       data = JSON.generate("os-getVNCConsole" => {:type => "novnc"})
-      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       JSON::parse(response.body)["console"]
     end
@@ -292,7 +292,7 @@ module Compute
     #Return output string object
     def get_console_output(length=50)
       data = JSON.generate("os-getConsoleOutput" => {:length => length})
-      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       JSON::parse(response.body)["output"]
     end
@@ -305,7 +305,7 @@ module Compute
     #   => true
     def pause
       data = JSON.generate(:pause => nil)
-      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       true
     end
@@ -318,7 +318,7 @@ module Compute
     #   => true
     def unpause
       data = JSON.generate(:unpause => nil)
-      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       true
     end
@@ -331,7 +331,7 @@ module Compute
     #   => true
     def suspend
       data = JSON.generate(:suspend => nil)
-      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       true
     end
@@ -344,7 +344,7 @@ module Compute
     #   => true
     def resume
       data = JSON.generate(:resume => nil)
-      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
+      response = @compute.connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode_www_form_component(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
       OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
       true
     end
diff --git a/lib/openstack/swift/connection.rb b/lib/openstack/swift/connection.rb
index 75df938..51b68da 100644
--- a/lib/openstack/swift/connection.rb
+++ b/lib/openstack/swift/connection.rb
@@ -74,7 +74,7 @@ module Swift
     #   => ["test", "video"]
     def containers(limit = nil, marker = nil)
       path = OpenStack.get_query_params({:limit=>limit, :marker=>marker, :format=>'json'}, [:limit, :marker, :format], "")
-      response = @connection.req("GET", URI.encode(path))
+      response = @connection.req("GET", URI.encode_www_form_component(path))
       OpenStack.symbolize_keys(JSON.parse(response.body)).inject([]){|res,cur| res << cur[:name]; res }
     end
     alias :list_containers :containers
@@ -90,7 +90,7 @@ module Swift
     #        "container2" => { :bytes => "105943", :count => "25" } }
     def containers_detail(limit = nil, marker = nil)
       path = OpenStack.get_query_params({:limit=>limit, :marker=>marker, :format=>'json'}, [:limit, :marker, :format], "")
-      response = @connection.req("GET", URI.encode(path))
+      response = @connection.req("GET", URI.encode_www_form_component(path))
       OpenStack.symbolize_keys(JSON.parse(response.body)).inject({}){|res,current| res.merge!({current[:name]=>{:bytes=>current[:bytes].to_s,:count=>current[:count].to_s}}) ; res }
     end
     alias :list_containers_info :containers_detail
@@ -103,7 +103,7 @@ module Swift
     #   cf.container_exists?('bad_container')
     #   => false
     def container_exists?(containername)
-      path = "/#{URI.encode(containername.to_s)}"
+      path = "/#{URI.encode_www_form_component(containername.to_s)}"
       begin
         response = @connection.req("HEAD", path)
       rescue OpenStack::Exception::ItemNotFound
@@ -126,7 +126,7 @@ module Swift
     def create_container(containername)
       raise OpenStack::Exception::InvalidArgument.new("Container name cannot contain '/'") if containername.match("/")
       raise OpenStack::Exception::InvalidArgument.new("Container name is limited to 256 characters") if containername.length > 256
-      path = "/#{URI.encode(containername.to_s)}"
+      path = "/#{URI.encode_www_form_component(containername.to_s)}"
       @connection.req("PUT", path, {:headers=>{"Content-Length"=>"0"}})
       OpenStack::Swift::Container.new(self, containername)
     end
@@ -145,7 +145,7 @@ module Swift
     #   cf.delete_container('nonexistent')
     #   => OpenStack::Exception::ItemNotFound: The container: "nonexistant" does not exist. The resource could not be found
     def delete_container(containername)
-      path = "/#{URI.encode(containername.to_s)}"
+      path = "/#{URI.encode_www_form_component(containername.to_s)}"
       begin
         @connection.req("DELETE", path)
       rescue OpenStack::Exception::ResourceStateConflict => conflict
diff --git a/lib/openstack/swift/container.rb b/lib/openstack/swift/container.rb
index 324ce7c..5086837 100644
--- a/lib/openstack/swift/container.rb
+++ b/lib/openstack/swift/container.rb
@@ -18,7 +18,7 @@ module Swift
 
     # Retrieves Metadata for the container
     def container_metadata
-      path = "/#{URI.encode(@name.to_s)}"
+      path = "/#{URI.encode_www_form_component(@name.to_s)}"
       response = @swift.connection.req("HEAD", path)
       resphash = response.to_hash
       meta = {:bytes=>resphash["x-container-bytes-used"][0], :count=>resphash["x-container-object-count"][0], :metadata=>{}}
@@ -54,7 +54,7 @@ module Swift
       headers = metadatahash.inject({}){|res, (k,v)| ((k.to_s.match /^X-Container-Meta-/i) ? res[k.to_s]=v : res["X-Container-Meta-#{k}"]=v ) ; res}
       headers.merge!({'content-type'=>'application/json'})
       begin
-        response = @swift.connection.req("POST", URI.encode("/#{@name.to_s}"), {:headers=>headers} )
+        response = @swift.connection.req("POST", URI.encode_www_form_component("/#{@name.to_s}"), {:headers=>headers} )
         true
       rescue OpenStack::Exception::ItemNotFound => not_found
         msg = "Cannot set metadata - container: \"#{@name}\" does not exist!.  #{not_found.message}"
@@ -112,7 +112,7 @@ module Swift
     def objects(params = {})
       path = "/#{@name.to_s}"
       path = (params.empty?)? path : OpenStack.get_query_params(params, [:limit, :marker, :prefix, :path, :delimiter], path)
-      response = @swift.connection.req("GET", URI.encode(path))
+      response = @swift.connection.req("GET", URI.encode_www_form_component(path))
       OpenStack.symbolize_keys(JSON.parse(response.body)).inject([]){|res, cur| res << cur[:name]; res }
     end
     alias :list_objects :objects
@@ -135,7 +135,7 @@ module Swift
     def objects_detail(params = {})
       path = "/#{@name.to_s}"
       path = (params.empty?)? path : OpenStack.get_query_params(params, [:limit, :marker, :prefix, :path, :delimiter], path)
-      response = @swift.connection.req("GET", URI.encode(path))
+      response = @swift.connection.req("GET", URI.encode_www_form_component(path))
       OpenStack.symbolize_keys(JSON.parse(response.body)).inject({}){|res, current| res.merge!({current[:name]=>{:bytes=>current[:bytes].to_s, :content_type=>current[:content_type].to_s, :last_modified=>current[:last_modified], :hash=>current[:hash]}}) ; res }
     end
     alias :list_objects_info :objects_detail
@@ -161,7 +161,7 @@ module Swift
     def object_exists?(objectname)
       path = "/#{@name.to_s}/#{objectname}"
       begin
-        response = @swift.connection.req("HEAD", URI.encode(path))
+        response = @swift.connection.req("HEAD", URI.encode_www_form_component(path))
         true
       rescue OpenStack::Exception::ItemNotFound
         false
@@ -196,7 +196,7 @@ module Swift
     def delete_object(objectname)
       path = "/#{@name.to_s}/#{objectname}"
       begin
-        response = @swift.connection.req("DELETE", URI.encode(path))
+        response = @swift.connection.req("DELETE", URI.encode_www_form_component(path))
         true
       rescue OpenStack::Exception::ItemNotFound => not_found
         msg = "The object: \"#{objectname}\" does not exist in container \"#{@name}\".  #{not_found.message}"
diff --git a/lib/openstack/swift/storage_object.rb b/lib/openstack/swift/storage_object.rb
index 26cb28d..650c67e 100644
--- a/lib/openstack/swift/storage_object.rb
+++ b/lib/openstack/swift/storage_object.rb
@@ -53,7 +53,7 @@ module Swift
       if data.nil? #just create an empty object
         path = "/#{container.name}/#{objectname}"
         provided_headers["content-length"] = "0"
-        container.swift.connection.req("PUT", URI.encode(path), {:headers=>provided_headers})
+        container.swift.connection.req("PUT", URI.encode_www_form_component(path), {:headers=>provided_headers})
       else
         self.new(container, objectname).write(data, provided_headers)
       end
@@ -131,7 +131,7 @@ module Swift
       end
       path = "/#{@containername}/#{@name}"
       begin
-        response = @container.swift.connection.req("GET", URI.encode(path), {:headers=>headers})
+        response = @container.swift.connection.req("GET", URI.encode_www_form_component(path), {:headers=>headers})
         response.body
       rescue OpenStack::Exception::ItemNotFound => not_found
         msg = "No Object \"#{@name}\" found in Container \"#{@containername}\".  #{not_found.message}"
@@ -160,7 +160,7 @@ module Swift
         headers['Range'] = range
       end
       server = @container.swift.connection.service_host
-      path = @container.swift.connection.service_path + URI.encode("/#{@containername}/#{@name}")
+      path = @container.swift.connection.service_path + URI.encode_www_form_component("/#{@containername}/#{@name}")
       port = @container.swift.connection.service_port
       scheme = @container.swift.connection.service_scheme
       response = @container.swift.connection.csreq("GET", server, path, port, scheme, headers, nil, 0, &block)
@@ -185,7 +185,7 @@ module Swift
       headers['content-type'] = 'application/json'
       path = "/#{@containername}/#{@name}"
       begin
-        response = @container.swift.connection.req("POST", URI.encode(path), {:headers=>headers})
+        response = @container.swift.connection.req("POST", URI.encode_www_form_component(path), {:headers=>headers})
       rescue OpenStack::Exception::ItemNotFound => not_found
         msg = "Can't set metadata: No Object \"#{@name}\" found in Container \"#{@containername}\".  #{not_found.message}"
         raise OpenStack::Exception::ItemNotFound.new(msg, not_found.response_code, not_found.response_body)
@@ -213,7 +213,7 @@ module Swift
       headers = {'X-Object-Manifest' => manifest}
       path = "/#{@containername}/#{@name}"
       begin
-        response = @container.swift.connection.req("POST", URI.encode(path), {:headers=>headers})
+        response = @container.swift.connection.req("POST", URI.encode_www_form_component(path), {:headers=>headers})
       rescue OpenStack::Exception::ItemNotFound => not_found
         msg = "Can't set manifest: No Object \"#{@name}\" found in Container \"#{@containername}\".  #{not_found.message}"
         raise OpenStack::Exception::ItemNotFound.new(msg, not_found.response_code, not_found.response_body)
@@ -244,7 +244,7 @@ module Swift
     #
     def write(data, headers = {})
       server = @container.swift.connection.service_host
-      path = @container.swift.connection.service_path + URI.encode("/#{@containername}/#{@name}")
+      path = @container.swift.connection.service_path + URI.encode_www_form_component("/#{@containername}/#{@name}")
       port = @container.swift.connection.service_port
       scheme = @container.swift.connection.service_scheme
       body = (data.is_a?(String))? StringIO.new(data) : data
@@ -279,7 +279,7 @@ module Swift
       provided_headers["content-length"] = "0"
       path = "/#{container_name}/#{object_name}"
       begin
-        response = @container.swift.connection.req("PUT", URI.encode(path), {:headers=>provided_headers})
+        response = @container.swift.connection.req("PUT", URI.encode_www_form_component(path), {:headers=>provided_headers})
       rescue OpenStack::Exception::ItemNotFound => not_found
         msg = "Can't copy \"#{@name}\": No Object \"#{@name}\" found in Container \"#{@containername}\".  #{not_found.message}"
         raise OpenStack::Exception::ItemNotFound.new(msg, not_found.response_code, not_found.response_body)
