fix: stream file responses with proper Content-Length header#217
Open
lohanidamodar wants to merge 1 commit into0.33.xfrom
Open
fix: stream file responses with proper Content-Length header#217lohanidamodar wants to merge 1 commit into0.33.xfrom
lohanidamodar wants to merge 1 commit into0.33.xfrom
Conversation
Swoole's HTTP layer forces chunked Transfer-Encoding when using $response->write() for streaming, which strips the Content-Length header. This prevents browsers from showing download progress bars and makes it impossible for clients to know the total file size upfront. This change introduces a stream() method that bypasses Swoole's HTTP abstraction by using detach() + $server->send() to write raw HTTP frames over TCP. This allows us to send both Content-Length and a streaming body simultaneously. Changes: - Add stream(callable $reader, int $totalSize) to base Response class as a default implementation using write() + end() - Override stream() in Swoole Response to use detach/send pattern for raw TCP streaming with Content-Length support - Inject SwooleServer into Response via setSwooleServer() so the adapter can access send()/close() after detach() - Fix duplicate header appending in chunk() with a $chunking guard Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Summary
This PR fixes file download responses to include a proper
Content-Lengthheader when streaming large files. Previously, Swoole's HTTP layer forcedTransfer-Encoding: chunkedwhen using$response->write()for streaming, which strips theContent-Lengthheader. This caused two major issues:Problem
When serving file downloads through Swoole, the framework uses
$response->write()to stream the body in chunks. However, Swoole's HTTP abstraction automatically switches to chunked transfer encoding whenwrite()is called, removing anyContent-Lengthheader that was set. This is a known Swoole behavior — once you call$response->write(), Swoole takes over the transfer encoding.This means even if you explicitly set
Content-Length: 52428800for a 50MB file, the client receives:...instead of:
Solution
New
stream()method on baseResponseA new
stream(callable $reader, int $totalSize)method is added to the abstractResponseclass (Response.php). It:$readercallbackfn(int $offset, int $length): stringand the total body sizeContent-Lengthheader, appends cookies/headers, then streams the body viawrite()in chunksSwoole adapter override using
detach()+ raw TCPThe Swoole
Responseadapter (Swoole/Response.php) overridesstream()to bypass Swoole's HTTP layer entirely:Content-Length), andSet-Cookieheaders manually$swooleResponse->detach()— Disconnects from Swoole's HTTP abstraction, giving us the raw file descriptor$server->send($fd, $data)— Writes the HTTP headers and body chunks directly over TCP$server->close($fd)when doneThis pattern is the officially supported way to send responses with both
Content-Lengthand a streaming body in Swoole.Server integration
The Swoole
Serveradapter (Swoole/Server.php) now injects theSwooleServerinstance into eachResponseviasetSwooleServer(), enabling thedetach()+send()pattern.Fix duplicate header appending in
chunk()A
$chunkingguard flag was added to the baseResponse::chunk()method to prevent headers and cookies from being appended multiple times whenchunk()is called repeatedly during a chunked response.Files Changed
src/Http/Response.phpstream()method and$chunkingguard inchunk()src/Http/Adapter/Swoole/Response.phpstream()override withdetach()/send(),setSwooleServer(), andbuildSetCookieHeader()helpersrc/Http/Adapter/Swoole/Server.phpSwooleServerintoResponseviasetSwooleServer()Usage
The caller provides a reader callback and the total size. The framework handles headers, chunking, and transport-level details automatically.
Test Plan
Content-Lengthheader in responseSwooleServeris not injectedchunk()no longer duplicates headers on repeated calls🤖 Generated with Claude Code