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

Lint checks for Content-Length or Body on Status with no Body #592

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions lib/Plack/Middleware/Lint.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions t/Plack-Middleware/lint.t
Original file line number Diff line number Diff line change
Expand Up @@ -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($_) } (
Expand Down