]> Sergey Matveev's repositories - public-inbox.git/commitdiff
gzip_filter: delay async wcb call
authorEric Wong <e@80x24.org>
Mon, 25 Oct 2021 02:45:52 +0000 (02:45 +0000)
committerEric Wong <e@80x24.org>
Mon, 25 Oct 2021 08:16:59 +0000 (08:16 +0000)
This will let us modify the response header later to set
a proper charset for Content-Type when displaying raw
messages.

Cc: Thomas Weißschuh <thomas@t-8ch.de>
lib/PublicInbox/GzipFilter.pm
lib/PublicInbox/Mbox.pm
lib/PublicInbox/WwwAtomStream.pm
lib/PublicInbox/WwwStream.pm

index c621617107259ec05954b0eef5370c282f773166..c4858a971495449ce7e4fd35b3a0c123153ed07b 100644 (file)
@@ -54,7 +54,7 @@ sub psgi_response {
                $http->{forward} = $self;
                sub {
                        my ($wcb) = @_; # -httpd provided write callback
-                       $self->{http_out} = $wcb->([$code, $res_hdr]);
+                       $self->{wcb_args} = [ $code, $res_hdr, $wcb ];
                        $self->can('async_next')->($http); # start stepping
                };
        } else { # generic PSGI code path
@@ -114,9 +114,17 @@ sub translate ($$) {
        }
 }
 
+sub http_out ($) {
+       my ($self) = @_;
+       $self->{http_out} //= do {
+               my $args = delete $self->{wcb_args} // return undef;
+               pop(@$args)->($args); # $wcb->([$code, $hdr_ary])
+       };
+}
+
 sub write {
        # my $ret = bytes::length($_[1]); # XXX does anybody care?
-       $_[0]->{http_out}->write(translate($_[0], $_[1]));
+       http_out($_[0])->write(translate($_[0], $_[1]));
 }
 
 # similar to ->translate; use this when we're sure we know we have
@@ -145,10 +153,9 @@ sub zflush ($;$) {
 
 sub close {
        my ($self) = @_;
-       if (my $http_out = delete $self->{http_out}) {
-               $http_out->write(zflush($self));
-               $http_out->close;
-       }
+       my $http_out = http_out($self) // return;
+       $http_out->write(zflush($self));
+       delete($self->{http_out})->close;
 }
 
 sub bail  {
index dede4825ff1381f4076fee8cc1f01d4a5035f09d..4f84eea6745d5cbc08f835e64200ebe3ce44dd00 100644 (file)
@@ -47,7 +47,7 @@ sub async_eml { # for async_blob_cb
        $ctx->{smsg} = $ctx->{ibx}->over->next_by_mid(@{$ctx->{next_arg}});
 
        $ctx->zmore(msg_hdr($ctx, $eml));
-       $ctx->{http_out}->write($ctx->translate(msg_body($eml)));
+       $ctx->write(msg_body($eml));
 }
 
 sub res_hdr ($$) {
index 5d32294eec159f58cb9b2ff7153f512c5d0d3b63..82895db6373e85da077048cf2257332fb7e3b025 100644 (file)
@@ -28,7 +28,7 @@ sub async_next ($) {
                if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
                        $ctx->smsg_blob($smsg);
                } else {
-                       $ctx->{http_out}->write($ctx->translate('</feed>'));
+                       $ctx->write('</feed>');
                        $ctx->close;
                }
        };
@@ -38,7 +38,7 @@ sub async_next ($) {
 sub async_eml { # for async_blob_cb
        my ($ctx, $eml) = @_;
        my $smsg = delete $ctx->{smsg};
-       $ctx->{http_out}->write($ctx->translate(feed_entry($ctx, $smsg, $eml)))
+       $ctx->write(feed_entry($ctx, $smsg, $eml));
 }
 
 sub response {
index 5be5ed0cad59988572a2499f5262a3d1c8c50dc7..6d7c447fe6a2ecc7880cd6119e41c788dd966c96 100644 (file)
@@ -32,7 +32,7 @@ sub init {
 
 sub async_eml { # for async_blob_cb
        my ($ctx, $eml) = @_;
-       $ctx->{http_out}->write($ctx->translate($ctx->{cb}->($ctx, $eml)));
+       $ctx->write($ctx->{cb}->($ctx, $eml));
 }
 
 sub html_top ($) {
@@ -187,8 +187,7 @@ sub async_next ($) {
                if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
                        $ctx->smsg_blob($smsg);
                } else {
-                       $ctx->{http_out}->write(
-                                       $ctx->translate(_html_end($ctx)));
+                       $ctx->write(_html_end($ctx));
                        $ctx->close; # GzipFilter->close
                }
        };