lib/httpx/channel.rb in httpx-0.0.5 vs lib/httpx/channel.rb in httpx-0.1.0

- old
+ new

@@ -40,18 +40,19 @@ BUFFER_SIZE = 1 << 14 class << self def by(uri, options) - io = case uri.scheme - when "http" - IO.registry("tcp").new(uri.host, uri.port, options) - when "https" - IO.registry("ssl").new(uri.host, uri.port, options) - else - raise Error, "#{uri}: #{uri.scheme}: unrecognized channel" + type = options.transport || begin + case uri.scheme + when "http" then "tcp" + when "https" then "ssl" + else + raise Error, "#{uri}: #{uri.scheme}: unrecognized channel" + end end + io = IO.registry(type).new(uri, options) new(io, options) end end def_delegator :@io, :closed? @@ -151,11 +152,13 @@ def dread(wsize = @window_size) loop do siz = @io.read(wsize, @read_buffer) unless siz - emit(:close) + ex = EOFError.new("descriptor closed") + ex.set_backtrace(caller) + on_error(ex) return end return if siz.zero? log { "READ: #{siz} bytes..." } parser << @read_buffer.to_s @@ -165,11 +168,13 @@ def dwrite loop do return if @write_buffer.empty? siz = @io.write(@write_buffer) unless siz - emit(:close) + ex = EOFError.new("descriptor closed") + ex.set_backtrace(caller) + on_error(ex) return end log { "WRITE: #{siz} bytes..." } return if siz.zero? end @@ -204,11 +209,11 @@ transition(:idle) transition(:open) end end parser.on(:error) do |request, ex| - response = ErrorResponse.new(ex, 0, @options) + response = ErrorResponse.new(ex, @options) emit(:response, request, response) end parser end @@ -244,11 +249,11 @@ handle_error(ex) reset end def handle_error(e) - parser.handle_error(e) - response = ErrorResponse.new(e, 0, @options) + parser.handle_error(e) if parser.respond_to?(:handle_error) + response = ErrorResponse.new(e, @options) @pending.each do |request, _| emit(:response, request, response) end end end