1 # $Id: FCGI.pm,v 1.15 2000/11/03 15:42:10 skimo Exp $
8 @ISA = qw(Exporter DynaLoader);
9 # Items to export into callers namespace by default. Note: do not export
10 # names by default without a very good reason. Use EXPORT_OK instead.
11 # Do not simply export all your public functions/methods/constants.
20 # Preloaded methods go here.
22 # Autoload methods go after __END__, and are processed by the autosplit program.
24 *FAIL_ACCEPT_ON_INTR = sub() { 1 };
26 sub Request(;***$$$) {
27 my @defaults = (\*STDIN, \*STDOUT, \*STDERR, \%ENV, 0, 0);
28 splice @defaults,0,@_,@_;
33 if (defined %FCGI::ENV) {
38 my $rc = Accept($global_request);
39 for (keys %FCGI::ENV) {
40 $ENV{$_} = $FCGI::ENV{$_} unless exists $ENV{$_};
44 $SIG{__WARN__} = $warn_handler if (tied (*STDIN));
45 $SIG{__DIE__} = $die_handler if (tied (*STDIN));
51 %ENV = %FCGI::ENV if (defined %FCGI::ENV);
55 delete $SIG{__WARN__} if ($SIG{__WARN__} == $warn_handler);
56 delete $SIG{__DIE__} if ($SIG{__DIE__} == $die_handler);
59 Finish ($global_request);
63 Flush($global_request);
67 Detach($global_request);
71 Attach($global_request);
78 sub start_filter_data() {
79 StartFilterData($global_request);
82 $global_request = Request();
83 $warn_handler = sub { print STDERR @_ };
84 $die_handler = sub { print STDERR @_ unless $^S };
89 shift->PRINT(sprintf(shift, @_));
95 my $rs = $/ eq '' ? "\n\n" : $/;
96 my $l = substr $rs, -1;
102 $c = $stream->GETC();
107 last if $c eq $l and substr($s, -$len) eq $rs;
108 $c = $stream->GETC();
116 return open($_[0], $_[1]);
119 eval("$rc = open($_[0], $_[1], $_[2])");
131 FCGI - Fast CGI module
138 my $request = FCGI::Request();
140 while($request->Accept() >= 0) {
141 print("Content-type: text/html\r\n\r\n", ++$count);
152 Creates a request handle. It has the following optional parameters:
156 =item input perl file handle (default: \*STDIN)
158 =item output perl file handle (default: \*STDOUT)
160 =item error perl file handle (default: \*STDERR)
162 These filehandles will be setup to act as input/output/error
165 =item environment hash reference (default: \%ENV)
167 The hash will be populated with the environment.
169 =item socket (default: 0)
171 Socket to communicate with the server.
172 Can be the result of the OpenSocket function.
173 For the moment, it's the file descriptor of the socket
174 that should be passed. This may change in the future.
176 =item flags (default: 0)
182 =item FCGI::FAIL_ACCEPT_ON_INTR
184 If set, Accept will fail if interrupted.
185 It not set, it will just keep on waiting.
192 my $req = FCGI::Request;
196 my $in = new IO::Handle;
197 my $out = new IO::Handle;
198 my $err = new IO::Handle;
199 my $req = FCGI::Request($in, $out, $err, \%env);
201 =item FCGI::OpenSocket(path, backlog)
203 =item FCGI::CloseSocket(socket)
205 Close a socket opened with OpenSocket.
209 Accepts a connection on $req, attaching the filehandles and
210 populating the environment hash.
211 Returns 0 on success.
212 If a connection has been accepted before, the old
213 one will be finished first.
215 Note that unlike with the old interface, no die and warn
216 handlers are installed by default. This means that if
217 you are not running an sfio enabled perl, any warn or
218 die message will not end up in the server's log by default.
219 It is advised you set up die and warn handlers yourself.
220 FCGI.pm contains an example of die and warn handlers.
224 Finishes accepted connection.
225 Also detaches filehandles.
229 Flushes accepted connection.
233 Temporarily detaches filehandles on an accepted connection.
237 Re-attaches filehandles on an accepted connection.
239 =item $env = $req->GetEnvironment()
241 Returns the environment parameter passed to FCGI::Request.
243 =item ($in, $out, $err) = $req->GetHandles()
245 Returns the file handle parameters passed to FCGI::Request.
247 =item $isfcgi = $req->IsFastCGI()
249 Returns whether or not the program was run as a FastCGI.
255 Sven Verdoolaege <skimo@kotnet.org>