]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WwwStream.pm
viewvcs: use shorter and simpler ctx->html_done
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
index 0416db0b474200dc9f534d4797276159d0e15a80..c23668a440bbcfe12c4eee3d7ffb52533c21ae1a 100644 (file)
@@ -27,6 +27,9 @@ sub init {
        my ($ctx, $cb) = @_;
        $ctx->{cb} = $cb;
        $ctx->{base_url} = base_url($ctx);
+       $ctx->{-res_hdr} = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
+       $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($ctx->{-res_hdr},
+                                                       $ctx->{env});
        bless $ctx, __PACKAGE__;
 }
 
@@ -164,8 +167,16 @@ sub getline {
        $ctx->zflush(_html_end($ctx));
 }
 
-sub html_oneshot ($$;$) {
-       my ($ctx, $code, $sref) = @_;
+sub html_done ($;@) {
+       my $ctx = $_[0];
+       my $bdy = $ctx->zflush(@_[1..$#_], _html_end($ctx));
+       my $res_hdr = delete $ctx->{-res_hdr};
+       push @$res_hdr, 'Content-Length', length($bdy);
+       [ 200, $res_hdr, [ $bdy ] ]
+}
+
+sub html_oneshot ($$;@) {
+       my ($ctx, $code) = @_[0, 1];
        my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
                'Content-Length' => undef ];
        bless $ctx, __PACKAGE__;
@@ -174,8 +185,7 @@ sub html_oneshot ($$;$) {
                $ctx->zmore(html_top($ctx));
                $ctx->{base_url} = base_url($ctx);
        };
-       $ctx->zmore($$sref) if $sref;
-       my $bdy = $ctx->zflush(_html_end($ctx));
+       my $bdy = $ctx->zflush(@_[2..$#_], _html_end($ctx));
        $res_hdr->[3] = length($bdy);
        [ $code, $res_hdr, [ $bdy ] ]
 }
@@ -195,10 +205,18 @@ sub async_next ($) {
 }
 
 sub aresponse {
-       my ($ctx, $code, $cb) = @_;
-       my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
+       my ($ctx, $cb) = @_;
        init($ctx, $cb);
-       $ctx->psgi_response($code, $res_hdr);
+       $ctx->psgi_response(200, delete $ctx->{-res_hdr});
+}
+
+sub html_init {
+       my ($ctx) = @_;
+       $ctx->{base_url} = base_url($ctx);
+       my $h = $ctx->{-res_hdr} = ['Content-Type', 'text/html; charset=UTF-8'];
+       $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($h, $ctx->{env});
+       bless $ctx, __PACKAGE__;
+       $ctx->zmore(html_top($ctx));
 }
 
 1;