Core\Request\BodyStream::peek
Returns the first bytes of the request body, up to max bytes. The bytes stay in the body, so a foreach loop over the same value still gives every byte from the start.
Use it to check the start of an upload before you read or save the rest. A body shorter than max is returned whole. A second call with a larger max reads further. The result is tainted bytes.
Call peek before the loop starts. After that, it throws a LogicError. A max larger than the request's memory limit also throws a LogicError, and nothing is read. To find the file type of an upload, use Core\Mime::detect with the body stream.
The examples show how to check a CSV header before the file is saved, how to reject an old file version, and that the loop still reads every byte.
Parameters
$maxuintThe most bytes to return. A body shorter thanmaxis returned whole. A second call with a largermaxreads further.
Return value
Returns tainted bytes. The first max bytes of the body, or fewer. The value is tainted.
Errors
LogicErrorThis program is not handling a request. Or aforeachloop has already read this value. Ormaxis larger than this request's[limits] memory.IOErrorThe connection failed while the body was read.
Examples
Checking a csv header before saving
<?nvs// Shows how to check the first line of an uploaded CSV file before the program saves it.
Core\Request\BodyStream $body = Core\Request::bodyStream();
// `peek` returns the first 21 bytes of the body. It does not remove them from the body.tainted bytes $start = $body->peek(21);
if (Core\Bytes::startsWith($start, "order,customer,total\n" as bytes)) { // `writeStream` still gets every byte, including the 21 bytes that `peek` read. string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'orders.csv'); Core\IO::writeStream($path, $body); // This prints `saved 86 bytes`. echo 'saved ', Core\IO::size($path), " bytes\n";} else { echo "this is not an order list\n";}saved 86 bytesRefusing an old file version
<?nvs// Shows how to reject an upload in an old file format before the program reads it.
// Every file this program accepts starts with the line `NOTES v2`.Core\Request\BodyStream $body = Core\Request::bodyStream();tainted bytes $marker = $body->peek(9);
if (Core\Bytes::startsWith($marker, "NOTES v2\n" as bytes)) { uint $total = 0; foreach ($body as tainted bytes $chunk) { $total = $total + Core\Bytes::length($chunk); } echo 'accepted ', $total, " bytes\n";} else { // The upload starts with `NOTES v1`, so this message is printed. echo "rejected: please export the notes again as version 2\n";}rejected: please export the notes again as version 2The loop still reads every byte
<?nvs// Shows that the bytes `peek` reads are still in the body for the `foreach` loop.
Core\Request\BodyStream $body = Core\Request::bodyStream();
// `peek(4)` returns the first 4 bytes. This prints `peeked 4 bytes`.tainted bytes $start = $body->peek(4);echo 'peeked ', Core\Bytes::length($start), " bytes\n";
// A second `peek` with a larger `max` reads further. This prints `peeked 10 bytes`.echo 'peeked ', Core\Bytes::length($body->peek(10)), " bytes\n";
// The loop starts at the first byte of the body. This prints `the loop read 48 bytes`.uint $total = 0;foreach ($body as tainted bytes $chunk) { $total = $total + Core\Bytes::length($chunk);}echo 'the loop read ', $total, " bytes\n";peeked 4 bytespeeked 10 bytesthe loop read 48 bytes