laminas / laminas-diactoros

PSR HTTP Message implementations
https://docs.laminas.dev/laminas-diactoros/
BSD 3-Clause "New" or "Revised" License
487 stars 63 forks source link

RFC: Read php input stream content into php temp stream to allow all stream features in PhpInputStream #151

Closed Xerkus closed 1 year ago

Xerkus commented 1 year ago

Feature Request

Q A
New Feature yes
RFC yes
BC Break no

Summary

PhpInputStream is currently using private cache property to store the content of php://input stream in order to allow multiple uses of __toString(). String PhpInputStream::$cache means that potentially large raw request body is read fully into memory.

As @boesing pointed out, guzzle reads content of php://input into php://temp which then in turn allows full utilization of stream features such as ability to seek, rewind, read in chunks, read multiple times. See https://github.com/guzzle/psr7/blob/fe18df3deb34d1e0eab8a63ef4fe2aeb2a8e8c94/src/Utils.php#L305-L310

PhpInputStream can benefit from this as well. Potentially, to avoid unnecessary performance hit stream can be copied and replaced internally only when metadata or content is accessed for the first time.

Additionally, because php://input stream can not be used with fstat() the PhpInputStream::getSize() currently always returns null. See #2 This change will provide support for getting body size.

Will fix #150 if adopted.

Xerkus commented 1 year ago

Apparently I missed the news. php://input is seekable in all supported PHP versions. I can not find the changelog for when that happened.

PhpInputStream should be deprecated instead as it no longer has the role.