package Catalyst::Engine::HTTP::Daemon;
use strict;
-use base 'Catalyst::Engine::LWP';
+use base 'Catalyst::Engine::HTTP::Base';
-use IO::Socket qw(AF_INET);
+use IO::Socket qw( SOCK_STREAM SOMAXCONN );
=head1 NAME
#!/usr/bin/perl -w
- BEGIN {
- $ENV{CATALYST_ENGINE} = 'HTTP::Daemon';
- }
+ BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP::Daemon' }
use strict;
use lib '/path/to/MyApp/lib';
=head1 OVERLOADED METHODS
-This class overloads some methods from C<Catalyst::Engine::LWP>.
+This class overloads some methods from C<Catalyst::Engine::HTTP::Base>.
=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: '$!'/ );
}
- printf( "You can connect to your server at %s\n", $daemon->url );
-
- while ( my $connection = $daemon->accept ) {
-
- while ( my $request = $connection->get_request ) {
-
- $request->uri->scheme('http'); # Force URI::http
-
- my $lwp = Catalyst::Engine::LWP::HTTP->new(
- request => $request,
- address => $connection->peerhost,
- hostname => gethostbyaddr( $connection->peeraddr, AF_INET )
- );
+ my $base = URI->new( $daemon->url )->canonical;
- $class->handler($lwp);
- $connection->send_response( $lwp->response );
- }
+ printf( "You can connect to your server at %s\n", $base );
- $connection->close;
- undef($connection);
+ while ( my $client = $daemon->accept ) {
+ $class->handler($client);
}
}
=head1 SEE ALSO
-L<Catalyst>, L<HTTP::Daemon>.
+L<Catalyst>, L<Catalyst::Engine>, L<Catalyst::Engine::HTTP::Base>,
+L<HTTP::Daemon>.
=head1 AUTHOR