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(0.1) ) {
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->request_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 $client->response->protocol( $client->request->protocol );
120 $class->handler( $client->request, $client->response, $client );
123 for my $client ( $select->can_write(0) ) {
125 next unless $client->response;
127 unless ( $client->response_buffer ) {
128 $client->response_buffer = $client->response->as_string;
129 $client->response_offset = 0;
132 my $write = $client->syswrite( $client->response_buffer,
133 $client->response_length,
134 $client->response_offset );
136 $client->response_offset += $write;
138 unless ( defined($write) ) {
140 $select->remove($client);
146 if ( $client->response_offset == $client->response_length ) {
148 my $connection = $client->request->header('Connection');
150 unless ( $connection && $connection =~ /Keep-Alive/i ) {
151 $select->remove($client);
155 $client->response = undef;
156 $client->request = undef;
157 $client->response_buffer = undef;
167 L<Catalyst>, L<Catalyst::Engine>, L<Catalyst::Engine::HTTP::Base>,
172 Sebastian Riedel, C<sri@cpan.org>
173 Christian Hansen, C<ch@ngmedia.com>
177 This program is free software, you can redistribute it and/or modify it under
178 the same terms as Perl itself.
182 package Catalyst::Engine::HTTP::Daemon::Catalyst;
185 use base 'HTTP::Daemon';
188 return shift->SUPER::accept('Catalyst::Engine::HTTP::Daemon::Client');
192 return "Catalyst/$Catalyst::VERSION";
195 package Catalyst::Engine::HTTP::Daemon::Client;
198 use base 'HTTP::Daemon::ClientConn';
200 sub request : lvalue {
202 ${*$self}{'request'};
205 sub request_buffer : lvalue {
207 ${*$self}{'httpd_rbuf'};
210 sub response : lvalue {
212 ${*$self}{'response'};
215 sub response_buffer : lvalue {
217 ${*$self}{'httpd_wbuf'};
220 sub response_length {
222 return length( $self->response_buffer );
225 sub response_offset : lvalue {
227 ${*$self}{'httpd_woffset'};