prepare for 0.77 release
[catagits/fcgi2.git] / perl / FCGI.pm
index e043b5a..ebc9356 100644 (file)
@@ -1,94 +1,32 @@
-# $Id: FCGI.pm,v 1.15 2000/11/03 15:42:10 skimo Exp $
-
 package FCGI;
+use strict;
 
-require Exporter;
-require DynaLoader;
-
-@ISA = qw(Exporter DynaLoader);
-# Items to export into callers namespace by default. Note: do not export
-# names by default without a very good reason. Use EXPORT_OK instead.
-# Do not simply export all your public functions/methods/constants.
-@EXPORT = qw(
-       
-);
-
-$VERSION = '0.56';
-
-bootstrap FCGI;
-
-# Preloaded methods go here.
-
-# Autoload methods go after __END__, and are processed by the autosplit program.
-
-*FAIL_ACCEPT_ON_INTR = sub() { 1 };
-
-sub Request(;***$$$) {
-    my @defaults = (\*STDIN, \*STDOUT, \*STDERR, \%ENV, 0, 0);
-    splice @defaults,0,@_,@_;
-    RequestX(@defaults);
-}
-
-sub accept() {
-    if (defined %FCGI::ENV) {
-       %ENV = %FCGI::ENV;
-    } else {
-       %FCGI::ENV = %ENV;
-    }
-    my $rc = Accept($global_request);
-    for (keys %FCGI::ENV) {
-       $ENV{$_} = $FCGI::ENV{$_} unless exists $ENV{$_};
-    }
-
-    # not SFIO
-    $SIG{__WARN__} = $warn_handler if (tied (*STDIN));
-    $SIG{__DIE__} = $die_handler if (tied (*STDIN));
+BEGIN {
+    our $VERSION = '0.77';
 
-    return $rc;
+    require XSLoader;
+    XSLoader::load(__PACKAGE__, $VERSION);
 }
 
-sub finish() {
-    %ENV = %FCGI::ENV if (defined %FCGI::ENV);
+sub FAIL_ACCEPT_ON_INTR () { 1 };
 
-    # not SFIO
-    if (tied (*STDIN)) {
-       delete $SIG{__WARN__} if ($SIG{__WARN__} == $warn_handler);
-       delete $SIG{__DIE__} if ($SIG{__DIE__} == $die_handler);
-    }
-
-    Finish ($global_request);
-}
-
-sub flush() {
-    Flush($global_request);
-}
-
-sub detach() {
-    Detach($global_request);
-}
-
-sub attach() {
-    Attach($global_request);
-}
-
-# deprecated
-sub set_exit_status {
-}
-
-sub start_filter_data() {
-    StartFilterData($global_request);
+sub Request(;***$*$) {
+    my @defaults = (\*STDIN, \*STDOUT, \*STDERR, \%ENV, 0, FAIL_ACCEPT_ON_INTR);
+    $_[4] = fileno($_[4]) if defined($_[4]) && defined(fileno($_[4]));
+    splice @defaults,0,@_,@_;
+    &RequestX(@defaults);
 }
 
-$global_request = Request();
-$warn_handler = sub { print STDERR @_ };
-$die_handler = sub { print STDERR @_ unless $^S };
-
 package FCGI::Stream;
+use strict;
 
 sub PRINTF {
   shift->PRINT(sprintf(shift, @_));
 }
 
+sub BINMODE {
+}
+
 sub READLINE {
     my $stream = shift;
     my ($s, $c);
@@ -98,28 +36,36 @@ sub READLINE {
 
     $c = $stream->GETC();
     if ($/ eq '') {
-       while ($c eq "\n") { 
-           $c = $stream->GETC();
-       }
+        while ($c eq "\n") {
+            $c = $stream->GETC();
+        }
     }
     while (defined $c) {
-       $s .= $c;
-       last if $c eq $l and substr($s, -$len) eq $rs;
-       $c = $stream->GETC();
+        $s .= $c;
+        last if $c eq $l and substr($s, -$len) eq $rs;
+        $c = $stream->GETC();
     }
     $s;
 }
 
 sub OPEN {
-    $_[0]->CLOSE;
-    if (@_ == 2) {
-       return open($_[0], $_[1]);
-    } else {
-       my $rc;
-       eval("$rc = open($_[0], $_[1], $_[2])");
-       die $@ if $@;
-       return $rc;
-    }
+    require Carp;
+    Carp::croak(q/Operation 'OPEN' not supported on FCGI::Stream handle/);
+}
+
+sub SEEK {
+    require Carp;
+    Carp::croak(q/Operation 'SEEK' not supported on FCGI::Stream handle/);
+}
+
+sub TELL {
+    require Carp;
+    Carp::croak(q/Operation 'TELL' not supported on FCGI::Stream handle/);
+}
+
+sub TIEHANDLE {
+    require Carp;
+    Carp::croak(q/Operation 'TIEHANDLE' not supported on FCGI::Stream handle/);
 }
 
 1;
@@ -138,7 +84,7 @@ FCGI - Fast CGI module
     my $request = FCGI::Request();
 
     while($request->Accept() >= 0) {
-       print("Content-type: text/html\r\n\r\n", ++$count);
+        print("Content-type: text/html\r\n\r\n", ++$count);
     }
 
 =head1 DESCRIPTION
@@ -160,7 +106,7 @@ Creates a request handle. It has the following optional parameters:
 =item error perl file handle (default: \*STDERR)
 
 These filehandles will be setup to act as input/output/error
-on succesful Accept.
+on successful Accept.
 
 =item environment hash reference (default: \%ENV)
 
@@ -173,6 +119,14 @@ Can be the result of the OpenSocket function.
 For the moment, it's the file descriptor of the socket
 that should be passed. This may change in the future.
 
+You should only use your own socket if your program
+is not started by a process manager such as mod_fastcgi
+(except for the FastCgiExternalServer case) or cgi-fcgi.
+If you use the option, you have to let your FastCGI
+server know which port (and possibly server) your program
+is listening on.
+See remote.pl for an example.
+
 =item flags (default: 0)
 
 Possible values:
@@ -200,6 +154,28 @@ or:
 
 =item FCGI::OpenSocket(path, backlog)
 
+Creates a socket suitable to use as an argument to Request.
+
+=over 8
+
+=item path
+
+Pathname of socket or colon followed by local tcp port.
+Note that some systems take file permissions into account
+on Unix domain sockets, so you'll have to make sure that
+the server can write to the created file, by changing
+the umask before the call and/or changing permissions and/or
+group of the file afterwards.
+
+=item backlog
+
+Maximum length of the queue of pending connections.
+If a connection
+request arrives with the queue full the client may receive
+an  error  with  an  indication of ECONNREFUSED.
+
+=back
+
 =item FCGI::CloseSocket(socket)
 
 Close a socket opened with OpenSocket.
@@ -236,6 +212,14 @@ Temporarily detaches filehandles on an accepted connection.
 
 Re-attaches filehandles on an accepted connection.
 
+=item $req->LastCall()
+
+Tells the library not to accept any more requests on this handle.
+It should be safe to call this method from signal handlers.
+
+Note that this method is still experimental and everything
+about it, including its name, is subject to change.
+
 =item $env = $req->GetEnvironment()
 
 Returns the environment parameter passed to FCGI::Request.
@@ -250,6 +234,21 @@ Returns whether or not the program was run as a FastCGI.
 
 =back
 
+=head1 LIMITATIONS
+
+FCGI.pm isn't Unicode aware, only characters within the range 0x00-0xFF are 
+supported. Attempts to output strings containing characters above 0xFF results
+in a exception: (F) C<Wide character in %s>.
+
+Users who wants the previous (FCGI.pm <= 0.68) incorrect behavior can disable the
+exception by using the C<bytes> pragma.
+
+    {
+        use bytes;
+        print "\x{263A}";
+    }
+
+
 =head1 AUTHOR
 
 Sven Verdoolaege <skimo@kotnet.org>