lib/httpx/channel.rb in httpx-0.0.2 vs lib/httpx/channel.rb in httpx-0.0.3
- old
+ new
@@ -67,10 +67,11 @@
@pending = []
@state = :idle
end
def match?(uri)
+ return false if @state == :closing
ips = begin
Resolv.getaddresses(uri.host)
rescue StandardError
[uri.host]
end
@@ -124,16 +125,12 @@
return
when :closing
dwrite
transition(:closed)
emit(:close)
- else
- catch(:called) do
- dread
- dwrite
- parser.consume
- end
+ when :open
+ consume
end
nil
end
def upgrade_parser(protocol)
@@ -141,10 +138,18 @@
@parser = build_parser(protocol)
end
private
+ def consume
+ catch(:called) do
+ dread
+ dwrite
+ parser.consume
+ end
+ end
+
def dread(wsize = @window_size)
loop do
siz = @io.read(wsize, @read_buffer)
unless siz
emit(:close)
@@ -199,19 +204,31 @@
# when :idle
when :open
return if @state == :closed
@io.connect
- return if @io.closed?
+ return unless @io.connected?
send_pending
when :closing
return unless @state == :open
when :closed
return unless @state == :closing
return unless @write_buffer.empty?
@io.close
@read_buffer.clear
end
@state = nextstate
+ rescue Errno::ECONNREFUSED,
+ Errno::EADDRNOTAVAIL => e
+ emit_error(e)
+ @state = :closed
+ emit(:close)
+ end
+
+ def emit_error(e)
+ response = ErrorResponse.new(e, 0, @options)
+ @pending.each do |request, _| # rubocop:disable Performance/HashEachMethods
+ emit(:response, request, response)
+ end
end
end
end