]> Sergey Matveev's repositories - public-inbox.git/commitdiff
stream HTML views as much as possible
authorEric Wong <e@80x24.org>
Sat, 22 Aug 2015 00:06:45 +0000 (00:06 +0000)
committerEric Wong <e@80x24.org>
Sat, 22 Aug 2015 01:57:35 +0000 (01:57 +0000)
This should allow progressive rendering on the client and reduce
memory usage on the server.  Unfortunately XML::Atom::SimpleFeed
does not yet support streaming, so we may not use it in the
future.

lib/PublicInbox/Feed.pm
lib/PublicInbox/Mbox.pm
lib/PublicInbox/View.pm
lib/PublicInbox/WWW.pm
t/html_index.t

index 40f08ac7237e3a2d5970f927e65000ea49285cdc..5d122ac6ffa67bd058f323bec5d4d925b6a88e5b 100644 (file)
@@ -52,7 +52,15 @@ sub generate {
 }
 
 sub generate_html_index {
-       my ($class, $ctx) = @_;
+       my ($ctx) = @_;
+       sub { emit_html_index($_[0], $ctx) };
+}
+
+# private subs
+
+sub emit_html_index {
+       my ($cb, $ctx) = @_;
+       my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
 
        my $max = $ctx->{max} || MAX_PER_PAGE;
        my $feed_opts = get_feedopts($ctx);
@@ -61,11 +69,11 @@ sub generate_html_index {
        $title = PublicInbox::Hval->new_oneline($title)->as_html;
        my $atom_url = $feed_opts->{atomurl};
 
-       my $html = "<html><head><title>$title</title>" .
-               "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
-               "href=\"$atom_url\"\ntype=\"application/atom+xml\"/>" .
-               '</head><body>' . PublicInbox::View::PRE_WRAP .
-               "<b>$title</b> (<a\nhref=\"$atom_url\">Atom feed</a>)\n";
+       $fh->write("<html><head><title>$title</title>" .
+                  "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
+                  "href=\"$atom_url\"\ntype=\"application/atom+xml\"/>" .
+                  '</head><body>' . PublicInbox::View::PRE_WRAP .
+                  "<b>$title</b> (<a\nhref=\"$atom_url\">Atom feed</a>)\n");
 
        my $state;
        my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
@@ -80,8 +88,7 @@ sub generate_html_index {
                        add_topic($git, $srch, $topics, $path, $ts, $u, $subj);
                } else {
                        my $mime = do_cat_mail($git, $path) or return 0;
-                       $html .=
-                            PublicInbox::View->index_entry($mime, 0, $state);
+                       PublicInbox::View::index_entry($fh, $mime, 0, $state);
                        1;
                }
        });
@@ -94,12 +101,11 @@ sub generate_html_index {
                $footer .= "\n" . $list_footer if $list_footer;
                $footer = "<hr /><pre>$footer</pre>";
        }
-       dump_topics(\$html, $topics) if $topics;
-       $html .= "$footer</body></html>";
+       $fh->write(dump_topics($topics)) if $topics;
+       $fh->write("$footer</body></html>");
+       $fh->close;
 }
 
