Class: HTTPX::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/options.rb,
sig/options.rbs

Overview

Contains a set of options which are passed and shared across from session to its requests or responses.

Constant Summary collapse

BUFFER_SIZE =

Returns:

  • (Integer)
1 << 14
WINDOW_SIZE =

16K

Returns:

  • (Integer)
1 << 14
MAX_BODY_THRESHOLD_SIZE =

112K

Returns:

  • (Integer)
(1 << 10) * 112
KEEP_ALIVE_TIMEOUT =

Returns:

  • (Integer)
20
PING_TIMEOUT =

Returns:

  • (Integer)
2
SETTINGS_TIMEOUT =

Returns:

  • (Integer)
10
CLOSE_HANDSHAKE_TIMEOUT =

Returns:

  • (Integer)
10
CONNECT_TIMEOUT =

Returns:

  • (Integer)
READ_TIMEOUT = WRITE_TIMEOUT = 60
REQUEST_TIMEOUT =

Returns:

  • (Integer)
OPERATION_TIMEOUT = TOTAL_REQUEST_TIMEOUT = nil
RESOLVER_TYPES =

Returns:

  • (Array[Symbol])
%i[memory file].freeze
USER_AGENT =

default value used for "user-agent" header, when not overridden.

Returns:

  • (String)
"httpx.rb/#{VERSION}".freeze
REQUEST_BODY_IVARS =

Returns:

  • (Array[Symbol])
%i[@headers].freeze
RESOLVER_IVARS =

Returns:

  • (Array[Symbol])
%i[
  @resolver_class @resolver_cache @resolver_options
  @resolver_native_class @resolver_system_class @resolver_https_class
].freeze
SET_TEMPORARY_NAME =

rubocop:disable Lint/UselessConstantScoping these really need to be defined at the end of the class

Returns:

  • (^(Class klass, ?Symbol pl) -> void)
->(klass, pl = nil) do
  if klass.respond_to?(:set_temporary_name) # ruby 3.4 only
    name = klass.name || "#{klass.superclass.name}(plugin)"
    name = "#{name}/#{pl}" if pl
    klass.set_temporary_name(name)
  end
end
DEFAULT_OPTIONS =

Returns:

  • (Hash[Symbol, untyped])
{
  :max_requests => Float::INFINITY,
  :debug => nil,
  :debug_level => (ENV["HTTPX_DEBUG"] || 1).to_i,
  :debug_redact => ENV.key?("HTTPX_DEBUG_REDACT"),
  :ssl => EMPTY_HASH,
  :http2_settings => { settings_enable_push: 0 }.freeze,
  :fallback_protocol => "http/1.1",
  :supported_compression_formats => %w[gzip deflate],
  :decompress_response_body => true,
  :compress_request_body => true,
  :max_response_headers => 1000,
  :max_response_header_value_size => nil,
  :max_response_body_size => Float::INFINITY,
  :timeout => {
    connect_timeout: CONNECT_TIMEOUT,
    settings_timeout: SETTINGS_TIMEOUT,
    close_handshake_timeout: CLOSE_HANDSHAKE_TIMEOUT,
    operation_timeout: OPERATION_TIMEOUT,
    keep_alive_timeout: KEEP_ALIVE_TIMEOUT,
    ping_timeout: PING_TIMEOUT,
    read_timeout: READ_TIMEOUT,
    write_timeout: WRITE_TIMEOUT,
    request_timeout: REQUEST_TIMEOUT,
    total_request_timeout: TOTAL_REQUEST_TIMEOUT,
  }.freeze,
  :headers_class => Class.new(Headers, &SET_TEMPORARY_NAME),
  :headers => EMPTY_HASH,
  :window_size => WINDOW_SIZE,
  :buffer_size => BUFFER_SIZE,
  :body_threshold_size => MAX_BODY_THRESHOLD_SIZE,
  :request_class => Class.new(Request, &SET_TEMPORARY_NAME),
  :response_class => Class.new(Response, &SET_TEMPORARY_NAME),
  :request_body_class => Class.new(Request::Body, &SET_TEMPORARY_NAME),
  :response_body_class => Class.new(Response::Body, &SET_TEMPORARY_NAME),
  :pool_class => Class.new(Pool, &SET_TEMPORARY_NAME),
  :connection_class => Class.new(Connection, &SET_TEMPORARY_NAME),
  :http1_class => Class.new(Connection::HTTP1, &SET_TEMPORARY_NAME),
  :http2_class => Class.new(Connection::HTTP2, &SET_TEMPORARY_NAME),
  :resolver_native_class => Class.new(Resolver::Native, &SET_TEMPORARY_NAME),
  :resolver_system_class => Class.new(Resolver::System, &SET_TEMPORARY_NAME),
  :resolver_https_class => Class.new(Resolver::HTTPS, &SET_TEMPORARY_NAME),
  :options_class => Class.new(self, &SET_TEMPORARY_NAME),
  :transport => nil,
  :addresses => nil,
  :persistent => false,
  :resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym,
  :resolver_cache => (ENV["HTTPX_RESOLVER_CACHE"] || :memory).to_sym,
  :resolver_options => { cache: true }.freeze,
  :pool_options => EMPTY_HASH,
  :ip_families => nil,
  :close_on_fork => false,
}.each_value(&:freeze).freeze
READ_TIMEOUT =

