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

added plain stream() method #575

Merged
merged 2 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions flight/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,16 @@ public function _start(): void

// If this route is to be streamed, we need to output the headers now
if ($route->is_streamed === true) {
$response->status($route->streamed_headers['status'] ?? 200);
unset($route->streamed_headers['status']);
if (count($route->streamed_headers) > 0) {
$response->status($route->streamed_headers['status'] ?? 200);
unset($route->streamed_headers['status']);
foreach ($route->streamed_headers as $header => $value) {
$response->header($header, $value);
}
}

$response->header('X-Accel-Buffering', 'no');
$response->header('Connection', 'close');
foreach ($route->streamed_headers as $header => $value) {
$response->header($header, $value);
}

// We obviously don't know the content length right now. This must be false.
$response->content_length = false;
Expand Down
11 changes: 11 additions & 0 deletions flight/net/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ public function addMiddleware($middleware): self
return $this;
}

/**
* If the response should be streamed
*
* @return self
*/
public function stream(): self
{
$this->is_streamed = true;
return $this;
}

/**
* This will allow the response for this route to be streamed.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/FlightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,30 @@ public function testHookOutputBufferingV2OutputBuffering()
}

public function testStreamRoute()
{
$response_mock = new class extends Response {
public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): Response
{
return $this;
}
};
$mock_response_class_name = get_class($response_mock);
Flight::register('response', $mock_response_class_name);
Flight::route('/stream', function () {
echo 'stream';
})->stream();
Flight::request()->url = '/stream';
$this->expectOutputString('stream');
Flight::start();
$this->assertEquals('', Flight::response()->getBody());
$this->assertEquals([
'X-Accel-Buffering' => 'no',
'Connection' => 'close'
], Flight::response()->getHeaders());
$this->assertEquals(200, Flight::response()->status());
}

public function testStreamRouteWithHeaders()
{
$response_mock = new class extends Response {
public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): Response
Expand Down
3 changes: 2 additions & 1 deletion tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ public function testResponseBodyCallbackGzip()
ob_start();
$response->send();
$gzip_body = ob_get_clean();
$this->assertEquals('H4sIAAAAAAAAAytJLS4BAAx+f9gEAAAA', base64_encode($gzip_body));
$expected = PHP_OS === 'WINNT' ? 'H4sIAAAAAAAACitJLS4BAAx+f9gEAAAA' : 'H4sIAAAAAAAAAytJLS4BAAx+f9gEAAAA';
$this->assertEquals($expected, base64_encode($gzip_body));
$this->assertEquals(strlen(gzencode('test')), strlen($gzip_body));
}

Expand Down
3 changes: 2 additions & 1 deletion tests/server/LayoutMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public function before()
<li><a href="/jsonp?jsonp=myjson">JSONP</a></li>
<li><a href="/halt">Halt</a></li>
<li><a href="/redirect">Redirect</a></li>
<li><a href="/streamResponse">Stream</a></li>
<li><a href="/streamResponse">Stream Plain</a></li>
<li><a href="/streamWithHeaders">Stream Headers</a></li>
<li><a href="/overwrite">Overwrite Body</a></li>
<li><a href="/redirect/before%2Fafter">Slash in Param</a></li>
<li><a href="/わたしはひとです">UTF8 URL</a></li>
Expand Down
12 changes: 12 additions & 0 deletions tests/server/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,19 @@
ob_flush();
}
echo "is successful!!";
})->stream();

// Test 12: Redirect with status code
Flight::route('/streamWithHeaders', function () {
echo "Streaming a response";
for ($i = 1; $i <= 50; $i++) {
echo ".";
usleep(50000);
ob_flush();
}
echo "is successful!!";
})->streamWithHeaders(['Content-Type' => 'text/html', 'status' => 200 ]);

// Test 14: Overwrite the body with a middleware
Flight::route('/overwrite', function () {
echo '<span id="infotext">Route text:</span> This route status is that it <span style="color:red; font-weight: bold;">failed</span>';
Expand Down