1 package Catalyst::Engine::HTTP::Daemon;
4 use base 'Catalyst::Engine::HTTP::Base';
11 if ( $^O eq 'MSWin32' ) {
13 *EINTR = sub { 10004 };
14 *EINPROGRESS = sub { 10036 };
15 *EWOULDBLOCK = sub { 10035 };
19 *IO::Socket::blocking = sub {
20 my ( $self, $blocking ) = @_;
21 my $nonblocking = $blocking ? 0 : 1;
22 ioctl( $self, 0x8004667e, \$nonblocking );
28 Errno->import( qw[EWOULDBLOCK EINPROGRESS EINTR] );
34 Catalyst::Engine::HTTP::Daemon - Catalyst HTTP Daemon Engine
38 A script using the Catalyst::Engine::HTTP::Daemon module might look like:
42 BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP::Daemon' }
45 use lib '/path/to/MyApp/lib';
52 This is the Catalyst engine specialized for development and testing.
54 =head1 OVERLOADED METHODS
56 This class overloads some methods from C<Catalyst::Engine::HTTP::Base>.
65 my ( $class, $request, $response, $client ) = @_;
67 $request->uri->scheme('http'); # Force URI::http
68 $request->uri->host( $request->header('Host') || $client->sockhost );
69 $request->uri->port( $client->sockport );
71 my $http = Catalyst::Engine::HTTP::Base::struct->new(
72 address => $client->peerhost,
77 $class->SUPER::handler($http);
86 my $port = shift || 3000;
88 $SIG{'PIPE'} = 'IGNORE';
90 my $daemon = Catalyst::Engine::HTTP::Daemon::Catalyst->new(
97 unless ( defined $daemon ) {
98 die(qq/Failed to create daemon. Reason: '$!'/);
101 my $base = URI->new( $daemon->url )->canonical;
103 printf( "You can connect to your server at %s\n", $base );
105 my $select = IO::Select->new($daemon);
109 for my $client ( $select->can_read(0.01) ) {
111 if ( $client == $daemon ) {
112 $client = $daemon->accept;
113 $client->timestamp = time;
114 $client->blocking(0);
115 $select->add($client);
119 next if $client->request;
120 next if $client->response;
122 my $nread = $client->sysread( my $buf, 4096 );
126 next if $! == EWOULDBLOCK;
127 next if $! == EINPROGRESS;
130 $select->remove($client);
136 $client->request_buffer .= $buf;
138 if ( my $request = $client->get_request ) {
139 $client->request = $request;
140 $client->timestamp = time
145 for my $client ( $select->handles ) {
147 next if $client == $daemon;
149 if ( ( time - $client->timestamp ) > 60 ) {
151 $select->remove($client);
157 next if $client->response;
158 next unless $client->request;
160 $client->response = HTTP::Response->new;
161 $client->response->protocol( $client->request->protocol );
163 $class->handler( $client->request, $client->response, $client );
166 for my $client ( $select->can_write(0.01) ) {
168 next unless $client->response;
170 unless ( $client->response_buffer ) {
172 my $connection = $client->request->header('Connection');
174 if ( $connection && $connection =~ /Keep-Alive/i ) {
175 $client->response->header( 'Connection' => 'Keep-Alive' );
176 $client->response->header( 'Keep-Alive' => 'timeout=60, max=100' );
179 $client->response_buffer = $client->response->as_string;
180 $client->response_offset = 0;
183 my $nwrite = $client->syswrite( $client->response_buffer,
184 $client->response_length,
185 $client->response_offset );
189 next if $! == EWOULDBLOCK;
190 next if $! == EINPROGRESS;
193 $select->remove($client);
199 $client->response_offset += $nwrite;
201 if ( $client->response_offset == $client->response_length ) {
203 my $connection = $client->request->header('Connection');
205 unless ( $connection && $connection =~ /Keep-Alive/i ) {
206 $select->remove($client);
210 $client->response = undef;
211 $client->request = undef;
212 $client->response_buffer = undef;
222 L<Catalyst>, L<Catalyst::Engine>, L<Catalyst::Engine::HTTP::Base>,
227 Sebastian Riedel, C<sri@cpan.org>
228 Christian Hansen, C<ch@ngmedia.com>
232 This program is free software, you can redistribute it and/or modify it under
233 the same terms as Perl itself.
237 package Catalyst::Engine::HTTP::Daemon::Catalyst;
240 use base 'HTTP::Daemon';
243 return shift->SUPER::accept('Catalyst::Engine::HTTP::Daemon::Client');
247 return "Catalyst/$Catalyst::VERSION";
250 package Catalyst::Engine::HTTP::Daemon::Client;
253 use base 'HTTP::Daemon::ClientConn';
255 sub request : lvalue {
257 ${*$self}{'request'};
260 sub request_buffer : lvalue {
262 ${*$self}{'httpd_rbuf'};
265 sub response : lvalue {
267 ${*$self}{'response'};
270 sub response_buffer : lvalue {
272 ${*$self}{'httpd_wbuf'};
275 sub response_length {
277 return length( $self->response_buffer );
280 sub response_offset : lvalue {
282 ${*$self}{'httpd_woffset'};
285 sub timestamp : lvalue {
287 ${*$self}{'timestamp'};