Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Set REMOTE_PORT environment variable

  • Loading branch information...
commit 32584424bb64e7d83c0ece5faac25072ce145251 1 parent 6f4d272
@kazeburo kazeburo authored
Showing with 42 additions and 0 deletions.
  1. +1 −0  lib/Starlet/Server.pm
  2. +41 −0 t/06remote_port.t
View
1  lib/Starlet/Server.pm
@@ -106,6 +106,7 @@ sub accept_loop {
SERVER_NAME => $self->{host},
SCRIPT_NAME => '',
REMOTE_ADDR => $conn->peerhost,
+ REMOTE_PORT => $conn->peerport,
'psgi.version' => [ 1, 1 ],
'psgi.errors' => *STDERR,
'psgi.url_scheme' => 'http',
View
41 t/06remote_port.t
@@ -0,0 +1,41 @@
+use strict;
+use warnings;
+use Test::More;
+use Test::TCP;
+use LWP::UserAgent;
+use IO::Socket::INET;
+use Plack::Loader;
+
+test_tcp(
+ client => sub {
+ my $port = shift;
+ sleep 1;
+ my $sock = IO::Socket::INET->new(
+ PeerAddr => "localhost:$port",
+ Proto => 'tcp',
+ );
+ ok($sock);
+ my $localport = $sock->sockport;
+ my $req = "GET / HTTP/1.0\015\012\015\012";
+ $sock->syswrite($req,length($req));
+ $sock->sysread( my $buf, 1024);
+ like( $buf, qr/HELLO $localport/);
+ },
+ server => sub {
+ my $port = shift;
+ my $loader = Plack::Loader->load(
+ 'Starlet',
+ port => $port,
+ max_workers => 5,
+ );
+ $loader->run(sub{
+ my $env = shift;
+ my @headers = ();
+ my $remote_port = $env->{REMOTE_PORT};
+ [200, ['Content-Type'=>'text/html'], ['HELLO '.$remote_port]];
+ });
+ },
+);
+
+done_testing;
+
Please sign in to comment.
Something went wrong with that request. Please try again.