Class: HTTParty::ConnectionAdapter
- Inherits:
-
Object
- Object
- HTTParty::ConnectionAdapter
- Defined in:
- lib/httparty/connection_adapter.rb
Overview
Default connection adapter that returns a new Net::HTTP each time
Custom Connection Factories
If you like to implement your own connection adapter, subclassing HTTParty::ConnectionAdapter will make it easier. Just override the #connection method. The uri and options attributes will have all the info you need to construct your http connection. Whatever you return from your connection method needs to adhere to the Net::HTTP interface as this is what HTTParty expects.
Configuration
There is lots of configuration data available for your connection adapter in the #options attribute. It is up to you to interpret them within your connection adapter. Take a look at the implementation of HTTParty::ConnectionAdapter#connection for examples of how they are used. The keys used in options are
-
:
timeout
: timeout in seconds -
:
open_timeout
: http connection open_timeout in seconds, overrides timeout if set -
:
read_timeout
: http connection read_timeout in seconds, overrides timeout if set -
:
write_timeout
: http connection write_timeout in seconds, overrides timeout if set (Ruby >= 2.6.0 required) -
:
debug_output
: see HTTParty::ClassMethods.debug_output. -
:
cert_store
: contains certificate data. see method ‘attach_ssl_certificates’ -
:
pem
: contains pem client certificate data. see method ‘attach_ssl_certificates’ -
:
p12
: contains PKCS12 client client certificate data. see method ‘attach_ssl_certificates’ -
:
verify
: verify the server’s certificate against the ca certificate. -
:
verify_peer
: set to false to turn off server verification but still send client certificate -
:
ssl_ca_file
: see HTTParty::ClassMethods.ssl_ca_file. -
:
ssl_ca_path
: see HTTParty::ClassMethods.ssl_ca_path. -
:
ssl_version
: SSL versions to allow. see method ‘attach_ssl_certificates’ -
:
ciphers
: The list of SSL ciphers to support -
:
connection_adapter_options
: contains the hash you passed to HTTParty.connection_adapter when you configured your connection adapter -
:
local_host
: The local address to bind to -
:
local_port
: The local port to bind to -
:
http_proxyaddr
: HTTP Proxy address -
:
http_proxyport
: HTTP Proxy port -
:
http_proxyuser
: HTTP Proxy user -
:
http_proxypass
: HTTP Proxy password
Inherited methods
-
:
clean_host
: Method used to sanitize host names
Constant Summary collapse
- StripIpv6BracketsRegex =
Private: Regex used to strip brackets from IPv6 URIs.
/\A\[(.*)\]\z/
- OPTION_DEFAULTS =
{ verify: true, verify_peer: true }
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
- #connection ⇒ Object
-
#initialize(uri, options = {}) ⇒ ConnectionAdapter
constructor
A new instance of ConnectionAdapter.
Constructor Details
#initialize(uri, options = {}) ⇒ ConnectionAdapter
Returns a new instance of ConnectionAdapter.
91 92 93 94 95 96 97 |
# File 'lib/httparty/connection_adapter.rb', line 91 def initialize(uri, = {}) uri_adapter = [:uri_adapter] || URI raise ArgumentError, "uri must be a #{uri_adapter}, not a #{uri.class}" unless uri.is_a? uri_adapter @uri = uri @options = OPTION_DEFAULTS.merge() end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
89 90 91 |
# File 'lib/httparty/connection_adapter.rb', line 89 def @options end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
89 90 91 |
# File 'lib/httparty/connection_adapter.rb', line 89 def uri @uri end |
Class Method Details
.call(uri, options) ⇒ Object
Public
79 80 81 |
# File 'lib/httparty/connection_adapter.rb', line 79 def self.call(uri, ) new(uri, ).connection end |
.default_cert_store ⇒ Object
83 84 85 86 87 |
# File 'lib/httparty/connection_adapter.rb', line 83 def self.default_cert_store @default_cert_store ||= OpenSSL::X509::Store.new.tap do |cert_store| cert_store.set_default_paths end end |
Instance Method Details
#connection ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/httparty/connection_adapter.rb', line 99 def connection host = clean_host(uri.host) port = uri.port || (uri.scheme == 'https' ? 443 : 80) if .key?(:http_proxyaddr) http = Net::HTTP.new( host, port, [:http_proxyaddr], [:http_proxyport], [:http_proxyuser], [:http_proxypass] ) else http = Net::HTTP.new(host, port) end http.use_ssl = ssl_implied?(uri) attach_ssl_certificates(http, ) if add_timeout?([:timeout]) http.open_timeout = [:timeout] http.read_timeout = [:timeout] from_ruby_version('2.6.0', option: :write_timeout, warn: false) do http.write_timeout = [:timeout] end end if add_timeout?([:read_timeout]) http.read_timeout = [:read_timeout] end if add_timeout?([:open_timeout]) http.open_timeout = [:open_timeout] end if add_timeout?([:write_timeout]) from_ruby_version('2.6.0', option: :write_timeout) do http.write_timeout = [:write_timeout] end end if add_max_retries?([:max_retries]) from_ruby_version('2.5.0', option: :max_retries) do http.max_retries = [:max_retries] end end if [:debug_output] http.set_debug_output([:debug_output]) end if [:ciphers] http.ciphers = [:ciphers] end # Bind to a specific local address or port # # @see https://bugs.ruby-lang.org/issues/6617 if [:local_host] from_ruby_version('2.0.0', option: :local_host) do http.local_host = [:local_host] end end if [:local_port] from_ruby_version('2.0.0', option: :local_port) do http.local_port = [:local_port] end end http end |