Repository navigation
fastcgi: explain 411 responses for unknown-length bodies - #7956
Merged
Merged
Conversation
Member
|
Commits should resolve to the same account as the PR submitter. Sign the CLA and address CI failures if any occur. |
steadytao
self-requested a review
August 24, 2026 16:00
A request whose body has no length of its own — Transfer-Encoding: chunked, or HTTP/2 and HTTP/3 requests sent without the header — cannot be forwarded over FastCGI until it has been buffered in full, because CGI/1.1 requires CONTENT_LENGTH and php-fpm hangs when it is absent or wrong and the body is not empty. When request_buffers is too small to hold the whole body, the length stays unknown and the request is refused with 411. The refusal said none of that. RoundTrip passes r.ContentLength to Post, which is -1 for such a request, so the operator got a bare "411 Length Required" with either no error at all or strconv's "invalid syntax" on a value they never wrote. On caddyserver#7386 that sent two people looking for the fault in their backend. The 411 now carries the reason and the remedy, and the unknown-length case is told apart from a genuinely malformed value. ParseUint becomes ParseInt plus an explicit negative check, which is strictly tighter: values above MaxInt64 used to be accepted and are unreachable anyway, since CONTENT_LENGTH is always FormatInt of an int64 by the time Do sees it. No status code changes. Every request that was refused before is still refused; raising request_buffers is still what makes a large chunked body work. Tests: the reachable path through Post, the unusable CONTENT_LENGTH values Do itself guards against, an integration test driving a chunked body through a fastcgi reverse_proxy over a unix socket at each side of the buffer boundary (including request_buffers -1, which succeeds at any size and is the remedy), and a boundary test recording that a body exactly the size of the buffer counts as partial. That last one is deliberately not changed here. Telling "exactly the limit" apart from "more to come" costs a read that a paused stream may never answer, and bufferedBody also backs response_buffers: measured against a body that delivers exactly the limit and stops, the current code hands the buffered prefix on immediately, while peeking one byte first delivers nothing at all.
Rohilalala
force-pushed
the
fix/fastcgi-chunked-411
branch
from
August 24, 2026 16:50
edb1d2f to
6ea2962
Compare
This was referenced Sep 30, 2026
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.
Closes #7386.
What happens
A request whose body has no length of its own —
Transfer-Encoding: chunked, or an HTTP/2 or HTTP/3 request sent without the header — cannot be forwarded over FastCGI until it has been buffered in full, because CGI/1.1 requiresCONTENT_LENGTHand php-fpm hangs when it is absent or wrong and the body is not empty. Whenrequest_buffersis too small to hold the whole body, the length stays unknown and the request is refused with 411.That part is working as intended. The problem is that the refusal said none of it.
Transport.RoundTrippassesr.ContentLengthtoclient.Post, andnet/httpreports-1for such a request, so the operator got a bare411 Length Requiredcarrying either no error at all orstrconv'sinvalid syntaxon a value they never wrote. On #7386 that sent two people looking for the fault in their backend, and the reporter closed by saying they were "still not 100% sure this issue is on the caddy side".What this changes
The 411 now carries the reason and the remedy, and the unknown-length case is told apart from a genuinely malformed value:
ParseUintbecomesParseIntplus an explicit negative check. That is strictly tighter — values aboveMaxInt64used to be accepted — and unreachable anyway, sinceCONTENT_LENGTHis alwaysstrconv.FormatIntof anint64by the timeDosees it.No status code changes. Every request that was refused before is still refused. Raising
request_buffers(or setting it to-1) is still what makes a large chunked body work, and the integration test demonstrates that it does.Why not just make it work
Two things I deliberately did not do, in case they come up in review:
request_buffersbytes is treated as partial, becauseio.CopyNfilling the buffer cannot be distinguished from a body with more to come. Finding out costs a read that a paused stream may never answer, andbufferedBodyalso backsresponse_buffers— measured against a body that delivers exactly the limit and then stops, the current code hands the buffered prefix on immediately, while peeking one byte first delivers nothing at all.TestBufferingBoundaryrecords the behaviour rather than changing it.Tests
TestPostRejectsUnknownLengthWithRemedygoes throughPost, the entry pointRoundTripactually uses, so the message cannot rot on a branch production never reaches.TestDoRejectsUnusableContentLengthcovers the valuesDoitself guards against.TestFastCGIChunkedRequestBodydrives a chunked body through a fastcgireverse_proxyover a unix socket against a realnet/http/fcgiresponder, at each side of the buffer boundary, includingrequest_buffers -1succeeding at any size. The responder reports how many body bytes arrived, so a truncated body fails rather than passing quietly.Verified with
gofmt,go vet,golangci-lint(0 issues), andgo test ./modules/caddyhttp/reverseproxy/... -race -count=2 -shuffle=on.Assistance Disclosure
AI used, extensively. Claude (Claude Code) did the investigation, wrote the change and the tests, and ran the verification described above. Notably it also talked me out of its own first attempt: the initial fix was reverted after review showed the boundary change could block a paused stream, and the measurement quoted above is why the current patch leaves that alone.