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

- old
+ new

@@ -90,11 +90,11 @@ end def handle_stream(stream, request) stream.on(:close, &method(:on_stream_close).curry[stream, request]) stream.on(:half_close) do - log(2, "#{stream.id}: ") { "waiting for response..." } + log(level: 2, label: "#{stream.id}: ") { "waiting for response..." } end # stream.on(:altsvc) stream.on(:headers, &method(:on_stream_headers).curry[stream, request]) stream.on(:data, &method(:on_stream_data).curry[stream, request]) end @@ -105,22 +105,22 @@ headers[":scheme"] = request.scheme headers[":method"] = request.verb.to_s.upcase headers[":path"] = headline_uri(request) headers[":authority"] = request.authority headers = headers.merge(request.headers) - log(1, "#{stream.id}: ") do + log(level: 1, label: "#{stream.id}: ", color: :yellow) do headers.map { |k, v| "-> HEADER: #{k}: #{v}" }.join("\n") end stream.headers(headers, end_stream: request.empty?) end def join_body(stream, request) chunk = @drains.delete(request) || request.drain_body while chunk next_chunk = request.drain_body - log(1, "#{stream.id}: ") { "-> DATA: #{chunk.bytesize} bytes..." } - log(2, "#{stream.id}: ") { "-> #{chunk.inspect}" } + log(level: 1, label: "#{stream.id}: ", color: :green) { "-> DATA: #{chunk.bytesize} bytes..." } + log(level: 2, label: "#{stream.id}: ", color: :green) { "-> #{chunk.inspect}" } stream.data(chunk, end_stream: !next_chunk) if next_chunk && @buffer.full? @drains[request] = next_chunk throw(:buffer_full) end @@ -131,31 +131,31 @@ ###### # HTTP/2 Callbacks ###### def on_stream_headers(stream, request, h) - log(stream.id) do + log(label: "#{stream.id}:", color: :yellow) do h.map { |k, v| "<- HEADER: #{k}: #{v}" }.join("\n") end _, status = h.shift headers = @options.headers_class.new(h) response = @options.response_class.new(request, status, "2.0", headers, @options) request.response = response @streams[request] = stream end def on_stream_data(stream, request, data) - log(1, "#{stream.id}: ") { "<- DATA: #{data.bytesize} bytes..." } - log(2, "#{stream.id}: ") { "<- #{data.inspect}" } + log(level: 1, label: "#{stream.id}: ", color: :green) { "<- DATA: #{data.bytesize} bytes..." } + log(level: 2, label: "#{stream.id}: ", color: :green) { "<- #{data.inspect}" } request.response << data end def on_stream_close(stream, request, error) return handle(request, stream) if request.expects? response = request.response || ErrorResponse.new(Error.new(error), @retries, @options) emit(:response, request, response) - log(2, "#{stream.id}: ") { "closing stream" } + log(level: 2, label: "#{stream.id}: ") { "closing stream" } @streams.delete(request) send(@pending.shift) unless @pending.empty? end @@ -172,37 +172,35 @@ return unless @connection.state == :closed && @connection.active_stream_count.zero? emit(:close) end def on_frame_sent(frame) - log(2, "#{frame[:stream]}: ") { "frame was sent!" } - log(2, "#{frame[:stream]}: ") do + log(level: 2, label: "#{frame[:stream]}: ") { "frame was sent!" } + log(level: 2, label: "#{frame[:stream]}: ", color: :blue) do case frame[:type] when :data frame.merge(payload: frame[:payload].bytesize).inspect - when :headers - "\e[33m#{frame.inspect}\e[0m" else frame.inspect end end end def on_frame_received(frame) - log(2, "#{frame[:stream]}: ") { "frame was received!" } - log(2, "#{frame[:stream]}: ") do + log(level: 2, label: "#{frame[:stream]}: ") { "frame was received!" } + log(level: 2, label: "#{frame[:stream]}: ", color: :magenta) do case frame[:type] when :data frame.merge(payload: frame[:payload].bytesize).inspect else frame.inspect end end end def on_altsvc(frame) - log(2, "#{frame[:stream]}: ") { "altsvc frame was received" } - log(2, "#{frame[:stream]}: ") { frame.inspect } + log(level: 2, label: "#{frame[:stream]}: ") { "altsvc frame was received" } + log(level: 2, label: "#{frame[:stream]}: ") { frame.inspect } end def on_promise(stream) emit(:promise, self, stream) end