]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Mbox.pm
mbox: show application/mbox for obfuscated inboxes
[public-inbox.git] / lib / PublicInbox / Mbox.pm
index 0258d8c72a5fb2b33e1cb85b3352432b9b2b1aed..88daba752153385ed74656cae0699078a65139c3 100644 (file)
@@ -6,16 +6,18 @@
 package PublicInbox::Mbox;
 use strict;
 use warnings;
-use PublicInbox::MID qw/mid_clean/;
-use URI::Escape qw/uri_escape_utf8/;
-use Plack::Util;
+use PublicInbox::MID qw/mid_clean mid_escape/;
 require Email::Simple;
 
 sub emit1 {
        my ($ctx, $msg) = @_;
        $msg = Email::Simple->new($msg);
-       # single message should be easily renderable in browsers
-       [200, ['Content-Type', 'text/plain'], [ msg_str($ctx, $msg)] ]
+
+       # single message should be easily renderable in browsers,
+       # unless obfuscation is enabled :<
+       [ 200, [ 'Content-Type',
+         $ctx->{-inbox}->{obfuscate} ? 'application/mbox' : 'text/plain' ],
+        [ msg_str($ctx, $msg)] ]
 }
 
 sub msg_str {
@@ -28,9 +30,9 @@ sub msg_str {
                $header_obj->header_set($d);
        }
        my $ibx = $ctx->{-inbox};
-       my $base = $ibx->base_url($ctx->{cgi});
+       my $base = $ibx->base_url($ctx->{env});
        my $mid = mid_clean($header_obj->header('Message-ID'));
-       $mid = uri_escape_utf8($mid);
+       $mid = mid_escape($mid);
        my @append = (
                'Archived-At', "<$base$mid/>",
                'List-Archive', "<$base>",
@@ -107,46 +109,41 @@ EOF
 package PublicInbox::MboxGz;
 use strict;
 use warnings;
-use PublicInbox::MID qw(mid2path);
 
 sub new {
        my ($class, $ctx, $cb) = @_;
-       my $buf;
+       my $buf = '';
        bless {
                buf => \$buf,
                gz => IO::Compress::Gzip->new(\$buf, Time => 0),
                cb => $cb,
                ctx => $ctx,
                msgs => [],
-               opts => { asc => 1, offset => 0 },
+               opts => { offset => 0 },
        }, $class;
 }
 
-sub _flush_buf {
-       my ($self) = @_;
-       my $ret = $self->{buf};
-       $ret = $$ret;
-       ${$self->{buf}} = undef;
-       $ret;
-}
-
 # called by Plack::Util::foreach or similar
 sub getline {
        my ($self) = @_;
+       my $ctx = $self->{ctx} or return;
        my $res;
-       my $ctx = $self->{ctx};
-       my $git = $ctx->{git};
+       my $ibx = $ctx->{-inbox};
        my $gz = $self->{gz};
        do {
                while (defined(my $smsg = shift @{$self->{msgs}})) {
-                       my $msg = eval {
-                               my $p = 'HEAD:'.mid2path($smsg->mid);
-                               Email::Simple->new($git->cat_file($p));
-                       };
-                       $msg or next;
+                       my $msg = eval { $ibx->msg_by_smsg($smsg) } or next;
+                       $msg = Email::Simple->new($msg);
                        $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg));
-                       my $ret = _flush_buf($self);
-                       return $ret if $ret;
+                       my $bref = $self->{buf};
+                       if (length($$bref) >= 8192) {
+                               my $ret = $$bref; # copy :<
+                               ${$self->{buf}} = '';
+                               return $ret;
+                       }
+
+                       # be fair to other clients on public-inbox-httpd:
+                       return '';
                }
                $res = $self->{cb}->($self->{opts});
                $self->{msgs} = $res->{msgs};
@@ -154,7 +151,8 @@ sub getline {
                $self->{opts}->{offset} += $res;
        } while ($res);
        $gz->close;
-       _flush_buf($self);
+       delete $self->{ctx};
+       ${delete $self->{buf}};
 }
 
 sub close {} # noop