Skip to content

Commit 7612ab9

Browse files
Merge pull request #80 from Earlopain/cgi-ruby-3.5
Fix Ruby 3.5 compatibility
2 parents 33fb921 + 4306a3d commit 7612ab9

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

lib/amq/uri.rb

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# encoding: utf-8
22

3-
require "cgi"
3+
if RUBY_VERSION < "3.5"
4+
require "cgi/util"
5+
else
6+
require "cgi/escape"
7+
end
48
require "uri"
59

610
module AMQ
@@ -43,28 +47,33 @@ def self.parse(connection_string)
4347
end
4448

4549
if uri.query
46-
query_params = CGI::parse(uri.query)
47-
48-
normalized_query_params = Hash[query_params.map { |param, value| [param, value.one? ? value.first : value] }]
50+
query_params = Hash.new { |hash, key| hash[key] = [] }
51+
::URI.decode_www_form(uri.query).each do |key, value|
52+
query_params[key] << value
53+
end
54+
query_params.each do |key, value|
55+
query_params[key] = value.one? ? value.first : value
56+
end
57+
query_params.default = nil
4958

50-
opts[:heartbeat] = normalized_query_params["heartbeat"].to_i
51-
opts[:connection_timeout] = normalized_query_params["connection_timeout"].to_i
52-
opts[:channel_max] = normalized_query_params["channel_max"].to_i
53-
opts[:auth_mechanism] = normalized_query_params["auth_mechanism"]
59+
opts[:heartbeat] = query_params["heartbeat"].to_i
60+
opts[:connection_timeout] = query_params["connection_timeout"].to_i
61+
opts[:channel_max] = query_params["channel_max"].to_i
62+
opts[:auth_mechanism] = query_params["auth_mechanism"]
5463

5564
%w(cacertfile certfile keyfile).each do |tls_option|
56-
if normalized_query_params[tls_option] && uri.scheme == "amqp"
65+
if query_params[tls_option] && uri.scheme == "amqp"
5766
raise ArgumentError.new("The option '#{tls_option}' can only be used in URIs that use amqps schema")
5867
else
59-
opts[tls_option.to_sym] = normalized_query_params[tls_option]
68+
opts[tls_option.to_sym] = query_params[tls_option]
6069
end
6170
end
6271

6372
%w(verify fail_if_no_peer_cert).each do |tls_option|
64-
if normalized_query_params[tls_option] && uri.scheme == "amqp"
73+
if query_params[tls_option] && uri.scheme == "amqp"
6574
raise ArgumentError.new("The option '#{tls_option}' can only be used in URIs that use amqps schema")
6675
else
67-
opts[tls_option.to_sym] = as_boolean(normalized_query_params[tls_option])
76+
opts[tls_option.to_sym] = as_boolean(query_params[tls_option])
6877
end
6978
end
7079
end
@@ -80,7 +89,7 @@ def self.parse_amqp_url(s)
8089
# Implementation
8190
#
8291

83-
# Normalizes values returned by CGI.parse.
92+
# Normalizes values returned by URI.decode_www_form.
8493
# @private
8594
def self.as_boolean(val)
8695
case val

0 commit comments

Comments
 (0)