Commit | Line | Data |
e40fb02c |
1 | # $Id: FCGI.pm,v 1.12 2000/10/18 20:39:15 skimo Exp $ |
1b64d24d |
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 | |
e40fb02c |
16 | $VERSION = '0.55'; |
1b64d24d |
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 | |
d2900ee8 |
24 | *FAIL_ACCEPT_ON_INTR = sub() { 1 }; |
25 | |
6ff77aff |
26 | sub Request(;***$$$) { |
27 | my @defaults = (\*STDIN, \*STDOUT, \*STDERR, \%ENV, 0, 0); |
28 | splice @defaults,0,@_,@_; |
29 | RequestX(@defaults); |
d8cc97fb |
30 | } |
31 | |
6ff77aff |
32 | sub accept() { |
d8cc97fb |
33 | if (defined %FCGI::ENV) { |
34 | %ENV = %FCGI::ENV; |
35 | } else { |
36 | %FCGI::ENV = %ENV; |
37 | } |
6ff77aff |
38 | my $rc = Accept($global_request); |
39 | for (keys %FCGI::ENV) { |
40 | $ENV{$_} = $FCGI::ENV{$_} unless exists $ENV{$_}; |
41 | } |
d8cc97fb |
42 | |
43 | # not SFIO |
44 | $SIG{__WARN__} = $SIG{__DIE__} = $warn_die_handler if (tied (*STDIN)); |
45 | |
46 | return $rc; |
47 | } |
48 | |
6ff77aff |
49 | sub finish() { |
d8cc97fb |
50 | %ENV = %FCGI::ENV if (defined %FCGI::ENV); |
51 | |
52 | # not SFIO |
53 | if (tied (*STDIN)) { |
54 | for (qw(__WARN__ __DIE__)) { |
55 | delete $SIG{$_} if ($SIG{$_} == $warn_die_handler); |
56 | } |
57 | } |
58 | |
59 | Finish ($global_request); |
60 | } |
61 | |
6ff77aff |
62 | sub flush() { |
d8cc97fb |
63 | Flush($global_request); |
64 | } |
65 | |
5baeeca7 |
66 | sub detach() { |
67 | Detach($global_request); |
68 | } |
69 | |
70 | sub attach() { |
71 | Attach($global_request); |
72 | } |
73 | |
d8cc97fb |
74 | # deprecated |
75 | sub set_exit_status { |
76 | } |
77 | |
6ff77aff |
78 | sub start_filter_data() { |
d8cc97fb |
79 | StartFilterData($global_request); |
80 | } |
81 | |
82 | $global_request = Request(); |
6c4b41b7 |
83 | $warn_die_handler = sub { print STDERR @_ unless $^S }; |
d8cc97fb |
84 | |
85 | package FCGI::Stream; |
86 | |
1b64d24d |
87 | sub PRINTF { |
88 | shift->PRINT(sprintf(shift, @_)); |
89 | } |
90 | |
96514004 |
91 | sub READLINE { |
92 | my $stream = shift; |
93 | my ($s, $c); |
94 | my $rs = $/ eq '' ? "\n\n" : $/; |
95 | my $l = substr $rs, -1; |
96 | my $len = length $rs; |
97 | |
98 | $c = $stream->GETC(); |
99 | if ($/ eq '') { |
100 | while ($c eq "\n") { |
101 | $c = $stream->GETC(); |
102 | } |
103 | } |
104 | while (defined $c) { |
105 | $s .= $c; |
106 | last if $c eq $l and substr($s, -$len) eq $rs; |
107 | $c = $stream->GETC(); |
108 | } |
109 | $s; |
110 | } |
111 | |
112 | sub OPEN { |
113 | $_[0]->CLOSE; |
e40fb02c |
114 | if (@_ == 2) { |
115 | return open($_[0], $_[1]); |
116 | } else { |
117 | my $rc; |
118 | eval("$rc = open($_[0], $_[1], $_[2])"); |
119 | die $@ if $@; |
120 | return $rc; |
121 | } |
96514004 |
122 | } |
123 | |
1b64d24d |
124 | 1; |
125 | |
d2900ee8 |
126 | =pod |
127 | |
1b64d24d |
128 | =head1 NAME |
129 | |
130 | FCGI - Fast CGI module |
131 | |
132 | =head1 SYNOPSIS |
133 | |
134 | use FCGI; |
135 | |
d2900ee8 |
136 | my $count = 0; |
137 | my $request = FCGI::Request(); |
138 | |
e40fb02c |
139 | while($request->Accept() >= 0) { |
1b64d24d |
140 | print("Content-type: text/html\r\n\r\n", ++$count); |
141 | } |
142 | |
143 | =head1 DESCRIPTION |
144 | |
145 | Functions: |
146 | |
147 | =over 4 |
148 | |
d2900ee8 |
149 | =item FCGI::Request |
150 | |
151 | Creates a request handle. It has the following optional parameters: |
152 | |
153 | =over 8 |
154 | |
155 | =item input perl file handle (default: \*STDIN) |
156 | |
157 | =item output perl file handle (default: \*STDOUT) |
158 | |
159 | =item error perl file handle (default: \*STDERR) |
160 | |
161 | These filehandles will be setup to act as input/output/error |
162 | on succesful Accept. |
163 | |
164 | =item environment hash reference (default: \%ENV) |
165 | |
166 | The hash will be populated with the environment. |
167 | |
168 | =item socket (default: 0) |
169 | |
170 | Socket to communicate with the server. |
171 | Can be the result of the OpenSocket function. |
172 | For the moment, it's the file descriptor of the socket |
173 | that should be passed. This may change in the future. |
174 | |
175 | =item flags (default: 0) |
176 | |
177 | Possible values: |
1b64d24d |
178 | |
d2900ee8 |
179 | =over 12 |
180 | |
181 | =item FCGI::FAIL_ACCEPT_ON_INTR |
182 | |
183 | If set, Accept will fail if interrupted. |
184 | It not set, it will just keep on waiting. |
185 | |
186 | =back |
187 | |
188 | =back |
189 | |
190 | Example usage: |
191 | my $req = FCGI::Request; |
192 | |
193 | or: |
194 | my %env; |
195 | my $in = new IO::Handle; |
196 | my $out = new IO::Handle; |
197 | my $err = new IO::Handle; |
198 | my $req = FCGI::Request($in, $out, $err, \%env); |
199 | |
200 | =item FCGI::OpenSocket(path, backlog) |
201 | |
202 | =item FCGI::CloseSocket(socket) |
203 | |
204 | Close a socket opened with OpenSocket. |
205 | |
206 | =item $req->Accept() |
207 | |
208 | Accepts a connection on $req, attaching the filehandles and |
209 | populating the environment hash. |
210 | Returns 0 on success. |
1b64d24d |
211 | If a connection has been accepted before, the old |
212 | one will be finished first. |
213 | |
d2900ee8 |
214 | Note that unlike with the old interface, no die and warn |
215 | handlers are installed by default. |
216 | |
217 | =item $req->Finish() |
1b64d24d |
218 | |
219 | Finishes accepted connection. |
d2900ee8 |
220 | Also detaches filehandles. |
1b64d24d |
221 | |
d2900ee8 |
222 | =item $req->Flush() |
1b64d24d |
223 | |
224 | Flushes accepted connection. |
225 | |
d2900ee8 |
226 | =item $req->Detach() |
1b64d24d |
227 | |
d2900ee8 |
228 | Temporarily detaches filehandles on an accepted connection. |
1b64d24d |
229 | |
d2900ee8 |
230 | =item $req->Attach() |
1b64d24d |
231 | |
d2900ee8 |
232 | Re-attaches filehandles on an accepted connection. |
1b64d24d |
233 | |
234 | =back |
235 | |
236 | =head1 AUTHOR |
237 | |
238 | Sven Verdoolaege <skimo@kotnet.org> |
239 | |
240 | =cut |
241 | |
242 | __END__ |