sub gzf_maybe ($$) { bless { gz => gz_or_noop(@_) }, __PACKAGE__ }
sub psgi_response {
+ # $code may be an HTTP response code (e.g. 200) or a CODE ref (mbox_hdr)
my ($self, $code, $res_hdr) = @_;
- my $env = $self->{env};
- $self->{gz} //= gz_or_noop($res_hdr, $env);
- if ($env->{'pi-httpd.async'}) {
- my $http = $env->{'psgix.io'}; # PublicInbox::HTTP
+ if ($self->{env}->{'pi-httpd.async'}) {
+ my $http = $self->{env}->{'psgix.io'}; # PublicInbox::HTTP
$http->{forward} = $self;
sub {
my ($wcb) = @_; # -httpd provided write callback
$self->can('async_next')->($http); # start stepping
};
} else { # generic PSGI code path
+ ref($code) eq 'CODE' and
+ ($code, $res_hdr) = @{$code->($self)};
+ $self->{gz} //= gz_or_noop($res_hdr, $self->{env});
[ $code, $res_hdr, $self ];
}
}
sub http_out ($) {
my ($self) = @_;
- $self->{http_out} //= do {
+ $self->{http_out} // do {
my $args = delete $self->{wcb_args} // return undef;
- pop(@$args)->($args); # $wcb->([$code, $hdr_ary])
+ my $wcb = pop @$args; # from PublicInbox:HTTP async
+ # $args->[0] may be \&mbox_hdr or similar
+ $args = $args->[0]->($self) if ref($args->[0]) eq 'CODE';
+ $self->{gz} //= gz_or_noop($args->[1], $self->{env});
+ $self->{http_out} = $wcb->($args); # $wcb->([$code, $hdr_ary])
};
}
# more data to buffer after this
sub zmore {
my $self = $_[0]; # $_[1] => input
+ http_out($self);
my $err = $self->{gz}->deflate($_[1], $self->{zbuf});
die "gzip->deflate: $err" if $err != Z_OK;
undef;
my $zbuf = delete $self->{zbuf};
my $gz = delete $self->{gz};
my $err;
- if (defined $_[1]) {
+ if (defined $_[1]) { # it's a bug iff $gz is undef w/ $_[1]
$err = $gz->deflate($_[1], $zbuf);
die "gzip->deflate: $err" if $err != Z_OK;
}
+ $gz // return; # not a bug, recursing on DS->write failure
$err = $gz->flush($zbuf);
die "gzip->flush: $err" if $err != Z_OK;
$zbuf;