1 # $Id: FCGI.PL,v 1.37 2002/12/15 20:02:48 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.
19 $VERSION = eval $VERSION;
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 $_[4] = fileno($_[4]) if defined($_[4]) && defined(fileno($_[4]));
29 splice @defaults,0,@_,@_;
34 warn "accept called as a method; you probably wanted to call Accept" if @_;
40 my $rc = Accept($global_request);
41 for (keys %FCGI::ENV) {
42 $ENV{$_} = $FCGI::ENV{$_} unless exists $ENV{$_};
46 $SIG{__WARN__} = $warn_handler if (tied (*STDIN));
47 $SIG{__DIE__} = $die_handler if (tied (*STDIN));
53 warn "finish called as a method; you probably wanted to call Finish" if @_;
54 %ENV = %FCGI::ENV if %FCGI::ENV;
58 delete $SIG{__WARN__} if ($SIG{__WARN__} == $warn_handler);
59 delete $SIG{__DIE__} if ($SIG{__DIE__} == $die_handler);
62 Finish ($global_request);
66 warn "flush called as a method; you probably wanted to call Flush" if @_;
67 Flush($global_request);
71 warn "detach called as a method; you probably wanted to call Detach" if @_;
72 Detach($global_request);
76 warn "attach called as a method; you probably wanted to call Attach" if @_;
77 Attach($global_request);
84 sub start_filter_data() {
85 StartFilterData($global_request);
88 $global_request = Request();
89 $warn_handler = sub { print STDERR @_ };
90 $die_handler = sub { print STDERR @_ unless $^S };
95 shift->PRINT(sprintf(shift, @_));
104 my $rs = $/ eq '' ? "\n\n" : $/;
105 my $l = substr $rs, -1;
106 my $len = length $rs;
108 $c = $stream->GETC();
111 $c = $stream->GETC();
116 last if $c eq $l and substr($s, -$len) eq $rs;
117 $c = $stream->GETC();
125 return open($_[0], $_[1]);
128 eval("$rc = open($_[0], $_[1], $_[2])");
134 # Some things (e.g. IPC::Run) use fileno to determine if a filehandle is open,
135 # so we return a defined, but meaningless value. (-1 being the error return
136 # value from the syscall in c, meaning it can never be a valid fd no)
137 # Probably a better alternative would be to return the fcgi stream fd.
146 FCGI - Fast CGI module
153 my $request = FCGI::Request();
155 while($request->Accept() >= 0) {
156 print("Content-type: text/html\r\n\r\n", ++$count);
167 Creates a request handle. It has the following optional parameters:
171 =item input perl file handle (default: \*STDIN)
173 =item output perl file handle (default: \*STDOUT)
175 =item error perl file handle (default: \*STDERR)
177 These filehandles will be setup to act as input/output/error
180 =item environment hash reference (default: \%ENV)
182 The hash will be populated with the environment.
184 =item socket (default: 0)
186 Socket to communicate with the server.
187 Can be the result of the OpenSocket function.
188 For the moment, it's the file descriptor of the socket
189 that should be passed. This may change in the future.
191 You should only use your own socket if your program
192 is not started by a process manager such as mod_fastcgi
193 (except for the FastCgiExternalServer case) or cgi-fcgi.
194 If you use the option, you have to let your FastCGI
195 server know which port (and possibly server) your program
197 See remote.pl for an example.
199 =item flags (default: 0)
205 =item FCGI::FAIL_ACCEPT_ON_INTR
207 If set, Accept will fail if interrupted.
208 It not set, it will just keep on waiting.
215 my $req = FCGI::Request;
219 my $in = new IO::Handle;
220 my $out = new IO::Handle;
221 my $err = new IO::Handle;
222 my $req = FCGI::Request($in, $out, $err, \%env);
224 =item FCGI::OpenSocket(path, backlog)
226 Creates a socket suitable to use as an argument to Request.
232 Pathname of socket or colon followed by local tcp port.
233 Note that some systems take file permissions into account
234 on Unix domain sockets, so you'll have to make sure that
235 the server can write to the created file, by changing
236 the umask before the call and/or changing permissions and/or
237 group of the file afterwards.
241 Maximum length of the queue of pending connections.
243 request arrives with the queue full the client may receive
244 an error with an indication of ECONNREFUSED.
248 =item FCGI::CloseSocket(socket)
250 Close a socket opened with OpenSocket.
254 Accepts a connection on $req, attaching the filehandles and
255 populating the environment hash.
256 Returns 0 on success.
257 If a connection has been accepted before, the old
258 one will be finished first.
260 Note that unlike with the old interface, no die and warn
261 handlers are installed by default. This means that if
262 you are not running an sfio enabled perl, any warn or
263 die message will not end up in the server's log by default.
264 It is advised you set up die and warn handlers yourself.
265 FCGI.pm contains an example of die and warn handlers.
269 Finishes accepted connection.
270 Also detaches filehandles.
274 Flushes accepted connection.
278 Temporarily detaches filehandles on an accepted connection.
282 Re-attaches filehandles on an accepted connection.
284 =item $req->LastCall()
286 Tells the library not to accept any more requests on this handle.
287 It should be safe to call this method from signal handlers.
289 Note that this method is still experimental and everything
290 about it, including its name, is subject to change.
292 =item $env = $req->GetEnvironment()
294 Returns the environment parameter passed to FCGI::Request.
296 =item ($in, $out, $err) = $req->GetHandles()
298 Returns the file handle parameters passed to FCGI::Request.
300 =item $isfcgi = $req->IsFastCGI()
302 Returns whether or not the program was run as a FastCGI.
308 Sven Verdoolaege <skimo@kotnet.org>