Repository navigation
Open a new connection for a redirect after sending Connection: close - #2645
Merged
yhirose merged 1 commit intoOct 25, 2026
Merged
Conversation
Without keep-alive, the client sends "Connection: close", but before following a redirect it disconnected only when the response repeated the option. RFC 9112 (section 9.6) lets a server close the connection without repeating it, and a client that sent "close" must not send another request on that connection. The redirected request was written to the closing connection and failed with "Failed to read connection" whenever the server closed it first. The client now disconnects before a redirect or an auth retry whenever the request asked for the close.
Owner
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With keep-alive off (the default), the client sends
Connection: close. Inhandle_request, the connection is only closed before following a redirect (or retrying with auth) if the response itself saysConnection: close, ifsocket_should_be_closed_when_request_is_done_is set, or if the response is HTTP/1.0. The request's ownclose_connectionisn't checked untilsend_finishes, which is after the redirect has already been sent.RFC 9112 section 9.6 says a server that receives
closecloses the connection after its response, and only SHOULD repeat the option in the response. A client that sentcloseMUST NOT send further requests on that connection. When a server doesn't repeat it, cpp-httplib writes the redirected request to the connection that is closing. Whether that works depends on timing, so following redirects from such servers fails intermittently withFailed to read connection. Against a server built on Python'shttp.server, 14 of 39 redirect cases failed in one run and 3 in the next.close_connectionis now part of that check, so the client disconnects before the redirect or auth retry and opens a new connection.The new
RedirectTest.NewConnectionAfterConnectionCloseuses a raw socket server. It answers the first request with a 302 that doesn't repeatConnection: close, then reads again from the same connection. Without the change, the client sends the redirected request on that connection and the test fails onreused. With it, the client closes that connection and connects again.test(OpenSSL build, with g++) passes all offline tests (1046), andgit clang-formatreports nothing for the change.