/
te-chunked-mw.p6w
51 lines (42 loc) · 1.06 KB
/
te-chunked-mw.p6w
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!smack
use v6;
constant CHUNK-SIZE = 16384;
constant CRLF = "\x0a\x0d";
sub app(%env) {
start {
my $file = "really-big-file.txt".IO;
200, [
Content-Type => 'text/plain',
],
on -> $out {
my $fh = $file.open(:r);
while my $data = $fh.read(CHUNK-SIZE) {
$out.emit($data);
}
$out.done;
()
};
}
}
sub mw(%env) {
nextsame.then(-> $p {
if %env<SERVER_PROTOCOL> eq 'HTTP/1.1'
&& %env<TE> ~~ /\<'chunked'\>/ {
my ($s, List() $h, Supply() $payload) = $p.result;
return $s, $h, $payload
if $h.first(*.key eq 'Transfer-Encoding');
$h.append: Transfer-Encoding => 'chunked';
$s, $h, on -> $out {
$payload => -> $data {
$out.emit(CRLF ~ $data.bytes.base(16) ~ CRLF);
$out.emit($data);
},
};
}
else {
$p.result
}
});
}
&app.wrap(&mw);
&app;