From 1343c74e31637cd95d06488b33c2bbf18c4c6a17 Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Mon, 20 Dec 2021 17:52:00 +0000 Subject: [PATCH] fixing overwriting connection #transition, which wasn't taking tcp connect on proxy errors --- lib/httpx/connection.rb | 20 ++++++++++++-------- lib/httpx/io/tcp.rb | 4 +++- lib/httpx/plugins/internal_telemetry.rb | 2 +- lib/httpx/plugins/proxy.rb | 2 +- lib/httpx/plugins/proxy/http.rb | 4 ++-- lib/httpx/plugins/proxy/socks4.rb | 2 +- lib/httpx/plugins/proxy/socks5.rb | 2 +- 7 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/httpx/connection.rb b/lib/httpx/connection.rb index 638403ad..1849091e 100644 --- a/lib/httpx/connection.rb +++ b/lib/httpx/connection.rb @@ -489,6 +489,18 @@ module HTTPX end def transition(nextstate) + handle_transition(nextstate) + rescue Errno::ECONNREFUSED, + Errno::EADDRNOTAVAIL, + Errno::EHOSTUNREACH, + TLSError => e + # connect errors, exit gracefully + handle_error(e) + @state = :closed + emit(:close) + end + + def handle_transition(nextstate) case nextstate when :idle @timeout = @current_timeout = @options.timeout[:connect_timeout] @@ -525,14 +537,6 @@ module HTTPX emit(:activate) end @state = nextstate - rescue Errno::ECONNREFUSED, - Errno::EADDRNOTAVAIL, - Errno::EHOSTUNREACH, - TLSError => e - # connect errors, exit gracefully - handle_error(e) - @state = :closed - emit(:close) end def purge_after_closed diff --git a/lib/httpx/io/tcp.rb b/lib/httpx/io/tcp.rb index 08bcfd4d..288b4ab2 100644 --- a/lib/httpx/io/tcp.rb +++ b/lib/httpx/io/tcp.rb @@ -57,7 +57,9 @@ module HTTPX @io = build_socket end try_connect - rescue Errno::EHOSTUNREACH => e + rescue Errno::ECONNREFUSED, + Errno::EADDRNOTAVAIL, + Errno::EHOSTUNREACH => e raise e if @ip_index <= 0 @ip_index -= 1 diff --git a/lib/httpx/plugins/internal_telemetry.rb b/lib/httpx/plugins/internal_telemetry.rb index 17badc9a..eb5daf6a 100644 --- a/lib/httpx/plugins/internal_telemetry.rb +++ b/lib/httpx/plugins/internal_telemetry.rb @@ -81,7 +81,7 @@ module HTTPX super end - def transition(nextstate) + def handle_transition(nextstate) state = @state super meter_elapsed_time("Connection##{object_id}[#{@origin}]: #{state} -> #{nextstate}") if nextstate == @state diff --git a/lib/httpx/plugins/proxy.rb b/lib/httpx/plugins/proxy.rb index 9f7b5ce7..03ffd88d 100644 --- a/lib/httpx/plugins/proxy.rb +++ b/lib/httpx/plugins/proxy.rb @@ -234,7 +234,7 @@ module HTTPX end end - def transition(nextstate) + def handle_transition(nextstate) return super unless @options.proxy case nextstate diff --git a/lib/httpx/plugins/proxy/http.rb b/lib/httpx/plugins/proxy/http.rb index 55333a67..64788f7d 100644 --- a/lib/httpx/plugins/proxy/http.rb +++ b/lib/httpx/plugins/proxy/http.rb @@ -13,7 +13,7 @@ module HTTPX private - def transition(nextstate) + def handle_transition(nextstate) return super unless @options.proxy && @options.proxy.uri.scheme == "http" case nextstate @@ -54,7 +54,7 @@ module HTTPX @inflight += 1 parser.send(connect_request) else - transition(:connected) + handle_transition(:connected) end end diff --git a/lib/httpx/plugins/proxy/socks4.rb b/lib/httpx/plugins/proxy/socks4.rb index 1778a798..a8757bb9 100644 --- a/lib/httpx/plugins/proxy/socks4.rb +++ b/lib/httpx/plugins/proxy/socks4.rb @@ -27,7 +27,7 @@ module HTTPX private - def transition(nextstate) + def handle_transition(nextstate) return super unless @options.proxy && PROTOCOLS.include?(@options.proxy.uri.scheme) case nextstate diff --git a/lib/httpx/plugins/proxy/socks5.rb b/lib/httpx/plugins/proxy/socks5.rb index 6d8cfc42..5eea71c2 100644 --- a/lib/httpx/plugins/proxy/socks5.rb +++ b/lib/httpx/plugins/proxy/socks5.rb @@ -46,7 +46,7 @@ module HTTPX private - def transition(nextstate) + def handle_transition(nextstate) return super unless @options.proxy && @options.proxy.uri.scheme == "socks5" case nextstate