]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAltId.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / WwwAltId.pm
1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # dumps using the ".dump" command of sqlite3(1)
5 package PublicInbox::WwwAltId;
6 use strict;
7 use PublicInbox::Qspawn;
8 use PublicInbox::WwwStream qw(html_oneshot);
9 use PublicInbox::AltId;
10 use PublicInbox::Spawn qw(which);
11 use PublicInbox::GzipFilter;
12 our $sqlite3 = $ENV{SQLITE3};
13
14 sub check_output {
15         my ($r, $bref, $ctx) = @_;
16         return html_oneshot($ctx, 500) if !defined($r);
17         if ($r == 0) {
18                 my $err = eval { $ctx->{env}->{'psgi.errors'} } // \*STDERR;
19                 $err->print("unexpected EOF from sqlite3\n");
20                 return html_oneshot($ctx, 501);
21         }
22         [200, [ qw(Content-Type application/gzip), 'Content-Disposition',
23                 "inline; filename=$ctx->{altid_pfx}.sql.gz" ] ]
24 }
25
26 # POST $INBOX/$prefix.sql.gz
27 # we use the sqlite3(1) binary here since that's where the ".dump"
28 # command is implemented, not (AFAIK) in the libsqlite3 library
29 # and thus not usable from DBD::SQLite.
30 sub sqldump ($$) {
31         my ($ctx, $altid_pfx) = @_;
32         my $env = $ctx->{env};
33         my $ibx = $ctx->{ibx};
34         my $altid_map = $ibx->altid_map;
35         my $fn = $altid_map->{$altid_pfx};
36         unless (defined $fn) {
37                 return html_oneshot($ctx, 404, \<<EOF);
38 <pre>`$altid_pfx' is not a valid altid for this inbox</pre>
39 EOF
40         }
41
42         if ($env->{REQUEST_METHOD} ne 'POST') {
43                 my $url = $ibx->base_url($ctx->{env}) . "$altid_pfx.sql.gz";
44                 return html_oneshot($ctx, 405, \<<EOF);
45 <pre>A POST request required to retrieve $altid_pfx.sql.gz
46
47         curl -XPOST -O $url
48
49 or
50
51         curl -XPOST $url | \\
52                 gzip -dc | \\
53                 sqlite3 /path/to/$altid_pfx.sqlite3
54 </pre>
55 EOF
56         }
57
58         $sqlite3 //= which('sqlite3') // return html_oneshot($ctx, 501, \<<EOF);
59 <pre>sqlite3 not available
60
61 The administrator needs to install the sqlite3(1) binary
62 to support gzipped sqlite3 dumps.</pre>
63 EOF
64
65         # setup stdin, POSIX requires writes <= 512 bytes to succeed so
66         # we can close the pipe right away.
67         pipe(my ($r, $w)) or die "pipe: $!";
68         syswrite($w, ".dump\n") == 6 or die "write: $!";
69         close($w) or die "close: $!";
70
71         # TODO: use -readonly if available with newer sqlite3(1)
72         my $qsp = PublicInbox::Qspawn->new([$sqlite3, $fn], undef, { 0 => $r });
73         $ctx->{altid_pfx} = $altid_pfx;
74         $env->{'qspawn.filter'} = PublicInbox::GzipFilter->new;
75         $qsp->psgi_return($env, undef, \&check_output, $ctx);
76 }
77
78 1;