diff --git a/lib/Plack/Middleware/Lint.pm b/lib/Plack/Middleware/Lint.pm index eb3deee52..aedf7840b 100644 --- a/lib/Plack/Middleware/Lint.pm +++ b/lib/Plack/Middleware/Lint.pm @@ -141,6 +141,11 @@ sub validate_res { unless (defined $val) { die("Response headers MUST be a defined string. Header: $key"); } + if ($key =~ /^Content-Length$/i) { + if (Plack::Util::status_with_no_entity_body($res->[0])) { + die("Response headers MUST NOT include Content-Length for Status $res->[0]"); + } + } } # @$res == 2 is only right in psgi.streaming, and it's already checked. @@ -156,6 +161,13 @@ sub validate_res { die("Body must be bytes and should not contain wide characters (UTF-8 strings)"); } + if (@$res == 3 + && Plack::Util::status_with_no_entity_body($res->[0]) + && Plack::Util::content_length($res->[2]) + ) { + die("Body must be empty for Status $res->[0]"); + } + return $res; } diff --git a/t/Plack-Middleware/lint.t b/t/Plack-Middleware/lint.t index cca1082aa..cf45c51d2 100644 --- a/t/Plack-Middleware/lint.t +++ b/t/Plack-Middleware/lint.t @@ -24,6 +24,9 @@ my @bad = map { Plack::Middleware::Lint->wrap($_) } ( sub { return sub { shift->([ 200, [], {} ]) } }, sub { return sub { shift->([ 200, [], undef ]) } }, sub { return [ 200, [ "X-Foo", undef ], [ "Hi" ] ] }, + sub { return [ 204, [ "Content-Length", 10 ], [ "No Content" ] ] }, + sub { return [ 204, [ "Content-Length", 0 ], [ "" ] ] }, + sub { return [ 204, [], [ "No Content" ] ] }, ); my @good = map { Plack::Middleware::Lint->wrap($_) } (