]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MboxGz.pm
716bf7b19d181af1791f8c81994e147fabbb9335
[public-inbox.git] / lib / PublicInbox / MboxGz.pm
1 # Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::MboxGz;
4 use strict;
5 use parent 'PublicInbox::GzipFilter';
6 use PublicInbox::Eml;
7 use PublicInbox::Hval qw/to_filename/;
8 use PublicInbox::Mbox;
9 use PublicInbox::GitAsyncCat;
10 *msg_hdr = \&PublicInbox::Mbox::msg_hdr;
11 *msg_body = \&PublicInbox::Mbox::msg_body;
12
13 # this is public-inbox-httpd-specific
14 sub mboxgz_blob_cb { # git->cat_async callback
15         my ($bref, $oid, $type, $size, $self) = @_;
16         my $http = $self->{env}->{'psgix.io'} or return; # client abort
17         my $smsg = delete $self->{smsg} or die 'BUG: no smsg';
18         if (!defined($oid)) {
19                 # it's possible to have TOCTOU if an admin runs
20                 # public-inbox-(edit|purge), just move onto the next message
21                 return $http->next_step(\&mboxgz_async_next);
22         } else {
23                 $smsg->{blob} eq $oid or die "BUG: $smsg->{blob} != $oid";
24         }
25         my $eml = PublicInbox::Eml->new($bref);
26         $self->zmore(msg_hdr($self, $eml, $smsg->{mid}));
27
28         # PublicInbox::HTTP::{Chunked,Identity}::write
29         $self->{http_out}->write($self->translate(msg_body($eml)));
30
31         $http->next_step(\&mboxgz_async_next);
32 }
33
34 # this is public-inbox-httpd-specific
35 sub mboxgz_async_step ($) {
36         my ($self) = @_;
37         if (my $smsg = $self->{smsg} = $self->{cb}->($self)) {
38                 git_async_cat($self->{-inbox}->git, $smsg->{blob},
39                                 \&mboxgz_blob_cb, $self);
40         } elsif (my $out = delete $self->{http_out}) {
41                 $out->write($self->zflush);
42                 $out->close;
43         }
44 }
45
46 # called by PublicInbox::DS::write
47 sub mboxgz_async_next {
48         my ($http) = @_; # PublicInbox::HTTP
49         mboxgz_async_step($http->{forward});
50 }
51
52 # called by PublicInbox::HTTP::close, or any other PSGI server
53 sub close { !!delete($_[0]->{http_out}) }
54
55 sub response {
56         my ($self, $cb, $res_hdr) = @_;
57         $self->{cb} = $cb;
58         bless $self, __PACKAGE__;
59         if ($self->{env}->{'pi-httpd.async'}) {
60                 sub {
61                         my ($wcb) = @_; # -httpd provided write callback
62                         $self->{http_out} = $wcb->([200, $res_hdr]);
63                         $self->{env}->{'psgix.io'}->{forward} = $self;
64                         mboxgz_async_step($self); # start stepping
65                 };
66         } else { # generic PSGI
67                 [ 200, $res_hdr, $self ];
68         }
69 }
70
71 sub mbox_gz {
72         my ($self, $cb, $fn) = @_;
73         $self->{base_url} = $self->{-inbox}->base_url($self->{env});
74         $self->{gz} = PublicInbox::GzipFilter::gzip_or_die();
75         $fn = to_filename($fn // 'no-subject');
76         $fn = 'no-subject' if $fn eq '';
77         # http://www.iana.org/assignments/media-types/application/gzip
78         response($self, $cb, [ qw(Content-Type application/gzip),
79                 'Content-Disposition', "inline; filename=$fn.mbox.gz" ]);
80 }
81
82 # called by Plack::Util::foreach or similar (generic PSGI)
83 sub getline {
84         my ($self) = @_;
85         my $cb = $self->{cb} or return;
86         while (my $smsg = $cb->($self)) {
87                 my $eml = $self->{-inbox}->smsg_eml($smsg) or next;
88                 $self->zmore(msg_hdr($self, $eml, $smsg->{mid}));
89                 return $self->translate(msg_body($eml));
90         }
91         # signal that we're done and can return undef next call:
92         delete $self->{cb};
93         $self->zflush;
94 }
95
96 1;