X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FWwwStream.pm;h=79ed6871e6f3e9224dfc422b3896d3d718881b14;hb=b8b03f9c896432816019828b27708fa3b6903d83;hp=0f4f55d0f7246a68ee737ecb7b445cc7d8ddee31;hpb=fece7fca6aeac74410a813cffcb0da338017d0ed;p=public-inbox.git diff --git a/lib/PublicInbox/WwwStream.pm b/lib/PublicInbox/WwwStream.pm index 0f4f55d0..79ed6871 100644 --- a/lib/PublicInbox/WwwStream.pm +++ b/lib/PublicInbox/WwwStream.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2016-2019 all contributors +# Copyright (C) 2016-2020 all contributors # License: AGPL-3.0+ # # HTML body stream for which yields getline+close methods @@ -9,23 +9,33 @@ package PublicInbox::WwwStream; use strict; use warnings; -use PublicInbox::Hval qw(ascii_html); +use base qw(Exporter); +our @EXPORT_OK = qw(html_oneshot); +use bytes (); # length +use PublicInbox::Hval qw(ascii_html prurl); +use Compress::Raw::Zlib qw(Z_FINISH Z_OK); +use PublicInbox::GzipFilter qw(gzip_maybe); our $TOR_URL = 'https://www.torproject.org/'; our $CODE_URL = 'https://public-inbox.org/public-inbox.git'; # noop for HTTP.pm (and any other PSGI servers) sub close {} +sub base_url ($) { + my $ctx = shift; + my $base_url = $ctx->{-inbox}->base_url($ctx->{env}); + chop $base_url; # no trailing slash for clone + $base_url; +} + sub new { my ($class, $ctx, $cb) = @_; - my $base_url = $ctx->{-inbox}->base_url($ctx->{env}); - chop $base_url; # no trailing slash for clone bless { nr => 0, - cb => $cb || *close, + cb => $cb, ctx => $ctx, - base_url => $base_url, + base_url => base_url($ctx), }, $class; } @@ -40,25 +50,20 @@ sub _html_top ($) { my $ctx = $self->{ctx}; my $ibx = $ctx->{-inbox}; my $desc = ascii_html($ibx->description); - my $title = $ctx->{-title_html} || $desc; + my $title = delete($ctx->{-title_html}) // $desc; my $upfx = $ctx->{-upfx} || ''; my $help = $upfx.'_/text/help'; my $color = $upfx.'_/text/color'; my $atom = $ctx->{-atom} || $upfx.'new.atom'; - my $tip = $ctx->{-html_tip} || ''; my $top = "$desc"; my $links = "help / ". "color / ". "Atom feed"; if ($ibx->search) { - my $q_val = $ctx->{-q_value_html}; - if (defined $q_val && $q_val ne '') { - $q_val = qq(\nvalue="$q_val"); - } else { - $q_val = ''; - } + my $q_val = delete($ctx->{-q_value_html}) // ''; + $q_val = qq(\nvalue="$q_val") if $q_val ne ''; # XXX gross, for SearchView.pm - my $extra = $ctx->{-extra_form_html} || ''; + my $extra = delete($ctx->{-extra_form_html}) // ''; my $action = $upfx eq '' ? './' : $upfx; $top = qq{
$top} .
 			  qq{\n} .
@@ -70,15 +75,15 @@ sub _html_top ($) {
 		$top = '
' . $top . "\n" . $links . '
'; } "$title" . - "" . + qq() . $ctx->{www}->style($upfx) . - "". $top . $tip; + ''. $top . (delete($ctx->{-html_tip}) // ''); } sub code_footer ($) { my ($env) = @_; - my $u = PublicInbox::Hval::prurl($env, $CODE_URL); + my $u = prurl($env, $CODE_URL); qq(AGPL code for this site: git clone $u) } @@ -89,12 +94,12 @@ sub _html_end { my $ibx = $ctx->{-inbox}; my $desc = ascii_html($ibx->description); - my (%seen, @urls); + my @urls; my $http = $self->{base_url}; my $max = $ibx->max_git_epoch; my $dir = (split(m!/!, $http))[-1]; + my %seen = ($http => 1); if (defined($max)) { # v2 - $seen{$http} = 1; for my $i (0..$max) { # old parts my be deleted: -d "$ibx->{inboxdir}/git/$i.git" or next; @@ -103,15 +108,13 @@ sub _html_end { push @urls, "$url $dir/git/$i.git"; } } else { # v1 - $seen{$http} = 1; push @urls, $http; } # FIXME: epoch splits can be different in other repositories, # use the "cloneurl" file as-is for now: foreach my $u (@{$ibx->cloneurl}) { - next if $seen{$u}; - $seen{$u} = 1; + next if $seen{$u}++; push @urls, $u =~ /\Ahttps?:/ ? qq($u) : $u; } @@ -171,4 +174,34 @@ sub getline { delete $self->{cb} ? _html_end($self) : undef; } +sub html_oneshot ($$;$) { + my ($ctx, $code, $sref) = @_; + my $self = bless { + ctx => $ctx, + base_url => base_url($ctx), + }, __PACKAGE__; + my @x; + my @h = ('Content-Type' => 'text/html; charset=UTF-8'); + if (my $gz = gzip_maybe($ctx->{env})) { + my $err = $gz->deflate(_html_top($self), $x[0]); + die "gzip->deflate: $err" if $err != Z_OK; + if ($sref) { + $err = $gz->deflate($sref, $x[0]); + die "gzip->deflate: $err" if $err != Z_OK; + } + $err = $gz->deflate(_html_end($self), $x[0]); + die "gzip->deflate: $err" if $err != Z_OK; + $err = $gz->flush($x[0], Z_FINISH); + die "gzip->flush: $err" if $err != Z_OK; + push @h, qw(Vary Accept-Encoding Content-Encoding gzip); + } else { + @x = (_html_top($self), $sref ? $$sref : (), _html_end($self)); + } + + my $len = 0; + $len += bytes::length($_) for @x; + push @h, 'Content-Length', $len; + [ $code, \@h, \@x ] +} + 1;