Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add vary option #5

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for {{$dist->name}}

{{$NEXT}}
- Add vary option (gh#5)

0.01 2024-05-10 11:53:04 -0600
- initial version
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ request header.
Compression level. Should be an integer from 1 to 22. If not provided, then the default will
be chosen by [Compress::Stream::Zstd](https://metacpan.org/pod/Compress::Stream::Zstd).

- vary

If set to true (the default), then the response will vary on `Content-Encoding`. This is usually
what you want, but if you have another middleware or application that is already vary'ing on that
header, you may want to set this to false.

# SEE ALSO

- [Plack::Middleware::Deflater](https://metacpan.org/pod/Plack::Middleware::Deflater)
Expand Down
2 changes: 2 additions & 0 deletions author.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pod_spelling_system:
# intentionally
stopwords:
- Zstandard
- ing
- vary'ing

pod_coverage:
skip: 0
Expand Down
16 changes: 12 additions & 4 deletions lib/Plack/Middleware/Zstandard.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package Plack::Middleware::Zstandard {

use parent qw( Plack::Middleware );
use Plack::Util ();
use Plack::Util::Accessor qw( level _constructor_args );
use Plack::Util::Accessor qw( level _constructor_args vary );
use Ref::Util qw( is_plain_arrayref );
use Compress::Stream::Zstd::Compressor ();

Expand All @@ -31,9 +31,11 @@ package Plack::Middleware::Zstandard {
return undef if Plack::Util::status_with_no_entity_body($res->[0]);
return undef if $h->exists('Cache-Control') && $h->get('Cache-Control') =~ /\bno-transform\b/;

my @vary = split /\s*,\s*/, ($h->get('Vary') || '');
push @vary, 'Accept-Encoding';
$h->set('Vary' => join(",", @vary));
if($self->vary // 1) {
my @vary = split /\s*,\s*/, ($h->get('Vary') || '');
push @vary, 'Accept-Encoding';
$h->set('Vary' => join(",", @vary));
}

# Do not clobber already existing encoding
return if $h->exists('Content-Encoding') && $h->get('Content-Encoding') ne 'identity';
Expand Down Expand Up @@ -88,6 +90,12 @@ request header.
Compression level. Should be an integer from 1 to 22. If not provided, then the default will
be chosen by L<Compress::Stream::Zstd>.

=item vary

If set to true (the default), then the response will vary on C<Content-Encoding>. This is usually
what you want, but if you have another middleware or application that is already vary'ing on that
header, you may want to set this to false.

=back

=head1 SEE ALSO
Expand Down
28 changes: 28 additions & 0 deletions t/plack_middleware_zstandard.t
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,35 @@ subtest 'level' => sub {
'expected args',
);
};
};

subtest 'vary' => sub {

subtest 'do not vary' => sub {

my $app = psgi_app_guard builder {
enable 'Zstandard', vary => 0;
sub { return [ 200, ['Content-Type' => 'text/plain'], ['Hello World']] };
};

req(
GET('/', 'Accept-Encoding' => 'zstd'),
res {
code 200;
content_type 'text/plain';
header 'Content-Length' => DNE();
header 'Content-Encoding' => 'zstd';
header 'Vary', DNE();
},
);

is(
decompress(),
'Hello World',
'content',
);

};
};

sub note_debug {
Expand Down
Loading