Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send static files as chunks using IO::Handle.Supply #106

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/Cro/HTTP/BodySerializers.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class Cro::HTTP::BodySerializer::SupplyFallback does Cro::HTTP::BodySerializer {
$body ~~ Supply
}

method serialize(Cro::HTTP::Message $message, $body --> Supply) {
method serialize(Cro::HTTP::Message $message, $body, Int :$content_length --> Supply) {
self!set-content-length($message, $content_length) if $content_length;
supply {
whenever $body -> $chunk {
unless $chunk ~~ Blob {
Expand Down
23 changes: 18 additions & 5 deletions lib/Cro/HTTP/Router.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ module Cro::HTTP::Router {
#| Specify content to be sent as a response, passing a content type and the
#| body. The body will be serialized using a body serializer. If no request
#| status was set, it will be set to 200 OK.
multi content(Str $content-type, $body, :$enc = $body ~~ Str ?? 'utf-8' !! Nil --> Nil) {
multi content(Str $content-type, $body, :$enc = $body ~~ Str ?? 'utf-8' !! Nil, :$content_length --> Nil) {
my $resp = $*CRO-ROUTER-RESPONSE //
die X::Cro::HTTP::Router::OnlyInHandler.new(:what<content>);
$resp.status //= 200;
Expand All @@ -839,6 +839,9 @@ module Cro::HTTP::Router {
else {
$resp.append-header('Content-type', $content-type);
}

$resp.append-header('content-length', $content_length) if $content_length;

$resp.set-body($body);
}

Expand Down Expand Up @@ -1180,7 +1183,7 @@ module Cro::HTTP::Router {
#| the first argument specifies a base path, and the remaining positional
#| arguments are treated as path segments. However, it is not possible to
#| reach a path above the base.
sub static(IO() $base, *@path, :$mime-types, :@indexes) is export {
sub static(IO() $base, *@path, :$mime-types, :@indexes, Int :$chunk_size) is export {
my $resp = $*CRO-ROUTER-RESPONSE //
die X::Cro::HTTP::Router::OnlyInHandler.new(:what<route>);

Expand All @@ -1196,14 +1199,24 @@ module Cro::HTTP::Router {
for @indexes {
my $index = $path.add($_);
if $index.e {
content get-mime-or-default($index.extension, %fallback), slurp($index, :bin);
return;
if $chunk_size.defined {
content get-mime-or-default($index.extension, %fallback), $index.IO.open(:bin).Supply(size => $chunk_size), content_length => $index.IO.s;
return;
} else {
content get-mime-or-default($index.extension, %fallback), slurp($index, :bin);
return;
}
}
}
$resp.status = 404;
return;
} else {
content get-mime-or-default($path.extension, %fallback), slurp($path, :bin);
if $chunk_size.defined {
content get-mime-or-default($path.extension, %fallback), $path.IO.open(:bin).Supply(size => $chunk_size), content_length => $path.IO.s;
} else {
content get-mime-or-default($path.extension, %fallback), slurp($path, :bin);
}

}
} else {
$resp.status = 404;
Expand Down
11 changes: 11 additions & 0 deletions t/http-router.t
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,9 @@ throws-like { bad-request }, X::Cro::HTTP::Router::OnlyInHandler, what => 'bad-r
'html' => 'text/html'
}
}
get -> 'chunks' {
static 't/samples/', :indexes(['index.xhtml']) , :3chunk_size;
}
}
my $source = Supplier.new;
my $responses = $app.transformer($source.Supply).Channel;
Expand All @@ -1918,6 +1921,7 @@ throws-like { bad-request }, X::Cro::HTTP::Router::OnlyInHandler, what => 'bad-r
given $responses.receive -> $r {
like body-text($r), rx{ '<HTML>Extended</HTML>' \n }, 'Get value from index';
is $r.status, 200, 'Static sets correct status code';
is $r.header('Content-length'), 22, 'Content-length for static served files';
}

$req = Cro::HTTP::Request.new(method => 'GET', target => '/index-plain');
Expand Down Expand Up @@ -1946,6 +1950,13 @@ throws-like { bad-request }, X::Cro::HTTP::Router::OnlyInHandler, what => 'bad-r
is $r.status, 200, 'Indexes with mime-types returns good status';
is $r.header('Content-Type'), 'text/html', 'Indexes with mime-types returns proper content-type';
}

$req = Cro::HTTP::Request.new(method => 'GET', target => '/chunks');
$source.emit($req);
given $responses.receive -> $r {
like body-text($r), rx{ '<HTML>Extended</HTML>' \n }, 'Get index.xhtml as chunks';
is $r.header('Content-length'), 22, 'Content-length for as chunks served files';
}
}

{
Expand Down