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 $client->response->header( Server => $daemon->product_tokens );
174 my $connection = $client->request->header('Connection') || '';
176 if ( $connection =~ /Keep-Alive/i ) {
177 $client->response->header( 'Connection' => 'Keep-Alive' );
178 $client->response->header( 'Keep-Alive' => 'timeout=60, max=100' );
181 if ( $connection =~ /close/i ) {
182 $client->response->header( 'Connection' => 'close' );
185 $client->response_buffer = $client->response->as_string("\x0D\x0A");
186 $client->response_offset = 0;
189 my $nwrite = $client->syswrite( $client->response_buffer,
190 $client->response_length,
191 $client->response_offset );
195 next if $! == EWOULDBLOCK;
196 next if $! == EINPROGRESS;
199 $select->remove($client);
205 $client->response_offset += $nwrite;
207 if ( $client->response_offset == $client->response_length ) {
209 my $connection = $client->request->header('Connection') || '';
210 my $protocol = $client->request->protocol;
213 if ( $protocol eq 'HTTP/1.1' && $connection !~ /close/i ) {
217 if ( $protocol ne 'HTTP/1.1' && $connection =~ /Keep-Alive/i ) {
221 unless ( $persistent ) {
222 $select->remove($client);
226 $client->response = undef;
227 $client->request = undef;
228 $client->response_buffer = undef;
238 L<Catalyst>, L<Catalyst::Engine>, L<Catalyst::Engine::HTTP::Base>,
243 Sebastian Riedel, C<sri@cpan.org>
244 Christian Hansen, C<ch@ngmedia.com>
248 This program is free software, you can redistribute it and/or modify it under
249 the same terms as Perl itself.
253 package Catalyst::Engine::HTTP::Daemon::Catalyst;
256 use base 'HTTP::Daemon';
259 return shift->SUPER::accept('Catalyst::Engine::HTTP::Daemon::Client');
263 return "Catalyst/$Catalyst::VERSION";
266 package Catalyst::Engine::HTTP::Daemon::Client;
269 use base 'HTTP::Daemon::ClientConn';
271 sub request : lvalue {
273 ${*$self}{'request'};
276 sub request_buffer : lvalue {
278 ${*$self}{'httpd_rbuf'};
281 sub response : lvalue {
283 ${*$self}{'response'};
286 sub response_buffer : lvalue {
288 ${*$self}{'httpd_wbuf'};
291 sub response_length {
293 return length( $self->response_buffer );
296 sub response_offset : lvalue {
298 ${*$self}{'httpd_woffset'};
301 sub timestamp : lvalue {
303 ${*$self}{'timestamp'};