-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathnull.rb
79 lines (63 loc) · 1.96 KB
/
null.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true
require "io/wait"
module HTTP
module Timeout
class Null
attr_reader :options, :socket
def initialize(options = {})
@options = options
end
# Connects to a socket
def connect(socket_class, host, port, nodelay = false)
@socket = socket_class.open(host, port)
@socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay
end
# Starts a SSL connection on a socket
def connect_ssl
@socket.connect
end
def close
@socket&.close
end
def closed?
@socket&.closed?
end
# Configures the SSL connection and starts the connection
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
# Read from the socket
def readpartial(size, buffer = nil)
@socket.readpartial(size, buffer)
rescue EOFError
:eof
end
# Write to the socket
def write(data)
@socket.write(data)
end
alias << write
private
# Retry reading
def rescue_readable(timeout = read_timeout)
yield
rescue IO::WaitReadable
retry if @socket.to_io.wait_readable(timeout)
raise TimeoutError, "Read timed out after #{timeout} seconds"
end
# Retry writing
def rescue_writable(timeout = write_timeout)
yield
rescue IO::WaitWritable
retry if @socket.to_io.wait_writable(timeout)
raise TimeoutError, "Write timed out after #{timeout} seconds"
end
end
end
end