Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

hmm... added print_banner attribute, but this is sucky interface

  • Loading branch information...
commit 9051489d554bbbc7d8b2025815c37b2cd7e63131 1 parent 0ea5297
@tokuhirom tokuhirom authored
View
3  lib/Plack/Impl/AnyEvent.pm
@@ -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;
}
@@ -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;
View
8 lib/Plack/Impl/Coro.pm
@@ -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});
}
@@ -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;
View
11 lib/Plack/Impl/ServerSimple.pm
@@ -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) = @_;
View
3  lib/Plack/Impl/Standalone.pm
@@ -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;
}
@@ -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) {
View
46 t/100_complex/print_banner.t
@@ -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();
+
Please sign in to comment.
Something went wrong with that request. Please try again.