X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FWwwStream.pm;h=42fb183f482152d211dd9fe32a038fc2740097c3;hb=b26588399620d9d201f838a1dedb4c3e838dc479;hp=915a71ba0bd82df950ee498f03c9a2c078a78a79;hpb=2fc67a18b7ccd75ea6eb945f18203cbf4bcf228f;p=public-inbox.git diff --git a/lib/PublicInbox/WwwStream.pm b/lib/PublicInbox/WwwStream.pm index 915a71ba..42fb183f 100644 --- a/lib/PublicInbox/WwwStream.pm +++ b/lib/PublicInbox/WwwStream.pm @@ -8,8 +8,7 @@ # more common "push" model) package PublicInbox::WwwStream; use strict; -use warnings; -use base qw(Exporter); +use parent qw(Exporter PublicInbox::GzipFilter); our @EXPORT_OK = qw(html_oneshot); use bytes (); # length use PublicInbox::Hval qw(ascii_html prurl); @@ -26,26 +25,23 @@ sub base_url ($) { $base_url; } -sub new { - my ($class, $ctx, $cb) = @_; - - bless { - nr => 0, - cb => $cb, - ctx => $ctx, - base_url => base_url($ctx), - }, $class; +sub init { + my ($ctx, $cb) = @_; + $ctx->{cb} = $cb; + $ctx->{base_url} = base_url($ctx); + bless $ctx, __PACKAGE__; } sub response { - my ($class, $ctx, $code, $cb) = @_; - [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ], - $class->new($ctx, $cb) ] + my ($ctx, $code, $cb) = @_; + my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8' ]; + init($ctx, $cb); + $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env}); + [ $code, $res_hdr, $ctx ] } -sub _html_top ($) { - my ($self) = @_; - my $ctx = $self->{ctx}; +sub html_top ($) { + my ($ctx) = @_; my $ibx = $ctx->{-inbox}; my $desc = ascii_html($ibx->description); my $title = delete($ctx->{-title_html}) // $desc; @@ -86,14 +82,13 @@ sub code_footer ($) { } sub _html_end { - my ($self) = @_; + my ($ctx) = @_; my $urls = 'Archives are clonable:'; - my $ctx = $self->{ctx}; my $ibx = $ctx->{-inbox}; my $desc = ascii_html($ibx->description); my @urls; - my $http = $self->{base_url}; + my $http = $ctx->{base_url}; my $max = $ibx->max_git_epoch; my $dir = (split(m!/!, $http))[-1]; my %seen = ($http => 1); @@ -160,31 +155,28 @@ EOF # callback for HTTP.pm (and any other PSGI servers) sub getline { - my ($self) = @_; - my $nr = $self->{nr}++; - - return _html_top($self) if $nr == 0; - - if (my $middle = $self->{cb}) { - $middle = $middle->($nr, $self->{ctx}) and return $middle; + my ($ctx) = @_; + my $cb = $ctx->{cb} or return; + if (defined(my $buf = $cb->($ctx))) { + return $ctx->translate($buf); } - - delete $self->{cb} ? _html_end($self) : undef; + delete $ctx->{cb}; + $ctx->zflush(_html_end($ctx)); } sub html_oneshot ($$;$) { my ($ctx, $code, $sref) = @_; - my $self = bless { - ctx => $ctx, - base_url => base_url($ctx), - }, __PACKAGE__; - my @x = (_html_top($self), $sref ? $$sref : (), _html_end($self)); - my $len = 0; - $len += bytes::length($_) for @x; - [ $code, [ - 'Content-Type' => 'text/html; charset=UTF-8', - 'Content-Length' => $len - ], \@x ]; + $ctx->{base_url} = base_url($ctx); + bless $ctx, __PACKAGE__; + my @bdy; + my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8', + 'Content-Length' => undef ]; + $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env}); + $ctx->zmore(html_top($ctx)); + $ctx->zmore($$sref) if $sref; + $bdy[0] = $ctx->zflush(_html_end($ctx)); + $res_hdr->[3] = bytes::length($bdy[0]); + [ $code, $res_hdr, \@bdy ] } 1;