lib/active_merchant/connection.rb in activemerchant-1.78.0 vs lib/active_merchant/connection.rb in activemerchant-1.79.0
- old
+ new
@@ -3,10 +3,11 @@
require 'net/https'
require 'benchmark'
module ActiveMerchant
class Connection
+ using NetHttpSslConnection
include NetworkConnectionRetries
MAX_RETRIES = 3
OPEN_TIMEOUT = 60
READ_TIMEOUT = 60
@@ -19,10 +20,15 @@
attr_accessor :endpoint
attr_accessor :open_timeout
attr_accessor :read_timeout
attr_accessor :verify_peer
attr_accessor :ssl_version
+ if Net::HTTP.instance_methods.include?(:min_version=)
+ attr_accessor :min_version
+ attr_accessor :max_version
+ end
+ attr_reader :ssl_connection
attr_accessor :ca_file
attr_accessor :ca_path
attr_accessor :pem
attr_accessor :pem_password
attr_reader :wiredump_device
@@ -42,11 +48,16 @@
@ca_file = CA_FILE
@ca_path = CA_PATH
@max_retries = MAX_RETRIES
@ignore_http_status = false
@ssl_version = nil
- @proxy_address = nil
+ if Net::HTTP.instance_methods.include?(:min_version=)
+ @min_version = nil
+ @max_version = nil
+ end
+ @ssl_connection = {}
+ @proxy_address = :ENV
@proxy_port = nil
end
def wiredump_device=(device)
raise ArgumentError, "can't wiredump to frozen #{device.class}" if device && device.frozen?
@@ -61,10 +72,14 @@
info "connection_http_method=#{method.to_s.upcase} connection_uri=#{endpoint}", tag
result = nil
realtime = Benchmark.realtime do
+ http.start unless http.started?
+ @ssl_connection = http.ssl_connection
+ info "connection_ssl_version=#{ssl_connection[:version]} connection_ssl_cipher=#{ssl_connection[:cipher]}", tag
+
result = case method
when :get
raise ArgumentError, "GET requests do not support a request body" if body
http.get(endpoint.request_uri, headers)
when :post
@@ -78,12 +93,18 @@
http.patch(endpoint.request_uri, body, headers)
when :delete
# It's kind of ambiguous whether the RFC allows bodies
# for DELETE requests. But Net::HTTP's delete method
# very unambiguously does not.
- raise ArgumentError, "DELETE requests do not support a request body" if body
- http.delete(endpoint.request_uri, headers)
+ if body
+ debug body
+ req = Net::HTTP::Delete.new(endpoint.request_uri, headers)
+ req.body = body
+ http.request(req)
+ else
+ http.delete(endpoint.request_uri, headers)
+ end
else
raise ArgumentError, "Unsupported request method #{method.to_s.upcase}"
end
end
@@ -93,20 +114,24 @@
end
end
ensure
info "connection_request_total_time=%.4fs" % [Process.clock_gettime(Process::CLOCK_MONOTONIC) - request_start], tag
+ http.finish if http.started?
end
private
+
def http
- http = Net::HTTP.new(endpoint.host, endpoint.port, proxy_address, proxy_port)
- configure_debugging(http)
- configure_timeouts(http)
- configure_ssl(http)
- configure_cert(http)
- http
+ @http ||= begin
+ http = Net::HTTP.new(endpoint.host, endpoint.port, proxy_address, proxy_port)
+ configure_debugging(http)
+ configure_timeouts(http)
+ configure_ssl(http)
+ configure_cert(http)
+ http
+ end
end
def configure_debugging(http)
http.set_debug_output(wiredump_device)
end
@@ -119,9 +144,13 @@
def configure_ssl(http)
return unless endpoint.scheme == "https"
http.use_ssl = true
http.ssl_version = ssl_version if ssl_version
+ if http.respond_to?(:min_version=)
+ http.min_version = min_version if min_version
+ http.max_version = max_version if max_version
+ end
if verify_peer
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.ca_file = ca_file
http.ca_path = ca_path