1 package Catalyst::Engine::HTTP::Daemon;
4 use base 'Catalyst::Engine::HTTP::Base';
11 if ( $^O eq 'MSWin32' ) {
13 *EINPROGRESS = sub { 10036 };
14 *EWOULDBLOCK = sub { 10035 };
18 *IO::Socket::blocking = sub {
19 my ( $self, $blocking ) = @_;
20 my $nonblocking = $blocking ? 0 : 1;
21 ioctl( $self, 0x8004667e, \$nonblocking );
27 Errno->import( qw[EWOULDBLOCK EINPROGRESS] );
33 Catalyst::Engine::HTTP::Daemon - Catalyst HTTP Daemon Engine
37 A script using the Catalyst::Engine::HTTP::Daemon module might look like:
41 BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP::Daemon' }
44 use lib '/path/to/MyApp/lib';
51 This is the Catalyst engine specialized for development and testing.
53 =head1 OVERLOADED METHODS
55 This class overloads some methods from C<Catalyst::Engine::HTTP::Base>.
64 my ( $class, $request, $response, $client ) = @_;
66 $request->uri->scheme('http'); # Force URI::http
67 $request->uri->host( $request->header('Host') || $client->sockhost );
68 $request->uri->port( $client->sockport );
70 my $http = Catalyst::Engine::HTTP::Base::struct->new(
71 address => $client->peerhost,
76 $class->SUPER::handler($http);
85 my $port = shift || 3000;
87 $SIG{'PIPE'} = 'IGNORE';
89 my $daemon = Catalyst::Engine::HTTP::Daemon::Catalyst->new(
96 unless ( defined $daemon ) {
97 die(qq/Failed to create daemon. Reason: '$!'/);
100 my $base = URI->new( $daemon->url )->canonical;
102 printf( "You can connect to your server at %s\n", $base );
104 my $select = IO::Select->new($daemon);
108 for my $client ( $select->can_read(1) ) {
110 if ( $client == $daemon ) {
111 $client = $daemon->accept;
112 $client->timestamp = time;
113 $client->blocking(0);
114 $select->add($client);
118 next if $client->request;
119 next if $client->response;
121 my $nread = $client->sysread( my $buf, 4096 );
123 unless ( defined($nread) && length($buf) ) {
125 $select->remove($client);
131 $client->request_buffer .= $buf;
133 if ( my $request = $client->get_request ) {
134 $client->request = $request;
135 $client->timestamp = time
140 for my $client ( $select->handles ) {
142 next if $client == $daemon;
144 if ( ( time - $client->timestamp ) > 60 ) {
146 $select->remove($client);
152 next if $client->response;
153 next unless $client->request;
155 $client->response = HTTP::Response->new;
156 $client->response->protocol( $client->request->protocol );
158 $class->handler( $client->request, $client->response, $client );
161 for my $client ( $select->can_write(1) ) {
163 next unless $client->response;
165 unless ( $client->response_buffer ) {
166 $client->response_buffer = $client->response->as_string;
167 $client->response_offset = 0;
170 my $nwrite = $client->syswrite( $client->response_buffer,
171 $client->response_length,
172 $client->response_offset );
174 unless ( defined($nwrite) ) {
176 $select->remove($client);
182 $client->response_offset += $nwrite;
184 if ( $client->response_offset == $client->response_length ) {
186 my $connection = $client->request->header('Connection');
188 unless ( $connection && $connection =~ /Keep-Alive/i ) {
189 $select->remove($client);
193 $client->response = undef;
194 $client->request = undef;
195 $client->response_buffer = undef;
205 L<Catalyst>, L<Catalyst::Engine>, L<Catalyst::Engine::HTTP::Base>,
210 Sebastian Riedel, C<sri@cpan.org>
211 Christian Hansen, C<ch@ngmedia.com>
215 This program is free software, you can redistribute it and/or modify it under
216 the same terms as Perl itself.
220 package Catalyst::Engine::HTTP::Daemon::Catalyst;
223 use base 'HTTP::Daemon';
226 return shift->SUPER::accept('Catalyst::Engine::HTTP::Daemon::Client');
230 return "Catalyst/$Catalyst::VERSION";
233 package Catalyst::Engine::HTTP::Daemon::Client;
236 use base 'HTTP::Daemon::ClientConn';
238 sub request : lvalue {
240 ${*$self}{'request'};
243 sub request_buffer : lvalue {
245 ${*$self}{'httpd_rbuf'};
248 sub response : lvalue {
250 ${*$self}{'response'};
253 sub response_buffer : lvalue {
255 ${*$self}{'httpd_wbuf'};
258 sub response_length {
260 return length( $self->response_buffer );
263 sub response_offset : lvalue {
265 ${*$self}{'httpd_woffset'};
268 sub timestamp : lvalue {
270 ${*$self}{'timestamp'};