Skip to content

Commit

Permalink
Fix response content-type handling for 204
Browse files Browse the repository at this point in the history
  • Loading branch information
Mons committed Oct 11, 2022
1 parent d464672 commit 5a72946
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
6 changes: 4 additions & 2 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ MANIFEST.SKIP
README
README.md
t/00-load.t
t/01-basic.t
t/02-sendfile.t
t/01-basic-ae.t
t/02-basic-ev.t
t/03-sendfile.t
t/basic.pl
t/pod.t
t/testlib.pm
test.sh
2 changes: 1 addition & 1 deletion META.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@
}
},
"release_status" : "stable",
"version" : "1.99995",
"version" : "1.99996",
"x_serialization_backend" : "JSON::PP version 4.06"
}
2 changes: 1 addition & 1 deletion META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ requires:
Digest::SHA1: '2'
HTTP::Easy: '0.02'
JSON::XS: '3'
version: '1.99995'
version: '1.99996'
x_serialization_backend: 'CPAN::Meta::YAML version 0.018'
2 changes: 1 addition & 1 deletion lib/AnyEvent/HTTP/Server.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ AnyEvent::HTTP::Server - AnyEvent HTTP/1.1 Server

our $VERSION;
BEGIN{
$VERSION = '1.99995';
$VERSION = '1.99996';
}

use AnyEvent::HTTP::Server::Kit;
Expand Down
12 changes: 10 additions & 2 deletions lib/AnyEvent/HTTP/Server/Req.pm
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ BEGIN {
sub sendfile {
my $self = shift;
my ( $code,$file,%args ) = @_;
$code ||=200;
$code ||= 200;
my $reply = "HTTP/1.0 $code $http{$code}$LF";
my $size = -s $file or $! and return warn "Can't sendfile `$file': $!";
open my $f, '<:raw',$file or return warn "Can't open file `$file': $!";
Expand Down Expand Up @@ -303,7 +303,8 @@ BEGIN {
my $self = shift;
#return $self->headers(@_) if @_ % 2;
my ($code,$content,%args) = @_;
$code ||=200;
$code ||= 200;
$content = '' unless defined $content;
utf8::encode $content if utf8::is_utf8 $content;
my $reply = "HTTP/$self->{version} $code $http{$code}$LF";
my @good;my @bad;
Expand Down Expand Up @@ -333,6 +334,13 @@ BEGIN {
}
$content = '';
}

# https://www.rfc-editor.org/rfc/rfc7230#section-3.3.2
if ($code == 204 or $code == 304 or $code < 200) {
delete $h->{'content-length'};
$content = '';
}

if (exists $h->{'content-type'}) {
if( $h->{'content-type'} !~ m{[^;]+;\s*charset\s*=}
and $h->{'content-type'} =~ m{(?:^(?:text/|application/(?:json|(?:x-)?javascript))|\+(?:json|xml)\b)}i) {
Expand Down
38 changes: 37 additions & 1 deletion t/basic.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env perl

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

test_server {
return 204, undef, headers => {};
} '204 - no cl - undef',
[["GET /test1 HTTP/1.1\nHost:localhost\nConnection:keep-alive\n\n"], 204, { 'content-length' => undef }, "" ],
if ALL;

test_server {
return 204, "", headers => {};
} '204 - no cl - empty',
[["GET /test1 HTTP/1.1\nHost:localhost\nConnection:keep-alive\n\n"], 204, { 'content-length' => undef }, "" ],
if ALL;

test_server {
my $s = shift;
my $r = shift;
return 204, "some content", headers => {};
} '204 - with content',
[["GET /test1 HTTP/1.1\nHost:localhost\nConnection:keep-alive\n\n"], 204, { 'content-length' => undef }, "" ],
if ALL;

test_server {
my $s = shift;
my $r = shift;
return 204, "", headers => {'content-length' => 0};
} '204 - with header',
[["GET /test1 HTTP/1.1\nHost:localhost\nConnection:keep-alive\n\n"], 204, { 'content-length' => undef }, "" ],
if ALL;

test_server {
my $s = shift;
my $r = shift;
return 200, undef;
} '200 - with undef body',
[["GET /test1 HTTP/1.1\nHost:localhost\nConnection:keep-alive\n\n"], 200, { 'content-length' => 0 }, "" ],
if ALL;

}

done_testing();

0 comments on commit 5a72946

Please sign in to comment.