Skip to content
Browse files

Merge pull request #4 from kazeburo/master

do not send more than one Server header
  • Loading branch information...
2 parents 89b50d2 + e52d3f1 commit 83b1035a799c9d0a7af0a33e3434150d25717e6c @kazuho kazuho committed May 27, 2011
Showing with 49 additions and 5 deletions.
  1. +8 −5 lib/Starlet/Server.pm
  2. +41 −0 t/05server-header.t
View
13 lib/Starlet/Server.pm
@@ -221,11 +221,7 @@ sub _handle_response {
my $headers = $res->[1];
my $body = $res->[2];
- my @lines = (
- "Date: @{[HTTP::Date::time2str()]}\015\012",
- "Server: $self->{server_software}\015\012",
- );
-
+ my @lines;
my %send_headers;
for (my $i = 0; $i < @$headers; $i += 2) {
my $k = $headers->[$i];
@@ -239,6 +235,13 @@ sub _handle_response {
$send_headers{$lck} = $v;
}
}
+ if ( ! exists $send_headers{server} ) {
+ unshift @lines, "Server: $self->{server_software}\015\012";
+ }
+ if ( ! exists $send_headers{date} ) {
+ unshift @lines, "Date: @{[HTTP::Date::time2str()]}\015\012";
+ }
+
# try to set content-length when keepalive can be used, or disable it
if ($$use_keepalive_r) {
if (defined $send_headers{'content-length'}
View
41 t/05server-header.t
@@ -0,0 +1,41 @@
+use strict;
+use warnings;
+use Test::More;
+use Test::TCP;
+use LWP::UserAgent;
+use Plack::Loader;
+
+test_tcp(
+ client => sub {
+ my $port = shift;
+ sleep 1;
+ my $ua = LWP::UserAgent->new;
+ my $res = $ua->get("http://localhost:$port/");
+ ok( $res->is_success );
+ like( scalar $res->header('Server'), qr/Starlet/ );
+ unlike( scalar $res->header('Server'), qr/Hello/ );
+
+ $res = $ua->get("http://localhost:$port/?server=1");
+ ok( $res->is_success );
+ unlike( scalar $res->header('Server'), qr/Starlet/ );
+ like( scalar $res->header('Server'), qr/Hello/ );
+
+ },
+ server => sub {
+ my $port = shift;
+ my $loader = Plack::Loader->load(
+ 'Starlet',
+ port => $port,
+ max_workers => 5,
+ );
+ $loader->run(sub{
+ my $env = shift;
+ my @headers = ('Content-Type','text/html');
+ push @headers, 'Server', 'Hello' if $env->{QUERY_STRING};
+ [200, \@headers, ['HELLO']];
+ });
+ },
+);
+
+done_testing;
+

0 comments on commit 83b1035

Please sign in to comment.
Something went wrong with that request. Please try again.