2 use ExtUtils::MakeMaker;
4 do 'FCGI.cfg' or die "no FCGI.cfg";
8 print "Generating FCGI.pm\n";
10 # $Id: FCGI.PL,v 1.37 2002/12/15 20:02:48 skimo Exp $
17 @ISA = qw(Exporter DynaLoader);
18 # Items to export into callers namespace by default. Note: do not export
19 # names by default without a very good reason. Use EXPORT_OK instead.
20 # Do not simply export all your public functions/methods/constants.
27 print OUT '$VERSION = q{'.MM->parse_version('version.pm')."};\n\n";
29 print OUT "bootstrap FCGI;\n" unless ($pure);
31 print OUT '$VERSION = eval $VERSION;';
33 print OUT <<'EOP' if ($pure);
37 use constant VERSION_1 => 1;
39 use constant BEGIN_REQUEST => 1;
40 use constant PARAMS => 4;
41 use constant FCGI_STDIN => 5;
42 use constant FCGI_STDOUT => 6;
43 use constant FCGI_STDERR => 7;
45 use constant RESPONDER => 1;
46 use constant AUTHORIZER => 2;
47 use constant FILTER => 3;
49 %FCGI::rolenames = (RESPONDER, "RESPONDER",
50 AUTHORIZER, "AUTHORIZER",
54 # This only works on Unix; anyone familiar with Windows is welcome
59 (!defined getpeername shift->{listen_sock}) && $! == ENOTCONN
60 unless exists $req->{isfastcgi};
61 return $req->{isfastcgi};
65 return shift->{'env'};
71 return undef unless read $stream, $buf, 1, 0;
72 my ($len) = unpack("C", $buf);
74 $buf = pack("C", $len & 0x7F);
75 return undef unless read $stream, $buf, 3, 1;
76 $len = unpack("N", $buf);
91 open $self->{listen_sock}, "<&=0";
100 unless ($req->IsFastCGI()) {
101 return -1 if $run_once;
107 $req->{socket} = gensym();
108 if ($req->{last} || !accept($req->{socket}, $req->{listen_sock})) {
109 $req->{error} = "accept";
112 my ($type, $id, $body) = $req->read_record();
113 if ($type != BEGIN_REQUEST) {
114 $req->{error} = "begin request";
117 my ($role, $flags) = unpack("nC", $body);
118 $req->{role} = $role;
119 $req->{flags} = $flags;
123 $req->{env}{FCGI_ROLE} = $FCGI::rolenames{$req->{role}};
124 my $param = FCGI::Stream->new($req, PARAMS);
126 while (defined($nlen = read_nv_len($param)) &&
127 defined($vlen = read_nv_len($param))) {
129 read $param, $name, $nlen;
130 read $param, $val, $vlen;
131 $req->{env}{$name} = $val;
134 $req->{accepted} = 1;
142 untie ${$req->{out}};
143 untie ${$req->{err}};
149 tie ${$req->{in}}, 'FCGI::Stream', $req, FCGI_STDIN;
150 tie ${$req->{out}}, 'FCGI::Stream', $req, FCGI_STDOUT;
151 tie ${$req->{err}}, 'FCGI::Stream', $req, FCGI_STDERR;
157 $req->Bind() if ($req->{accepted} && !$req->{bound});
162 $req->UndoBindings() if ($req->{accepted} && $req->{bound});
167 return unless $req->{accepted};
169 $req->UndoBindings();
170 # apparently these are harmful
171 # close ${$req->{out}};
172 # close ${$req->{err}};
174 $req->{accepted} = 0;
189 read($self->{socket}, $header, 8);
190 my ($version, $type, $id, $clen, $plen) = unpack("CCnnC", $header);
191 read($self->{socket}, $body, $clen+$plen);
192 $body = undef if $clen == 0;
197 my ($self, $rtype, $len) = @_;
198 while (length $self->{buf} < $len) {
199 my ($type, $id, $buf) = $self->read_record();
200 return undef unless defined $buf;
201 if ($type != $rtype) {
202 $self->{error} = "unexpected stream type";
205 $self->{buf} .= $buf;
207 my ($newbuf, $result) = (substr($self->{buf}, $len),
208 substr($self->{buf}, 0, $len));
209 $self->{buf} = $newbuf;
218 my ($self, $type, $content, $len) = @_;
219 return unless $len > 0;
220 $self->write_record($type, $content, $len);
224 my ($self, $type, $content, $length) = @_;
226 while ($length > 0) {
227 my $len = $length > 32*1024 ? 32*1024 : $length;
228 my $padlen = (8 - ($len % 8)) % 8;
229 my $templ = "CCnnCxa${len}x$padlen";
230 my $data = pack($templ,
231 VERSION_1, $type, $self->{id}, $len, $padlen,
232 substr($content, $offset, $len));
233 syswrite $self->{socket}, $data;
239 { package FCGI::Stream;
242 my ($class, $src, $type) = @_;
243 my $handle = do { \local *FH };
244 tie($$handle, $class, $src, $type);
249 my ($class, $src, $type) = @_;
250 bless { src => $src, type => $type }, $class;
254 my ($stream, undef, $len, $offset) = @_;
256 my $buf = $stream->{src}->read($stream->{type}, $len);
257 return undef unless defined $buf;
258 substr($$ref, $offset, 0, $buf);
263 my ($stream) = shift;
265 $stream->{src}->write($stream->{type}, $_, length($_));
271 $stream->{src}->write_record($stream->{type}, undef, 0);
277 print OUT while <DATA>;
281 # Preloaded methods go here.
283 # Autoload methods go after __END__, and are processed by the autosplit program.
285 *FAIL_ACCEPT_ON_INTR = sub() { 1 };
287 sub Request(;***$*$) {
288 my @defaults = (\*STDIN, \*STDOUT, \*STDERR, \%ENV, 0, 0);
289 $_[4] = fileno($_[4]) if defined($_[4]) && defined(fileno($_[4]));
290 splice @defaults,0,@_,@_;
295 warn "accept called as a method; you probably wanted to call Accept" if @_;
296 if (defined %FCGI::ENV) {
301 my $rc = Accept($global_request);
302 for (keys %FCGI::ENV) {
303 $ENV{$_} = $FCGI::ENV{$_} unless exists $ENV{$_};
307 $SIG{__WARN__} = $warn_handler if (tied (*STDIN));
308 $SIG{__DIE__} = $die_handler if (tied (*STDIN));
314 warn "finish called as a method; you probably wanted to call Finish" if @_;
315 %ENV = %FCGI::ENV if (defined %FCGI::ENV);
319 delete $SIG{__WARN__} if ($SIG{__WARN__} == $warn_handler);
320 delete $SIG{__DIE__} if ($SIG{__DIE__} == $die_handler);
323 Finish ($global_request);
327 warn "flush called as a method; you probably wanted to call Flush" if @_;
328 Flush($global_request);
332 warn "detach called as a method; you probably wanted to call Detach" if @_;
333 Detach($global_request);
337 warn "attach called as a method; you probably wanted to call Attach" if @_;
338 Attach($global_request);
342 sub set_exit_status {
345 sub start_filter_data() {
346 StartFilterData($global_request);
349 $global_request = Request();
350 $warn_handler = sub { print STDERR @_ };
351 $die_handler = sub { print STDERR @_ unless $^S };
353 package FCGI::Stream;
356 shift->PRINT(sprintf(shift, @_));
365 my $rs = $/ eq '' ? "\n\n" : $/;
366 my $l = substr $rs, -1;
367 my $len = length $rs;
369 $c = $stream->GETC();
372 $c = $stream->GETC();
377 last if $c eq $l and substr($s, -$len) eq $rs;
378 $c = $stream->GETC();
386 return open($_[0], $_[1]);
389 eval("$rc = open($_[0], $_[1], $_[2])");
395 # Some things (e.g. IPC::Run) use fileno to determine if a filehandle is open,
396 # so we return a defined, but meaningless value. (-1 being the error return
397 # value from the syscall in c, meaning it can never be a valid fd no)
398 # Probably a better alternative would be to return the fcgi stream fd.
407 FCGI - Fast CGI module
414 my $request = FCGI::Request();
416 while($request->Accept() >= 0) {
417 print("Content-type: text/html\r\n\r\n", ++$count);
428 Creates a request handle. It has the following optional parameters:
432 =item input perl file handle (default: \*STDIN)
434 =item output perl file handle (default: \*STDOUT)
436 =item error perl file handle (default: \*STDERR)
438 These filehandles will be setup to act as input/output/error
441 =item environment hash reference (default: \%ENV)
443 The hash will be populated with the environment.
445 =item socket (default: 0)
447 Socket to communicate with the server.
448 Can be the result of the OpenSocket function.
449 For the moment, it's the file descriptor of the socket
450 that should be passed. This may change in the future.
452 You should only use your own socket if your program
453 is not started by a process manager such as mod_fastcgi
454 (except for the FastCgiExternalServer case) or cgi-fcgi.
455 If you use the option, you have to let your FastCGI
456 server know which port (and possibly server) your program
458 See remote.pl for an example.
460 =item flags (default: 0)
466 =item FCGI::FAIL_ACCEPT_ON_INTR
468 If set, Accept will fail if interrupted.
469 It not set, it will just keep on waiting.
476 my $req = FCGI::Request;
480 my $in = new IO::Handle;
481 my $out = new IO::Handle;
482 my $err = new IO::Handle;
483 my $req = FCGI::Request($in, $out, $err, \%env);
485 =item FCGI::OpenSocket(path, backlog)
487 Creates a socket suitable to use as an argument to Request.
493 Pathname of socket or colon followed by local tcp port.
494 Note that some systems take file permissions into account
495 on Unix domain sockets, so you'll have to make sure that
496 the server can write to the created file, by changing
497 the umask before the call and/or changing permissions and/or
498 group of the file afterwards.
502 Maximum length of the queue of pending connections.
504 request arrives with the queue full the client may receive
505 an error with an indication of ECONNREFUSED.
509 =item FCGI::CloseSocket(socket)
511 Close a socket opened with OpenSocket.
515 Accepts a connection on $req, attaching the filehandles and
516 populating the environment hash.
517 Returns 0 on success.
518 If a connection has been accepted before, the old
519 one will be finished first.
521 Note that unlike with the old interface, no die and warn
522 handlers are installed by default. This means that if
523 you are not running an sfio enabled perl, any warn or
524 die message will not end up in the server's log by default.
525 It is advised you set up die and warn handlers yourself.
526 FCGI.pm contains an example of die and warn handlers.
530 Finishes accepted connection.
531 Also detaches filehandles.
535 Flushes accepted connection.
539 Temporarily detaches filehandles on an accepted connection.
543 Re-attaches filehandles on an accepted connection.
545 =item $req->LastCall()
547 Tells the library not to accept any more requests on this handle.
548 It should be safe to call this method from signal handlers.
550 Note that this method is still experimental and everything
551 about it, including its name, is subject to change.
553 =item $env = $req->GetEnvironment()
555 Returns the environment parameter passed to FCGI::Request.
557 =item ($in, $out, $err) = $req->GetHandles()
559 Returns the file handle parameters passed to FCGI::Request.
561 =item $isfcgi = $req->IsFastCGI()
563 Returns whether or not the program was run as a FastCGI.
569 Sven Verdoolaege <skimo@kotnet.org>