]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Mbox.pm
.txt links return an mbox instead
[public-inbox.git] / lib / PublicInbox / Mbox.pm
index fc9df1adcbc8b6f1c5ffb1393e61552faea6d714..bc4d639ebad0809f3591cc38ecea7bb736039cb0 100644 (file)
@@ -4,15 +4,24 @@
 package PublicInbox::Mbox;
 use strict;
 use warnings;
-use PublicInbox::MID qw/mid_clean mid_compressed mid2path/;
-use Fcntl qw(SEEK_SET);
+use PublicInbox::MID qw/mid_compressed mid2path/;
+require Email::Simple;
 
 sub thread_mbox {
-       my ($ctx, $srch) = @_;
+       my ($ctx, $srch, $sfx) = @_;
        sub {
                my ($response) = @_; # Plack callback
-               my $w = $response->([200, ['Content-Type' => 'text/plain']]);
-               emit_mbox($w, $ctx, $srch);
+               emit_mbox($response, $ctx, $srch, $sfx);
+       }
+}
+
+sub emit1 {
+       my $simple = Email::Simple->new(pop);
+       sub {
+               my ($response) = @_;
+               # single message should be easily renderable in browsers
+               my $fh = $response->([200, ['Content-Type'=>'text/plain']]);
+               emit_msg($fh, $simple);
        }
 }
 
@@ -40,10 +49,20 @@ sub emit_msg {
 }
 
 sub emit_mbox {
-       my ($fh, $ctx, $srch) = @_;
+       my ($response, $ctx, $srch, $sfx) = @_;
+       my $type = 'mbox';
+       if ($sfx) {
+               eval { require IO::Compress::Gzip };
+               return need_gzip($response) if $@;
+               $type = 'gzip';
+       }
+
+       # http://www.iana.org/assignments/media-types/application/gzip
+       # http://www.iana.org/assignments/media-types/application/mbox
+       my $fh = $response->([200, ['Content-Type' => "application/$type"]]);
+       $fh = PublicInbox::MboxGz->new($fh) if $sfx;
 
        require PublicInbox::GitCatFile;
-       require Email::Simple;
        my $mid = mid_compressed($ctx->{mid});
        my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
        my %opts = (offset => 0);
@@ -62,6 +81,59 @@ sub emit_mbox {
 
                $opts{offset} += $nr;
        } while ($nr > 0);
+
+       $fh->close;
+}
+
+sub need_gzip {
+       my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
+       my $title = 'gzipped mbox not available';
+       $fh->write(<<EOF);
+<html><head><title>$title</title><body><pre>$title
+The administrator needs to install the IO::Compress::Gzip Perl module
+to support gzipped mboxes.
+<a href="../">Return to index</a></pre></body></html>
+EOF
+       $fh->close;
+}
+
+1;
+
+# fh may not be a proper IO, so we wrap the write and close methods
+# to prevent IO::Compress::Gzip from complaining
+package PublicInbox::MboxGz;
+use strict;
+use warnings;
+use fields qw(gz fh buf);
+
+sub new {
+       my ($class, $fh) = @_;
+       my $self = fields::new($class);
+       my $buf;
+       $self->{buf} = \$buf;
+       $self->{gz} = IO::Compress::Gzip->new(\$buf);
+       $self->{fh} = $fh;
+       $self;
+}
+
+sub _flush_buf {
+       my ($self) = @_;
+       if (defined ${$self->{buf}}) {
+               $self->{fh}->write(${$self->{buf}});
+               ${$self->{buf}} = undef;
+       }
+}
+
+sub write {
+       $_[0]->{gz}->write($_[1]);
+       _flush_buf($_[0]);
+}
+
+sub close {
+       my ($self) = @_;
+       $self->{gz}->close;
+       _flush_buf($self);
+       $self->{fh}->close;
 }
 
 1;