-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Emit non seekable streams #2803
Emit non seekable streams #2803
Conversation
if ($seekable) { | ||
$stream->rewind(); | ||
} | ||
return $seekable ? $stream->read(1) === '' : $stream->eof(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be made easier with ... ?
return $stream->getSize() > 0;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your logic condition need to invert. return !$stream->getSize() > 0
. Please, run unit tests with your code. You will see some errors (AppTest errors can be ignored). The most interesting is the error Slim\Tests\ResponseEmitterTest::testRespondIndeterminateLength
. It shows that reading from a stream of at least one byte is the most reliable way. I myself don’t like such excessive complexity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found yet another example. Resource $resource = popen('echo 12', 'r')
will be defined as empty, but in reality has content.
Every time when we emit any response we read all data from a stream twice.
There may be problems with slow streams or responses with too big body. In addition, with a such
ResponseEmitter
work, it is impossible to send a response with non seekable body.