about summary refs log tree commit homepage
path: root/lib/PublicInbox/Mbox.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2015-08-21 21:42:22 +0000
committerEric Wong <e@80x24.org>2015-08-21 21:42:53 +0000
commitea926383498a4883140086aec3f58d9e468433b9 (patch)
tree3c7808c391807350627da905d2da998765a5e8c8 /lib/PublicInbox/Mbox.pm
parentf71e9e9b67a6ff23642ccd119390bd6b3cb0d91e (diff)
downloadpublic-inbox-ea926383498a4883140086aec3f58d9e468433b9.tar.gz
Since mbox is usually downloaded, support fetching infinitely large
responses via streaming.
Diffstat (limited to 'lib/PublicInbox/Mbox.pm')
-rw-r--r--lib/PublicInbox/Mbox.pm46
1 files changed, 29 insertions, 17 deletions
diff --git a/lib/PublicInbox/Mbox.pm b/lib/PublicInbox/Mbox.pm
index 2ec50656..fc9df1ad 100644
--- a/lib/PublicInbox/Mbox.pm
+++ b/lib/PublicInbox/Mbox.pm
@@ -9,27 +9,14 @@ use Fcntl qw(SEEK_SET);
 
 sub thread_mbox {
         my ($ctx, $srch) = @_;
-        my $mid = mid_compressed($ctx->{mid});
-        my $res = $srch->get_thread($mid);
-        my $msgs = delete $res->{msgs};
-        require PublicInbox::GitCatFile;
-        require Email::Simple;
-        my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
-
         sub {
-                my ($res) = @_; # Plack callback
-                my $w = $res->([200, [ 'Content-Type' => 'text/plain' ] ]);
-                while (defined(my $smsg = shift @$msgs)) {
-                        my $msg = eval {
-                                my $path = 'HEAD:' . mid2path($smsg->mid);
-                                Email::Simple->new($git->cat_file($path));
-                        };
-                        emit($w, $msg) if $msg;
-                }
+                my ($response) = @_; # Plack callback
+                my $w = $response->([200, ['Content-Type' => 'text/plain']]);
+                emit_mbox($w, $ctx, $srch);
         }
 }
 
-sub emit {
+sub emit_msg {
         my ($fh, $simple) = @_; # Email::Simple object
 
         # drop potentially confusing headers, ssoma already should've dropped
@@ -52,4 +39,29 @@ sub emit {
         $fh->write($buf);
 }
 
+sub emit_mbox {
+        my ($fh, $ctx, $srch) = @_;
+
+        require PublicInbox::GitCatFile;
+        require Email::Simple;
+        my $mid = mid_compressed($ctx->{mid});
+        my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
+        my %opts = (offset => 0);
+        my $nr;
+        do {
+                my $res = $srch->get_thread($mid, \%opts);
+                my $msgs = $res->{msgs};
+                $nr = scalar @$msgs;
+                while (defined(my $smsg = shift @$msgs)) {
+                        my $msg = eval {
+                                my $p = 'HEAD:'.mid2path($smsg->mid);
+                                Email::Simple->new($git->cat_file($p));
+                        };
+                        emit_msg($fh, $msg) if $msg;
+                }
+
+                $opts{offset} += $nr;
+        } while ($nr > 0);
+}
+
 1;