-# private subs
-
 sub nav_footer {
        my ($cgi, $last, $feed_opts, $state) = @_;
        $cgi or return '';
@@ -328,27 +334,28 @@ sub add_topic {
 }
 
 sub dump_topics {
-       my ($dst, $topics) = @_;
+       my ($topics) = @_;
        my ($order, $subjs) = @$topics;
-       $$dst .= "\n[No recent topics]" unless (scalar @$order);
+       my $dst = '';
+       $dst .= "\n[No recent topics]" unless (scalar @$order);
        while (defined(my $info = shift @$order)) {
                my ($mid, $ts, $u, $subj) = @$info;
                my $n = delete $subjs->{$subj};
                $mid = PublicInbox::Hval->new($mid)->as_href;
                $subj = PublicInbox::Hval->new($subj)->as_html;
                $u = PublicInbox::Hval->new($u)->as_html;
-               $$dst .= "\n<a\nhref=\"t/$mid.html#u\"><b>$subj</b></a>\n- ";
+               $dst .= "\n<a\nhref=\"t/$mid.html#u\"><b>$subj</b></a>\n- ";
                $ts = POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
                if ($n == 1) {
-                       $$dst .= "created by $u @ $ts UTC\n"
+                       $dst .= "created by $u @ $ts UTC\n"
                } else {
                        # $n isn't the total number of posts on the topic,
                        # just the number of posts in the current "git log"
                        # window, so leave it unlabeled
-                       $$dst .= "updated by $u @ $ts UTC ($n)\n"
+                       $dst .= "updated by $u @ $ts UTC ($n)\n"
                }
        }
-       $$dst .= '</pre>'
+       $dst .= '</pre>'
 }
 
 1;
index fcb266939e1707e39d3e82b70751cd3e600793a2..5f5612a4fc3c81a74bb7649a792c9d5420f6b2f7 100644 (file)
@@ -80,6 +80,7 @@ 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;
@@ -118,7 +119,7 @@ sub close {
        my ($self) = @_;
        $self->{gz}->close;
        _flush_buf($self);
-       # do not actually close $fh
+       $self->{fh}->close;
 }
 
 1;
index 8105affecffa7fa8412b8c0f8013ce18e14de0d2..ab2720baf024af3b8186d0084364aa5d76265a8a 100644 (file)
@@ -48,7 +48,7 @@ sub feed_entry {
 # this is already inside a <pre>
 # state = [ time, seen = {}, first_commit, page_nr = 0 ]
 sub index_entry {
-       my (undef, $mime, $level, $state) = @_;
+       my ($fh, $mime, $level, $state) = @_;
        my ($srch, $seen, $first_commit) = @$state;
        my $midx = $state->[3]++;
        my ($prev, $next) = ($midx - 1, $midx + 1);
@@ -107,7 +107,7 @@ sub index_entry {
        if ($prev >= 0) {
                $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
        }
-       $rv .= "\n\n";
+       $fh->write($rv .= "\n\n");
 
        my ($fhref, $more_ref);
        my $mhref = "${path}m/$href.html";
@@ -117,13 +117,12 @@ sub index_entry {
        }
        # scan through all parts, looking for displayable text
        $mime->walk_parts(sub {
-               $rv .= index_walk($_[0], $enc, \$part_nr, $fhref, $more_ref);
+               index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
        });
        $mime->body_set('');
 
-       $rv .= "\n<a\nhref=\"$mhref\">$more</a> ";
        my $txt = "${path}m/$href.txt";
-       $rv .= "<a\nhref=\"$txt\">raw</a> ";
+       $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
        $rv .= html_footer($mime, 0);
 
        if (defined $irt) {
@@ -141,23 +140,30 @@ sub index_entry {
                       "threadlink</a>";
        }
 
-       $rv .= '</pre></td></tr></table>';
+       $fh->write($rv .= '</pre></td></tr></table>');
 }
 
 sub thread_html {
-       my (undef, $ctx, $foot, $srch) = @_;
+       my ($ctx, $foot, $srch) = @_;
+       sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
+}
+
+# only private functions below.
+
+sub emit_thread_html {
+       my ($cb, $ctx, $foot, $srch) = @_;
        my $mid = mid_compressed($ctx->{mid});
        my $res = $srch->get_thread($mid);
-       my $rv = '';
        my $msgs = load_results($res);
        my $nr = scalar @$msgs;
-       return $rv if $nr == 0;
+       return missing_thread($cb) if $nr == 0;
+       my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
        my $th = thread_results($msgs);
        my $state = [ $srch, { root_anchor => anchor_for($mid) }, undef, 0 ];
        {
                require PublicInbox::GitCatFile;
                my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
-               thread_entry(\$rv, $git, $state, $_, 0) for $th->rootset;
+               thread_entry($fh, $git, $state, $_, 0) for $th->rootset;
        }
        my $final_anchor = $state->[3];
        my $next = "<a\nid=\"s$final_anchor\">";
@@ -169,13 +175,13 @@ sub thread_html {
        }
        $next .= "</a>, back to <a\nhref=\"../\">index</a>\n";
 
-       $rv .= "<hr />" . PRE_WRAP . $next . $foot . "</pre>";
+       $fh->write("<hr />" . PRE_WRAP . $next . $foot .
+                  "</pre></body></html>");
+       $fh->close;
 }
 
-# only private functions below.
-
 sub index_walk {
-       my ($part, $enc, $part_nr, $fhref, $more) = @_;
+       my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
        my $s = add_text_body($enc, $part, $part_nr, $fhref);
 
        if ($more) {
@@ -196,7 +202,7 @@ sub index_walk {
                $s =~ s/[ \t]+$//sgm;
                $s .= "\n" unless $s =~ /\n\z/s;
        }
-       $s;
+       $fh->write($s);
 }
 
 sub enc_for {
@@ -533,7 +539,7 @@ sub thread_html_head {
 }
 
 sub thread_entry {
-       my ($dst, $git, $state, $node, $level) = @_;
+       my ($fh, $git, $state, $node, $level) = @_;
        return unless $node;
        # $state = [ $search_res, $seen, undef, 0 (msg_nr) ];
        # $seen is overloaded with 3 types of fields:
@@ -546,14 +552,14 @@ sub thread_entry {
                my $path = mid2path(mid_clean($mime->header('Message-ID')));
                $mime = eval { Email::MIME->new($git->cat_file("HEAD:$path")) };
                if ($mime) {
-                       if (length($$dst) == 0) {
-                               $$dst .= thread_html_head($mime);
+                       if ($state->[3] == 0) {
+                               $fh->write(thread_html_head($mime));
                        }
-                       $$dst .= index_entry(undef, $mime, $level, $state);
+                       index_entry($fh, $mime, $level, $state);
                }
        }
-       thread_entry($dst, $git, $state, $node->child, $level + 1);
-       thread_entry($dst, $git, $state, $node->next, $level);
+       thread_entry($fh, $git, $state, $node->child, $level + 1);
+       thread_entry($fh, $git, $state, $node->next, $level);
 }
 
 sub load_results {
@@ -577,4 +583,13 @@ sub thread_results {
        $th
 }
 
+sub missing_thread {
+       my ($cb) = @_;
+       my $title = 'Thread does not exist';
+       $cb->([404, ['Content-Type' => 'text/html']])->write(<<EOF);
+<html><head><title>$title</title></head><body><pre>$title
+<a href="../">Return to index</a></pre></body></html>
+EOF
+}
+
 1;
index 68839d7cee3ef3af33717f48b5d3aacda1c20c94..2de54719163e483ccf12c0127892270c544dc274 100644 (file)
@@ -128,8 +128,7 @@ sub get_index {
        $ctx->{pi_config} = $pi_config;
        $ctx->{cgi} = $cgi;
        footer($ctx);
-       [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
-         [ PublicInbox::Feed->generate_html_index($ctx) ] ]
+       PublicInbox::Feed::generate_html_index($ctx);
 }
 
 # just returns a string ref for the blob in the current ctx
@@ -195,10 +194,7 @@ sub get_thread {
        my $srch = searcher($ctx) or return need_search($ctx);
        require PublicInbox::View;
        my $foot = footer($ctx);
-       my $body = PublicInbox::View->thread_html($ctx, $foot, $srch) or
-               return r404();
-       [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
-         [ $body ] ];
+       PublicInbox::View::thread_html($ctx, $foot, $srch);
 }
 
 sub self_url {
index 690368f16aca39fd887ab9ae9837125e19f9ffe8..6286fc479e874e135a303bdd9d684140e52faaf2 100644 (file)
@@ -50,11 +50,23 @@ EOF
 
 # check HTML index
 {
-       my $feed = PublicInbox::Feed->generate_html_index({
+       use IO::File;
+       my $cb = PublicInbox::Feed::generate_html_index({
                git_dir => $git_dir,
                max => 3
        });
+       my $headers;
+       my $io = IO::File->new_tmpfile;
+       use POSIX qw/dup/;
+       my $dup = dup($io->fileno);
+       my $response = sub { $headers = \@_, $io };
+       $cb->($response);
+       $io = IO::File->new;
+       $io->fdopen($dup, 'r+');
+       $io->seek(0, 0);
+       $io->read(my $feed, 666666);
        like($feed, qr/html/, "feed is valid HTML :)");
+       $io->close;
 }
 
 done_testing();