1 package Catalyst::Engine::SCGI;
4 use base 'Catalyst::Engine::CGI';
6 die "Please install SCGI\n" if $@;
11 my $uri_proto=URI->new();
15 Catalyst::Engine::SCGI - SCGI Engine
19 This is the SCGI engine.
21 =head1 OVERLOADED METHODS
23 This class overloads some methods from C<Catalyst::Engine::CGI>.
25 =head2 $self->run($c, $port, $detach)
27 Start the SCGI server. If $port is not set default to port 9000. If $detach is set, server will go into the background.
32 my ( $self, $class, $port, $detach ) = @_;
35 $port = 9000 unless defined $port;
36 my $socket = IO::Socket::INET->new(
40 ) or die "cannot bind to port $port: $!";
41 $sock = SCGI->new( $socket, blocking => 1 )
42 or die "Failed to open SCGI socket; $!";
44 $self->daemon_fork() if defined $detach;
45 $self->daemon_detach() if defined $detach;
46 while ( my $request = $sock->accept ) {
47 eval { $request->read_env };
53 $self->{_request} = $request;
54 $class->handle_request( env => $request->env );
55 # make sure to close once we are done.
61 =head2 $self->finalize_headers ( $c )
63 Write finalized headers to socket
66 sub finalize_headers {
67 my ( $self, $c ) = @_;
68 $c->response->header( Status => $c->response->status );
69 $self->{_request}->connection->print(
70 $c->response->headers->as_string("\015\012") . "\015\012" );
73 =head2 $self->write ( $c, $buffer )
75 Write directly to socket
79 my ( $self, $c, $buffer ) = @_;
81 unless ( $self->{_prepared_write} ) {
82 $self->prepare_write($c);
83 $self->{_prepared_write} = 1;
86 $self->{_request}->connection->print($buffer);
89 =head2 $self->read_chunk ( $c, $buffer, $readlen )
91 Read Body content to $_[3]'s set length and direct output to $_[2].
95 my ( $self, $c ) = @_;
96 my $rc = read( $self->{_request}->connection, $_[2], $_[3] );
100 =head2 $self->daemon_fork()
102 Performs the first part of daemon initialisation. Specifically,
103 forking. STDERR, etc are still connected to a terminal.
112 =head2 $self->daemon_detach( )
114 Performs the second part of daemon initialisation. Specifically,
115 disassociates from the terminal.
117 However, this does B<not> change the current working directory to "/",
118 as normal daemons do. It also does not close all open file
119 descriptors (except STDIN, STDOUT and STDERR, which are re-opened from
126 print "SCGI daemon started (pid $$)\n";
127 open STDIN, "+</dev/null" or die $!;
128 open STDOUT, ">&STDIN" or die $!;
129 open STDERR, ">&STDIN" or die $!;