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 );
124 unless ( defined($nread) && length($buf) ) {
126 $select->remove($client);
132 $client->request_buffer .= $buf;
134 if ( my $request = $client->get_request ) {
135 $client->request = $request;
136 $client->timestamp = time
141 for my $client ( $select->handles ) {
143 next if $client == $daemon;
145 if ( ( time - $client->timestamp ) > 60 ) {
147 $select->remove($client);
153 next if $client->response;
154 next unless $client->request;
156 $client->response = HTTP::Response->new;
157 $client->response->protocol( $client->request->protocol );
159 $class->handler( $client->request, $client->response, $client );
162 for my $client ( $select->can_write(0.01) ) {
164 next unless $client->response;
166 unless ( $client->response_buffer ) {
168 my $connection = $client->request->header('Connection');
170 if ( $connection && $connection =~ /Keep-Alive/i ) {
171 $client->response->header( 'Connection' => 'Keep-Alive' );
172 $client->response->header( 'Keep-Alive' => 'timeout=60, max=100' );
175 $client->response_buffer = $client->response->as_string;
176 $client->response_offset = 0;
179 my $nwrite = $client->syswrite( $client->response_buffer,
180 $client->response_length,
181 $client->response_offset );
183 unless ( defined($nwrite) ) {
185 $select->remove($client);
191 $client->response_offset += $nwrite;
193 if ( $client->response_offset == $client->response_length ) {
195 my $connection = $client->request->header('Connection');
197 unless ( $connection && $connection =~ /Keep-Alive/i ) {
198 $select->remove($client);
202 $client->response = undef;
203 $client->request = undef;
204 $client->response_buffer = undef;
214 L<Catalyst>, L<Catalyst::Engine>, L<Catalyst::Engine::HTTP::Base>,
219 Sebastian Riedel, C<sri@cpan.org>
220 Christian Hansen, C<ch@ngmedia.com>
224 This program is free software, you can redistribute it and/or modify it under
225 the same terms as Perl itself.
229 package Catalyst::Engine::HTTP::Daemon::Catalyst;
232 use base 'HTTP::Daemon';
235 return shift->SUPER::accept('Catalyst::Engine::HTTP::Daemon::Client');
239 return "Catalyst/$Catalyst::VERSION";
242 package Catalyst::Engine::HTTP::Daemon::Client;
245 use base 'HTTP::Daemon::ClientConn';
247 sub request : lvalue {
249 ${*$self}{'request'};
252 sub request_buffer : lvalue {
254 ${*$self}{'httpd_rbuf'};
257 sub response : lvalue {
259 ${*$self}{'response'};
262 sub response_buffer : lvalue {
264 ${*$self}{'httpd_wbuf'};
267 sub response_length {
269 return length( $self->response_buffer );
272 sub response_offset : lvalue {
274 ${*$self}{'httpd_woffset'};
277 sub timestamp : lvalue {
279 ${*$self}{'timestamp'};