lib/httpx/connection/http1.rb in httpx-0.8.1 vs lib/httpx/connection/http1.rb in httpx-0.8.2

- old
+ new

@@ -76,10 +76,11 @@ requests_limit = [@max_concurrent_requests, @max_requests, @requests.size].min @requests.each_with_index do |request, idx| break if idx >= requests_limit next if request.state == :done + request.headers["connection"] ||= request.options.persistent || idx < requests_limit - 1 ? "keep-alive" : "close" handle(request) end end # HTTP Parser callbacks @@ -168,10 +169,15 @@ emit(:error, request, ex) end end end + def ping + emit(:reset) + emit(:exhausted) + end + private def manage_connection(response) connection = response.headers["connection"] case connection @@ -204,20 +210,27 @@ end def disable_pipelining return if @requests.empty? - @requests.each { |r| r.transition(:idle) } + @requests.each do |r| + r.transition(:idle) + + # when we disable pipelining, we still want to try keep-alive. + # only when keep-alive with one request fails, do we fallback to + # connection: close. + r.headers["connection"] = "close" if @max_concurrent_requests == 1 + end # server doesn't handle pipelining, and probably # doesn't support keep-alive. Fallback to send only # 1 keep alive request. @max_concurrent_requests = 1 @pipelining = false end def set_request_headers(request) request.headers["host"] ||= request.authority - request.headers["connection"] ||= "keep-alive" + request.headers["connection"] ||= request.options.persistent ? "keep-alive" : "close" if !request.headers.key?("content-length") && request.body.bytesize == Float::INFINITY request.chunk! end end