lib/httpx/connection/http1.rb in httpx-1.1.5 vs lib/httpx/connection/http1.rb in httpx-1.2.0

- old
+ new

@@ -10,10 +10,12 @@ MAX_REQUESTS = 200 CRLF = "\r\n" attr_reader :pending, :requests + attr_accessor :max_concurrent_requests + def initialize(buffer, options) @options = Options.new(options) @max_concurrent_requests = @options.max_concurrent_requests || MAX_REQUESTS @max_requests = @options.max_requests @parser = Parser::HTTP1.new(self) @@ -45,10 +47,11 @@ def reset @max_requests = @options.max_requests || MAX_REQUESTS @parser.reset! @handshake_completed = false + @pending.concat(@requests) unless @requests.empty? end def close reset emit(:close, true) @@ -216,10 +219,11 @@ end end end def ping + reset emit(:reset) emit(:exhausted) end private @@ -260,10 +264,11 @@ end end def disable disable_pipelining + reset emit(:reset) throw(:called) end def disable_pipelining @@ -290,33 +295,35 @@ if !request.headers.key?("content-length") && request.body.bytesize == Float::INFINITY request.body.chunk! end - connection = request.headers["connection"] + extra_headers = {} - connection ||= if request.persistent? - # when in a persistent connection, the request can't be at - # the edge of a renegotiation - if @requests.index(request) + 1 < @max_requests - "keep-alive" + unless request.headers.key?("connection") + connection_value = if request.persistent? + # when in a persistent connection, the request can't be at + # the edge of a renegotiation + if @requests.index(request) + 1 < @max_requests + "keep-alive" + else + "close" + end else - "close" + # when it's not a persistent connection, it sets "Connection: close" always + # on the last request of the possible batch (either allowed max requests, + # or if smaller, the size of the batch itself) + requests_limit = [@max_requests, @requests.size].min + if request == @requests[requests_limit - 1] + "close" + else + "keep-alive" + end end - else - # when it's not a persistent connection, it sets "Connection: close" always - # on the last request of the possible batch (either allowed max requests, - # or if smaller, the size of the batch itself) - requests_limit = [@max_requests, @requests.size].min - if request == @requests[requests_limit - 1] - "close" - else - "keep-alive" - end - end - extra_headers = { "connection" => connection } + extra_headers["connection"] = connection_value + end extra_headers["host"] = request.authority unless request.headers.key?("host") extra_headers end def handle(request) @@ -368,15 +375,13 @@ log { "<- " } @buffer << CRLF end def join_headers2(headers) - buffer = "".b headers.each do |field, value| - buffer << "#{capitalized(field)}: #{value}" << CRLF + buffer = "#{capitalized(field)}: #{value}#{CRLF}" log(color: :yellow) { "<- HEADER: #{buffer.chomp}" } @buffer << buffer - buffer.clear end end UPCASED = { "www-authenticate" => "WWW-Authenticate",