diff --git a/lib/Plack/Middleware/Zstandard.pm b/lib/Plack/Middleware/Zstandard.pm index c1a81bb..795e977 100644 --- a/lib/Plack/Middleware/Zstandard.pm +++ b/lib/Plack/Middleware/Zstandard.pm @@ -29,6 +29,9 @@ package Plack::Middleware::Zstandard { 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'; + return undef unless ($env->{HTTP_ACCEPT_ENCODING} // '') =~ /\bzstd\b/; $h->set('Content-Encoding' => 'zstd'); diff --git a/t/plack_middleware_zstandard.t b/t/plack_middleware_zstandard.t index 78f4622..21ed551 100644 --- a/t/plack_middleware_zstandard.t +++ b/t/plack_middleware_zstandard.t @@ -107,6 +107,32 @@ subtest 'basic' => sub { }; + subtest 'do not clobber' => sub { + + my $content = 'Hello World'; + local @res = ( + 200, [ + 'Content-Type' => 'text/plain', + 'Content-ENcoding' => 'foo', + ], [ + $content + ] + ); + + req( + GET('/', 'Accept-Encoding' => 'zstd'), + res { + code 200; + header 'Content-Encoding' => 'foo'; + header 'Vary', 'Accept-Encoding'; + call content => 'Hello World'; + }, + ); + + note_debug(); + + }; + subtest 'No accept' => sub { my $content = 'Hello World';