Core\Http\Stream::lines
$stream->lines(): Core\Http\LinesReads the body of a streamed reply one line at a time.
A foreach over lines() gives each line as a tainted string, because another server sent it. A line ends at \n. A \r just before the \n is removed too, so both kinds of line ending work. An empty line is given as an empty string. The last line is given even when it has no line ending.
The body of a stream can be read only once. After lines(), a call to lines(), chunks(), events() or saveTo() on the same stream throws a LogicError.
Good to know: only the current line is kept in memory, so the reply can be much larger than the memory your program may use. One line may have at most 64 KB. A longer line throws an error.
The examples below number the lines of a log, show which line endings are removed, and add up a column of a large export.
Return value
Returns Core\Http\Lines. An Iterable<tainted string> that a foreach can loop over once. A line ends at \n, and a \r at the end of a line is removed. The last line does not need a \n.
Errors
LogicErrorThe body of this stream has already been read. The error message names the method that read it.RuntimeErrorA line of the reply is longer than the line limit, or a line is not valid UTF-8. It is also thrown when the reply's chunked encoding is broken.TimeoutErrorNo data arrived for longer thanidle, or the body took longer than itsmaxDuration.IOErrorThe connection failed while the body was arriving.
Examples
Printing each line of a log
<?nvs// Shows how to read a text reply one line at a time.
// The reply comes from this table, so nothing is sent over a network.Core\Test::answerHttp('https://logs.example.com/today', 200, { body: "09:00 server started\n09:05 first request\n09:07 backup done\n",});
Core\Http\Stream $log = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://logs.example.com/today');uint $number = 1;// Each line is given without its line ending. This prints three numbered lines.foreach ($log->lines() as tainted string $line) { echo $number, ': ', $line, "\n"; $number = $number + 1;}1: 09:00 server started2: 09:05 first request3: 09:07 backup doneLine endings and the last line
<?nvs// Shows which line endings `lines()` removes, and what it does with the last line.
// The first line ends with "\r\n", the second with "\n". The third line is empty.// The last line has no line ending at all.Core\Test::answerHttp('https://data.example.com/names.txt', 200, { body: "Ada\r\nGrace\n\nLinus",});
Core\Http\Stream $names = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://data.example.com/names.txt');// Both line endings are removed, and the last line is given when the body ends.// This prints [Ada], [Grace], [] and [Linus].foreach ($names->lines() as tainted string $line) { echo '[', $line, "]\n";}[Ada][Grace][][Linus]Adding up a large export
<?nvs// Shows how to add up a column of a large export, one line at a time.
// Each line of this export is one order: its id and its price in cents.// The first line names the columns.Core\Test::answerHttp('https://shop.example.com/export/orders.csv', 200, { body: "id,cents\nA-1,1250\nA-2,899\nA-3,4000\n",});
Core\Http\Stream $export = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://shop.example.com/export/orders.csv');bool $first = true;uint $orders = 0;int $total = 0;// Only one line is in memory at a time, so the export can be very large.foreach ($export->lines() as tainted string $line) { if ($first) { $first = false; continue; } array<tainted string> $fields = Core\Str::split($line, ','); // `as int` converts the text to a whole number. Text that is not a number throws an error. $total = $total + ($fields[1] as int); $orders = $orders + 1;}
// This prints "3 orders, 6149 cents".echo $orders, ' orders, ', $total, " cents\n";3 orders, 6149 cents