tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

36 lignes · 922 octetsBrut · Historique
1// Reading a response's body in hand (ADR 0053): a streamed body is
2// written into memory, chunk by chunk, as the server would send it.
3
4import std/bytes
5import std/http
6import std/io
7import std/process.{type proc}
8import std/random.{type random}
9import std/time.{type clock}
10
11fn text :: resp: http.Response -> String / <io, proc, clock, random, ffi>
12{
13 bytes.to_string_lossy (http.collect_body resp)
14}
15
16pub fn main :: -> () / <io, proc, clock, random, ffi>
17{
18 io.println (text (http.text 200 "whole"))
19 let streamed =
20 http.stream
21 "text/html; charset=utf-8"
22 fn w
23 {
24 let _ = http.write_string w "<p>one</p>"
25 let _ = http.write_string w ""
26 let _ = http.write_chunk w (bytes.from_string "<p>two</p>")
27 ()
28 }
29 io.println (text streamed)
30 io.println (text (http.stream "text/plain" fn _ { () }))
31 io.println "end"
32}
33//> whole
34//> <p>one</p><p>two</p>
35//>
36//> end