Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: inappropriate connection reuse when using HTTP proxy if the initial CONNECT failed #2072

Merged

Conversation

jasonjoo2010
Copy link
Contributor

@jasonjoo2010 jasonjoo2010 commented Mar 9, 2025

What This MR Resolves

A CONNECT request is needed to sent to the HTTP proxy first before the actual client request to establish the tunnel on the proxy. A HTTP/1.1 200 Connection established is expected for the initial CONNECT request. Only when the CONNECT is successful, the client continues sending the actual request through the "tunnel". And when CONNECT failed, the connection remains the initial state unconnected.

There are following circumstances that a CONNECT fails under but not limited to following situations:

  • The destination is not whitelisted.
  • The dest domain can't be resolved(timeout/SERVFAIL/NX/etc.).
  • The dest IP can't be connected(timeout/unreachable/etc.).

There could be 2 following strategies to deal with CONNECT failures on the client side:

  1. Close the connection before return to the caller.
  2. Mark this connection "unconnected" and put it into the pool. Then retry the CONNECT next time it's picked out of the pool.

The 2nd one needs to add extra state to Channel in the manager which brings bigger change to the code.
This MR employs the 1st strategy to resolve it. The issue is described in #2071 .

Readings

The CONNECT is documented in Section 5.3 in RFC2871: https://www.ietf.org/rfc/rfc2817.txt

The proxy won't actively terminate the connection if the CONNECT failed if keep-alive is enabled. Unless the tunnel is established and there is any communication failures in the middle. Therefore the client needs to deal with this error by its own.

@hyperxpro
Copy link
Member

Thanks for the PR.

Can you please add an unit test around this?

@jasonjoo2010 jasonjoo2010 force-pushed the fix/http-proxy-connect-failure branch from 73adc33 to 77d254c Compare March 9, 2025 16:38
@jasonjoo2010
Copy link
Contributor Author

Hi @hyperxpro
Added a test for this scenario, pls take a look, thanks

There is an extra CONNECT request needs to send before the real request to the HTTP proxy and the 2nd request only happens if the CONNECT request succeeds. When CONNECT failed, the connection should be dropped as it's not in connected state.

Signed-off-by: Jason Joo <[email protected]>
@jasonjoo2010 jasonjoo2010 force-pushed the fix/http-proxy-connect-failure branch from 77d254c to b4859c8 Compare March 9, 2025 17:22
@hyperxpro hyperxpro merged commit f194152 into AsyncHttpClient:main Mar 9, 2025
3 checks passed
@hyperxpro
Copy link
Member

Thanks a lot!

@jasonjoo2010
Copy link
Contributor Author

Thanks for merging, this issue can be closed now #2071

May I back port this fix to 2.12.4? As 2.12.x is still commonly used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants