-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Fix HTTP 414 errors hanging until timeout #2260
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
Conversation
|
@chansikpark thanks for the pull request! Could you take a look at the following review from Copilot?
|
It's generally unclear to me specifically how a dangling connection can be dangerous, but this sounds pretty reasonable as far as I can tell. I couldn't find anything in the specs that mandate/recommend either way. From 2.4 Conformance.ErrorHandling:
So I've made all client/server errors close the connection here: diff --git a/httplib.h b/httplib.h
--- a/httplib.h
+++ b/httplib.h
@@ -7692,7 +7692,8 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection,
if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); }
// Prepare additional headers
- if (close_connection || req.get_header_value("Connection") == "close") {
+ if (close_connection || req.get_header_value("Connection") == "close" ||
+ 400 <= res.status) { // Don't leave connections open after errors
res.set_header("Connection", "close");
} else {
std::string s = "timeout=";Anticipating changes in multiple codepaths, I started by modifying 4 different erroring tests to set Keep Alive on and check whether the right thing happens afterwards. Then I realized I can just change one place in
Before Change (with modified tests)[----------] Global test environment tear-down
[==========] 458 tests from 103 test suites ran. (271157 ms total)
[ PASSED ] 453 tests.
[ FAILED ] 5 tests, listed below:
[ FAILED ] ServerTest.TooLongRequest
[ FAILED ] ServerTest.LongQueryValue
[ FAILED ] ServerTest.HeaderCountExceedsLimit
[ FAILED ] ServerTest.HeaderCountSecurityTest
[ FAILED ] ServerTest.BadRequestLineCancelsKeepAliveAfter Change[----------] Global test environment tear-down
[==========] 458 tests from 103 test suites ran. (217041 ms total)
[ PASSED ] 458 tests. |
|
@chansikpark this latest update looks very good to me and I have just merged as it is. Thanks for the fine improvement! |
It appears that #2046 introduced a bit of an uncommonly manifesting bug/quirk where overly long queries/requests would hang until either the server or client times out, at which point the 414 error code returns at the last moment. It looks like some unnecessary logic from older code was retained when the checks were reordered. I've modified two tests that were passing but taking 5000+ ms. They now must pass in < 100 ms.
Please advise.
Before
After