]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WwwAltId.pm
treewide: replace {-inbox} with {ibx} for consistency
[public-inbox.git] / lib / PublicInbox / WwwAltId.pm
index a45d8061069e9402d8ab72c9d3af1f09895aa356..204e2f826871d2b8ffca559346d288c55aef1e2c 100644 (file)
@@ -5,28 +5,19 @@
 package PublicInbox::WwwAltId;
 use strict;
 use PublicInbox::Qspawn;
-use PublicInbox::WwwStream;
+use PublicInbox::WwwStream qw(html_oneshot);
 use PublicInbox::AltId;
 use PublicInbox::Spawn qw(which);
+use PublicInbox::GzipFilter;
 our $sqlite3 = $ENV{SQLITE3};
 
-sub sqlite3_missing ($) {
-       PublicInbox::WwwResponse::oneshot($_[0], 501, \<<EOF);
-<pre>sqlite3 not available
-
-The administrator needs to install the sqlite3(1) binary
-to support gzipped sqlite3 dumps.</pre>
-</pre>
-EOF
-}
-
 sub check_output {
        my ($r, $bref, $ctx) = @_;
-       return PublicInbox::WwwResponse::oneshot($ctx, 500) if !defined($r);
+       return html_oneshot($ctx, 500) if !defined($r);
        if ($r == 0) {
                my $err = eval { $ctx->{env}->{'psgi.errors'} } // \*STDERR;
                $err->print("unexpected EOF from sqlite3\n");
-               return PublicInbox::WwwResponse::oneshot($ctx, 501);
+               return html_oneshot($ctx, 501);
        }
        [200, [ qw(Content-Type application/gzip), 'Content-Disposition',
                "inline; filename=$ctx->{altid_pfx}.sql.gz" ] ]
@@ -38,32 +29,38 @@ sub check_output {
 # and thus not usable from DBD::SQLite.
 sub sqldump ($$) {
        my ($ctx, $altid_pfx) = @_;
-       my $ibx = $ctx->{-inbox};
+       my $env = $ctx->{env};
+       my $ibx = $ctx->{ibx};
        my $altid_map = $ibx->altid_map;
        my $fn = $altid_map->{$altid_pfx};
        unless (defined $fn) {
-               return PublicInbox::WwwStream::oneshot($ctx, 404, \<<EOF);
+               return html_oneshot($ctx, 404, \<<EOF);
 <pre>`$altid_pfx' is not a valid altid for this inbox</pre>
 EOF
        }
 
-       eval { require PublicInbox::GzipFilter } or
-               return PublicInbox::WwwStream::oneshot($ctx, 501, \<<EOF);
-<pre>gzip output not available
+       if ($env->{REQUEST_METHOD} ne 'POST') {
+               my $url = $ibx->base_url($ctx->{env}) . "$altid_pfx.sql.gz";
+               return html_oneshot($ctx, 405, \<<EOF);
+<pre>A POST request required to retrieve $altid_pfx.sql.gz
 
-The administrator needs to install the Compress::Raw::Zlib Perl module
-to support gzipped sqlite3 dumps.</pre>
+       curl -XPOST -O $url
+
+or
+
+       curl -XPOST $url | \\
+               gzip -dc | \\
+               sqlite3 /path/to/$altid_pfx.sqlite3
+</pre>
 EOF
-       $sqlite3 //= which('sqlite3');
-       if (!defined($sqlite3)) {
-               return PublicInbox::WwwStream::oneshot($ctx, 501, \<<EOF);
+       }
+
+       $sqlite3 //= which('sqlite3') // return html_oneshot($ctx, 501, \<<EOF);
 <pre>sqlite3 not available
 
 The administrator needs to install the sqlite3(1) binary
 to support gzipped sqlite3 dumps.</pre>
-</pre>
 EOF
-       }
 
        # setup stdin, POSIX requires writes <= 512 bytes to succeed so
        # we can close the pipe right away.
@@ -73,7 +70,6 @@ EOF
 
        # TODO: use -readonly if available with newer sqlite3(1)
        my $qsp = PublicInbox::Qspawn->new([$sqlite3, $fn], undef, { 0 => $r });
-       my $env = $ctx->{env};
        $ctx->{altid_pfx} = $altid_pfx;
        $env->{'qspawn.filter'} = PublicInbox::GzipFilter->new;
        $qsp->psgi_return($env, undef, \&check_output, $ctx);