1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# current
package M::Gzip;
use strict;
use base qw/Middleware/;

sub gen_middleware {
    my $app = shift;
    sub {
        my $env = shift;
        my $res = $app->($env);
        ...
        unshift @{$res->[1]}, 'Content-Encoding' => 'gzip';
        return [$res->[0], $res->[1], M::Gzip::Streamer->new($res->[2])];
    }
}

1;
# wsgi like
package M::Gzip;
use strict;
use base qw/Middleware/;

sub gen_middleware {
    my $app = shift;
    sub {
        my ($env, $orig_starter) = @_;
        my $res = $app->($env, sub {
            my ($status, $header) = @_;
            unshift @{$header}, 'Content-Encoding' => 'gzip';
            my $writer = $orig_starter->($status, $header);
            return M::GZip::Writer->new(orig_writer => $orig_starter);
        });
        ...
        return [SAME_THING_LIKE_CURRENT];
    }
}

package M::Gzip::Writer;

has orig_writer => ( is => 'ro', isa => 'Object' );

sub write {
    my ($self, $stuff) = @_;
    $self->orig_writer->write(gzip($stuff));
}

1;