]> Sergey Matveev's repositories - public-inbox.git/commitdiff
feed: remove dependence on fh->write for streaming
authorEric Wong <e@80x24.org>
Mon, 20 Jun 2016 00:57:13 +0000 (00:57 +0000)
committerEric Wong <e@80x24.org>
Mon, 20 Jun 2016 00:57:35 +0000 (00:57 +0000)
We'll be switching to a getline/close response body
to give the HTTP server more control when dealing
with slow clients.

lib/PublicInbox/Feed.pm
lib/PublicInbox/SearchView.pm

index d88421b0d98ef11e4052770e85c592e3e5aede02..07dce9eee83bf2458eb933548c02f638ca247f53 100644 (file)
@@ -72,7 +72,9 @@ sub emit_atom {
                        $fh->write($x . feed_updated(undef, $ts));
                        $x = undef;
                }
-               add_to_feed($feed_opts, $fh, $path, $git);
+               my $s = feed_entry($feed_opts, $path, $git) or return 0;
+               $fh->write($s);
+               1;
        });
        end_feed($fh);
 }
@@ -103,7 +105,8 @@ sub emit_atom_thread {
 
        my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
        foreach my $msg (@{$res->{msgs}}) {
-               add_to_feed($feed_opts, $fh, mid2path($msg->mid), $git);
+               my $s = feed_entry($feed_opts, mid2path($msg->mid), $git);
+               $fh->write($s) if defined $s;
        }
        end_feed($fh);
 }
@@ -306,52 +309,51 @@ sub feed_updated {
        '<updated>' . strftime(DATEFMT, @t) . '</updated>';
 }
 
-# returns 0 (skipped) or 1 (added)
-sub add_to_feed {
-       my ($feed_opts, $fh, $add, $git) = @_;
+# returns undef or string
+sub feed_entry {
+       my ($feed_opts, $add, $git) = @_;
 
-       my $mime = do_cat_mail($git, $add) or return 0;
+       my $mime = do_cat_mail($git, $add) or return;
        my $url = $feed_opts->{url};
        my $midurl = $feed_opts->{midurl};
 
        my $header_obj = $mime->header_obj;
        my $mid = $header_obj->header_raw('Message-ID');
-       defined $mid or return 0;
+       defined $mid or return;
        $mid = PublicInbox::Hval->new_msgid($mid);
        my $href = $midurl.$mid->as_href;
 
-       my $content = qq(<pre\nstyle="white-space:pre-wrap">) .
-               PublicInbox::View::multipart_text_as_html($mime, $href) .
-               '</pre>';
        my $date = $header_obj->header('Date');
        my $updated = feed_updated($date);
 
        my $title = $header_obj->header('Subject');
-       defined $title or return 0;
+       defined $title or return;
        $title = title_tag($title);
 
-       my $from = $header_obj->header('From') or return 0;
+       my $from = $header_obj->header('From') or return;
        my ($email) = PublicInbox::Address::emails($from);
        my $name = PublicInbox::Address::from_name($from);
        $name = ascii_html($name);
        $email = ascii_html($email);
 
+       my $s = '';
        if (delete $feed_opts->{emit_header}) {
-               $fh->write(atom_header($feed_opts, $title) . $updated);
+               $s .= atom_header($feed_opts, $title) . $updated;
        }
-       $fh->write("<entry><author><name>$name</name><email>$email</email>" .
-                  "</author>$title$updated" .
-                  qq{<content\ntype="xhtml">} .
-                  qq{<div\nxmlns="http://www.w3.org/1999/xhtml">});
-       $fh->write($content);
+       $s .= "<entry><author><name>$name</name><email>$email</email>" .
+               "</author>$title$updated" .
+               qq{<content\ntype="xhtml">} .
+               qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
+               qq(<pre\nstyle="white-space:pre-wrap">) .
+               PublicInbox::View::multipart_text_as_html($mime, $href) .
+               '</pre>';
 
        $add =~ tr!/!!d;
        my $h = '[a-f0-9]';
        my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
        my $id = 'urn:uuid:' . join('-', @uuid5);
-       $fh->write(qq!</div></content><link\nhref="$href/"/>!.
-                  "<id>$id</id></entry>");
-       1;
+       $s .= qq!</div></content><link\nhref="$href/"/>!.
+               "<id>$id</id></entry>";
 }
 
 sub do_cat_mail {
index 2ec7ddf843340568519922a9241fe02f115a3beb..d4c44bab277d3578e2228551086d08c2a213db6c 100644 (file)
@@ -249,7 +249,8 @@ sub adump {
        for ($mset->items) {
                $x = PublicInbox::SearchMsg->load_doc($_->get_document)->mid;
                $x = mid2path($x);
-               PublicInbox::Feed::add_to_feed($feed_opts, $fh, $x, $git);
+               my $s = PublicInbox::Feed::feed_entry($feed_opts, $x, $git);
+               $fh->write($s) if defined $s;
        }
        PublicInbox::Feed::end_feed($fh);
 }