X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FMbox.pm;h=fc83a893c60ab384f21061d3b2dff12edb6f365c;hb=0821af5f21fdb083020ae2e3e79e4227ef59cd4f;hp=1f9ac6ec88cfd1a0989a0da5f7d44ab8590230cd;hpb=65e3cc8f6cc73e45db827cbeee4ccecbf1502496;p=public-inbox.git diff --git a/lib/PublicInbox/Mbox.pm b/lib/PublicInbox/Mbox.pm index 1f9ac6ec..fc83a893 100644 --- a/lib/PublicInbox/Mbox.pm +++ b/lib/PublicInbox/Mbox.pm @@ -1,85 +1,62 @@ -# Copyright (C) 2015-2019 all contributors +# Copyright (C) 2015-2020 all contributors # License: AGPL-3.0+ -# 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::SearchMsg; -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) }; - $fn = 'no-subject' if $@; - } - $fn =~ s/^re:\s+//i; - $fn = to_filename($fn); - $fn eq '' ? 'no-subject' : $fn; -} - -sub mb_stream { - my ($more) = @_; - bless $more, 'PublicInbox::Mbox'; -} +use PublicInbox::Smsg; +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 - return msg_hdr($ctx, $hdr); - } - if ($mref) { # all messages hit this - pop @$more; # $mref - return 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, $mref); # $next may be undef, here - msg_hdr($ctx, $hdr); # all but first message hits this + 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; + } } -sub close {} # noop +# called by PublicInbox::DS::write after http->next_step +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 $@; +} -# /$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); +sub 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}}); + + $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 // ''; + $fn =~ s/^re:\s+//i; + $fn = to_filename($fn) // 'no-subject'; 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'; @@ -88,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); } 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 @@ -109,12 +109,15 @@ sub msg_hdr ($$;$) { 'List-Post', "{-primary_address}>", ); my $crlf = $header_obj->crlf; - my $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970\n" . - $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; @@ -127,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 { @@ -152,13 +156,12 @@ sub thread_cb { sub thread_mbox { my ($ctx, $over, $sfx) = @_; - eval { require PublicInbox::MboxGz }; - return need_gzip() 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 { @@ -196,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 { @@ -205,7 +209,7 @@ sub results_cb { my $srch = $ctx->{srch}; while (1) { while (my $mi = (($mset->items)[$ctx->{iter}++])) { - my $smsg = PublicInbox::SearchMsg::from_mitem($mi, + my $smsg = PublicInbox::Smsg::from_mitem($mi, $srch) or next; return $smsg; } @@ -221,8 +225,6 @@ sub results_cb { sub mbox_all { my ($ctx, $query) = @_; - eval { require PublicInbox::MboxGz }; - return need_gzip() if $@; return mbox_all_ids($ctx) if $query eq ''; my $qopts = $ctx->{qopts} = { mset => 2 }; my $srch = $ctx->{srch} = $ctx->{-inbox}->search or @@ -233,20 +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 { - my $title = 'gzipped mbox not available'; - my $body = <$title
$title
-The administrator needs to install the Compress::Raw::Zlib Perl module
-to support gzipped mboxes.
-Return to index
-EOF - - [501,[qw(Content-Type text/html Content-Length), bytes::length($body)], - [ $body ] ]; + require PublicInbox::MboxGz; + PublicInbox::MboxGz::mbox_gz($ctx, \&results_cb, 'results-'.$query); } 1;