X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine%2FHTTP%2FDaemon.pm;h=92faf420b7669637389d21e189e19bf974b8c8c9;hb=b4ca0ee8572ea5c33295686b7f786ab5ff43a2b7;hp=1e2a211749bb8fc2933b9197e1851eec91fb4882;hpb=523d44ecdd184a0a56c8094c60fe545098e06cff;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine/HTTP/Daemon.pm b/lib/Catalyst/Engine/HTTP/Daemon.pm index 1e2a211..92faf42 100644 --- a/lib/Catalyst/Engine/HTTP/Daemon.pm +++ b/lib/Catalyst/Engine/HTTP/Daemon.pm @@ -1,9 +1,9 @@ package Catalyst::Engine::HTTP::Daemon; use strict; -use base 'Catalyst::Engine::Test'; +use base 'Catalyst::Engine::HTTP::Base'; -use IO::Socket qw(AF_INET); +use IO::Socket qw( SOCK_STREAM SOMAXCONN ); =head1 NAME @@ -15,9 +15,7 @@ A script using the Catalyst::Engine::HTTP::Daemon module might look like: #!/usr/bin/perl -w - BEGIN { - $ENV{CATALYST_ENGINE} = 'HTTP::Daemon'; - } + BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP::Daemon' } use strict; use lib '/path/to/MyApp/lib'; @@ -31,53 +29,69 @@ This is the Catalyst engine specialized for development and testing. =head1 OVERLOADED METHODS -This class overloads some methods from C. +This class overloads some methods from C. =over 4 -=item $c->run +=item $c->handler =cut -$SIG{'PIPE'} = 'IGNORE'; +sub handler { + my ( $class, $client ) = @_; + + $client->timeout(5); + + while ( my $request = $client->get_request ) { + + $request->uri->scheme('http'); # Force URI::http + $request->uri->host( $request->header('Host') || $client->sockhost ); + $request->uri->port( $client->sockport ); + + my $http = Catalyst::Engine::HTTP::Base::struct->new( + address => $client->peerhost, + request => $request, + response => HTTP::Response->new + ); + + $class->SUPER::handler($http); + + $client->send_response( $http->response ); + } + + $client->close; +} + +=item $c->run + +=cut sub run { my $class = shift; my $port = shift || 3000; + + $SIG{'PIPE'} = 'IGNORE'; + + $HTTP::Daemon::PROTO = 'HTTP/1.0'; # For now until we resolve the blocking + # issues with HTTP 1.1 my $daemon = Catalyst::Engine::HTTP::Daemon::Catalyst->new( + Listen => SOMAXCONN, LocalPort => $port, - ReuseAddr => 1 + ReuseAddr => 1, + Type => SOCK_STREAM, ); - - unless ($daemon) { - die("Failed to create daemon: $!\n"); + + unless ( defined $daemon ) { + die( qq/Failed to create daemon. Reason: '$!'/ ); } my $base = URI->new( $daemon->url )->canonical; printf( "You can connect to your server at %s\n", $base ); - while ( my $connection = $daemon->accept ) { - - while ( my $request = $connection->get_request ) { - - $request->uri->scheme('http'); # Force URI::http - $request->uri->host( $base->host ); - $request->uri->port( $base->port ); - - my $lwp = Catalyst::Engine::Test::LWP->new( - request => $request, - address => $connection->peerhost, - hostname => gethostbyaddr( $connection->peeraddr, AF_INET ) - ); - - $class->handler($lwp); - $connection->send_response( $lwp->response ); - } - - $connection->close; - undef($connection); + while ( my $client = $daemon->accept ) { + $class->handler($client); } } @@ -85,7 +99,8 @@ sub run { =head1 SEE ALSO -L, L. +L, L, L, +L. =head1 AUTHOR