From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.0 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 256B42018A for ; Sat, 18 Jun 2016 04:47:08 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 2/3] view: introduce WwwStream interface Date: Sat, 18 Jun 2016 04:47:04 +0000 Message-Id: <20160618044705.8337-3-e@80x24.org> In-Reply-To: <20160618044705.8337-1-e@80x24.org> References: <20160618044705.8337-1-e@80x24.org> List-Id: This will allow us to commonalize HTML generation in the future and is the start of moving existing HTML generation to a "pull" streaming model (from the existing "push" one). Using the getline/close pull model is superior to the existing $fh->write streaming as it allows us to throttle response generation based on backpressure from slow clients. --- lib/PublicInbox/View.pm | 51 +++++++++---------------- lib/PublicInbox/WwwStream.pm | 88 ++++++++++++++++++++++++++++++++++++++++++++ t/view.t | 14 ++++++- 3 files changed, 119 insertions(+), 34 deletions(-) create mode 100644 lib/PublicInbox/WwwStream.pm diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm index 534f85e..e7e387d 100644 --- a/lib/PublicInbox/View.pm +++ b/lib/PublicInbox/View.pm @@ -15,6 +15,7 @@ use PublicInbox::Linkify; use PublicInbox::MID qw/mid_clean id_compress mid2path mid_mime/; use PublicInbox::MsgIter; use PublicInbox::Address; +use PublicInbox::WwwStream; require POSIX; use constant INDENT => ' '; @@ -22,31 +23,22 @@ use constant TCHILD => '` '; sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD }; # public functions: (unstable) -# TODO: stream this, since threading is expensive but also oh-so-important sub msg_html { my ($ctx, $mime, $footer) = @_; - $footer = defined($footer) ? "\n$footer" : ''; my $hdr = $mime->header_obj; - my $n = 0; - Plack::Util::inline_object( - close => sub {}, # noop - getline => sub { - my $nr = $n++; - if ($nr == 0) { - headers_to_html_header($hdr, $ctx) . - multipart_text_as_html($mime, '') . - '
' - } elsif ($nr == 1) { - '
' .
-					html_footer($hdr, 1, $ctx) .
-					'
' . msg_reply($ctx, $hdr) . - '
'.  $footer .
-					'
' - } else { - undef - } + my $tip = _msg_html_prepare($hdr, $ctx); + PublicInbox::WwwStream->new($ctx, sub { + my ($nr, undef) = @_; + if ($nr == 1) { + $tip . multipart_text_as_html($mime, '') . + '
' + } elsif ($nr == 2) { + '
' . html_footer($hdr, 1, $ctx) .
+			'
' . msg_reply($ctx, $hdr) . '
' + } else { + undef } - ) + }); } # /$INBOX/$MESSAGE_ID/#R @@ -318,18 +310,15 @@ sub add_text_body { $s; } -sub headers_to_html_header { +sub _msg_html_prepare { my ($hdr, $ctx) = @_; my $srch = $ctx->{srch} if $ctx; my $atom = ''; - my $rv = ''; - my $upfx = ''; + my $rv = ""; # anchor for body start if ($srch) { - $atom = qq{!; + $ctx->{-upfx} = '../'; } - my @title; my $mid = $hdr->header_raw('Message-ID'); $mid = PublicInbox::Hval->new_msgid($mid); @@ -352,15 +341,11 @@ sub headers_to_html_header { $rv .= "$h: " . $v->as_html . "\n"; } + $ctx->{-title_html} = join(' - ', @title); $rv .= 'Message-ID: <' . $mid->as_html . '> '; - $rv .= "(raw)\n"; + $rv .= "(raw)\n"; $rv .= _parent_headers($hdr, $srch); $rv .= "\n"; - - ("". join(' - ', @title) . "$atom". - PublicInbox::Hval::STYLE . - "" . # anchor for body start - $rv); } sub thread_skel { diff --git a/lib/PublicInbox/WwwStream.pm b/lib/PublicInbox/WwwStream.pm new file mode 100644 index 0000000..62a4fe2 --- /dev/null +++ b/lib/PublicInbox/WwwStream.pm @@ -0,0 +1,88 @@ +# Copyright (C) 2016 all contributors +# License: AGPL-3.0+ +# +# HTML body stream for which yields getline+close methods +package PublicInbox::WwwStream; +use strict; +use warnings; +use PublicInbox::Hval qw(ascii_html); +use URI; +use constant PI_URL => 'https://public-inbox.org/README.html'; + +sub new { + my ($class, $ctx, $cb) = @_; + bless { nr => 0, cb => $cb, ctx => $ctx }, $class; +} + +sub _html_top ($) { + my ($self) = @_; + my $ctx = $self->{ctx}; + my $obj = $ctx->{-inbox}; + my $desc = ascii_html($obj->description); + my $title = $ctx->{-title_html} || $desc; + my $upfx = $ctx->{-upfx} || ''; + my $atom = $ctx->{-atom} || $upfx.'new.atom'; + my $top = "$desc (Atom feed)"; + if ($obj->search) { + $top = qq{
$top} .
+			  qq{ } .
+			  qq{} .
+			  q{
} + } else { + $top = '
' . $top . '
'; + } + "$title" . + "" . + PublicInbox::Hval::STYLE . + "$top"; +} + +sub _html_end { + my ($self) = @_; + my $urls = 'Archives are clone-able:'; + my $ctx = $self->{ctx}; + my $obj = $ctx->{-inbox}; + my $desc = ascii_html($obj->description); + my @urls = @{$obj->cloneurl}; + my %seen = map { $_ => 1 } @urls; + + # FIXME: cleanup + my $env = $ctx->{env}; + my $scheme = $env->{'psgi.url_scheme'}; + my $host_port = $env->{HTTP_HOST} || + "$env->{SERVER_NAME}:$env->{SERVER_PORT}"; + my $http = "$scheme://$host_port".($env->{SCRIPT_NAME} || '/'); + $http = URI->new($http . $obj->{name})->canonical->as_string; + $seen{$http} or unshift @urls, $http; + if (scalar(@urls) == 1) { + $urls .= " git clone --mirror $urls[0]"; + } else { + $urls .= "\n" . + join("\n", map { "\tgit clone --mirror $_" } @urls); + } + my $url = PublicInbox::Hval::prurl($ctx->{env}, PI_URL); + '
'.join("\n",
+		'- ' . $desc,
+		$urls,
+		'served with software from public-inbox: '
+			."$url",
+	).'
'; +} + +sub getline { + my ($self) = @_; + my $nr = $self->{nr}++; + + return _html_top($self) if $nr == 0; + + if (my $mid = $self->{cb}) { # middle + $mid = $mid->($nr, $self->{ctx}) and return $mid; + } + + delete $self->{cb} ? _html_end($self) : undef; +} + +sub close {} + +1; diff --git a/t/view.t b/t/view.t index 6c08599..4ce3c77 100644 --- a/t/view.t +++ b/t/view.t @@ -5,12 +5,24 @@ use warnings; use Test::More; use Email::MIME; use PublicInbox::View; +use Plack::Util; + +# FIXME: make this test less fragile +my $ctx = { + env => { HTTP_HOST => 'example.com', 'psgi.url_scheme' => 'http' }, + -inbox => Plack::Util::inline_object( + name => 'test', + search => sub { undef }, + cloneurl => sub {[]}, + description => sub { '' }), +}; +$ctx->{-inbox}->{-primary_address} = 'test@example.com'; sub msg_html ($) { my ($mime) = @_; my $s = ''; - my $body = PublicInbox::View::msg_html(undef, $mime); + my $body = PublicInbox::View::msg_html($ctx, $mime); while (defined(my $buf = $body->getline)) { $s .= $buf; }