forked from igrigorik/em-http-request
/
http_connection_options.rb
44 lines (35 loc) · 1.22 KB
/
http_connection_options.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
class HttpConnectionOptions
attr_reader :host, :port, :tls, :proxy, :bind, :bind_port
attr_reader :connect_timeout, :inactivity_timeout
def initialize(uri, options)
@connect_timeout = options[:connect_timeout] || 5 # default connection setup timeout
@inactivity_timeout = options[:inactivity_timeout] ||= 10 # default connection inactivity (post-setup) timeout
@tls = options[:tls] || options[:ssl] || {}
@proxy = options[:proxy]
if bind = options[:bind]
@bind = bind[:host] || '0.0.0.0'
# Eventmachine will open a UNIX socket if bind :port
# is explicitly set to nil
@bind_port = bind[:port]
end
uri = uri.kind_of?(Addressable::URI) ? uri : Addressable::URI::parse(uri.to_s)
@https = uri.scheme == "https"
uri.port ||= (@https ? 443 : 80)
if proxy = options[:proxy]
@host = proxy[:host]
@port = proxy[:port]
else
@host = uri.host
@port = uri.port
end
end
def http_proxy?
@proxy && (@proxy[:type] == :http || @proxy[:type].nil?) && !@https
end
def connect_proxy?
@proxy && (@proxy[:type] == :http || @proxy[:type].nil?) && @https
end
def socks_proxy?
@proxy && (@proxy[:type] == :socks5)
end
end