Returns:

  • (Integer)
WRITE_TIMEOUT =

Returns:

  • (Integer)
TOTAL_REQUEST_TIMEOUT =

Returns:

  • (Integer)
OPERATION_TIMEOUT =

Returns:

  • (Integer)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = EMPTY_HASH) ⇒ Options

creates a new options instance from a given hash, which optionally define the following:

:debug :: an object which log messages are written to (must respond to <<) :debug_level :: the log level of messages (can be 1, 2, or 3). :debug_redact :: whether header/body payload should be redacted (defaults to false). :ssl :: a hash of options which can be set as params of OpenSSL::SSL::SSLContext (see HTTPX::SSL) :http2_settings :: a hash of options to be passed to a HTTP2::Connection (ex: { max_concurrent_streams: 2 }) :fallback_protocol :: version of HTTP protocol to use by default in the absence of protocol negotiation like ALPN (defaults to "http/1.1") :supported_compression_formats :: list of compressions supported by the transcoder layer (defaults to %w[gzip deflate]). :decompress_response_body :: whether to auto-decompress response body (defaults to true). :compress_request_body :: whether to auto-decompress response body (defaults to true) :timeout :: hash of timeout configurations (supports :connect_timeout, :settings_timeout, :operation_timeout, :keep_alive_timeout, :read_timeout, :write_timeout, :request_timeout, :total_request_timeout and :ping_timeout, :headers :: hash of HTTP headers (ex: { "x-custom-foo" => "bar" }) :max_response_body_size :: maximum size (in bytes) that the response body can consume (no threshold by default), after which an error is raised. :max_response_headers :: maximum number of header fields that a response can receive, after which an error is raised. :max_response_header_value_size :: maximum size (in bytes) a header value can have (no threshold by default). for cases where the value is broken into multiple header fields (such as "cookie" or "set-cookie"), this is the total aggregated size. :window_size :: number of bytes to read from a socket :buffer_size :: internal read and write buffer size in bytes :body_threshold_size :: maximum size in bytes of response payload that is buffered in memory. :request_class :: class used to instantiate a request :response_class :: class used to instantiate a response :headers_class :: class used to instantiate headers :request_body_class :: class used to instantiate a request body :response_body_class :: class used to instantiate a response body :connection_class :: class used to instantiate connections :http1_class :: class used to manage HTTP1 sessions :http2_class :: class used to imanage HTTP2 sessions :resolver_native_class :: class used to resolve names using pure ruby DNS implementation :resolver_system_class :: class used to resolve names using system-based (getaddrinfo) name resolution :resolver_https_class :: class used to resolve names using DoH :pool_class :: class used to instantiate the session connection pool :options_class :: class used to instantiate options :transport :: type of transport to use (set to "unix" for UNIX sockets) :addresses :: bucket of peer addresses (can be a list of IP addresses, a hash of domain to list of adddresses; paths should be used for UNIX sockets instead) :io :: open socket, or domain/ip-to-socket hash, which requests should be sent to :persistent :: whether to persist connections in between requests (defaults to true) :resolver_class :: which resolver to use (defaults to :native, can also be :system for using getaddrinfo or :https for DoH resolver, or a custom class inheriting from HTTPX::Resolver::Resolver) :resolver_cache :: strategy to cache DNS results, ignored by the :system resolver, can be set to :memory or an instance of a custom class inheriting from HTTPX::Resolver::Cache::Base :resolver_options :: hash of options passed to the resolver. Accepted keys depend on the resolver type. :pool_options :: hash of options passed to the connection pool (See Pool#initialize). :ip_families :: which socket families are supported (system-dependent) :origin :: HTTP origin to set on requests with relative path (ex: "https://api.serv.com&quot;) :base_path :: path to prefix given relative paths with (ex: "/v2") :max_concurrent_requests :: max number of requests which can be set concurrently :max_requests :: max number of requests which can be made on socket before it reconnects. :close_on_fork :: whether the session automatically closes when the process is fork (defaults to false). it only works if the session is persistent (and ruby 3.1 or higher is used).

This list of options are enhanced with each loaded plugin, see the plugin docs for details.

Parameters:

  • options (options) (defaults to: EMPTY_HASH)


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
# File 'lib/httpx/options.rb', line 128

def initialize(options = EMPTY_HASH)
  options_names = self.class.options_names

  defaults =
    case options
    when Options
      unknown_options = options.class.options_names - options_names

      raise Error, "unknown option: `:#{unknown_options.first}`" unless unknown_options.empty?

      DEFAULT_OPTIONS.merge(options)
    else
      options.each_key do |k|
        raise Error, "unknown option: `:#{k}`" unless options_names.include?(k)
      end

      options.empty? ? DEFAULT_OPTIONS : DEFAULT_OPTIONS.merge(options)
    end

  options_names.each do |k|
    v = defaults[k]

    if v.nil?
      instance_variable_set(:"@#{k}", v)

      next
    end

    value = __send__(:"option_#{k}", v)
    instance_variable_set(:"@#{k}", value)
  end

  do_initialize
  freeze
end

Class Attribute Details

.options_namesArray[Symbol] (readonly)

Returns the value of attribute options_names.

Returns:

  • (Array[Symbol])


23
24
25
# File 'lib/httpx/options.rb', line 23

def options_names
  @options_names
end

Instance Attribute Details

#addressesArray[Resolver::Entry]? (readonly)

addresses

Returns:



77
78
79
# File 'sig/options.rbs', line 77

def addresses
  @addresses
end

#base_pathString? (readonly)

base_path

Returns:

  • (String, nil)


92
93
94
# File 'sig/options.rbs', line 92

def base_path
  @base_path
end

#body_threshold_sizeInteger (readonly)

body_threshold_size

Returns:

  • (Integer)


62
63
64
# File 'sig/options.rbs', line 62

def body_threshold_size
  @body_threshold_size
end

#buffer_sizeInteger (readonly)

buffer_size

Returns:

  • (Integer)


59
60
61
# File 'sig/options.rbs', line 59

def buffer_size
  @buffer_size
end

#close_on_forkBoolean (readonly)

close_on_fork

Returns:

  • (Boolean)


142
143
144
# File 'sig/options.rbs', line 142

def close_on_fork
  @close_on_fork
end

#compress_request_bodyBoolean (readonly)

compress_request_body

Returns:

  • (Boolean)


83
84
85
# File 'sig/options.rbs', line 83

def compress_request_body
  @compress_request_body
end

#connection_classsingleton(Connection) (readonly)

Returns the value of attribute connection_class.

Returns:



105
106
107
# File 'sig/options.rbs', line 105

def connection_class
  @connection_class
end

#debug_IOLogger? (readonly)

debug

Returns:



133
134
135
# File 'sig/options.rbs', line 133

def debug
  @debug
end

#debug_levelInteger (readonly)

debug_level

Returns:

  • (Integer)


136
137
138
# File 'sig/options.rbs', line 136

def debug_level
  @debug_level
end

#decompress_response_bodyBoolean (readonly)

decompress_response_body

Returns:

  • (Boolean)


86
87
88
# File 'sig/options.rbs', line 86

def decompress_response_body
  @decompress_response_body
end

#fallback_protocolString (readonly)

fallback_protocol

Returns:

  • (String)


130
131
132
# File 'sig/options.rbs', line 130

def fallback_protocol
  @fallback_protocol
end

#headersheaders? (readonly)

headers

Returns:



41
42
43
# File 'sig/options.rbs', line 41

def headers
  @headers
end

#headers_classsingleton(Headers) (readonly)

Returns the value of attribute headers_class.

Returns:



113
114
115
# File 'sig/options.rbs', line 113

def headers_class
  @headers_class
end

#http1_classsingleton(Connection::HTTP1) (readonly)

Returns the value of attribute http1_class.

Returns:



101
102
103
# File 'sig/options.rbs', line 101

def http1_class
  @http1_class
end

#http2_classsingleton(Connection::HTTP2) (readonly)

Returns the value of attribute http2_class.

Returns:



103
104
105
# File 'sig/options.rbs', line 103

def http2_class
  @http2_class
end

#http2_settingsHash[Symbol, Integer | bool] (readonly)

http2_settings

Returns:

  • (Hash[Symbol, Integer | bool])


47
48
49
# File 'sig/options.rbs', line 47

def http2_settings
  @http2_settings
end

#ioio_option? (readonly)

Returns the value of attribute io.

Returns:

  • (io_option, nil)


127
128
129
# File 'sig/options.rbs', line 127

def io
  @io
end

#ip_familiesArray[ip_family]? (readonly)

ip_families

Returns:

  • (Array[ip_family], nil)


151
152
153
# File 'sig/options.rbs', line 151

def ip_families
  @ip_families
end

#max_concurrent_requestsmaybe_unbounded? (readonly)

max_concurrent_requests

Returns:

  • (maybe_unbounded, nil)


50
51
52
# File 'sig/options.rbs', line 50

def max_concurrent_requests
  @max_concurrent_requests
end

#max_requestsmaybe_unbounded? (readonly)

max_requests

Returns:

  • (maybe_unbounded, nil)


53
54
55
# File 'sig/options.rbs', line 53

def max_requests
  @max_requests
end

#max_response_body_sizemaybe_unbounded (readonly)

max_response_body_size

Returns:

  • (maybe_unbounded)


71
72
73
# File 'sig/options.rbs', line 71

def max_response_body_size
  @max_response_body_size
end

#max_response_header_value_sizemaybe_unbounded? (readonly)

max_response_header_value_size

Returns:

  • (maybe_unbounded, nil)


68
69
70
# File 'sig/options.rbs', line 68

def max_response_header_value_size
  @max_response_header_value_size
end

#max_response_headersInteger (readonly)

max_response_headers

Returns:

  • (Integer)


65
66
67
# File 'sig/options.rbs', line 65

def max_response_headers
  @max_response_headers
end

#options_classsingleton(Options) (readonly)

Returns the value of attribute options_class.

Returns:



119
120
121
# File 'sig/options.rbs', line 119

def options_class
  @options_class
end

#originURI::Generic? (readonly)

origin

Returns:

  • (URI::Generic, nil)


89
90
91
# File 'sig/options.rbs', line 89

def origin
  @origin
end

#persistentBoolean (readonly)

persistent

Returns:

  • (Boolean)


139
140
141
# File 'sig/options.rbs', line 139

def persistent
  @persistent
end

#pool_classsingleton(Pool) (readonly)

Returns the value of attribute pool_class.

Returns:



107
108
109
# File 'sig/options.rbs', line 107

def pool_class
  @pool_class
end

#pool_optionspool_options (readonly)

resolver_options

Returns:



148
149
150
# File 'sig/options.rbs', line 148

def pool_options
  @pool_options
end

#request_body_classsingleton(Request::Body) (readonly)

Returns the value of attribute request_body_class.

Returns:



115
116
117
# File 'sig/options.rbs', line 115

def request_body_class
  @request_body_class
end

#request_classsingleton(Request) (readonly)

Returns the value of attribute request_class.

Returns:



109
110
111
# File 'sig/options.rbs', line 109

def request_class
  @request_class
end

#resolver_https_classsingleton(Resolver::HTTPS) (readonly)

Returns the value of attribute resolver_https_class.

Returns:



99
100
101
# File 'sig/options.rbs', line 99

def resolver_https_class
  @resolver_https_class
end

#resolver_native_classsingleton(Resolver::Native) (readonly)

classes

Returns:



95
96
97
# File 'sig/options.rbs', line 95

def resolver_native_class
  @resolver_native_class
end

#resolver_optionsHash[Symbol, untyped] (readonly)

resolver_options

Returns:

  • (Hash[Symbol, untyped])


145
146
147
# File 'sig/options.rbs', line 145

def resolver_options
  @resolver_options
end

#resolver_system_classsingleton(Resolver::System) (readonly)

Returns the value of attribute resolver_system_class.

Returns:



97
98
99
# File 'sig/options.rbs', line 97

def resolver_system_class
  @resolver_system_class
end

#response_body_classsingleton(Response::Body) (readonly)

Returns the value of attribute response_body_class.

Returns:



117
118
119
# File 'sig/options.rbs', line 117

def response_body_class
  @response_body_class
end

#response_classsingleton(Response) (readonly)

Returns the value of attribute response_class.

Returns:



111
112
113
# File 'sig/options.rbs', line 111

def response_class
  @response_class
end

#sslHash[Symbol, untyped] (readonly)

Returns the value of attribute ssl.

Returns:

  • (Hash[Symbol, untyped])


121
122
123
# File 'sig/options.rbs', line 121

def ssl
  @ssl
end

#supported_compression_formatsArray[String] (readonly)

supported_compression_formats

Returns:

  • (Array[String])


80
81
82
# File 'sig/options.rbs', line 80

def supported_compression_formats
  @supported_compression_formats
end

#timeouttimeout (readonly)

timeout

Returns:



44
45
46
# File 'sig/options.rbs', line 44

def timeout
  @timeout
end

#transportio_type? (readonly)

transport

Returns:

  • (io_type, nil)


74
75
76
# File 'sig/options.rbs', line 74

def transport
  @transport
end

#uriURI? (readonly)

headers

Returns:

  • (URI, nil)


38
39
40
# File 'sig/options.rbs', line 38

def uri
  @uri
end

#window_sizeInteger (readonly)

window_size

Returns:

  • (Integer)


56
57
58
# File 'sig/options.rbs', line 56

def window_size
  @window_size
end

Class Method Details

.freezeObject



38
39
40
41
# File 'lib/httpx/options.rb', line 38

def freeze
  @options_names.freeze
  super
end

.inherited(klass) ⇒ Object



25
26
27
28
# File 'lib/httpx/options.rb', line 25

def inherited(klass)
  super
  klass.instance_variable_set(:@options_names, @options_names.dup)
end

.method_added(meth) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/httpx/options.rb', line 43

def method_added(meth)
  super

  return unless meth =~ /^option_(.+)$/

  optname = Regexp.last_match(1) #: String

  if optname =~ /^(.+[^_])_+with/
    # ignore alias method chain generated methods.
    # this is the case with RBS runtime tests.
    # it relies on the "_with/_without" separator, which is the most used convention,
    # however it shouldn't be used in practice in httpx given the plugin architecture
    # as the main extension API.
    orig_name = Regexp.last_match(1) #: String

    return if @options_names.include?(orig_name.to_sym)
  end

  optname = optname.to_sym

  attr_reader(optname) unless method_defined?(optname)

  @options_names << optname unless @options_names.include?(optname)
end

.new(options = {}) ⇒ instance

Parameters:

  • (options)

Returns:

  • (instance)


30
31
32
33
34
35
36
# File 'lib/httpx/options.rb', line 30

def new(options = {})
  # let enhanced options go through
  return options if self == Options && options.class < self
  return options if options.is_a?(self)

  super
end

Instance Method Details

#==Boolean

Parameters:

Returns:

  • (Boolean)


159
# File 'sig/options.rbs', line 159

def ==: (Options other) -> bool

#access_option(obj, k, ivar_map) ⇒ Object

Parameters:

  • obj (Hash[Symbol, untyped], Object, nil)
  • k (Symbol)
  • ivar_map (Hash[Symbol, Symbol], nil)

Returns:

  • (Object)


542
543
544
545
546
547
548
549
# File 'lib/httpx/options.rb', line 542

def access_option(obj, k, ivar_map)
  case obj
  when Hash
    obj[ivar_map[k]]
  else
    obj.instance_variable_get(k)
  end
end

#connection_options_match?(other, ignore_ivars = nil) ⇒ Boolean

checks whether other matches the same connection-level options

Parameters:

  • other (Options)
  • ignore_ivars (Array[Symbol]) (defaults to: nil)

Returns:

  • (Boolean)


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/httpx/options.rb', line 212

def connection_options_match?(other, ignore_ivars = nil)
  return true if self == other

  # headers and other request options do not play a role, as they are
  # relevant only for the request.
  ivars = instance_variables
  ivars.reject! { |iv| REQUEST_BODY_IVARS.include?(iv) }
  ivars.reject! { |iv| ignore_ivars.include?(iv) } if ignore_ivars

  other_ivars = other.instance_variables
  other_ivars.reject! { |iv| REQUEST_BODY_IVARS.include?(iv) }
  other_ivars.reject! { |iv| ignore_ivars.include?(iv) } if ignore_ivars

  return false if ivars.size != other_ivars.size

  return false if ivars.sort != other_ivars.sort

  ivars.all? do |ivar|
    instance_variable_get(ivar) == other.instance_variable_get(ivar)
  end
end

#do_initializevoid

This method returns an undefined value.

called after all options are initialized



529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/httpx/options.rb', line 529

def do_initialize
  hs = @headers

  # initialized default request headers
  hs["user-agent"] = USER_AGENT unless hs.key?("user-agent")
  hs["accept"] = "*/*" unless hs.key?("accept")
  if hs.key?("range")
    hs.delete("accept-encoding")
  else
    hs["accept-encoding"] = supported_compression_formats unless hs.key?("accept-encoding")
  end
end

#extend_with_plugin_classes(pl) ⇒ void

This method returns an undefined value.

Parameters:

  • pl (Module)


312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/httpx/options.rb', line 312

def extend_with_plugin_classes(pl)
  # extend request class
  if defined?(pl::RequestMethods) || defined?(pl::RequestClassMethods)
    @request_class = @request_class.dup
    SET_TEMPORARY_NAME[@request_class, pl]
    @request_class.__send__(:include, pl::RequestMethods) if defined?(pl::RequestMethods)
    @request_class.extend(pl::RequestClassMethods) if defined?(pl::RequestClassMethods)
  end
  # extend response class
  if defined?(pl::ResponseMethods) || defined?(pl::ResponseClassMethods)
    @response_class = @response_class.dup
    SET_TEMPORARY_NAME[@response_class, pl]
    @response_class.__send__(:include, pl::ResponseMethods) if defined?(pl::ResponseMethods)
    @response_class.extend(pl::ResponseClassMethods) if defined?(pl::ResponseClassMethods)
  end
  # extend headers class
  if defined?(pl::HeadersMethods) || defined?(pl::HeadersClassMethods)
    @headers_class = @headers_class.dup
    SET_TEMPORARY_NAME[@headers_class, pl]
    @headers_class.__send__(:include, pl::HeadersMethods) if defined?(pl::HeadersMethods)
    @headers_class.extend(pl::HeadersClassMethods) if defined?(pl::HeadersClassMethods)
  end
  # extend request body class
  if defined?(pl::RequestBodyMethods) || defined?(pl::RequestBodyClassMethods)
    @request_body_class = @request_body_class.dup
    SET_TEMPORARY_NAME[@request_body_class, pl]
    @request_body_class.__send__(:include, pl::RequestBodyMethods) if defined?(pl::RequestBodyMethods)
    @request_body_class.extend(pl::RequestBodyClassMethods) if defined?(pl::RequestBodyClassMethods)
  end
  # extend response body class
  if defined?(pl::ResponseBodyMethods) || defined?(pl::ResponseBodyClassMethods)
    @response_body_class = @response_body_class.dup
    SET_TEMPORARY_NAME[@response_body_class, pl]
    @response_body_class.__send__(:include, pl::ResponseBodyMethods) if defined?(pl::ResponseBodyMethods)
    @response_body_class.extend(pl::ResponseBodyClassMethods) if defined?(pl::ResponseBodyClassMethods)
  end
  # extend connection pool class
  if defined?(pl::PoolMethods)
    @pool_class = @pool_class.dup
    SET_TEMPORARY_NAME[@pool_class, pl]
    @pool_class.__send__(:include, pl::PoolMethods)
  end
  # extend connection class
  if defined?(pl::ConnectionMethods)
    @connection_class = @connection_class.dup
    SET_TEMPORARY_NAME[@connection_class, pl]
    @connection_class.__send__(:include, pl::ConnectionMethods)
  end
  # extend http1 class
  if defined?(pl::HTTP1Methods)
    @http1_class = @http1_class.dup
    SET_TEMPORARY_NAME[@http1_class, pl]
    @http1_class.__send__(:include, pl::HTTP1Methods)
  end
  # extend http2 class
  if defined?(pl::HTTP2Methods)
    @http2_class = @http2_class.dup
    SET_TEMPORARY_NAME[@http2_class, pl]
    @http2_class.__send__(:include, pl::HTTP2Methods)
  end
  # extend native resolver class
  if defined?(pl::ResolverNativeMethods)
    @resolver_native_class = @resolver_native_class.dup
    SET_TEMPORARY_NAME[@resolver_native_class, pl]
    @resolver_native_class.__send__(:include, pl::ResolverNativeMethods)
  end
  # extend system resolver class
  if defined?(pl::ResolverSystemMethods)
    @resolver_system_class = @resolver_system_class.dup
    SET_TEMPORARY_NAME[@resolver_system_class, pl]
    @resolver_system_class.__send__(:include, pl::ResolverSystemMethods)
  end
  # extend https resolver class
  if defined?(pl::ResolverHTTPSMethods)
    @resolver_https_class = @resolver_https_class.dup
    SET_TEMPORARY_NAME[@resolver_https_class, pl]
    @resolver_https_class.__send__(:include, pl::ResolverHTTPSMethods)
  end

  return unless defined?(pl::OptionsMethods)

  # extend option class
  # works around lack of initialize_dup callback
  @options_class = @options_class.dup
  # (self.class.options_names)
  @options_class.__send__(:include, pl::OptionsMethods)
end

#freezeObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/httpx/options.rb', line 194

def freeze
  self.class.options_names.each do |ivar|
    # avoid freezing debug option, as when it's set, it's usually an
    # object which cannot be frozen, like stderr or stdout. It's a
    # documented exception then, and still does not defeat the purpose
    # here, which is to make option objects shareable across ractors,
    # and in most cases debug should be nil, or one of the objects
    # which will eventually be shareable, like STDOUT or STDERR.
    next if ivar == :debug

    instance_variable_get(:"@#{ivar}").freeze
  end
  super
end

#merge(other) ⇒ instance, self

returns a HTTPX::Options instance resulting of the merging of other with self. it may return self if other is self or equal to self.

Parameters:

  • other (Object & _ToHash[Symbol, untyped])

Returns:

  • (instance, self)


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/httpx/options.rb', line 249

def merge(other)
  if (is_options = other.is_a?(Options))

    return self if eql?(other)

    opts_names = other.class.options_names

    return self if opts_names.all? { |opt| public_send(opt) == other.public_send(opt) }

    other_opts = opts_names
  else
    other_opts = other #: Hash[Symbol, untyped]
    other_opts = Hash[other] unless other.is_a?(Hash)

    return self if other_opts.empty?

    return self if other_opts.all? do |opt, v|
      unless respond_to?(opt)
        # TODO: do this instead in a major or minor version bump
        # raise Error, "unknown option: #{opt}" unless respond_to?(opt)
        warn "DEPRECATION WARNING: unknown option: `:#{opt}`. an exception will be raised in a future version."
        next(true)
      end

      !respond_to?(opt) || public_send(opt) == v
    end
  end

  opts = dup

  other_opts.each do |opt, v|
    next unless respond_to?(opt)

    v = other.public_send(opt) if is_options
    ivar = :"@#{opt}"

    unless v
      opts.instance_variable_set(ivar, v)
      next
    end

    v = opts.__send__(:"option_#{opt}", v)

    orig_v = public_send(opt)

    v = orig_v.merge(v) if orig_v.respond_to?(:merge) && v.respond_to?(:merge)

    opts.instance_variable_set(ivar, v)
  end

  opts
end

#option_addresses(value) ⇒ Array[ipaddr]

Parameters:

  • value (ipaddr, _ToAry[ipaddr])

Returns:

  • (Array[ipaddr])


485
486
487
# File 'lib/httpx/options.rb', line 485

def option_addresses(value)
  Array(value).map { |entry| Resolver::Entry.convert(entry) }
end

#option_base_path(value) ⇒ String

Parameters:

  • value (_ToStr)

Returns:

  • (String)


447
448
449
# File 'lib/httpx/options.rb', line 447

def option_base_path(value)
  String(value)
end

#option_body_threshold_sizeInteger

Parameters:

  • value (int)

Returns:

  • (Integer)


184
# File 'sig/options.rbs', line 184

def option_body_threshold_size: (int value) -> Integer

#option_buffer_sizeInteger

Parameters:

  • value (int)

Returns:

  • (Integer)


183
# File 'sig/options.rbs', line 183

def option_buffer_size: (int value) -> Integer

#option_close_on_forkBoolean

Parameters:

  • value (Boolean)

Returns:

  • (Boolean)


210
# File 'sig/options.rbs', line 210

def option_close_on_fork: (bool value) -> bool

#option_compress_request_bodyBoolean

Parameters:

  • value (Boolean)

Returns:

  • (Boolean)


207
# File 'sig/options.rbs', line 207

def option_compress_request_body: (bool value) -> bool

#option_connection_classsingleton(Connection)

Parameters:

Returns:



199
# File 'sig/options.rbs', line 199

def option_connection_class: (singleton(Connection) value) -> singleton(Connection)

#option_debug_IOLogger

Parameters:

Returns:



205
# File 'sig/options.rbs', line 205

def option_debug: (_IOLogger value) -> _IOLogger

#option_debug_levelInteger

Parameters:

  • value (int)

Returns:

  • (Integer)


185
# File 'sig/options.rbs', line 185

def option_debug_level: (int value) -> Integer

#option_debug_redactredact_value

Parameters:

  • value (redact_value)

Returns:

  • (redact_value)


206
# File 'sig/options.rbs', line 206

def option_debug_redact: (redact_value value) -> redact_value

#option_decompress_response_bodyBoolean

Parameters:

  • value (Boolean)

Returns:

  • (Boolean)


208
# File 'sig/options.rbs', line 208

def option_decompress_response_body: (bool value) -> bool

#option_fallback_protocolString

Parameters:

  • value (String)

Returns:

  • (String)


204
# File 'sig/options.rbs', line 204

def option_fallback_protocol: (String value) -> String

#option_headers(value) ⇒ Headers

Parameters:

Returns:



451
452
453
454
455
# File 'lib/httpx/options.rb', line 451

def option_headers(value)
  value = value.dup if value.frozen?

  headers_class.new(value)
end

#option_headers_classsingleton(Headers)

Parameters:

Returns:



196
# File 'sig/options.rbs', line 196

def option_headers_class: (singleton(Headers) value) -> singleton(Headers)

#option_http2_settingsHash[Symbol, untyped]

Parameters:

  • value (_ToHash[Symbol, untyped])

Returns:

  • (Hash[Symbol, untyped])


189
# File 'sig/options.rbs', line 189

def option_http2_settings: (_ToHash[Symbol, untyped] value) -> Hash[Symbol, untyped]

#option_ioio_option

Parameters:

  • value (io_option)

Returns:

  • (io_option)


203
# File 'sig/options.rbs', line 203

def option_io: (io_option value) -> io_option

#option_ip_families(value) ⇒ Array[ip_family]

Parameters:

  • value (ip_family, _ToAry[ip_family])

Returns:

  • (Array[ip_family])


489
490
491
# File 'lib/httpx/options.rb', line 489

def option_ip_families(value)
  Array(value)
end

#option_max_concurrent_requestsmaybe_unbounded

integer

Parameters:

  • value (int)

Returns:

  • (maybe_unbounded)


180
# File 'sig/options.rbs', line 180

def option_max_concurrent_requests: (int value) -> maybe_unbounded

#option_max_requestsmaybe_unbounded

Parameters:

  • value (int)

Returns:

  • (maybe_unbounded)


181
# File 'sig/options.rbs', line 181

def option_max_requests: (int value) -> maybe_unbounded

#option_options_classsingleton(Options)

Parameters:

Returns:



200
# File 'sig/options.rbs', line 200

def option_options_class: (singleton(Options) value) -> singleton(Options)

#option_origin(value) ⇒ http_uri

Parameters:

  • value (http_uri, String)

Returns:

  • (http_uri)


443
444
445
# File 'lib/httpx/options.rb', line 443

def option_origin(value)
  URI(value)
end

#option_persistentBoolean

Parameters:

  • value (Boolean)

Returns:

  • (Boolean)


209
# File 'sig/options.rbs', line 209

def option_persistent: (bool value) -> bool

#option_pool_classsingleton(Pool)

Parameters:

  • value (singleton(Pool))

Returns:



201
# File 'sig/options.rbs', line 201

def option_pool_class: (singleton(Pool) value) -> singleton(Pool)

#option_pool_optionsHash[Symbol, untyped]

Parameters:

  • value (_ToHash[Symbol, untyped])

Returns:

  • (Hash[Symbol, untyped])


191
# File 'sig/options.rbs', line 191

def option_pool_options: (_ToHash[Symbol, untyped] value) -> Hash[Symbol, untyped]

#option_request_body_classsingleton(Request::Body)

Parameters:

Returns:



197
# File 'sig/options.rbs', line 197

def option_request_body_class: (singleton(Request::Body) value) -> singleton(Request::Body)

#option_request_classsingleton(Request)

no transform

Parameters:

Returns:



194
# File 'sig/options.rbs', line 194

def option_request_class: (singleton(Request) value) -> singleton(Request)

#option_resolver_cache(cache_type) ⇒ resolver_cache_option

Parameters:

  • value (resolver_cache_option)

Returns:

  • (resolver_cache_option)


510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/httpx/options.rb', line 510

def option_resolver_cache(cache_type)
  if cache_type.is_a?(Symbol)
    raise TypeError, ":resolver_cache: #{cache_type} is invalid" unless RESOLVER_TYPES.include?(cache_type)

    require "httpx/resolver/cache/file" if cache_type == :file

  else
    unless cache_type.respond_to?(:resolve) &&
           cache_type.respond_to?(:get) &&
           cache_type.respond_to?(:set) &&
           cache_type.respond_to?(:evict)
      raise TypeError, "`:resolver_cache` must be a compatible resolver cache and implement `#resolve`, `#get`, `#set` and `#evict`"
    end
  end

  cache_type
end

#option_resolver_class(resolver_type) ⇒ Symbol, resolver_type

Parameters:

  • value (Symbol, resolver_type)

Returns:

  • (Symbol, resolver_type)


493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/httpx/options.rb', line 493

def option_resolver_class(resolver_type)
  case resolver_type
  when Symbol
    meth = :"resolver_#{resolver_type}_class"

    raise TypeError, "`:resolver_class` must be a supported type" unless respond_to?(meth)

    resolver_type
  when Class
    raise TypeError, "`:resolver_class` must be a subclass of `#{Resolver::Resolver}`" unless resolver_type < Resolver::Resolver

    resolver_type
  else
    raise TypeError, "`:resolver_class` must be a supported type"
  end
end

#option_resolver_optionsHash[Symbol, untyped]

Parameters:

  • value (_ToHash[Symbol, untyped])

Returns:

  • (Hash[Symbol, untyped])


190
# File 'sig/options.rbs', line 190

def option_resolver_options: (_ToHash[Symbol, untyped] value) -> Hash[Symbol, untyped]

#option_response_body_classsingleton(Response::Body)

Parameters:

Returns:



198
# File 'sig/options.rbs', line 198

def option_response_body_class: (singleton(Response::Body) value) -> singleton(Response::Body)

#option_response_classsingleton(Response)

Parameters:

Returns:



195
# File 'sig/options.rbs', line 195

def option_response_class: (singleton(Response) value) -> singleton(Response)

#option_sslHash[Symbol, untyped]

to hash

Parameters:

  • value (_ToHash[Symbol, untyped])

Returns:

  • (Hash[Symbol, untyped])


188
# File 'sig/options.rbs', line 188

def option_ssl: (_ToHash[Symbol, untyped] value) -> Hash[Symbol, untyped]

#option_supported_compression_formats(value) ⇒ Array[String]

Parameters:

  • value (_ToS, _ToAry[_ToS])

Returns:

  • (Array[String])


474
475
476
# File 'lib/httpx/options.rb', line 474

def option_supported_compression_formats(value)
  Array(value).map(&:to_s)
end

#option_timeout(value) ⇒ timeout

Parameters:

  • (_ToHash[timeout_type, interval?])

Returns:



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/httpx/options.rb', line 457

def option_timeout(value)
  timeout_hash = Hash[value]

  default_timeouts = DEFAULT_OPTIONS[:timeout]

  # Validate keys and values
  timeout_hash.each do |key, val|
    raise TypeError, "invalid timeout: `:#{key}`" unless default_timeouts.key?(key)

    next if val.nil?

    raise TypeError, "`:#{key}` must be numeric" unless val.is_a?(Numeric)
  end

  timeout_hash
end

#option_transport(value) ⇒ String

Parameters:

  • value (_ToS)

Returns:

  • (String)

Raises:

  • (TypeError)


478
479
480
481
482
483
# File 'lib/httpx/options.rb', line 478

def option_transport(value)
  transport = value.to_s
  raise TypeError, "#{transport} is an unsupported transport type" unless %w[unix].include?(transport)

  transport
end

#option_window_sizeInteger

Parameters:

  • value (int)

Returns:

  • (Integer)


182
# File 'sig/options.rbs', line 182

def option_window_size: (int value) -> Integer

#resolver_cacheObject & Resolver::_Cache

Returns:



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/httpx/options.rb', line 174

def resolver_cache
  cache_type = @resolver_cache

  case cache_type
  when :memory
    Resolver::Cache::Memory.cache(cache_type)
  when :file
    Resolver::Cache::File.cache(cache_type)
  else
    unless cache_type.respond_to?(:resolve) &&
           cache_type.respond_to?(:get) &&
           cache_type.respond_to?(:set) &&
           cache_type.respond_to?(:evict)
      raise TypeError, "`:resolver_cache` must be a compatible resolver cache and implement `#get`, `#set` and `#evict`"
    end

    cache_type #: Object & Resolver::_Cache
  end
end

#resolver_classsingleton(Resolver::Resolver)

returns the class with which to instantiate the DNS resolver.

Returns:



165
166
167
168
169
170
171
172
# File 'lib/httpx/options.rb', line 165

def resolver_class
  case @resolver_class
  when Symbol
    public_send(:"resolver_#{@resolver_class}_class")
  else
    @resolver_class
  end
end

#resolver_options_match?(other) ⇒ Boolean

checks whether other matches the same resolver-level options

Parameters:

Returns:

  • (Boolean)


240
241
242
243
244
245
# File 'lib/httpx/options.rb', line 240

def resolver_options_match?(other)
  self == other ||
    RESOLVER_IVARS.all? do |ivar|
      instance_variable_get(ivar) == other.instance_variable_get(ivar)
    end
end

#to_hashHash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


302
303
304
305
306
307
308
309
310
# File 'lib/httpx/options.rb', line 302

def to_hash
  instance_variables.each_with_object({}) do |ivar, hs|
    val = instance_variable_get(ivar)

    next if val.nil?

    hs[ivar[1..-1].to_sym] = val
  end
end