]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Mbox.pm
www: update internal docs
[public-inbox.git] / lib / PublicInbox / Mbox.pm
index 16de1a728ab6345f58c0637f537bfca6485a44ce..8726b9f641b248e9f581f568a4eba67fb91e3657 100644 (file)
@@ -1,82 +1,62 @@
 # Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
-# Streaming (via getline) interface for formatting messages as an mboxrd.
-# Used by the PSGI web interface.
-#
-# public-inbox-httpd favors "getline" response bodies to take a
-# "pull"-based approach to feeding slow clients (as opposed to a
-# more common "push" model)
+# Streaming interface for mboxrd HTTP responses
+# See PublicInbox::GzipFilter for details.
 package PublicInbox::Mbox;
 use strict;
-use warnings;
+use parent 'PublicInbox::GzipFilter';
 use PublicInbox::MID qw/mid_escape/;
 use PublicInbox::Hval qw/to_filename/;
 use PublicInbox::Smsg;
-use PublicInbox::WwwStream qw(html_oneshot);
-use Email::Simple;
-use Email::MIME::Encode;
-
-sub subject_fn ($) {
-       my ($hdr) = @_;
-       my $fn = $hdr->header('Subject');
-       return 'no-subject' if (!defined($fn) || $fn eq '');
-
-       # no need for full Email::MIME, here
-       if ($fn =~ /=\?/) {
-               eval { $fn = Encode::decode('MIME-Header', $fn) };
-               return 'no-subject' if $@;
-       }
-       $fn =~ s/^re:\s+//i;
-       $fn eq '' ? 'no-subject' : to_filename($fn);
-}
-
-sub mb_stream {
-       my ($more) = @_;
-       bless $more, 'PublicInbox::Mbox';
-}
+use PublicInbox::Eml;
 
 # called by PSGI server as body response
 # this gets called twice for every message, once to return the header,
 # once to retrieve the body
 sub getline {
-       my ($more) = @_; # self
-       my ($ctx, $id, $prev, $next, $mref, $hdr) = @$more;
-       if ($hdr) { # first message hits this, only
-               pop @$more; # $hdr
-               pop @$more; # $mref
-               return msg_hdr($ctx, $hdr) . msg_body($$mref);
-       }
-       my $cur = $next or return;
+       my ($ctx) = @_; # ctx
+       my $smsg = $ctx->{smsg} or return;
        my $ibx = $ctx->{-inbox};
-       $next = $ibx->over->next_by_mid($ctx->{mid}, \$id, \$prev);
-       $mref = $ibx->msg_by_smsg($cur) or return;
-       $hdr = Email::Simple->new($mref)->header_obj;
-       @$more = ($ctx, $id, $prev, $next); # $next may be undef, here
-       msg_hdr($ctx, $hdr) . msg_body($$mref);
+       my $eml = $ibx->smsg_eml($smsg) or return;
+       my $n = $ctx->{smsg} = $ibx->over->next_by_mid(@{$ctx->{next_arg}});
+       $ctx->zmore(msg_hdr($ctx, $eml, $smsg->{mid}));
+       if ($n) {
+               $ctx->translate(msg_body($eml));
+       } else { # last message
+               $ctx->zmore(msg_body($eml));
+               $ctx->zflush;
+       }
+}
+
+# called by PublicInbox::DS::write
+sub async_next {
+       my ($http) = @_; # PublicInbox::HTTP
+       my $ctx = $http->{forward} or return; # client aborted
+       eval {
+               my $smsg = $ctx->{smsg} or return $ctx->close;
+               $ctx->smsg_blob($smsg);
+       };
+       warn "E: $@" if $@;
 }
 
