warn handler fix; 0.56 release
[catagits/fcgi2.git] / perl / FCGI.pm
1 # $Id: FCGI.pm,v 1.15 2000/11/03 15:42:10 skimo Exp $
2
3 package FCGI;
4
5 require Exporter;
6 require DynaLoader;
7
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.
12 @EXPORT = qw(
13         
14 );
15
16 $VERSION = '0.56';
17
18 bootstrap FCGI;
19
20 # Preloaded methods go here.
21
22 # Autoload methods go after __END__, and are processed by the autosplit program.
23
24 *FAIL_ACCEPT_ON_INTR = sub() { 1 };
25
26 sub Request(;***$$$) {
27     my @defaults = (\*STDIN, \*STDOUT, \*STDERR, \%ENV, 0, 0);
28     splice @defaults,0,@_,@_;
29     RequestX(@defaults);
30 }
31
32 sub accept() {
33     if (defined %FCGI::ENV) {
34         %ENV = %FCGI::ENV;
35     } else {
36         %FCGI::ENV = %ENV;
37     }
38     my $rc = Accept($global_request);
39     for (keys %FCGI::ENV) {
40         $ENV{$_} = $FCGI::ENV{$_} unless exists $ENV{$_};
41     }
42
43     # not SFIO
44     $SIG{__WARN__} = $warn_handler if (tied (*STDIN));
45     $SIG{__DIE__} = $die_handler if (tied (*STDIN));
46
47     return $rc;
48 }
49
50 sub finish() {
51     %ENV = %FCGI::ENV if (defined %FCGI::ENV);
52
53     # not SFIO
54     if (tied (*STDIN)) {
55         delete $SIG{__WARN__} if ($SIG{__WARN__} == $warn_handler);
56         delete $SIG{__DIE__} if ($SIG{__DIE__} == $die_handler);
57     }
58
59     Finish ($global_request);
60 }
61
62 sub flush() {
63     Flush($global_request);
64 }
65
66 sub detach() {
67     Detach($global_request);
68 }
69
70 sub attach() {
71     Attach($global_request);
72 }
73
74 # deprecated
75 sub set_exit_status {
76 }
77
78 sub start_filter_data() {
79     StartFilterData($global_request);
80 }
81
82 $global_request = Request();
83 $warn_handler = sub { print STDERR @_ };
84 $die_handler = sub { print STDERR @_ unless $^S };
85
86 package FCGI::Stream;
87
88 sub PRINTF {
89   shift->PRINT(sprintf(shift, @_));
90 }
91
92 sub READLINE {
93     my $stream = shift;
94     my ($s, $c);
95     my $rs = $/ eq '' ? "\n\n" : $/;
96     my $l = substr $rs, -1;
97     my $len = length $rs;
98
99     $c = $stream->GETC();
100     if ($/ eq '') {
101         while ($c eq "\n") { 
102             $c = $stream->GETC();
103         }
104     }
105     while (defined $c) {
106         $s .= $c;
107         last if $c eq $l and substr($s, -$len) eq $rs;
108         $c = $stream->GETC();
109     }
110     $s;
111 }
112
113 sub OPEN {
114     $_[0]->CLOSE;
115     if (@_ == 2) {
116         return open($_[0], $_[1]);
117     } else {
118         my $rc;
119         eval("$rc = open($_[0], $_[1], $_[2])");
120         die $@ if $@;
121         return $rc;
122     }
123 }
124
125 1;
126
127 =pod
128
129 =head1 NAME
130
131 FCGI - Fast CGI module
132
133 =head1 SYNOPSIS
134
135     use FCGI;
136
137     my $count = 0;
138     my $request = FCGI::Request();
139
140     while($request->Accept() >= 0) {
141         print("Content-type: text/html\r\n\r\n", ++$count);
142     }
143
144 =head1 DESCRIPTION
145
146 Functions:
147
148 =over 4
149
150 =item FCGI::Request
151
152 Creates a request handle. It has the following optional parameters:
153
154 =over 8
155
156 =item input perl file handle (default: \*STDIN)
157
158 =item output perl file handle (default: \*STDOUT)
159
160 =item error perl file handle (default: \*STDERR)
161
162 These filehandles will be setup to act as input/output/error
163 on succesful Accept.
164
165 =item environment hash reference (default: \%ENV)
166
167 The hash will be populated with the environment.
168
169 =item socket (default: 0)
170
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.
175
176 =item flags (default: 0)
177
178 Possible values:
179
180 =over 12
181
182 =item FCGI::FAIL_ACCEPT_ON_INTR
183
184 If set, Accept will fail if interrupted.
185 It not set, it will just keep on waiting.
186
187 =back
188
189 =back
190
191 Example usage:
192     my $req = FCGI::Request;
193
194 or:
195     my %env;
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);
200
201 =item FCGI::OpenSocket(path, backlog)
202
203 =item FCGI::CloseSocket(socket)
204
205 Close a socket opened with OpenSocket.
206
207 =item $req->Accept()
208
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.
214
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.
221
222 =item $req->Finish()
223
224 Finishes accepted connection.
225 Also detaches filehandles.
226
227 =item $req->Flush()
228
229 Flushes accepted connection.
230
231 =item $req->Detach()
232
233 Temporarily detaches filehandles on an accepted connection.
234
235 =item $req->Attach()
236
237 Re-attaches filehandles on an accepted connection.
238
239 =item $env = $req->GetEnvironment()
240
241 Returns the environment parameter passed to FCGI::Request.
242
243 =item ($in, $out, $err) = $req->GetHandles()
244
245 Returns the file handle parameters passed to FCGI::Request.
246
247 =item $isfcgi = $req->IsFastCGI()
248
249 Returns whether or not the program was run as a FastCGI.
250
251 =back
252
253 =head1 AUTHOR
254
255 Sven Verdoolaege <skimo@kotnet.org>
256
257 =cut
258
259 __END__