Skip to content

Commit 5a72946

Browse files
committed
Fix response content-type handling for 204
1 parent d464672 commit 5a72946

File tree

6 files changed

+54
-8
lines changed

6 files changed

+54
-8
lines changed

MANIFEST

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ MANIFEST.SKIP
2121
README
2222
README.md
2323
t/00-load.t
24-
t/01-basic.t
25-
t/02-sendfile.t
24+
t/01-basic-ae.t
25+
t/02-basic-ev.t
26+
t/03-sendfile.t
27+
t/basic.pl
2628
t/pod.t
2729
t/testlib.pm
2830
test.sh

META.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
}
4141
},
4242
"release_status" : "stable",
43-
"version" : "1.99995",
43+
"version" : "1.99996",
4444
"x_serialization_backend" : "JSON::PP version 4.06"
4545
}

META.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ requires:
2222
Digest::SHA1: '2'
2323
HTTP::Easy: '0.02'
2424
JSON::XS: '3'
25-
version: '1.99995'
25+
version: '1.99996'
2626
x_serialization_backend: 'CPAN::Meta::YAML version 0.018'

lib/AnyEvent/HTTP/Server.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ AnyEvent::HTTP::Server - AnyEvent HTTP/1.1 Server
88

99
our $VERSION;
1010
BEGIN{
11-
$VERSION = '1.99995';
11+
$VERSION = '1.99996';
1212
}
1313

1414
use AnyEvent::HTTP::Server::Kit;

lib/AnyEvent/HTTP/Server/Req.pm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ BEGIN {
252252
sub sendfile {
253253
my $self = shift;
254254
my ( $code,$file,%args ) = @_;
255-
$code ||=200;
255+
$code ||= 200;
256256
my $reply = "HTTP/1.0 $code $http{$code}$LF";
257257
my $size = -s $file or $! and return warn "Can't sendfile `$file': $!";
258258
open my $f, '<:raw',$file or return warn "Can't open file `$file': $!";
@@ -303,7 +303,8 @@ BEGIN {
303303
my $self = shift;
304304
#return $self->headers(@_) if @_ % 2;
305305
my ($code,$content,%args) = @_;
306-
$code ||=200;
306+
$code ||= 200;
307+
$content = '' unless defined $content;
307308
utf8::encode $content if utf8::is_utf8 $content;
308309
my $reply = "HTTP/$self->{version} $code $http{$code}$LF";
309310
my @good;my @bad;
@@ -333,6 +334,13 @@ BEGIN {
333334
}
334335
$content = '';
335336
}
337+
338+
# https://www.rfc-editor.org/rfc/rfc7230#section-3.3.2
339+
if ($code == 204 or $code == 304 or $code < 200) {
340+
delete $h->{'content-length'};
341+
$content = '';
342+
}
343+
336344
if (exists $h->{'content-type'}) {
337345
if( $h->{'content-type'} !~ m{[^;]+;\s*charset\s*=}
338346
and $h->{'content-type'} =~ m{(?:^(?:text/|application/(?:json|(?:x-)?javascript))|\+(?:json|xml)\b)}i) {

t/basic.pl

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env perl
22

3-
use Test::More tests => 194;
3+
use Test::More tests => 224;
44
use Data::Dumper;
55
use FindBin;
66
use lib "$FindBin::Bin/..";
@@ -255,6 +255,42 @@
255255
[["GET /test1 HTTP/1.1\nHost:localhost\nConnection:close\n\n"], 500, {}, "Request not handled\nGET /test1\n" ],
256256
if ALL;
257257

258+
test_server {
259+
return 204, undef, headers => {};
260+
} '204 - no cl - undef',
261+
[["GET /test1 HTTP/1.1\nHost:localhost\nConnection:keep-alive\n\n"], 204, { 'content-length' => undef }, "" ],
262+
if ALL;
263+
264+
test_server {
265+
return 204, "", headers => {};
266+
} '204 - no cl - empty',
267+
[["GET /test1 HTTP/1.1\nHost:localhost\nConnection:keep-alive\n\n"], 204, { 'content-length' => undef }, "" ],
268+
if ALL;
269+
270+
test_server {
271+
my $s = shift;
272+
my $r = shift;
273+
return 204, "some content", headers => {};
274+
} '204 - with content',
275+
[["GET /test1 HTTP/1.1\nHost:localhost\nConnection:keep-alive\n\n"], 204, { 'content-length' => undef }, "" ],
276+
if ALL;
277+
278+
test_server {
279+
my $s = shift;
280+
my $r = shift;
281+
return 204, "", headers => {'content-length' => 0};
282+
} '204 - with header',
283+
[["GET /test1 HTTP/1.1\nHost:localhost\nConnection:keep-alive\n\n"], 204, { 'content-length' => undef }, "" ],
284+
if ALL;
285+
286+
test_server {
287+
my $s = shift;
288+
my $r = shift;
289+
return 200, undef;
290+
} '200 - with undef body',
291+
[["GET /test1 HTTP/1.1\nHost:localhost\nConnection:keep-alive\n\n"], 200, { 'content-length' => 0 }, "" ],
292+
if ALL;
293+
258294
}
259295

260296
done_testing();

0 commit comments

Comments
 (0)