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;
5 use parent 'PublicInbox::GzipFilter';
7 use PublicInbox::Hval qw/to_filename/;
9 use PublicInbox::GitAsyncCat;
10 *msg_hdr = \&PublicInbox::Mbox::msg_hdr;
11 *msg_body = \&PublicInbox::Mbox::msg_body;
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';
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);
23 $smsg->{blob} eq $oid or die "BUG: $smsg->{blob} != $oid";
25 my $eml = PublicInbox::Eml->new($bref);
26 $self->zmore(msg_hdr($self, $eml, $smsg->{mid}));
28 # PublicInbox::HTTP::{Chunked,Identity}::write
29 $self->{http_out}->write($self->translate(msg_body($eml)));
31 $http->next_step(\&mboxgz_async_next);
34 # this is public-inbox-httpd-specific
35 sub mboxgz_async_step ($) {
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);
46 # called by PublicInbox::DS::write
47 sub mboxgz_async_next {
48 my ($http) = @_; # PublicInbox::HTTP
49 mboxgz_async_step($http->{forward});
52 # called by PublicInbox::HTTP::close, or any other PSGI server
53 sub close { !!delete($_[0]->{http_out}) }
56 my ($self, $cb, $res_hdr) = @_;
58 bless $self, __PACKAGE__;
59 if ($self->{env}->{'pi-httpd.async'}) {
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
66 } else { # generic PSGI
67 [ 200, $res_hdr, $self ];
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" ]);
82 # called by Plack::Util::foreach or similar (generic PSGI)
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));
91 # signal that we're done and can return undef next call: