1 package Catalyst::Engine::HTTP::Daemon;
4 use base 'Catalyst::Engine::HTTP::Base';
10 Catalyst::Engine::HTTP::Daemon - Catalyst HTTP Daemon Engine
14 A script using the Catalyst::Engine::HTTP::Daemon module might look like:
18 BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP::Daemon' }
21 use lib '/path/to/MyApp/lib';
28 This is the Catalyst engine specialized for development and testing.
30 =head1 OVERLOADED METHODS
32 This class overloads some methods from C<Catalyst::Engine::HTTP::Base>.
41 my ( $class, $request, $response, $client ) = @_;
43 $request->uri->scheme('http'); # Force URI::http
44 $request->uri->host( $request->header('Host') || $client->sockhost );
45 $request->uri->port( $client->sockport );
47 my $http = Catalyst::Engine::HTTP::Base::struct->new(
48 address => $client->peerhost,
53 $class->SUPER::handler($http);
62 my $port = shift || 3000;
64 $SIG{'PIPE'} = 'IGNORE';
66 my $daemon = Catalyst::Engine::HTTP::Daemon::Catalyst->new(
73 unless ( defined $daemon ) {
74 die(qq/Failed to create daemon. Reason: '$!'/);
77 my $base = URI->new( $daemon->url )->canonical;
79 printf( "You can connect to your server at %s\n", $base );
81 my $select = IO::Select->new($daemon);
85 for my $client ( $select->can_read ) {
87 if ( $client == $daemon ) {
88 $client = $daemon->accept;
90 $select->add($client);
94 next if $client->request;
95 next if $client->response;
97 my $read = $client->sysread( my $buf, 4096 );
99 unless ( defined($read) && length($buf) ) {
101 $select->remove($client);
107 $client->read_buffer($buf);
108 $client->request( $client->get_request );
112 for my $client ( $select->handles ) {
114 next if $client == $daemon;
115 next if $client->response;
116 next unless $client->request;
118 $client->response( HTTP::Response->new );
119 $class->handler( $client->request, $client->response, $client );
122 for my $client ( $select->can_write(0) ) {
124 next unless $client->response;
126 $client->send_response( $client->response );
128 my $connection = $client->request->header('Connection');
130 unless ( $connection && $connection =~ /Keep-Alive/i ) {
131 $select->remove($client);
135 $client->request(undef);
136 $client->response(undef);
145 L<Catalyst>, L<Catalyst::Engine>, L<Catalyst::Engine::HTTP::Base>,
150 Sebastian Riedel, C<sri@cpan.org>
151 Christian Hansen, C<ch@ngmedia.com>
155 This program is free software, you can redistribute it and/or modify it under
156 the same terms as Perl itself.
160 package Catalyst::Engine::HTTP::Daemon::Catalyst;
163 use base 'HTTP::Daemon';
166 return shift->SUPER::accept('Catalyst::Engine::HTTP::Daemon::Client');
170 return "Catalyst/$Catalyst::VERSION";
173 package Catalyst::Engine::HTTP::Daemon::Client;
176 use base 'HTTP::Daemon::ClientConn';
182 ${*$self}{'httpd_rbuf'} .= shift;
185 return ${*$self}{'httpd_rbuf'};
192 ${*$self}{'request'} = shift;
195 return ${*$self}{'request'};
202 ${*$self}{'response'} = shift;
205 return ${*$self}{'response'};