Skip to content

Commit

Permalink
hmm... added print_banner attribute, but this is sucky interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tokuhirom committed Sep 18, 2009
1 parent 0ea5297 commit 9051489
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/Plack/Impl/AnyEvent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sub new {
my $self = bless {}, $class;
$self->{host} = delete $args{host} || undef;
$self->{port} = delete $args{port} || undef;
$self->{print_banner} = delete $args{print_banner} || sub { warn "Accepting requests at http://$_[0]:$_[1]/\n" };

$self;
}
Expand Down Expand Up @@ -99,7 +100,7 @@ sub run {
my ( $fh, $host, $port ) = @_;
$self->{prepared_host} = $host;
$self->{prepared_port} = $port;
warn "Accepting requests at http://$host:$port/\n";
$self->{print_banner}->($host, $port);
return 0;
};
$self->{listen_guard} = $guard;
Expand Down
8 changes: 8 additions & 0 deletions lib/Plack/Impl/Coro.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sub run {

my $server = Plack::Impl::Coro::Server->new(host => $self->{host} || '*');
$server->{app} = $app;
$server->{print_banner} = $self->{print_banner};
$server->run(port => $self->{port});
}

Expand All @@ -26,6 +27,13 @@ use List::Util qw(sum max);
use Plack::HTTPParser qw( parse_http_request );
use constant MAX_REQUEST_SIZE => 131072;

sub pre_loop_hook {
my $self = shift;
if (my $cb = $self->{print_banner}) {
$cb->($self->{server}->{host}->[0], $self->{server}->{port}->[0]);
}
}

sub process_request {
my $self = shift;

Expand Down
11 changes: 10 additions & 1 deletion lib/Plack/Impl/ServerSimple.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@ sub new {

my $port = delete $args{port};
my $host = delete $args{host};
my $print_banner = delete $args{print_banner};

my $self = $class->SUPER::new($port);
$self->host($host) if defined $host;
$self->{print_banner} = $print_banner if defined $print_banner;

$self;
}

#sub print_banner { }
sub print_banner {
my ($self, ) = @_;
if (defined $self->{print_banner}) {
$self->{print_banner}->($self->host, $self->port);
} else {
$self->SUPER::print_banner();
}
}

sub run {
my($self, $app) = @_;
Expand Down
3 changes: 2 additions & 1 deletion lib/Plack/Impl/Standalone.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sub new {
timeout => $args{timeout} || 300,
max_keepalive_reqs => $args{max_keepalive_reqs} || 100,
keepalive_timeout => $args{keepalive_timeout} || 5,
print_banner => $args{print_banner} || sub { warn "Accepting requests at http://$_[0]:$_[1]/\n" },
}, $class;
}

Expand All @@ -41,7 +42,7 @@ sub run {
ReuseAddr => 1,
) or die "failed to listen to port $self->{port}:$!";

warn "Accepting connections at http://$self->{host}:$self->{port}/\n";
$self->{print_banner}->($self->{host}, $self->{port});
while (1) {
local $SIG{PIPE} = 'IGNORE';
if (my $conn = $listen_sock->accept) {
Expand Down
46 changes: 46 additions & 0 deletions t/100_complex/print_banner.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use strict;
use warnings;
use Test::More;
use Test::Requires qw(HTTP::Server::Simple Net::Server::Coro);
use Test::TCP;

use Plack;
use Plack::Loader;
use CGI::Emulate::PSGI;
use CGI;
use LWP::UserAgent;

my @data = (
'AnyEvent' => "OKAY http://127.0.0.1:10001/\n",
'Coro' => "OKAY http://127.0.0.1:10001/\n",
'Standalone' => "OKAY http://127.0.0.1:10001/\n",
'ServerSimple' => "OKAY http://127.0.0.1:10001/\n",
);

while (my ($impl_class, $msg) = splice(@data, 0, 2)) {
diag $impl_class;
test_tcp(
client => sub {
my $port = shift;
my $ua = LWP::UserAgent->new;
my $res = $ua->get("http://127.0.0.1:$port/");
is $res->code, 200;
is $res->content, $msg;
},
server => sub {
my $port = shift;
open local(*STDOUT), '>', \my $out or die $!;
my $impl = Plack::Loader->load($impl_class, port => $port, host => "127.0.0.1", print_banner => sub { print "OKAY http://$_[0]:$_[1]/\n" });
$impl->run(
sub {
my $env = shift;
[200, [], [$out]]
}
);
$impl->run_loop if $impl->can('run_loop');
},
);
}

done_testing();

0 comments on commit 9051489

Please sign in to comment.