lib/httpx/channel.rb in httpx-0.0.1 vs lib/httpx/channel.rb in httpx-0.0.2
- old
+ new
@@ -1,7 +1,8 @@
# frozen_string_literal: true
+require "resolv"
require "forwardable"
require "httpx/io"
require "httpx/buffer"
module HTTPX
@@ -66,17 +67,17 @@
@pending = []
@state = :idle
end
def match?(uri)
- ip = begin
- TCPSocket.getaddress(uri.host)
- rescue StandardError
- uri.host
- end
+ ips = begin
+ Resolv.getaddresses(uri.host)
+ rescue StandardError
+ [uri.host]
+ end
- ip == @io.ip &&
+ ips.include?(@io.ip) &&
uri.port == @io.port &&
uri.scheme == @io.scheme
end
def interests
@@ -96,22 +97,13 @@
transition(:open)
end
@io.to_io
end
- def close(hard = false)
- pr = @parser
+ def close
+ @parser.close if @parser
transition(:closing)
- if hard || (pr && pr.empty?)
- pr.close
- @parser = nil
- else
- transition(:idle)
- @parser = pr
- parser.reenqueue!
- return
- end
end
def reset
transition(:closing)
transition(:closed)
@@ -152,22 +144,28 @@
private
def dread(wsize = @window_size)
loop do
siz = @io.read(wsize, @read_buffer)
- throw(:close, self) unless siz
+ unless siz
+ emit(:close)
+ return
+ end
return if siz.zero?
log { "READ: #{siz} bytes..." }
parser << @read_buffer.to_s
end
end
def dwrite
loop do
return if @write_buffer.empty?
siz = @io.write(@write_buffer)
- throw(:close, self) unless siz
+ unless siz
+ emit(:close)
+ return
+ end
log { "WRITE: #{siz} bytes..." }
return if siz.zero?
end
end
@@ -188,14 +186,11 @@
emit(:response, *args)
end
parser.on(:promise) do |*args|
emit(:promise, *args)
end
- # parser.inherit_callbacks(self)
- parser.on(:complete) { throw(:close, self) }
parser.on(:close) do
- transition(:closed)
- emit(:close)
+ transition(:closing)
end
parser
end
def transition(nextstate)