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