Class: HTTP::Timeout::Null

Inherits:
Object
  • Object
show all
Defined in:
lib/http/timeout/null.rb

Overview

Base timeout handler with no timeout enforcement

Direct Known Subclasses

Global, PerOperation

Constant Summary collapse

NATIVE_CONNECT_TIMEOUT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Whether TCPSocket natively supports connect_timeout and Happy Eyeballs (RFC 8305). Available in Ruby 3.4+.

RUBY_VERSION >= "3.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(read_timeout: nil, write_timeout: nil, connect_timeout: nil, global_timeout: nil) ⇒ HTTP::Timeout::Null

Initializes the null timeout handler

Examples:

HTTP::Timeout::Null.new(read_timeout: 5)

Parameters:

  • read_timeout (Numeric, nil) (defaults to: nil)

    Read timeout in seconds

  • write_timeout (Numeric, nil) (defaults to: nil)

    Write timeout in seconds

  • connect_timeout (Numeric, nil) (defaults to: nil)

    Connect timeout in seconds

  • global_timeout (Numeric, nil) (defaults to: nil)

    Global timeout in seconds



46
47
48
49
# File 'lib/http/timeout/null.rb', line 46

def initialize(read_timeout: nil, write_timeout: nil, connect_timeout: nil, global_timeout: nil)
  @options = { read_timeout: read_timeout, write_timeout: write_timeout,
               connect_timeout: connect_timeout, global_timeout: global_timeout }.compact
end

Instance Attribute Details

#optionsHash (readonly)

Timeout configuration options

Examples:

timeout.options # => {read_timeout: 5}

Returns:

  • (Hash)

    timeout options



24
25
26
# File 'lib/http/timeout/null.rb', line 24

def options
  @options
end

#socketObject (readonly)

The underlying socket

Examples:

timeout.socket

Returns:

  • (Object)

    the underlying socket



33
34
35
# File 'lib/http/timeout/null.rb', line 33

def socket
  @socket
end

Instance Method Details

#closevoid

This method returns an undefined value.

Closes the underlying socket

Examples:

timeout.close


85
86
87
# File 'lib/http/timeout/null.rb', line 85

def close
  @socket&.close
end

#closed?Boolean

Checks whether the socket is closed

Examples:

timeout.closed?

Returns:

  • (Boolean)


96
97
98
# File 'lib/http/timeout/null.rb', line 96

def closed?
  @socket&.closed?
end

#connect(socket_class, host, port, nodelay: false) ⇒ void

This method returns an undefined value.

Connects to a socket

Examples:

timeout.connect(TCPSocket, "example.com", 80)

Parameters:

  • socket_class (Class)
  • host (String)
  • port (Integer)
  • nodelay (Boolean) (defaults to: false)


62
63
64
65
# File 'lib/http/timeout/null.rb', line 62

def connect(socket_class, host, port, nodelay: false)
  @socket = open_socket(socket_class, host, port)
  @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay
end

#connect_sslvoid

This method returns an undefined value.

Starts a SSL connection on a socket

Examples:

timeout.connect_ssl


74
75
76
# File 'lib/http/timeout/null.rb', line 74

def connect_ssl
  @socket.connect
end

#readpartial(size, buffer = nil) ⇒ String, :eof

Read from the socket

Examples:

timeout.readpartial(1024)

Parameters:

  • size (Integer)
  • buffer (String, nil) (defaults to: nil)

Returns:

  • (String, :eof)


132
133
134
135
136
# File 'lib/http/timeout/null.rb', line 132

def readpartial(size, buffer = nil)
  @socket.readpartial(size, buffer)
rescue EOFError
  :eof
end

#start_tls(host, ssl_socket_class, ssl_context) ⇒ void

This method returns an undefined value.

Configures the SSL connection and starts it

Examples:

timeout.start_tls("example.com", ssl_class, ssl_ctx)

Parameters:

  • host (String)
  • ssl_socket_class (Class)
  • ssl_context (OpenSSL::SSL::SSLContext)


110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/http/timeout/null.rb', line 110

def start_tls(host, ssl_socket_class, ssl_context)
  @socket = ssl_socket_class.new(socket, ssl_context)
  @socket.hostname = host if @socket.respond_to? :hostname=
  @socket.sync_close = true if @socket.respond_to? :sync_close=

  connect_ssl

  return unless ssl_context.verify_mode == OpenSSL::SSL::VERIFY_PEER
  return if ssl_context.respond_to?(:verify_hostname) && !ssl_context.verify_hostname

  @socket.post_connection_check(host)
end

#write(data) ⇒ Integer Also known as: <<

Write to the socket

Examples:

timeout.write("GET / HTTP/1.1")

Parameters:

  • data (String)

Returns:

  • (Integer)


146
147
148
# File 'lib/http/timeout/null.rb', line 146

def write(data)
  @socket.write(data)
end