Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 766 lines (669 sloc) 20.086 kB
f61f188 @spiritloose Initial commit
authored
1 /*
2 * Copyright 2009 Jiro Nishiguchi <jiro@cpan.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
03fb93e @mattn win32 porting.
mattn authored
16 #ifdef _WIN32
17 /* avoid to define duplicate definition of uid_t/gid_t in perl/CORE.h */
18 #define uid_t _uid_t
19 #define gid_t _gid_t
20 #endif
f61f188 @spiritloose Initial commit
authored
21 #include "httpd.h"
22 #include "http_log.h"
23 #include "http_config.h"
24 #include "http_protocol.h"
25 #include "util_script.h"
26 #include "ap_config.h"
27 #include "ap_mpm.h"
377b40d @spiritloose sendfile support. closes #1
authored
28 #include "apr_file_io.h"
29 #include "apr_file_info.h"
03fb93e @mattn win32 porting.
mattn authored
30 #include "apr_buckets.h"
f61f188 @spiritloose Initial commit
authored
31 #include "apr_strings.h"
c115693 @spiritloose 'do $psgi_app' at post_config
authored
32 #include "apr_hash.h"
f61f188 @spiritloose Initial commit
authored
33
03fb93e @mattn win32 porting.
mattn authored
34 #ifdef _WIN32
35 /* use perl's uid_t/gid_t. disable apr's macros. */
36 #undef uid_t
37 #undef gid_t
38 #undef exit
39 #endif
40
f61f188 @spiritloose Initial commit
authored
41 #include "EXTERN.h"
42 #include "perl.h"
43 #include "XSUB.h"
44 #define NEED_eval_pv
45 #define NEED_newRV_noinc
46 #define NEED_sv_2pv_flags
47 #include "ppport.h"
48
fae83c4 @spiritloose oops, define CLEAR_ERRSV after #include.
authored
49 #ifndef CLEAR_ERRSV /* should support in ppport.h ? */
50 #define CLEAR_ERRSV() STMT_START { sv_setpvn(ERRSV,"",0); if (SvMAGICAL(ERRSV)) { mg_free(ERRSV); } SvPOK_only(ERRSV); } STMT_END
51 #endif /* CLEAR_ERRSV */
52
03fb93e @mattn win32 porting.
mattn authored
53 #ifdef _WIN32
54 /* no use perl compatible macros. it break apr's structure. ex: bucket->link */
55 #undef link
56 #undef read
57 #endif
58
098e77e @spiritloose ap_add_version_component
authored
59 #define PSGI_HANDLER_NAME "psgi"
60
f61f188 @spiritloose Initial commit
authored
61 #ifdef DEBUG
62 #define TRACE(...) ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, NULL, __VA_ARGS__)
63 #endif
64
65 module AP_MODULE_DECLARE_DATA psgi_module;
66
67 typedef struct {
ace1272 @spiritloose Make PerlInterpreter persistent
authored
68 char *file;
430ccf9 @fujiwara fix PATH_INFO and SCRIPT_NAME
fujiwara authored
69 char *location;
f61f188 @spiritloose Initial commit
authored
70 } psgi_dir_config;
71
c115693 @spiritloose 'do $psgi_app' at post_config
authored
72 static PerlInterpreter *perlinterp = NULL;
73
74 static apr_hash_t *app_mapping = NULL;
75
76 static apr_array_header_t *psgi_apps = NULL;
e284234 @spiritloose Call PERL_SYS_INIT3,PERL_SYS_TERM once per process
authored
77
f61f188 @spiritloose Initial commit
authored
78 static void server_error(request_rec *r, const char *fmt, ...)
79 {
80 va_list argp;
81 const char *msg;
82 va_start(argp, fmt);
83 msg = apr_pvsprintf(r->pool, fmt, argp);
84 va_end(argp);
85 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r->server, "%s", msg);
86 }
87
88 EXTERN_C void xs_init (pTHX);
89
90 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
91
92 XS(ModPSGI_exit);
93 XS(ModPSGI_exit)
94 {
95 dXSARGS;
22b6017 @spiritloose suppress warnings
authored
96 I32 exitval = items > 0 ? SvIV(ST(0)) : 0;
97 croak("exit(%d) was called", exitval);
e2c9fd5 @spiritloose use XSRETURN_*
authored
98 XSRETURN_UNDEF;
f61f188 @spiritloose Initial commit
authored
99 }
100
101 XS(ModPSGI_Input_read);
102 XS(ModPSGI_Input_read)
103 {
104 dXSARGS;
105 SV *self = ST(0);
106 SV *buf = ST(1);
107 request_rec *r = (request_rec *) mg_find(SvRV(self), PERL_MAGIC_ext)->mg_obj;
108 apr_size_t len = SvIV(ST(2));
109 apr_bucket_brigade *bb;
7733f9b @spiritloose Refactored ModPSGI_Input_read
authored
110 apr_size_t nread = 0;
111 char *pv, *tmp;
f61f188 @spiritloose Initial commit
authored
112 int eos = 0;
113
3b98db7 @spiritloose croak if offset was given
authored
114 if (items >= 4) {
115 croak("$env->{'psgi.input'}->read: mod_psgi can't handle offset");
116 }
117
7733f9b @spiritloose Refactored ModPSGI_Input_read
authored
118 if (len <= 0) {
e2c9fd5 @spiritloose use XSRETURN_*
authored
119 XSRETURN_IV(0);
f61f188 @spiritloose Initial commit
authored
120 }
121
7733f9b @spiritloose Refactored ModPSGI_Input_read
authored
122 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
123 if (bb == NULL) {
124 server_error(r, "apr_brigade_create() failed");
125 XSRETURN_UNDEF;
126 }
127
128 pv = apr_pcalloc(r->pool, len);
129 tmp = pv;
130
131 do {
132 apr_size_t read;
133 apr_status_t rc;
134
135 rc = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES, APR_BLOCK_READ, len);
136 if (rc != APR_SUCCESS) {
137 apr_brigade_destroy(bb);
138 server_error(r, "ap_get_brigade() failed");
139 XSRETURN_UNDEF;
140 }
141
142 if (APR_BRIGADE_EMPTY(bb)) {
143 apr_brigade_destroy(bb);
144 server_error(r, "APR_BRIGADE_EMPTY");
145 XSRETURN_UNDEF;
146 }
147
148 if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
f61f188 @spiritloose Initial commit
authored
149 eos = 1;
150 }
7733f9b @spiritloose Refactored ModPSGI_Input_read
authored
151
152 read = len;
153 rc = apr_brigade_flatten(bb, tmp, &read);
154 if (rc != APR_SUCCESS) {
155 apr_brigade_destroy(bb);
156 server_error(r, "apr_brigade_flatten() failed");
157 XSRETURN_UNDEF;
f61f188 @spiritloose Initial commit
authored
158 }
159
7733f9b @spiritloose Refactored ModPSGI_Input_read
authored
160 nread += read;
161 tmp += read;
162 len -= read;
163 apr_brigade_cleanup(bb);
164 } while (len > 0 && !eos);
165
166 apr_brigade_destroy(bb);
167 sv_setpvn(buf, pv, nread);
e2c9fd5 @spiritloose use XSRETURN_*
authored
168 XSRETURN_IV(nread);
f61f188 @spiritloose Initial commit
authored
169 }
170
171 XS(ModPSGI_Errors_print);
172 XS(ModPSGI_Errors_print)
173 {
174 dXSARGS;
175 SV *self = ST(0);
22b6017 @spiritloose suppress warnings
authored
176 SV *msg = NULL;
f61f188 @spiritloose Initial commit
authored
177 request_rec *r = (request_rec *) mg_find(SvRV(self), PERL_MAGIC_ext)->mg_obj;
22b6017 @spiritloose suppress warnings
authored
178 int i;
179 for (i = 1; i < items; i++) {
180 msg = ST(i);
181 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r->server, "%s", SvPV_nolen(msg));
182 }
e2c9fd5 @spiritloose use XSRETURN_*
authored
183 XSRETURN_IV(1);
f61f188 @spiritloose Initial commit
authored
184 }
185
186 EXTERN_C void
187 xs_init(pTHX)
188 {
189 char *file = __FILE__;
190 dXSUB_SYS;
191
192 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
22b6017 @spiritloose suppress warnings
authored
193 newXSproto("ModPSGI::exit", ModPSGI_exit, file, ";$");
f61f188 @spiritloose Initial commit
authored
194 newXSproto("ModPSGI::Input::read", ModPSGI_Input_read, file, "$$$;$");
22b6017 @spiritloose suppress warnings
authored
195 newXSproto("ModPSGI::Errors::print", ModPSGI_Errors_print, file, "$@");
f61f188 @spiritloose Initial commit
authored
196 }
197
198 static int copy_env(void *rec, const char *key, const char *val)
199 {
fe98cae @spiritloose use dTHX
authored
200 dTHX;
f61f188 @spiritloose Initial commit
authored
201 HV *env = (HV *) rec;
202 hv_store(env, key, strlen(key), newSVpv(val, 0), 0);
203 return 1;
204 }
205
cad20bf @spiritloose Thanks to fujiwara. fixes #4
authored
206 static SV *make_env(request_rec *r, psgi_dir_config *c)
f61f188 @spiritloose Initial commit
authored
207 {
fe98cae @spiritloose use dTHX
authored
208 dTHX;
f61f188 @spiritloose Initial commit
authored
209 HV *env;
210 AV *version;
cad20bf @spiritloose Thanks to fujiwara. fixes #4
authored
211 char *url_scheme, *script_name, *vpath, *path_info;
f61f188 @spiritloose Initial commit
authored
212 SV *input, *errors;
213
214 env = newHV();
215
216 ap_add_cgi_vars(r);
217 ap_add_common_vars(r);
430ccf9 @fujiwara fix PATH_INFO and SCRIPT_NAME
fujiwara authored
218
cad20bf @spiritloose Thanks to fujiwara. fixes #4
authored
219 /* fix SCRIPT_NAME & PATH_INFO */
220 if (c->location == NULL || strcmp(c->location, "/") == 0) {
221 script_name = "";
222 } else {
223 script_name = c->location;
f61f188 @spiritloose Initial commit
authored
224 }
430ccf9 @fujiwara fix PATH_INFO and SCRIPT_NAME
fujiwara authored
225 vpath = apr_pstrcat(r->pool,
cad20bf @spiritloose Thanks to fujiwara. fixes #4
authored
226 apr_table_get(r->subprocess_env, "SCRIPT_NAME"),
227 apr_table_get(r->subprocess_env, "PATH_INFO"),
228 NULL);
229 path_info = &vpath[strlen(script_name)];
430ccf9 @fujiwara fix PATH_INFO and SCRIPT_NAME
fujiwara authored
230 apr_table_set(r->subprocess_env, "PATH_INFO", path_info);
cad20bf @spiritloose Thanks to fujiwara. fixes #4
authored
231 apr_table_set(r->subprocess_env, "SCRIPT_NAME", script_name);
430ccf9 @fujiwara fix PATH_INFO and SCRIPT_NAME
fujiwara authored
232
f61f188 @spiritloose Initial commit
authored
233 apr_table_do(copy_env, env, r->subprocess_env, NULL);
234
235 version = newAV();
236 av_push(version, newSViv(1));
237 av_push(version, newSViv(0));
238 hv_store(env, "psgi.version", 12, newRV_inc((SV *) version), 0);
239
240 url_scheme = apr_table_get(r->subprocess_env, "HTTPS") == NULL ? "http" : "https";
241 hv_store(env, "psgi.url_scheme", 15, newSVpv(url_scheme, 0), 0);
242
243 input = newRV_noinc(newSV(0));
244 sv_magic(SvRV(input), NULL, PERL_MAGIC_ext, NULL, 0);
245 mg_find(SvRV(input), PERL_MAGIC_ext)->mg_obj = (void *) r;
246 sv_bless(input, gv_stashpv("ModPSGI::Input", 1));
247 hv_store(env, "psgi.input", 10, input, 0);
248
249 errors = newRV_noinc(newSV(0));
250 sv_magic(SvRV(errors), NULL, PERL_MAGIC_ext, NULL, 0);
251 mg_find(SvRV(errors), PERL_MAGIC_ext)->mg_obj = (void *) r;
252 sv_bless(errors, gv_stashpv("ModPSGI::Errors", 1));
253 hv_store(env, "psgi.errors", 11, errors, 0);
254
255 hv_store(env, "psgi.multithread", 16, newSViv(0), 0);
256 hv_store(env, "psgi.multiprocess", 17, newSViv(1), 0);
257 hv_store(env, "psgi.run_once", 13, newSViv(1), 0);
3377795 @spiritloose psgi.async -> psgi.nonblocking
authored
258 hv_store(env, "psgi.nonblocking", 16, newSViv(0), 0);
f61f188 @spiritloose Initial commit
authored
259
260 return newRV_inc((SV *) env);
261 }
262
263 static SV *run_app(request_rec *r, SV *app, SV *env)
264 {
fe98cae @spiritloose use dTHX
authored
265 dTHX;
f61f188 @spiritloose Initial commit
authored
266 int count;
267 SV *res;
268 dSP;
269 ENTER;
270 SAVETMPS;
271 PUSHMARK(SP) ;
272 XPUSHs(sv_2mortal(env));
273 PUTBACK;
274
0e90b5f @spiritloose use G_KEEPERR and CLEAR_ERRSV() to catch errors
authored
275 count = call_sv(app, G_EVAL|G_SCALAR|G_KEEPERR);
f61f188 @spiritloose Initial commit
authored
276 SPAGAIN;
277 if (SvTRUE(ERRSV)) {
278 res = NULL;
279 server_error(r, "%s", SvPV_nolen(ERRSV));
0e90b5f @spiritloose use G_KEEPERR and CLEAR_ERRSV() to catch errors
authored
280 CLEAR_ERRSV();
22b6017 @spiritloose suppress warnings
authored
281 (void) POPs;
f61f188 @spiritloose Initial commit
authored
282 } else if (count > 0) {
283 res = POPs;
284 SvREFCNT_inc(res);
285 } else {
286 res = NULL;
287 }
288 PUTBACK;
289 FREETMPS;
290 LEAVE;
291 return res;
292 }
293
294 static int output_status(request_rec *r, SV *status)
295 {
fe98cae @spiritloose use dTHX
authored
296 dTHX;
f61f188 @spiritloose Initial commit
authored
297 int s = SvIV(status);
298 if (s < 100) {
299 server_error(r, "invalid response status %d", s);
300 return HTTP_INTERNAL_SERVER_ERROR;
301 }
302 r->status = s;
303 return OK;
304 }
305
306 static int output_headers(request_rec *r, AV *headers)
307 {
fe98cae @spiritloose use dTHX
authored
308 dTHX;
f61f188 @spiritloose Initial commit
authored
309 SV *key_sv, *val_sv;
947bdac @spiritloose use ap_set_content_length
authored
310 char *key;
311
312 r->content_type = NULL;
f61f188 @spiritloose Initial commit
authored
313 while (av_len(headers) > -1) {
314 key_sv = av_shift(headers);
315 val_sv = av_shift(headers);
316 if (key_sv == NULL || val_sv == NULL) break;
317 key = SvPV_nolen(key_sv);
aab7f6a @spiritloose Do not check header value
authored
318 if (strcmp(key, "Content-Type") == 0) {
947bdac @spiritloose use ap_set_content_length
authored
319 r->content_type = apr_pstrdup(r->pool, SvPV_nolen(val_sv));
320 } else if (strcmp(key, "Content-Length") == 0) {
321 ap_set_content_length(r, SvIV(val_sv));
f61f188 @spiritloose Initial commit
authored
322 } else if (strcmp(key, "Status") == 0) {
323 server_error(r, "headers must not contain a Status");
324 return HTTP_INTERNAL_SERVER_ERROR;
325 } else {
947bdac @spiritloose use ap_set_content_length
authored
326 apr_table_add(r->headers_out, key, SvPV_nolen(val_sv));
f61f188 @spiritloose Initial commit
authored
327 }
32a1013 @spiritloose app runs in the new scope
authored
328 SvREFCNT_dec(key_sv);
329 SvREFCNT_dec(val_sv);
f61f188 @spiritloose Initial commit
authored
330 }
331 return OK;
332 }
333
334 static int respond_to(SV *obj, const char *method)
335 {
fe98cae @spiritloose use dTHX
authored
336 dTHX;
f61f188 @spiritloose Initial commit
authored
337 int res;
338 dSP;
339 ENTER;
340 SAVETMPS;
341 PUSHMARK(SP);
342 XPUSHs(obj);
343 XPUSHs(sv_2mortal(newSVpv(method, 0)));
344 PUTBACK;
345
346 call_method("can", G_SCALAR);
347 SPAGAIN;
348 res = SvROK(POPs);
349 PUTBACK;
350 FREETMPS;
351 LEAVE;
352 return res;
353 }
354
355 static int output_body_ary(request_rec *r, AV *bodys)
356 {
fe98cae @spiritloose use dTHX
authored
357 dTHX;
f61f188 @spiritloose Initial commit
authored
358 SV **body;
359 I32 i;
360 I32 lastidx;
361 char *buf;
362 STRLEN len;
03fb93e @mattn win32 porting.
mattn authored
363 apr_off_t clen = 0;
f61f188 @spiritloose Initial commit
authored
364
365 lastidx = av_len(bodys);
366 for (i = 0; i <= lastidx; i++) {
367 body = av_fetch(bodys, i, 0);
368 if (SvOK(*body)) {
369 buf = SvPV(*body, len);
370 ap_rwrite(buf, len, r);
371 clen += len;
372 }
373 }
947bdac @spiritloose use ap_set_content_length
authored
374 if (clen > 0) {
375 ap_set_content_length(r, clen);
376 }
f61f188 @spiritloose Initial commit
authored
377 return OK;
378 }
379
380 static int output_body_obj(request_rec *r, SV *obj, int type)
381 {
fe98cae @spiritloose use dTHX
authored
382 dTHX;
22b6017 @spiritloose suppress warnings
authored
383 SV *buf_sv;
f61f188 @spiritloose Initial commit
authored
384 apr_off_t clen = 0;
385 STRLEN len;
03fb93e @mattn win32 porting.
mattn authored
386 dSP;
f61f188 @spiritloose Initial commit
authored
387 char *buf;
388 int count;
389
390 if (type == SVt_PVMG && !respond_to(obj, "getline")) {
391 server_error(r, "response body object must be able to getline");
392 return HTTP_INTERNAL_SERVER_ERROR;
393 }
394
395 ENTER;
396 SAVETMPS;
397 SAVESPTR(PL_rs);
398 PL_rs = newRV_inc(newSViv(AP_IOBUFSIZE));
399 while (1) {
400 PUSHMARK(SP);
401 XPUSHs(obj);
402 PUTBACK;
403 count = call_method("getline", G_SCALAR);
404 if (count != 1) croak("Big trouble\n");
405 SPAGAIN;
406 buf_sv = POPs;
407 if (SvOK(buf_sv)) {
408 buf = SvPV(buf_sv, len);
409 clen += len;
410 ap_rwrite(buf, len, r);
411 } else {
412 break;
413 }
414 }
947bdac @spiritloose use ap_set_content_length
authored
415 if (len > 0) {
416 ap_set_content_length(r, len);
417 }
f61f188 @spiritloose Initial commit
authored
418 PUSHMARK(SP);
419 XPUSHs(obj);
420 PUTBACK;
421 call_method("close", G_DISCARD);
422 SPAGAIN;
423 PUTBACK;
424 FREETMPS;
425 LEAVE;
426 return OK;
427 }
428
377b40d @spiritloose sendfile support. closes #1
authored
429 static int output_body_sendfile(request_rec *r, const char *path)
430 {
431 apr_file_t *fd;
432 apr_status_t status;
433 apr_size_t len, nbytes;
434 apr_finfo_t finfo;
435 int rc;
436
437 status = apr_file_open(&fd, path, APR_READ|APR_BINARY, APR_OS_DEFAULT, r->pool);
438 if (status != APR_SUCCESS) {
439 return HTTP_INTERNAL_SERVER_ERROR;
440 }
441
442 apr_file_info_get(&finfo, APR_FINFO_NORM, fd);
443 len = finfo.size;
444
445 status = ap_send_fd(fd, r, 0, len, &nbytes);
446 apr_file_close(fd);
447
448 if (status == APR_SUCCESS) {
449 ap_set_content_length(r, nbytes);
450 rc = OK;
451 } else {
452 rc = HTTP_INTERNAL_SERVER_ERROR;
453 }
454
455 return rc;
456 }
457
458 static int output_body_path(request_rec *r, SV *body)
459 {
460 dTHX;
461 int count;
462 apr_status_t rc;
463 SV *path_sv;
22b6017 @spiritloose suppress warnings
authored
464 char *path = NULL;
377b40d @spiritloose sendfile support. closes #1
authored
465 dSP;
466 ENTER;
467 SAVETMPS;
468 PUSHMARK(SP);
469 XPUSHs(body);
470 PUTBACK;
471
472 count = call_method("path", G_EVAL|G_SCALAR|G_KEEPERR);
473 SPAGAIN;
474 if (SvTRUE(ERRSV)) {
475 rc = DECLINED;
476 server_error(r, "unable to get path\n%s", SvPV_nolen(ERRSV));
477 CLEAR_ERRSV();
22b6017 @spiritloose suppress warnings
authored
478 (void) POPs;
377b40d @spiritloose sendfile support. closes #1
authored
479 } else if (count > 0) {
480 path_sv = POPs;
481 path = apr_pstrdup(r->pool, SvPV_nolen(path_sv));
482 rc = OK;
483 } else {
484 rc = DECLINED;
485 }
486 PUTBACK;
487 FREETMPS;
488 LEAVE;
489
490 return rc != OK ? rc : output_body_sendfile(r, path);
491 }
492
f61f188 @spiritloose Initial commit
authored
493 static int output_body(request_rec *r, SV *body)
494 {
fe98cae @spiritloose use dTHX
authored
495 dTHX;
f61f188 @spiritloose Initial commit
authored
496 int rc, type;
497 switch (type = SvTYPE(SvRV(body))) {
498 case SVt_PVAV:
499 rc = output_body_ary(r, (AV *) SvRV(body));
500 break;
501 case SVt_PVGV:
377b40d @spiritloose sendfile support. closes #1
authored
502 /* TODO:
503 * It's possible to get fd by PerlIO_fileno(IoIFP(sv_2io(body)))
504 * It's possible to get apr_file_t by apr_os_file_put
505 * Is it possible to implement above portable?
506 */
f61f188 @spiritloose Initial commit
authored
507 require_pv("IO/Handle.pm");
508 case SVt_PVMG:
377b40d @spiritloose sendfile support. closes #1
authored
509 if (respond_to(body, "path")) {
510 rc = output_body_path(r, body);
511 if (rc != DECLINED) break;
512 }
f61f188 @spiritloose Initial commit
authored
513 rc = output_body_obj(r, body, type);
514 break;
515 default:
516 server_error(r, "response body must be an array reference or object");
517 rc = HTTP_INTERNAL_SERVER_ERROR;
518 break;
519 }
520 return rc;
521 }
522
523 static int output_response(request_rec *r, SV *res)
524 {
fe98cae @spiritloose use dTHX
authored
525 dTHX;
f61f188 @spiritloose Initial commit
authored
526 AV *res_av;
527 SV **status;
528 SV **headers;
529 AV *headers_av;
530 SV **body;
531 int rc;
532
533 if (!SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVAV) {
534 server_error(r, "response must be an array reference");
535 return HTTP_INTERNAL_SERVER_ERROR;
536 }
537 res_av = (AV *) SvRV(res);
538 if (av_len(res_av) != 2) {
539 server_error(r, "response must have 3 elements");
540 return HTTP_INTERNAL_SERVER_ERROR;
541 }
542
543 status = av_fetch(res_av, 0, 0);
544 if (!SvOK(*status)) {
545 server_error(r, "response status must be a scalar value");
546 return HTTP_INTERNAL_SERVER_ERROR;
547 }
548 rc = output_status(r, *status);
549 if (rc != OK) return rc;
550
551 headers = av_fetch(res_av, 1, 0);
552 if (!SvROK(*headers) || SvTYPE(SvRV(*headers)) != SVt_PVAV) {
553 server_error(r, "response headers must be an array reference");
554 return HTTP_INTERNAL_SERVER_ERROR;
555 }
556 headers_av = (AV *) SvRV(*headers);
557 if ((av_len(headers_av) + 1) % 2 != 0) {
558 server_error(r, "num of response headers must be even");
559 return HTTP_INTERNAL_SERVER_ERROR;
560 }
561 rc = output_headers(r, headers_av);
562 if (rc != OK) return rc;
563
564 body = av_fetch(res_av, 2, 0);
565 if (!SvROK(*body)) {
566 server_error(r, "response body must be a reference");
567 return HTTP_INTERNAL_SERVER_ERROR;
568 }
569 rc = output_body(r, *body);
570
571 return rc;
572 }
573
ace1272 @spiritloose Make PerlInterpreter persistent
authored
574 static void init_perl_variables()
f61f188 @spiritloose Initial commit
authored
575 {
fe98cae @spiritloose use dTHX
authored
576 dTHX;
f61f188 @spiritloose Initial commit
authored
577 GV *exit_gv = gv_fetchpv("CORE::GLOBAL::exit", TRUE, SVt_PVCV);
578 GvCV(exit_gv) = get_cv("ModPSGI::exit", TRUE);
579 GvIMPORTED_CV_on(exit_gv);
580 hv_store(GvHV(PL_envgv), "MOD_PSGI", 8, newSVpv(MOD_PSGI_VERSION, 0), 0);
581 }
582
ecd58df @spiritloose Load .psgi from .htaccess
authored
583 static SV *load_psgi(apr_pool_t *pool, const char *file)
584 {
585 dTHX;
586 SV *app;
587 char *code;
588
589 code = apr_psprintf(pool, "do q\"%s\" or die $@",
590 ap_escape_quotes(pool, file));
591 app = eval_pv(code, FALSE);
592
593 if (SvTRUE(ERRSV)) {
594 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, NULL, "%s", SvPV_nolen(ERRSV));
0e90b5f @spiritloose use G_KEEPERR and CLEAR_ERRSV() to catch errors
authored
595 CLEAR_ERRSV();
ecd58df @spiritloose Load .psgi from .htaccess
authored
596 return NULL;
597 }
598 if (!SvOK(app) || !SvROK(app) || SvTYPE(SvRV(app)) != SVt_PVCV) {
599 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, NULL,
600 "%s does not return an application code reference", file);
601 return NULL;
602 }
603 return app;
604 }
605
4e00d63 @spiritloose init and destroy in handler
authored
606 static int psgi_handler(request_rec *r)
f61f188 @spiritloose Initial commit
authored
607 {
608 SV *app, *env, *res;
609 psgi_dir_config *c;
32a1013 @spiritloose app runs in the new scope
authored
610 int rc;
f61f188 @spiritloose Initial commit
authored
611
098e77e @spiritloose ap_add_version_component
authored
612 if (strcmp(r->handler, PSGI_HANDLER_NAME)) {
f61f188 @spiritloose Initial commit
authored
613 return DECLINED;
614 }
615 c = (psgi_dir_config *) ap_get_module_config(r->per_dir_config, &psgi_module);
ace1272 @spiritloose Make PerlInterpreter persistent
authored
616 if (c->file == NULL) {
f61f188 @spiritloose Initial commit
authored
617 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, r->server,
618 "PSGIApp not configured");
619 return DECLINED;
620 }
5b66763 @spiritloose PERL_SET_CONTEXT
authored
621
622 PERL_SET_CONTEXT(perlinterp);
32a1013 @spiritloose app runs in the new scope
authored
623 ENTER;
624 SAVETMPS;
625
c115693 @spiritloose 'do $psgi_app' at post_config
authored
626 app = apr_hash_get(app_mapping, c->file, APR_HASH_KEY_STRING);
ecd58df @spiritloose Load .psgi from .htaccess
authored
627 if (app == NULL) {
628 app = load_psgi(r->pool, c->file);
629 if (app == NULL) {
630 server_error(r, "%s had compilation errors.", c->file);
32a1013 @spiritloose app runs in the new scope
authored
631 rc = HTTP_INTERNAL_SERVER_ERROR;
632 goto exit;
ecd58df @spiritloose Load .psgi from .htaccess
authored
633 }
634 apr_hash_set(app_mapping, c->file, APR_HASH_KEY_STRING, app);
635 }
32a1013 @spiritloose app runs in the new scope
authored
636
cad20bf @spiritloose Thanks to fujiwara. fixes #4
authored
637 env = make_env(r, c);
c115693 @spiritloose 'do $psgi_app' at post_config
authored
638 res = run_app(r, app, env);
f61f188 @spiritloose Initial commit
authored
639 if (res == NULL) {
640 server_error(r, "invalid response");
32a1013 @spiritloose app runs in the new scope
authored
641 rc = HTTP_INTERNAL_SERVER_ERROR;
642 goto exit;
f61f188 @spiritloose Initial commit
authored
643 }
32a1013 @spiritloose app runs in the new scope
authored
644 rc = output_response(r, res);
645 SvREFCNT_dec(res);
646
647 exit:
648 FREETMPS;
649 LEAVE;
650 return rc;
f61f188 @spiritloose Initial commit
authored
651 }
652
e284234 @spiritloose Call PERL_SYS_INIT3,PERL_SYS_TERM once per process
authored
653 static apr_status_t psgi_child_exit(void *p)
654 {
03fb93e @mattn win32 porting.
mattn authored
655 if (perlinterp == NULL) {
656 PERL_SET_CONTEXT(perlinterp);
657 PL_perl_destruct_level = 1;
658 perl_destruct(perlinterp);
659 perl_free(perlinterp);
660 PERL_SYS_TERM();
661 perlinterp = NULL;
662 }
e284234 @spiritloose Call PERL_SYS_INIT3,PERL_SYS_TERM once per process
authored
663 return OK;
664 }
665
666 static void psgi_child_init(apr_pool_t *p, server_rec *s)
667 {
668 apr_pool_cleanup_register(p, NULL, psgi_child_exit, psgi_child_exit);
669 }
670
c115693 @spiritloose 'do $psgi_app' at post_config
authored
671 static apr_status_t
672 psgi_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
ace1272 @spiritloose Make PerlInterpreter persistent
authored
673 {
674 int argc = 2;
675 char *argv[] = { "perl", "-e;0", NULL };
676 char **envp = NULL;
677
678 PERL_SYS_INIT3(&argc, (char ***) argv, &envp);
679 perlinterp = perl_alloc();
680 PL_perl_destruct_level = 1;
681 perl_construct(perlinterp);
682 perl_parse(perlinterp, xs_init, argc, argv, envp);
683 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
684 perl_run(perlinterp);
685 init_perl_variables();
c115693 @spiritloose 'do $psgi_app' at post_config
authored
686
687 psgi_apps = apr_array_make(pconf, 10, sizeof(char *));
688 app_mapping = apr_hash_make(pconf);
689
690 return OK;
691 }
692
693 static int
694 psgi_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
695 {
696 dTHX;
697 int i;
698 char *file, **elts;
699 SV *app;
8fd6f10 @spiritloose Ignore first call psgi_post_config
authored
700 void *data;
701 const char *userdata_key = "psgi_post_config";
702
703 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
704 if (data == NULL) {
705 apr_pool_userdata_set((const void *)1, userdata_key,
706 apr_pool_cleanup_null, s->process->pool);
707 return OK;
708 }
c115693 @spiritloose 'do $psgi_app' at post_config
authored
709 elts = (char **) psgi_apps->elts;
710 for (i = 0; i < psgi_apps->nelts; i++) {
711 file = elts[i];
712 app = load_psgi(pconf, file);
713 if (app == NULL) {
714 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, NULL,
715 "%s had compilation errors.", file);
716 return DONE;
717 }
718 apr_hash_set(app_mapping, file, APR_HASH_KEY_STRING, app);
719 }
098e77e @spiritloose ap_add_version_component
authored
720
721 ap_add_version_component(pconf, apr_psprintf(pconf, "mod_psgi/%s", MOD_PSGI_VERSION));
722
ace1272 @spiritloose Make PerlInterpreter persistent
authored
723 return OK;
724 }
725
f61f188 @spiritloose Initial commit
authored
726 static void psgi_register_hooks(apr_pool_t *p)
727 {
03fb93e @mattn win32 porting.
mattn authored
728 ap_hook_pre_config(psgi_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
729 ap_hook_post_config(psgi_post_config, NULL, NULL, APR_HOOK_MIDDLE);
730 ap_hook_child_init(psgi_child_init, NULL, NULL, APR_HOOK_MIDDLE);
731 ap_hook_handler(psgi_handler, NULL, NULL, APR_HOOK_MIDDLE);
f61f188 @spiritloose Initial commit
authored
732 }
733
734 static void *create_dir_config(apr_pool_t *p, char *path)
735 {
736 psgi_dir_config *c = apr_pcalloc(p, sizeof(psgi_dir_config));
ace1272 @spiritloose Make PerlInterpreter persistent
authored
737 c->file = NULL;
430ccf9 @fujiwara fix PATH_INFO and SCRIPT_NAME
fujiwara authored
738 c->location = path;
f61f188 @spiritloose Initial commit
authored
739 return (void *) c;
740 }
741
742 static const char *cmd_psgi_app(cmd_parms *cmd, void *conf, const char *v)
743 {
744 psgi_dir_config *c = (psgi_dir_config *) conf;
ace1272 @spiritloose Make PerlInterpreter persistent
authored
745 c->file = (char *) apr_pstrdup(cmd->pool, v);
c115693 @spiritloose 'do $psgi_app' at post_config
authored
746 *(const char **) apr_array_push(psgi_apps) = c->file;
f61f188 @spiritloose Initial commit
authored
747 return NULL;
748 }
749
750 static const command_rec command_table[] = {
751 AP_INIT_TAKE1("PSGIApp", cmd_psgi_app, NULL,
b991f34 @spiritloose Allow PSGIApp in .htaccess
authored
752 OR_LIMIT, "set PSGI application"),
f61f188 @spiritloose Initial commit
authored
753 { NULL }
754 };
755
756 module AP_MODULE_DECLARE_DATA psgi_module = {
757 STANDARD20_MODULE_STUFF,
758 create_dir_config, /* create per-dir config structures */
759 NULL, /* merge per-dir config structures */
760 NULL, /* create per-server config structures */
761 NULL, /* merge per-server config structures */
762 command_table, /* table of config file commands */
763 psgi_register_hooks /* register hooks */
764 };
765
Something went wrong with that request. Please try again.