Skip to content

Commit

Permalink
depend on HTTP::Server::Simple::PSGI server on its control Content-Le…
Browse files Browse the repository at this point in the history
…ngth header.

Well, not reallly control.

Exsting PSGI servers behave differently when it comes to controlling
the value of Content-Length header, most implementation calculates the
value should the message body is determined, and none of them passthru
the values given by the inner application.

It also seems to be the case that all existing PSGI server will change
the Content-Length header to "0", if the request method is "HEAD" and
the inner-app sets a non-zero value to Content-Length.

Qouting from this old RFC, it looks like the value could be non-zero:

    https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13

    The Content-Length entity-header field indicates the size of the
    entity-body, in decimal number of OCTETs, sent to the recipient
    or, in the case of the HEAD method, the size of the entity-body
    that would have been sent had the request been a GET.

However, if it is never the case de facto, there is probably no point
to arg with a phanthom spec.

Until next time, when we meet a HTTP server in the wild...
  • Loading branch information
gugod committed Jul 10, 2018
1 parent 7f361ac commit 4e512f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ on test => sub {
requires "Test::More";
requires "Test::Exception";
requires "Plack";
requires "HTTP::Server::Simple::PSGI";
requires "Net::Ping", '2.41';
};
22 changes: 14 additions & 8 deletions t/plack-head-request.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ die "Fail to fork then start a plack server" unless defined $pid;
if ($pid == 0) {
require Plack::Runner;
my $runner = Plack::Runner->new;
$runner->parse_options("--port", $port, "$FindBin::Bin/bin/head-request.psgi");
$runner->parse_options("--server", "HTTP::Server::Simple", "--port", $port, "$FindBin::Bin/bin/head-request.psgi");
$runner->run;
exit;
}

sleep 10; # hopfully this is enough to launch that psgi.
sleep 2; # hopfully this is enough to launch that psgi.

my %args = (
timeout => 1,
Expand All @@ -32,17 +32,23 @@ my %args = (
subtest "expect HEAD response with a Content-Length" => sub {
my $res = Hijk::request({%args, query_string => "gimme_content_length=1"});
ok !exists $res->{error}, '$res->{error} should not exist because this request should have been successful';
cmp_ok $res->{head}->{"Content-Length"}, "==", 11, "Got a Content-Length";
cmp_ok $res->{body}, "eq", "", "Got no body even though we had a Content-Length";
ok defined($res->{head}->{"Content-Length"}), "Got a Content-Length";

cmp_ok $res->{body}, "eq", "", "Got no body even though we had a Content-Length header";

if ($res->{head}->{"Content-Length"} == 0) {
pass 'Content-Length: 0, looks OK because this response has no http body';
} elsif ($res->{head}->{"Content-Length"} == 11) {
pass 'Content-Length: 11, looks OK because it is the length of body should this be a GET request';
} else {
fail "Content-Length: " . $res->{head}->{'Content-Length'} . ' does not look like a legit value.';
}
};

subtest "expect HEAD response without a Content-Length" => sub {
my $res = Hijk::request({%args, query_string => "gimme_content_length="});
ok !exists $res->{error}, '$res->{error} should not exist because this request should have been successful';
TODO: {
local $TODO = "I can't figure out how to get plackup(1) not to implicitly add Content-Length";
ok !exists $res->{head}->{"Content-Length"}, "We should get no Content-Length";
}
ok !exists $res->{head}->{"Content-Length"}, "We should get no Content-Length";
cmp_ok $res->{body}, "eq", "", "Got no body wit the HEAD response, also have no Content-Length";
};

Expand Down

0 comments on commit 4e512f7

Please sign in to comment.