Commit | Line | Data |
1b64d24d |
1 | use Config; |
2 | |
3 | open OUT, ">FCGI.xs"; |
4 | |
5 | print "Generating FCGI.xs for Perl version $]\n"; |
6 | #unless (exists $Config{apiversion} && $Config{apiversion} >= 5.005) |
7 | unless ($] >= 5.005) { |
8 | for (qw(sv_undef diehook warnhook in_eval)) { |
9 | print OUT "#define PL_$_ $_\n" |
10 | } |
11 | } |
12 | print OUT while <DATA>; |
13 | close OUT; |
14 | __END__ |
37a69de1 |
15 | /* $Id: FCGI.PL,v 1.15 2000/07/10 09:56:49 skimo Exp $ */ |
1b64d24d |
16 | |
17 | #include "EXTERN.h" |
18 | #include "perl.h" |
19 | #include "XSUB.h" |
20 | |
b716743a |
21 | #include "fcgi_config.h" |
1b64d24d |
22 | #include "fcgiapp.h" |
5baeeca7 |
23 | #include "fastcgi.h" |
1b64d24d |
24 | |
25 | #ifndef FALSE |
26 | #define FALSE (0) |
27 | #endif |
28 | |
29 | #ifndef TRUE |
30 | #define TRUE (1) |
31 | #endif |
32 | |
b716743a |
33 | #ifndef dTHX |
34 | #define dTHX |
35 | #endif |
36 | |
1b64d24d |
37 | #ifdef USE_SFIO |
38 | typedef struct |
39 | { |
40 | Sfdisc_t disc; |
41 | FCGX_Stream *stream; |
42 | } FCGI_Disc; |
43 | |
44 | static ssize_t |
45 | sffcgiread(f, buf, n, disc) |
46 | Sfio_t* f; /* stream involved */ |
47 | Void_t* buf; /* buffer to read into */ |
48 | size_t n; /* number of bytes to read */ |
49 | Sfdisc_t* disc; /* discipline */ |
50 | { |
51 | return FCGX_GetStr(buf, n, ((FCGI_Disc *)disc)->stream); |
52 | } |
53 | |
54 | static ssize_t |
55 | sffcgiwrite(f, buf, n, disc) |
56 | Sfio_t* f; /* stream involved */ |
57 | const Void_t* buf; /* buffer to read into */ |
58 | size_t n; /* number of bytes to read */ |
59 | Sfdisc_t* disc; /* discipline */ |
60 | { |
61 | n = FCGX_PutStr(buf, n, ((FCGI_Disc *)disc)->stream); |
62 | FCGX_FFlush(((FCGI_Disc *)disc)->stream); |
63 | return n; |
64 | } |
65 | |
66 | Sfdisc_t * |
67 | sfdcnewfcgi(stream) |
68 | FCGX_Stream *stream; |
69 | { |
70 | FCGI_Disc* disc; |
71 | |
72 | New(1000,disc,1,FCGI_Disc); |
73 | if (!disc) return (Sfdisc_t *)disc; |
74 | |
75 | disc->disc.exceptf = (Sfexcept_f)NULL; |
76 | disc->disc.seekf = (Sfseek_f)NULL; |
77 | disc->disc.readf = sffcgiread; |
78 | disc->disc.writef = sffcgiwrite; |
79 | disc->stream = stream; |
80 | return (Sfdisc_t *)disc; |
81 | } |
82 | |
83 | Sfdisc_t * |
84 | sfdcdelfcgi(disc) |
85 | Sfdisc_t* disc; |
86 | { |
87 | Safefree(disc); |
88 | return 0; |
89 | } |
90 | #endif |
91 | |
b716743a |
92 | #if defined(USE_LOCKING) && defined(USE_THREADS) |
93 | static perl_mutex accept_mutex; |
94 | #endif |
95 | |
90a18d65 |
96 | typedef struct FCGP_Request { |
5baeeca7 |
97 | int accepted; |
b716743a |
98 | int bound; |
90a18d65 |
99 | SV* svin; |
100 | SV* svout; |
101 | SV* sverr; |
eede4b76 |
102 | GV* gv[3]; |
34bfd355 |
103 | HV* hvEnv; |
90a18d65 |
104 | FCGX_Request* requestPtr; |
2c38ecd7 |
105 | #ifdef USE_SFIO |
106 | int sfcreated[3]; |
5baeeca7 |
107 | IO* io[3]; |
2c38ecd7 |
108 | #endif |
90a18d65 |
109 | } FCGP_Request; |
110 | |
5baeeca7 |
111 | static void FCGI_Finish(FCGP_Request* request); |
112 | |
1b64d24d |
113 | static int |
90a18d65 |
114 | FCGI_Flush(FCGP_Request* request) |
1b64d24d |
115 | { |
b716743a |
116 | dTHX; |
117 | |
118 | if(!request->bound) { |
1b64d24d |
119 | return; |
120 | } |
121 | #ifdef USE_SFIO |
2c38ecd7 |
122 | sfsync(IoOFP(GvIOp(request->gv[1]))); |
123 | sfsync(IoOFP(GvIOp(request->gv[2]))); |
1b64d24d |
124 | #else |
90a18d65 |
125 | FCGX_FFlush((FCGX_Stream *) SvIV((SV*) SvRV(request->svout))); |
126 | FCGX_FFlush((FCGX_Stream *) SvIV((SV*) SvRV(request->sverr))); |
1b64d24d |
127 | #endif |
128 | } |
129 | |
34bfd355 |
130 | static void |
131 | FCGI_UndoBinding(FCGP_Request* request) |
132 | { |
b716743a |
133 | dTHX; |
2c38ecd7 |
134 | |
135 | #ifdef USE_SFIO |
5baeeca7 |
136 | sfdcdelfcgi(sfdisc(IoIFP(request->io[0]), SF_POPDISC)); |
137 | sfdcdelfcgi(sfdisc(IoOFP(request->io[1]), SF_POPDISC)); |
138 | sfdcdelfcgi(sfdisc(IoOFP(request->io[2]), SF_POPDISC)); |
34bfd355 |
139 | #else |
34bfd355 |
140 | sv_unmagic((SV *)request->gv[0], 'q'); |
141 | sv_unmagic((SV *)request->gv[1], 'q'); |
142 | sv_unmagic((SV *)request->gv[2], 'q'); |
143 | #endif |
b716743a |
144 | request->bound = FALSE; |
145 | } |
146 | |
147 | static void |
5baeeca7 |
148 | FCGI_Bind(FCGP_Request* request) |
149 | { |
150 | dTHX; |
151 | |
152 | #ifdef USE_SFIO |
37a69de1 |
153 | sfdisc(IoIFP(request->io[0]), sfdcnewfcgi(request->requestPtr->in)); |
154 | sfdisc(IoOFP(request->io[1]), sfdcnewfcgi(request->requestPtr->out)); |
155 | sfdisc(IoOFP(request->io[2]), sfdcnewfcgi(request->requestPtr->err)); |
5baeeca7 |
156 | #else |
157 | sv_magic((SV *)request->gv[1], request->svout, 'q', Nullch, 0); |
158 | sv_magic((SV *)request->gv[2], request->sverr, 'q', Nullch, 0); |
159 | sv_magic((SV *)request->gv[0], request->svin, 'q', Nullch, 0); |
160 | #endif |
161 | request->bound = TRUE; |
162 | } |
163 | |
164 | static void |
b716743a |
165 | populate_env(envp, hv) |
166 | char **envp; |
167 | HV *hv; |
168 | { |
169 | int i; |
170 | char *p, *p1; |
171 | SV *sv; |
172 | dTHX; |
173 | |
6ff77aff |
174 | hv_clear(hv); |
b716743a |
175 | for(i = 0; ; i++) { |
176 | if((p = envp[i]) == NULL) { |
177 | break; |
178 | } |
179 | p1 = strchr(p, '='); |
180 | assert(p1 != NULL); |
181 | sv = newSVpv(p1 + 1, 0); |
182 | /* call magic for this value ourselves */ |
183 | hv_store(hv, p, p1 - p, sv, 0); |
184 | SvSETMAGIC(sv); |
185 | } |
34bfd355 |
186 | } |
187 | |
1b64d24d |
188 | static int |
cebfd7c4 |
189 | FCGI_Accept(FCGP_Request* request) |
1b64d24d |
190 | { |
5baeeca7 |
191 | static int isCGI = -1; /* -1: not checked; 0: FCGI; 1: FCGI */ |
192 | |
193 | int req_isCGI = |
194 | request->requestPtr->listen_sock == FCGI_LISTENSOCK_FILENO ? |
195 | isCGI : 0; |
196 | |
794c66be |
197 | dTHX; |
198 | |
5baeeca7 |
199 | if(req_isCGI == -1) { |
1b64d24d |
200 | /* |
201 | * First call to FCGI_Accept. Is application running |
202 | * as FastCGI or as CGI? |
203 | */ |
5baeeca7 |
204 | req_isCGI = isCGI = FCGX_IsCGI(); |
205 | } else if(req_isCGI) { |
1b64d24d |
206 | /* |
207 | * Not first call to FCGI_Accept and running as CGI means |
208 | * application is done. |
209 | */ |
210 | return(EOF); |
eede4b76 |
211 | } |
5baeeca7 |
212 | if(!req_isCGI) { |
37a69de1 |
213 | #ifdef USE_SFIO |
214 | int i; |
215 | #endif |
6ff77aff |
216 | FCGX_Request *fcgx_req = request->requestPtr; |
b716743a |
217 | int acceptResult; |
218 | |
5baeeca7 |
219 | FCGI_Finish(request); |
b716743a |
220 | #if defined(USE_LOCKING) && defined(USE_THREADS) |
221 | MUTEX_LOCK(&accept_mutex); |
222 | #endif |
6ff77aff |
223 | acceptResult = FCGX_Accept_r(fcgx_req); |
b716743a |
224 | #if defined(USE_LOCKING) && defined(USE_THREADS) |
225 | MUTEX_UNLOCK(&accept_mutex); |
226 | #endif |
1b64d24d |
227 | if(acceptResult < 0) { |
228 | return acceptResult; |
229 | } |
d8cc97fb |
230 | |
6ff77aff |
231 | populate_env(fcgx_req->envp, request->hvEnv); |
d8cc97fb |
232 | |
1b64d24d |
233 | #ifdef USE_SFIO |
2c38ecd7 |
234 | for (i = 0; i < 3; ++i) { |
5baeeca7 |
235 | request->io[i] = GvIOn(request->gv[i]); |
236 | if (!(i == 0 ? IoIFP(request->io[i]) |
237 | : IoOFP(request->io[i]))) { |
238 | IoIFP(request->io[i]) = sftmp(0); |
239 | /*IoIFP(request->io[i]) = sfnew(NULL, NULL, SF_UNBOUND, 0, |
2c38ecd7 |
240 | SF_STRING | (i ? SF_WRITE : SF_READ));*/ |
5baeeca7 |
241 | if (i != 0) |
242 | IoOFP(request->io[i]) = IoIFP(request->io[i]); |
2c38ecd7 |
243 | request->sfcreated[i] = TRUE; |
244 | } |
245 | } |
1b64d24d |
246 | #else |
90a18d65 |
247 | if (!request->svout) { |
eede4b76 |
248 | newSVrv(request->svout = newSV(0), "FCGI::Stream"); |
eede4b76 |
249 | newSVrv(request->sverr = newSV(0), "FCGI::Stream"); |
eede4b76 |
250 | newSVrv(request->svin = newSV(0), "FCGI::Stream"); |
1b64d24d |
251 | } |
6ff77aff |
252 | sv_setiv(SvRV(request->svout), (IV) fcgx_req->out); |
253 | sv_setiv(SvRV(request->sverr), (IV) fcgx_req->err); |
254 | sv_setiv(SvRV(request->svin), (IV) fcgx_req->in); |
1b64d24d |
255 | #endif |
5baeeca7 |
256 | FCGI_Bind(request); |
257 | request->accepted = TRUE; |
1b64d24d |
258 | } |
1b64d24d |
259 | return 0; |
260 | } |
261 | |
262 | static void |
90a18d65 |
263 | FCGI_Finish(FCGP_Request* request) |
1b64d24d |
264 | { |
37a69de1 |
265 | #ifdef USE_SFIO |
266 | int i; |
267 | #endif |
b716743a |
268 | dTHX; |
269 | |
5baeeca7 |
270 | if(!request->accepted) { |
1b64d24d |
271 | return; |
272 | } |
5baeeca7 |
273 | |
274 | if (request->bound) { |
275 | FCGI_UndoBinding(request); |
276 | } |
277 | #ifdef USE_SFIO |
278 | for (i = 0; i < 3; ++i) { |
279 | if (request->sfcreated[i]) { |
280 | sfclose(IoIFP(request->io[i])); |
281 | IoIFP(request->io[i]) = IoOFP(request->io[i]) = Nullfp; |
282 | request->sfcreated[i] = FALSE; |
283 | } |
284 | } |
285 | #endif |
286 | FCGX_Finish_r(request->requestPtr); |
287 | request->accepted = FALSE; |
1b64d24d |
288 | } |
289 | |
290 | static int |
90a18d65 |
291 | FCGI_StartFilterData(FCGP_Request* request) |
292 | { |
6ff77aff |
293 | return request->requestPtr->in ? |
294 | FCGX_StartFilterData(request->requestPtr->in) : -1; |
90a18d65 |
295 | } |
296 | |
297 | static FCGP_Request * |
6ff77aff |
298 | FCGI_Request(in, out, err, env, socket, flags) |
299 | GV* in; |
300 | GV* out; |
301 | GV* err; |
302 | HV* env; |
303 | int socket; |
304 | int flags; |
1b64d24d |
305 | { |
90a18d65 |
306 | FCGX_Request* fcgx_req; |
307 | FCGP_Request* req; |
308 | |
309 | Newz(551, fcgx_req, 1, FCGX_Request); |
6ff77aff |
310 | FCGX_InitRequest(fcgx_req, socket, flags); |
90a18d65 |
311 | Newz(551, req, 1, FCGP_Request); |
312 | req->requestPtr = fcgx_req; |
6ff77aff |
313 | req->gv[0] = in; |
314 | req->gv[1] = out; |
315 | req->gv[2] = err; |
316 | req->hvEnv = env; |
90a18d65 |
317 | |
318 | return req; |
1b64d24d |
319 | } |
320 | |
321 | static void |
90a18d65 |
322 | FCGI_Release_Request(FCGP_Request *req) |
1b64d24d |
323 | { |
6ff77aff |
324 | FCGI_Finish(req); |
90a18d65 |
325 | Safefree(req->requestPtr); |
326 | Safefree(req); |
1b64d24d |
327 | } |
328 | |
34bfd355 |
329 | static void |
b716743a |
330 | FCGI_Init() |
34bfd355 |
331 | { |
b716743a |
332 | #if defined(USE_LOCKING) && defined(USE_THREADS) |
333 | dTHX; |
34bfd355 |
334 | |
b716743a |
335 | MUTEX_INIT(&accept_mutex); |
336 | #endif |
34bfd355 |
337 | } |
338 | |
eede4b76 |
339 | typedef FCGX_Stream * FCGI__Stream; |
340 | typedef FCGP_Request * FCGI; |
d8cc97fb |
341 | typedef GV* GLOBREF; |
342 | typedef HV* HASHREF; |
1b64d24d |
343 | |
344 | MODULE = FCGI PACKAGE = FCGI |
345 | |
b716743a |
346 | BOOT: |
347 | FCGI_Init(); |
eede4b76 |
348 | |
349 | SV * |
6ff77aff |
350 | RequestX(in, out, err, env, socket, flags) |
351 | GLOBREF in; |
352 | GLOBREF out; |
353 | GLOBREF err; |
354 | HASHREF env; |
355 | int socket; |
356 | int flags; |
eede4b76 |
357 | |
6ff77aff |
358 | PROTOTYPE: ***$$$ |
eede4b76 |
359 | CODE: |
6ff77aff |
360 | RETVAL = sv_setref_pv(newSV(0), "FCGI", |
361 | FCGI_Request(in, out, err, env, socket, flags)); |
eede4b76 |
362 | |
363 | OUTPUT: |
364 | RETVAL |
365 | |
6b312a77 |
366 | int |
367 | OpenSocket(path, backlog) |
368 | char* path; |
369 | int backlog; |
370 | |
371 | PROTOTYPE: $$ |
372 | CODE: |
373 | RETVAL = FCGX_OpenSocket(path, backlog); |
374 | OUTPUT: |
375 | RETVAL |
376 | |
377 | void |
378 | CloseSocket(socket) |
379 | int socket; |
380 | |
381 | PROTOTYPE: $ |
382 | CODE: |
383 | close(socket); |
eede4b76 |
384 | |
385 | int |
6ff77aff |
386 | Accept(request) |
eede4b76 |
387 | |
d8cc97fb |
388 | FCGI request; |
eede4b76 |
389 | |
6ff77aff |
390 | PROTOTYPE: $ |
eede4b76 |
391 | |
392 | CODE: |
d8cc97fb |
393 | RETVAL = FCGI_Accept(request); |
eede4b76 |
394 | |
eede4b76 |
395 | OUTPUT: |
396 | RETVAL |
397 | |
398 | |
399 | void |
d8cc97fb |
400 | Finish(request) |
eede4b76 |
401 | |
d8cc97fb |
402 | FCGI request; |
eede4b76 |
403 | |
d8cc97fb |
404 | PROTOTYPE: $ |
eede4b76 |
405 | |
406 | CODE: |
eede4b76 |
407 | { |
408 | /* |
eede4b76 |
409 | * Finish the request. |
410 | */ |
411 | FCGI_Finish(request); |
412 | } |
413 | |
414 | |
415 | void |
d8cc97fb |
416 | Flush(request) |
eede4b76 |
417 | |
d8cc97fb |
418 | FCGI request; |
eede4b76 |
419 | |
d8cc97fb |
420 | PROTOTYPE: $ |
eede4b76 |
421 | |
422 | CODE: |
eede4b76 |
423 | FCGI_Flush(request); |
424 | |
5baeeca7 |
425 | void |
426 | Detach(request) |
427 | |
428 | FCGI request; |
429 | |
430 | PROTOTYPE: $ |
431 | |
432 | CODE: |
433 | if (request->accepted && request->bound) |
434 | FCGI_UndoBinding(request); |
435 | |
436 | void |
437 | Attach(request) |
438 | |
439 | FCGI request; |
440 | |
441 | PROTOTYPE: $ |
442 | |
443 | CODE: |
444 | if (request->accepted && !request->bound) |
445 | FCGI_Bind(request); |
446 | |
eede4b76 |
447 | |
448 | int |
d8cc97fb |
449 | StartFilterData(request) |
eede4b76 |
450 | |
d8cc97fb |
451 | FCGI request; |
eede4b76 |
452 | |
d8cc97fb |
453 | PROTOTYPE: $ |
eede4b76 |
454 | |
455 | CODE: |
eede4b76 |
456 | RETVAL = FCGI_StartFilterData(request); |
457 | |
458 | OUTPUT: |
459 | RETVAL |
460 | |
eede4b76 |
461 | void |
462 | DESTROY(request) |
463 | FCGI request; |
464 | |
465 | CODE: |
466 | FCGI_Release_Request(request); |
467 | |
468 | |
469 | |
470 | MODULE = FCGI PACKAGE = FCGI::Stream |
90a18d65 |
471 | |
1b64d24d |
472 | #ifndef USE_SFIO |
1b64d24d |
473 | |
474 | void |
475 | PRINT(stream, ...) |
eede4b76 |
476 | FCGI::Stream stream; |
1b64d24d |
477 | |
478 | PREINIT: |
479 | int n; |
480 | |
481 | CODE: |
482 | for (n = 1; n < items; ++n) { |
483 | STRLEN len; |
484 | register char *tmps = (char *)SvPV(ST(n),len); |
485 | FCGX_PutStr(tmps, len, stream); |
486 | } |
487 | if (SvTRUEx(perl_get_sv("|", FALSE))) |
488 | FCGX_FFlush(stream); |
489 | |
490 | int |
491 | WRITE(stream, bufsv, len, ...) |
eede4b76 |
492 | FCGI::Stream stream; |
1b64d24d |
493 | SV * bufsv; |
494 | int len; |
495 | |
496 | PREINIT: |
497 | int offset; |
498 | char * buf; |
499 | STRLEN blen; |
500 | int n; |
501 | |
502 | CODE: |
503 | offset = (items == 4) ? (int)SvIV(ST(3)) : 0; |
504 | buf = SvPV(bufsv, blen); |
505 | if (offset < 0) offset += blen; |
506 | if (len > blen - offset) |
507 | len = blen - offset; |
508 | if (offset < 0 || offset >= blen || |
509 | (n = FCGX_PutStr(buf+offset, len, stream)) < 0) |
510 | ST(0) = &PL_sv_undef; |
511 | else { |
512 | ST(0) = sv_newmortal(); |
513 | sv_setpvf(ST(0), "%c", n); |
514 | } |
515 | |
516 | int |
517 | READ(stream, bufsv, len, ...) |
eede4b76 |
518 | FCGI::Stream stream; |
1b64d24d |
519 | SV * bufsv; |
520 | int len; |
521 | |
522 | PREINIT: |
523 | int offset; |
524 | char * buf; |
525 | |
526 | CODE: |
527 | offset = (items == 4) ? (int)SvIV(ST(3)) : 0; |
528 | if (! SvOK(bufsv)) |
529 | sv_setpvn(bufsv, "", 0); |
530 | buf = SvGROW(bufsv, len+offset+1); |
531 | len = FCGX_GetStr(buf+offset, len, stream); |
532 | SvCUR_set(bufsv, len+offset); |
533 | *SvEND(bufsv) = '\0'; |
534 | (void)SvPOK_only(bufsv); |
535 | SvSETMAGIC(bufsv); |
536 | RETVAL = len; |
537 | |
538 | OUTPUT: |
539 | RETVAL |
540 | |
541 | SV * |
542 | GETC(stream) |
eede4b76 |
543 | FCGI::Stream stream; |
1b64d24d |
544 | |
545 | PREINIT: |
546 | int retval; |
547 | |
548 | CODE: |
549 | if ((retval = FCGX_GetChar(stream)) != -1) { |
550 | ST(0) = sv_newmortal(); |
551 | sv_setpvf(ST(0), "%c", retval); |
552 | } else ST(0) = &PL_sv_undef; |
553 | |
554 | bool |
555 | CLOSE(stream) |
eede4b76 |
556 | FCGI::Stream stream; |
1b64d24d |
557 | |
5baeeca7 |
558 | # ALIAS: |
559 | # DESTROY = 1 |
1b64d24d |
560 | |
561 | CODE: |
562 | RETVAL = FCGX_FClose(stream) != -1; |
563 | |
564 | OUTPUT: |
565 | RETVAL |
566 | |
567 | #endif |