1 # $Id: FCGI.pm,v 1.17 2000/11/12 13:11:13 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 warn "accept called as a method; you probably wanted to call Accept" if @_;
34 if (defined %FCGI::ENV) {
39 my $rc = Accept($global_request);
40 for (keys %FCGI::ENV) {
41 $ENV{$_} = $FCGI::ENV{$_} unless exists $ENV{$_};
45 $SIG{__WARN__} = $warn_handler if (tied (*STDIN));
46 $SIG{__DIE__} = $die_handler if (tied (*STDIN));
52 warn "finish called as a method; you probably wanted to call Finish" if @_;
53 %ENV = %FCGI::ENV if (defined %FCGI::ENV);
57 delete $SIG{__WARN__} if ($SIG{__WARN__} == $warn_handler);
58 delete $SIG{__DIE__} if ($SIG{__DIE__} == $die_handler);
61 Finish ($global_request);
65 warn "flush called as a method; you probably wanted to call Flush" if @_;
66 Flush($global_request);
70 warn "detach called as a method; you probably wanted to call Detach" if @_;
71 Detach($global_request);
75 warn "attach called as a method; you probably wanted to call Attach" if @_;
76 Attach($global_request);
83 sub start_filter_data() {
84 StartFilterData($global_request);
87 $global_request = Request();
88 $warn_handler = sub { print STDERR @_ };
89 $die_handler = sub { print STDERR @_ unless $^S };
94 shift->PRINT(sprintf(shift, @_));
100 my $rs = $/ eq '' ? "\n\n" : $/;
101 my $l = substr $rs, -1;
102 my $len = length $rs;
104 $c = $stream->GETC();
107 $c = $stream->GETC();
112 last if $c eq $l and substr($s, -$len) eq $rs;
113 $c = $stream->GETC();
121 return open($_[0], $_[1]);
124 eval("$rc = open($_[0], $_[1], $_[2])");
136 FCGI - Fast CGI module
143 my $request = FCGI::Request();
145 while($request->Accept() >= 0) {
146 print("Content-type: text/html\r\n\r\n", ++$count);
157 Creates a request handle. It has the following optional parameters:
161 =item input perl file handle (default: \*STDIN)
163 =item output perl file handle (default: \*STDOUT)
165 =item error perl file handle (default: \*STDERR)
167 These filehandles will be setup to act as input/output/error
170 =item environment hash reference (default: \%ENV)
172 The hash will be populated with the environment.
174 =item socket (default: 0)
176 Socket to communicate with the server.
177 Can be the result of the OpenSocket function.
178 For the moment, it's the file descriptor of the socket
179 that should be passed. This may change in the future.
181 =item flags (default: 0)
187 =item FCGI::FAIL_ACCEPT_ON_INTR
189 If set, Accept will fail if interrupted.
190 It not set, it will just keep on waiting.
197 my $req = FCGI::Request;
201 my $in = new IO::Handle;
202 my $out = new IO::Handle;
203 my $err = new IO::Handle;
204 my $req = FCGI::Request($in, $out, $err, \%env);
206 =item FCGI::OpenSocket(path, backlog)
208 Creates a socket suitable to use as an argument to Request.
214 Pathname of socket or colon followed by local tcp port.
218 Maximum length of the queue of pending connections.
220 request arrives with the queue full the client may receive
221 an error with an indication of ECONNREFUSED.
225 =item FCGI::CloseSocket(socket)
227 Close a socket opened with OpenSocket.
231 Accepts a connection on $req, attaching the filehandles and
232 populating the environment hash.
233 Returns 0 on success.
234 If a connection has been accepted before, the old
235 one will be finished first.
237 Note that unlike with the old interface, no die and warn
238 handlers are installed by default. This means that if
239 you are not running an sfio enabled perl, any warn or
240 die message will not end up in the server's log by default.
241 It is advised you set up die and warn handlers yourself.
242 FCGI.pm contains an example of die and warn handlers.
246 Finishes accepted connection.
247 Also detaches filehandles.
251 Flushes accepted connection.
255 Temporarily detaches filehandles on an accepted connection.
259 Re-attaches filehandles on an accepted connection.
261 =item $env = $req->GetEnvironment()
263 Returns the environment parameter passed to FCGI::Request.
265 =item ($in, $out, $err) = $req->GetHandles()
267 Returns the file handle parameters passed to FCGI::Request.
269 =item $isfcgi = $req->IsFastCGI()
271 Returns whether or not the program was run as a FastCGI.
277 Sven Verdoolaege <skimo@kotnet.org>