Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #4 from kazeburo/master

do not send more than one Server header
  • Loading branch information...
commit 83b1035a799c9d0a7af0a33e3434150d25717e6c 2 parents 89b50d2 + e52d3f1
Kazuho Oku authored May 27, 2011
13  lib/Starlet/Server.pm
@@ -221,11 +221,7 @@ sub _handle_response {
221 221
     my $headers = $res->[1];
222 222
     my $body = $res->[2];
223 223
     
224  
-    my @lines = (
225  
-        "Date: @{[HTTP::Date::time2str()]}\015\012",
226  
-        "Server: $self->{server_software}\015\012",
227  
-    );
228  
-    
  224
+    my @lines;
229 225
     my %send_headers;
230 226
     for (my $i = 0; $i < @$headers; $i += 2) {
231 227
         my $k = $headers->[$i];
@@ -239,6 +235,13 @@ sub _handle_response {
239 235
             $send_headers{$lck} = $v;
240 236
         }
241 237
     }
  238
+    if ( ! exists $send_headers{server} ) {
  239
+        unshift @lines, "Server: $self->{server_software}\015\012";
  240
+    }
  241
+    if ( ! exists $send_headers{date} ) {
  242
+        unshift @lines, "Date: @{[HTTP::Date::time2str()]}\015\012";
  243
+    }
  244
+
242 245
     # try to set content-length when keepalive can be used, or disable it
243 246
     if ($$use_keepalive_r) {
244 247
         if (defined $send_headers{'content-length'}
41  t/05server-header.t
... ...
@@ -0,0 +1,41 @@
  1
+use strict;
  2
+use warnings;
  3
+use Test::More;
  4
+use Test::TCP;
  5
+use LWP::UserAgent;
  6
+use Plack::Loader;
  7
+
  8
+test_tcp(
  9
+    client => sub {
  10
+        my $port = shift;
  11
+        sleep 1;
  12
+        my $ua = LWP::UserAgent->new;
  13
+        my $res = $ua->get("http://localhost:$port/");
  14
+        ok( $res->is_success );
  15
+        like( scalar $res->header('Server'), qr/Starlet/ );
  16
+        unlike( scalar $res->header('Server'), qr/Hello/ );
  17
+
  18
+        $res = $ua->get("http://localhost:$port/?server=1");
  19
+        ok( $res->is_success );
  20
+        unlike( scalar $res->header('Server'), qr/Starlet/ );
  21
+        like( scalar $res->header('Server'), qr/Hello/ );
  22
+
  23
+    },
  24
+    server => sub {
  25
+        my $port = shift;
  26
+        my $loader = Plack::Loader->load(
  27
+            'Starlet',
  28
+            port => $port,
  29
+            max_workers => 5,
  30
+        );
  31
+        $loader->run(sub{
  32
+            my $env = shift;
  33
+            my @headers = ('Content-Type','text/html');
  34
+            push @headers, 'Server', 'Hello' if $env->{QUERY_STRING};
  35
+            [200, \@headers, ['HELLO']];
  36
+        });
  37
+    },
  38
+);
  39
+
  40
+done_testing;
  41
+

0 notes on commit 83b1035

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