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(\&async_next);
23 $smsg->{blob} eq $oid or die "BUG: $smsg->{blob} != $oid";
25 $self->zmore(msg_hdr($self,
26 PublicInbox::Eml->new($bref)->header_obj,
29 # PublicInbox::HTTP::{Chunked,Identity}::write
30 $self->{http_out}->write($self->translate(msg_body($$bref)));
32 $http->next_step(\&async_next);
35 # this is public-inbox-httpd-specific
38 if (my $smsg = $self->{smsg} = $self->{cb}->($self)) {
39 git_async_cat($self->{-inbox}->git, $smsg->{blob},
40 \&mboxgz_blob_cb, $self);
41 } elsif (my $out = delete $self->{http_out}) {
42 $out->write($self->zflush);
47 # called by PublicInbox::DS::write
49 my ($http) = @_; # PublicInbox::HTTP
50 async_step($http->{forward});
53 # called by PublicInbox::HTTP::close, or any other PSGI server
54 sub close { !!delete($_[0]->{http_out}) }
57 my ($class, $self, $cb, $fn) = @_;
58 $self->{base_url} = $self->{-inbox}->base_url($self->{env});
60 $self->{gz} = PublicInbox::GzipFilter::gzip_or_die();
62 # http://www.iana.org/assignments/media-types/application/gzip
63 $fn = defined($fn) && $fn ne '' ? to_filename($fn) : 'no-subject';
64 my $h = [ qw(Content-Type application/gzip),
65 'Content-Disposition', "inline; filename=$fn.mbox.gz" ];
66 if ($self->{env}->{'pi-httpd.async'}) {
68 my ($wcb) = @_; # -httpd provided write callback
69 $self->{http_out} = $wcb->([200, $h]);
70 $self->{env}->{'psgix.io'}->{forward} = $self;
71 async_step($self); # start stepping
73 } else { # generic PSGI
78 # called by Plack::Util::foreach or similar (generic PSGI)
81 my $cb = $self->{cb} or return;
82 while (my $smsg = $cb->($self)) {
83 my $mref = $self->{-inbox}->msg_by_smsg($smsg) or next;
84 my $h = PublicInbox::Eml->new($mref)->header_obj;
85 $self->zmore(msg_hdr($self, $h, $smsg->{mid}));
86 return $self->translate(msg_body($$mref));
88 # signal that we're done and can return undef next call: