Project

General

Profile

Feature #13719 ยป 0001-net-http-allow-existing-socket-arg-for-Net-HTTP.star.patch

normalperson (Eric Wong), 07/05/2017 07:03 PM

View differences:

lib/net/http.rb
#
# _opt_ sets following values by its accessor.
# The keys are ca_file, ca_path, cert, cert_store, ciphers,
# close_on_empty_response, key, open_timeout, read_timeout, ssl_timeout,
# ssl_version, use_ssl, verify_callback, verify_depth and verify_mode.
# close_on_empty_response, key, open_timeout, read_timeout, socket,
# ssl_timeout, ssl_version, use_ssl, verify_callback, verify_depth
# and verify_mode.
# If you set :use_ssl as true, you can use https and default value of
# verify_mode is set as OpenSSL::SSL::VERIFY_PEER.
# :socket may be any connected Socket object, including UNIXSocket
# for connecting to nginx or similar.
#
# If the optional block is given, the newly
# created Net::HTTP object is passed to it and closed when the
......
opt.key?(key) or next
http.__send__(meth, opt[key])
end
http.instance_variable_set(:@live_socket, opt[:socket])
end
http.start(&block)
......
@last_communicated = nil
@close_on_empty_response = false
@socket = nil
@live_socket = nil
@started = false
@open_timeout = 60
@read_timeout = 60
......
conn_address = address
conn_port = port
end
D "opening connection to #{conn_address}:#{conn_port}..."
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) {
begin
TCPSocket.open(conn_address, conn_port, @local_host, @local_port)
rescue => e
raise e, "Failed to open TCP connection to " +
"#{conn_address}:#{conn_port} (#{e.message})"
end
}
s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
D "opened"
unless s = @live_socket
D "opening connection to #{conn_address}:#{conn_port}..."
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) {
begin
TCPSocket.open(conn_address, conn_port, @local_host, @local_port)
rescue => e
raise e, "Failed to open TCP connection to " +
"#{conn_address}:#{conn_port} (#{e.message})"
end
}
s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
D "opened"
end
if use_ssl?
if proxy?
plain_sock = BufferedIO.new(s, read_timeout: @read_timeout,
......
def do_finish
@started = false
@socket.close if @socket
@socket = nil
@live_socket = @socket = nil
end
private :do_finish
test/net/http/test_http.rb
assert_equal false, h.instance_variable_get(:@proxy_from_env)
end
def test_socket_arg_unix
UNIXSocket.pair do |c,s|
th = Thread.new do
req = ''.b
req << s.readpartial(128) until req.include?("\r\n\r\n")
s.write("HTTP/1.1 200 OK\r\n" \
"Content-Type: text/plain\r\n" \
"Content-Length: 2\r\n\r\n")
s.close
req
end
Net::HTTP.start('example.com', 80, socket: c) do |http|
res = http.head('/')
assert_instance_of Net::HTTPOK, res
end
assert_match %r{\AHEAD / HTTP/1\.1\r\n}, th.value
assert_predicate c, :closed?
end
end if defined?(UNIXSocket)
def test_s_get
assert_equal $test_net_http_data,
Net::HTTP.get(config('host'), '/', config('port'))
-
    (1-1/1)