lib/httpx/channel/http1.rb in httpx-0.0.3 vs lib/httpx/channel/http1.rb in httpx-0.0.4

- old
+ new

@@ -69,46 +69,46 @@ # HTTP Parser callbacks # # must be public methods, or else they won't be reachable def on_message_begin - log(2) { "parsing begins" } + log(level: 2) { "parsing begins" } end def on_headers_complete(h) return on_trailer_headers_complete(h) if @parser_trailers # Wait for fix: https://github.com/tmm1/http_parser.rb/issues/52 # callback is called 2 times when chunked request = @requests.first return if request.response - log(2) { "headers received" } + log(level: 2) { "headers received" } headers = @options.headers_class.new(h) response = @options.response_class.new(@requests.last, @parser.status_code, @parser.http_version.join("."), headers, @options) - log { "-> HEADLINE: #{response.status} HTTP/#{@parser.http_version.join(".")}" } - log { response.headers.each.map { |f, v| "-> HEADER: #{f}: #{v}" }.join("\n") } + log(color: :yellow) { "-> HEADLINE: #{response.status} HTTP/#{@parser.http_version.join(".")}" } + log(color: :yellow) { response.headers.each.map { |f, v| "-> HEADER: #{f}: #{v}" }.join("\n") } request.response = response @has_response = true if response.complete? end def on_body(chunk) - log { "-> DATA: #{chunk.bytesize} bytes..." } - log(2) { "-> #{chunk.inspect}" } + log(color: :green) { "-> DATA: #{chunk.bytesize} bytes..." } + log(level: 2, color: :green) { "-> #{chunk.inspect}" } response = @requests.first.response response << chunk # dispatch if response.complete? end def on_message_complete - log(2) { "parsing complete" } + log(level: 2) { "parsing complete" } request = @requests.first response = request.response if !@parser_trailers && response.headers.key?("trailer") @parser_trailers = true @@ -142,22 +142,25 @@ throw(:called) end close send(@pending.shift) unless @pending.empty? return unless response.headers["connection"] == "close" - unless @requests.empty? - @requests.map { |r| r.transition(:idle) } - # server doesn't handle pipelining, and probably - # doesn't support keep-alive. Fallback to send only - # 1 keep alive request. - @max_concurrent_requests = 1 - end - emit(:close) + disable_concurrency + emit(:reset) end private + def disable_concurrency + return if @requests.empty? + @requests.each { |r| r.transition(:idle) } + # server doesn't handle pipelining, and probably + # doesn't support keep-alive. Fallback to send only + # 1 keep alive request. + @max_concurrent_requests = 1 + end + def set_request_headers(request) request.headers["host"] ||= request.authority request.headers["connection"] ||= "keep-alive" if !request.headers.key?("content-length") && request.body.bytesize == Float::INFINITY @@ -182,27 +185,27 @@ end def join_headers(request) buffer = +"" buffer << "#{request.verb.to_s.upcase} #{headline_uri(request)} HTTP/#{@version.join(".")}" << CRLF - log { "<- HEADLINE: #{buffer.chomp.inspect}" } + log(color: :yellow) { "<- HEADLINE: #{buffer.chomp.inspect}" } @buffer << buffer buffer.clear request.headers.each do |field, value| buffer << "#{capitalized(field)}: #{value}" << CRLF - log { "<- HEADER: #{buffer.chomp}" } + log(color: :yellow) { "<- HEADER: #{buffer.chomp}" } @buffer << buffer buffer.clear end log { "<- " } @buffer << CRLF end def join_body(request) return if request.empty? while (chunk = request.drain_body) - log { "<- DATA: #{chunk.bytesize} bytes..." } - log(2) { "<- #{chunk.inspect}" } + log(color: :green) { "<- DATA: #{chunk.bytesize} bytes..." } + log(level: 2, color: :green) { "<- #{chunk.inspect}" } @buffer << chunk throw(:buffer_full, request) if @buffer.full? end end