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(0.01) ) {
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(0.01) ) {
163 next unless $client->response;
165 unless ( $client->response_buffer ) {
167 my $connection = $client->request->header('Connection');
169 if ( $connection && $connection =~ /Keep-Alive/i ) {
170 $client->response->header( 'Connection' => 'Keep-Alive' );
171 $client->response->header( 'Keep-Alive' => 'timeout=60, max=100' );
174 $client->response_buffer = $client->response->as_string;
175 $client->response_offset = 0;
178 my $nwrite = $client->syswrite( $client->response_buffer,
179 $client->response_length,
180 $client->response_offset );
182 unless ( defined($nwrite) ) {
184 $select->remove($client);
190 $client->response_offset += $nwrite;
192 if ( $client->response_offset == $client->response_length ) {
194 my $connection = $client->request->header('Connection');
196 unless ( $connection && $connection =~ /Keep-Alive/i ) {
197 $select->remove($client);
201 $client->response = undef;
202 $client->request = undef;
203 $client->response_buffer = undef;
213 L<Catalyst>, L<Catalyst::Engine>, L<Catalyst::Engine::HTTP::Base>,
218 Sebastian Riedel, C<sri@cpan.org>
219 Christian Hansen, C<ch@ngmedia.com>
223 This program is free software, you can redistribute it and/or modify it under
224 the same terms as Perl itself.
228 package Catalyst::Engine::HTTP::Daemon::Catalyst;
231 use base 'HTTP::Daemon';
234 return shift->SUPER::accept('Catalyst::Engine::HTTP::Daemon::Client');
238 return "Catalyst/$Catalyst::VERSION";
241 package Catalyst::Engine::HTTP::Daemon::Client;
244 use base 'HTTP::Daemon::ClientConn';
246 sub request : lvalue {
248 ${*$self}{'request'};
251 sub request_buffer : lvalue {
253 ${*$self}{'httpd_rbuf'};
256 sub response : lvalue {
258 ${*$self}{'response'};
261 sub response_buffer : lvalue {
263 ${*$self}{'httpd_wbuf'};
266 sub response_length {
268 return length( $self->response_buffer );
271 sub response_offset : lvalue {
273 ${*$self}{'httpd_woffset'};
276 sub timestamp : lvalue {
278 ${*$self}{'timestamp'};