From c8fe05f1c72412c2f1fd5f1adc9989eab4fc0d50 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Sun, 30 Apr 2017 18:07:48 +0300 Subject: nix-serve: do not compress when NAR size < 1 KiB --- modules/pkgs/nix-serve/nix-serve.psgi | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'modules/pkgs/nix-serve') diff --git a/modules/pkgs/nix-serve/nix-serve.psgi b/modules/pkgs/nix-serve/nix-serve.psgi index ac4071b..696f752 100644 --- a/modules/pkgs/nix-serve/nix-serve.psgi +++ b/modules/pkgs/nix-serve/nix-serve.psgi @@ -19,15 +19,24 @@ my $app = sub { return [200, ['Content-Type' => 'text/plain'], ["StoreDir: $Nix::Config::storeDir\nWantMassQuery: 1\nPriority: 30\n"]]; } - elsif ($path =~ "/([0-9a-z]+)\.narinfo") { + elsif ($path =~ '/([0-9a-z]+)\.narinfo$') { my $hashPart = $1; my $storePath = queryPathFromHashPart($hashPart); return [404, ['Content-Type' => 'text/plain'], ["No such path.\n"]] unless $storePath; my ($deriver, $narHash, $time, $narSize, $refs) = queryPathInfo($storePath, 1) or die; + my $compression; + my $ext; + if ($narSize < 1024) { + $compression = 'none'; + $ext = ''; + } else { + $compression = 'xz'; + $ext = '.xz'; + } my $res = "StorePath: $storePath\n" . - "URL: nar/$hashPart.nar.xz\n" . - "Compression: xz\n" . + "URL: nar/$hashPart.nar$ext\n" . + "Compression: $compression\n" . "NarHash: $narHash\n" . "NarSize: $narSize\n"; $res .= "References: " . join(" ", map { stripPath($_) } @$refs) . "\n" @@ -46,7 +55,7 @@ my $app = sub { return [200, ['Content-Type' => 'text/x-nix-narinfo'], [$res]]; } - elsif ($path =~ "/nar/([0-9a-z]+)\.nar.xz") { + elsif ($path =~ '/nar/([0-9a-z]+)\.nar.xz$') { my $hashPart = $1; my $storePath = queryPathFromHashPart($hashPart); return [404, ['Content-Type' => 'text/plain'], ["No such path.\n"]] unless $storePath; @@ -55,6 +64,15 @@ my $app = sub { return [200, ['Content-Type' => 'application/x-xz'], $fh]; } + elsif ($path =~ '/nar/([0-9a-z]+)\.nar$') { + my $hashPart = $1; + my $storePath = queryPathFromHashPart($hashPart); + return [404, ['Content-Type' => 'text/plain'], ["No such path.\n"]] unless $storePath; + my $fh = new IO::Handle; + open $fh, "nix-store --dump '$storePath' |"; + return [200, ['Content-Type' => 'application/octet-stream'], $fh]; + } + else { return [404, ['Content-Type' => 'text/plain'], ["File not found.\n"]]; } -- cgit v1.2.3