Core\Request::bodyStream
Core\Request::bodyStream(): Core\Request\BodyStreamReturns the body of the request in pieces, in the order they arrive. A foreach loop reads the pieces one at a time, and each piece is a tainted bytes value.
Use it when the body can be large, for example an uploaded backup or video. Your program handles each piece and can then forget it, so the whole body is never in memory at once. How the body is split into pieces has no meaning, and a word or a character can be split between two pieces. When the request has no body, the loop runs zero times.
The body can be read only once in this way. After bodyStream, the methods body, bytes, json and files throw a LogicError. Calling bodyStream after one of them throws a LogicError too. A command-line program answers no request, so bodyStream throws a LogicError there.
Return value
Returns Core\Request\BodyStream. A value that a foreach loop reads once. Each piece is a tainted bytes value. How the body is split has no meaning. The loop runs zero times when the request has no body.
Errors
LogicErrorThe program is not handling a request. Orbody,bytes,json,filesorbodyStreamalready read the body. After this call, those methods throw this error too.IOErrorThe connection failed while the loop read the body. The pieces before the failure are still given to the loop.
Examples
Measuring a body as it arrives
<?nvs// Shows how to read the body of a request in pieces and add up its size.
uint $total = 0;
// `Core\Request::bodyStream` returns the body in pieces, in the order they arrive.// Each piece is a `tainted bytes` value. How the body is split has no meaning.foreach (Core\Request::bodyStream() as tainted bytes $chunk) { $total = $total + Core\Bytes::length($chunk);}
// The body is 48 bytes long, so this prints `received 48 bytes`.echo 'received ', $total, " bytes\n";received 48 bytesStopping at a size limit
<?nvs// Shows how to stop reading a request body as soon as it is larger than you allow.
uint $limit = 64;uint $total = 0;bool $tooLarge = false;
foreach (Core\Request::bodyStream() as tainted bytes $chunk) { $total = $total + Core\Bytes::length($chunk); // `break` ends the loop. The rest of the body is not read. if ($total > $limit) { $tooLarge = true; break; }}
// The body in this request is longer than 64 bytes.// So this prints `rejected: the body is larger than 64 bytes`.if ($tooLarge) { echo 'rejected: the body is larger than ', $limit, " bytes\n";} else { echo 'accepted ', $total, " bytes\n";}rejected: the body is larger than 64 bytesA checksum of a large upload
<?nvs// Shows how a file service computes the checksum of an upload while it arrives.// The program never needs the whole file in memory at once.
// `Core\Hash::stream` starts a SHA-256 digest that you can feed piece by piece.Core\Hash\Stream $digest = Core\Hash::stream(Core\Hash::Digest::Sha256);uint $size = 0;
foreach (Core\Request::bodyStream() as tainted bytes $chunk) { // `update` adds this piece to the digest. The piece is then no longer needed. $digest->update($chunk); $size = $size + Core\Bytes::length($chunk);}
// `finish` returns the digest of every byte that arrived.// It is the same checksum that `Core\Hash::of` gives for the whole file.echo 'stored ', $size, " bytes\n";echo 'sha-256 ', Core\Encoding::toHex($digest->finish()), "\n";stored 86 bytessha-256 7d1c8a8b4e6e896ce68088e570822efaa2c0b9ee2dd43ae3c5380aa7db3edb0b