-sub close {} # noop
+sub async_eml { # ->{async_eml} for async_blob_cb
+       my ($ctx, $eml) = @_;
+       my $smsg = delete $ctx->{smsg};
+       # next message
+       $ctx->{smsg} = $ctx->{-inbox}->over->next_by_mid(@{$ctx->{next_arg}});
 
-# /$INBOX/$MESSAGE_ID/raw
-sub emit_raw {
-       my ($ctx) = @_;
-       my $mid = $ctx->{mid};
-       my $ibx = $ctx->{-inbox};
-       $ctx->{base_url} = $ibx->base_url($ctx->{env});
-       my ($mref, $more, $id, $prev, $next);
-       if (my $over = $ibx->over) {
-               my $smsg = $over->next_by_mid($mid, \$id, \$prev) or return;
-               $mref = $ibx->msg_by_smsg($smsg) or return;
-               $next = $over->next_by_mid($mid, \$id, \$prev);
-       } else {
-               $mref = $ibx->msg_by_mid($mid) or return;
-       }
-       my $hdr = Email::Simple->new($mref)->header_obj;
-       $more = [ $ctx, $id, $prev, $next, $mref, $hdr ]; # for ->getline
-       my $fn = subject_fn($hdr);
+       $ctx->zmore(msg_hdr($ctx, $eml, $smsg->{mid}));
+       $ctx->{http_out}->write($ctx->translate(msg_body($eml)));
+}
+
+sub res_hdr ($$) {
+       my ($ctx, $subject) = @_;
+       my $fn = $subject // 'no-subject';
+       $fn =~ s/^re:\s+//i;
+       $fn = $fn eq '' ? 'no-subject' : to_filename($fn);
        my @hdr = ('Content-Type');
-       if ($ibx->{obfuscate}) {
+       if ($ctx->{-inbox}->{obfuscate}) {
                # obfuscation is stupid, but maybe scrapers are, too...
                push @hdr, 'application/mbox';
                $fn .= '.mbox';
@@ -85,11 +65,34 @@ sub emit_raw {
                $fn .= '.txt';
        }
        push @hdr, 'Content-Disposition', "inline; filename=$fn";
-       [ 200, \@hdr, mb_stream($more) ];
+       \@hdr;
+}
+
+# for rare cases where v1 inboxes aren't indexed w/ ->over at all
+sub no_over_raw ($) {
+       my ($ctx) = @_;
+       my $mref = $ctx->{-inbox}->msg_by_mid($ctx->{mid}) or return;
+       my $eml = PublicInbox::Eml->new($mref);
+       [ 200, res_hdr($ctx, $eml->header_str('Subject')),
+               [ msg_hdr($ctx, $eml, $ctx->{mid}) . msg_body($eml) ] ]
+}
+
+# /$INBOX/$MESSAGE_ID/raw
+sub emit_raw {
+       my ($ctx) = @_;
+       $ctx->{base_url} = $ctx->{-inbox}->base_url($ctx->{env});
+       my $over = $ctx->{-inbox}->over or return no_over_raw($ctx);
+       my ($id, $prev);
+       my $mip = $ctx->{next_arg} = [ $ctx->{mid}, \$id, \$prev ];
+       my $smsg = $ctx->{smsg} = $over->next_by_mid(@$mip) or return;
+       my $res_hdr = res_hdr($ctx, $smsg->{subject});
+       bless $ctx, __PACKAGE__;
+       $ctx->psgi_response(200, $res_hdr, \&async_next, \&async_eml);
 }
 
 sub msg_hdr ($$;$) {
-       my ($ctx, $header_obj, $mid) = @_;
+       my ($ctx, $eml, $mid) = @_;
+       my $header_obj = $eml->header_obj;
 
        # drop potentially confusing headers, ssoma already should've dropped
        # Lines and Content-Length
@@ -106,12 +109,15 @@ sub msg_hdr ($$;$) {
                'List-Post', "<mailto:$ibx->{-primary_address}>",
        );
        my $crlf = $header_obj->crlf;
-       my $buf = 'From mboxrd@z Thu Jan  1 00:00:00 1970' . $crlf .
-                       $header_obj->as_string;
+       my $buf = $header_obj->as_string;
+       # fixup old bug from import (pre-a0c07cba0e5d8b6a)
+       $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+       $buf = "From mboxrd\@z Thu Jan  1 00:00:00 1970" . $crlf . $buf;
+
        for (my $i = 0; $i < @append; $i += 2) {
                my $k = $append[$i];
                my $v = $append[$i + 1];
-               my @v = $header_obj->header($k);
+               my @v = $header_obj->header_raw($k);
                foreach (@v) {
                        if ($v eq $_) {
                                $v = undef;
@@ -124,12 +130,13 @@ sub msg_hdr ($$;$) {
 }
 
 sub msg_body ($) {
+       my $bdy = $_[0]->{bdy} // return "\n";
        # mboxrd quoting style
        # https://en.wikipedia.org/wiki/Mbox#Modified_mbox
        # https://www.loc.gov/preservation/digital/formats/fdd/fdd000385.shtml
        # https://web.archive.org/http://www.qmail.org/man/man5/mbox.html
-       $_[0] =~ s/^(>*From )/>$1/gm;
-       $_[0] .= "\n";
+       $$bdy =~ s/^(>*From )/>$1/gm;
+       $$bdy .= "\n";
 }
 
 sub thread_cb {
@@ -149,13 +156,12 @@ sub thread_cb {
 
 sub thread_mbox {
        my ($ctx, $over, $sfx) = @_;
-       eval { require PublicInbox::MboxGz };
-       return need_gzip($ctx) if $@;
        my $msgs = $ctx->{msgs} = $over->get_thread($ctx->{mid}, {});
        return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
        $ctx->{prev} = $msgs->[-1];
        $ctx->{over} = $over; # bump refcnt
-       PublicInbox::MboxGz->response($ctx, \&thread_cb, $msgs->[0]->subject);
+       require PublicInbox::MboxGz;
+       PublicInbox::MboxGz::mbox_gz($ctx, \&thread_cb, $msgs->[0]->{subject});
 }
 
 sub emit_range {
@@ -193,7 +199,8 @@ sub mbox_all_ids {
                return PublicInbox::WWW::need($ctx, 'Overview');
        $ctx->{ids} = $ids;
        $ctx->{prev} = $prev;
-       return PublicInbox::MboxGz->response($ctx, \&all_ids_cb, 'all');
+       require PublicInbox::MboxGz;
+       PublicInbox::MboxGz::mbox_gz($ctx, \&all_ids_cb, 'all');
 }
 
 sub results_cb {
@@ -218,8 +225,6 @@ sub results_cb {
 sub mbox_all {
        my ($ctx, $query) = @_;
 
-       eval { require PublicInbox::MboxGz };
-       return need_gzip($ctx) if $@;
        return mbox_all_ids($ctx) if $query eq '';
        my $qopts = $ctx->{qopts} = { mset => 2 };
        my $srch = $ctx->{srch} = $ctx->{-inbox}->search or
@@ -230,18 +235,8 @@ sub mbox_all {
                                ["No results found\n"]];
        $ctx->{iter} = 0;
        $ctx->{query} = $query;
-       PublicInbox::MboxGz->response($ctx, \&results_cb, 'results-'.$query);
-}
-
-sub need_gzip {
-       html_oneshot($_[0], 501, \<<EOF);
-<pre>gzipped mbox not available
-
-The administrator needs to install the Compress::Raw::Zlib Perl module
-to support gzipped mboxes.
-
-<a href="../">Return to index</a></pre>
-EOF
+       require PublicInbox::MboxGz;
+       PublicInbox::MboxGz::mbox_gz($ctx, \&results_cb, 'results-'.$query);
 }
 
 1;