lib/httpx/io/tcp.rb in httpx-0.3.1 vs lib/httpx/io/tcp.rb in httpx-0.4.0
- old
+ new
@@ -11,32 +11,33 @@
attr_reader :addresses
alias_method :host, :ip
- def initialize(uri, addresses, options)
+ def initialize(origin, addresses, options)
@state = :idle
- @hostname = uri.host
+ @hostname = origin.host
@addresses = addresses
- @ip_index = @addresses.size - 1
@options = Options.new(options)
@fallback_protocol = @options.fallback_protocol
- @port = uri.port
+ @port = origin.port
if @options.io
@io = case @options.io
when Hash
- @ip = @addresses[@ip_index]
- @options.io[@ip] || @options.io["#{@ip}:#{@port}"]
+ @options.io[origin.authority]
else
- @ip = @hostname
@options.io
end
+ _, _, _, @ip = @io.addr
+ @addresses ||= [@ip]
+ @ip_index = @addresses.size - 1
unless @io.nil?
@keep_open = true
@state = :connected
end
else
+ @ip_index = @addresses.size - 1
@ip = @addresses[@ip_index]
end
@io ||= build_socket
end
@@ -52,10 +53,11 @@
@fallback_protocol
end
def connect
return unless closed?
+
begin
if @io.closed?
transition(:idle)
@io = build_socket
end
@@ -63,10 +65,11 @@
rescue Errno::EISCONN
end
transition(:connected)
rescue Errno::EHOSTUNREACH => e
raise e if @ip_index <= 0
+
@ip_index -= 1
retry
rescue Errno::EINPROGRESS,
Errno::EALREADY,
::IO::WaitReadable
@@ -94,23 +97,26 @@
else
def read(size, buffer)
ret = @io.read_nonblock(size, buffer, exception: false)
return 0 if ret == :wait_readable
return if ret.nil?
+
buffer.bytesize
end
def write(buffer)
siz = @io.write_nonblock(buffer, exception: false)
return 0 if siz == :wait_writable
return if siz.nil?
+
buffer.slice!(0, siz)
siz
end
end
def close
return if @keep_open || closed?
+
begin
@io.close
ensure
transition(:closed)
end