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

Add App::terminate() stream support #2028

Merged
merged 25 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Nyholm\Psr7Server\ServerRequestCreator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Log\LoggerInterface;

class App
Expand Down Expand Up @@ -394,7 +395,7 @@ public function setResponseHeader(string $name, string $value): self
* directly, instead call it form Callback, JsCallback or similar
* other classes.
*
* @param string|array $output Array type is supported only for JSON response
* @param string|array|StreamInterface $output Array type is supported only for JSON response
*
* @return never
*/
Expand All @@ -405,7 +406,10 @@ public function terminate($output = ''): void
throw new Exception('Content type must be always set');
}

if ($type === 'application/json') {
if ($output instanceof StreamInterface) {
$this->response = $this->response->withBody($output);
$this->outputResponse('');
} elseif ($type === 'application/json') {
if (is_string($output)) {
$output = $this->decodeJson($output);
}
Expand Down Expand Up @@ -1128,7 +1132,11 @@ protected function outputResponse(string $data): void
return;
}

$this->response->getBody()->write($data);
$stream = $this->response->getBody();
if ($data !== '' && $stream->isWritable()) {
DarkSide666 marked this conversation as resolved.
Show resolved Hide resolved
$stream->write($data);
}

$this->emitResponse();
}

Expand Down
20 changes: 20 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Atk4\Core\Phpunit\TestCase;
use Atk4\Ui\Exception\LateOutputError;
use Atk4\Ui\HtmlTemplate;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\ServerRequestInterface;

class AppTest extends TestCase
{
Expand Down Expand Up @@ -68,4 +70,22 @@ public function testUnexpectedOutputLateError(): void
ob_end_clean();
}
}

/* throws headers already sent exception so not sure how to write this test for stream output
public function testStreamResponse(): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test should test sending large (10GB+) data with known pattern to test the response is not materialized somewhere

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest I have no idea how to write that. Will have to ask ChatGPT :)

Copy link
Member

@mvorisek mvorisek Apr 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented and tested with 6GB

the main bottleneck seems to be in Guzzle HTTP client - guzzle/guzzle#3115

until improved, let's test the streaming support with 50 MB only

{
$app = $this->createApp();

ob_start();

$content = 'Hello, world!';
$factory = new Psr17Factory();
$stream = $factory->createStream($content);
$app->setResponseHeader('Content-Type', 'text/html');
$app->terminate($stream); // @todo headers already sent

static::assertSame($content, ob_get_contents());
ob_end_clean();
}
*/
}