| Catalyst-Engine-SCGI documentation | Contained in the Catalyst-Engine-SCGI distribution. |
Catalyst::Engine::SCGI - SCGI Engine
This is the SCGI engine.
This class overloads some methods from Catalyst::Engine::CGI.
Start the SCGI server. If $port is not set default to port 9000. If $detach is set, server will go into the background.
Write finalized headers to socket
Write directly to socket
Read Body content to $_[3]'s set length and direct output to $_[2].
Performs the first part of daemon initialisation. Specifically, forking. STDERR, etc are still connected to a terminal.
Performs the second part of daemon initialisation. Specifically, disassociates from the terminal.
However, this does not change the current working directory to "/", as normal daemons do. It also does not close all open file descriptors (except STDIN, STDOUT and STDERR, which are re-opened from /dev/null).
| Catalyst-Engine-SCGI documentation | Contained in the Catalyst-Engine-SCGI distribution. |
package Catalyst::Engine::SCGI; use strict; use warnings; use base 'Catalyst::Engine::CGI'; eval "use SCGI"; die "Please install SCGI\n" if $@; use IO::Socket; our $VERSION = '0.03';
sub run { my ( $self, $class, $port, $detach ) = @_; my $sock = 0; $port = 9000 unless defined $port; my $socket = IO::Socket::INET->new( Listen => 5, ReuseAddr => 1, LocalPort => $port, ) or die "cannot bind to port $port: $!"; $sock = SCGI->new( $socket, blocking => 1 ) or die "Failed to open SCGI socket; $!"; $self->daemon_fork() if defined $detach; $self->daemon_detach() if defined $detach; while ( my $request = $sock->accept ) { eval { $request->read_env }; if ($@) { # some error } else { $self->{_request} = $request; $class->handle_request( env => $request->env ); # make sure to close once we are done. $request->close(); } } }
sub finalize_headers { my ( $self, $c ) = @_; $c->response->header( Status => $c->response->status ); $self->{_request}->connection->print( $c->response->headers->as_string("\015\012") . "\015\012" ); }
sub write { my ( $self, $c, $buffer ) = @_; unless ( $self->{_prepared_write} ) { $self->prepare_write($c); $self->{_prepared_write} = 1; } $self->{_request}->connection->print($buffer); }
sub read_chunk { my ( $self, $c ) = @_; my $rc = read( $self->{_request}->connection, $_[2], $_[3] ); return $rc; }
sub daemon_fork { require POSIX; fork && exit; }
sub daemon_detach { my $self = shift; print "SCGI daemon started (pid $$)\n"; open STDIN, "+</dev/null" or die $!; open STDOUT, ">&STDIN" or die $!; open STDERR, ">&STDIN" or die $!; POSIX::setsid(); } 1;