Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 642 lines (566 sloc) 17.142 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"
03fb93e @mattn win32 porting.
mattn authored
28 #include "apr_buckets.h"
f61f188 @spiritloose Initial commit
authored
29 #include "apr_strings.h"
c115693 @spiritloose 'do $psgi_app' at post_config
authored
30 #include "apr_hash.h"
f61f188 @spiritloose Initial commit
authored
31
03fb93e @mattn win32 porting.
mattn authored
32 #ifdef _WIN32
33 /* use perl's uid_t/gid_t. disable apr's macros. */
34 #undef uid_t
35 #undef gid_t
36 #undef exit
37 #endif
38
f61f188 @spiritloose Initial commit
authored
39 #include "EXTERN.h"
40 #include "perl.h"
41 #include "XSUB.h"
42 #define NEED_eval_pv
43 #define NEED_newRV_noinc
44 #define NEED_sv_2pv_flags
45 #include "ppport.h"
46
03fb93e @mattn win32 porting.
mattn authored
47 #ifdef _WIN32
48 /* no use perl compatible macros. it break apr's structure. ex: bucket->link */
49 #undef link
50 #undef read
51 #endif
52
098e77e @spiritloose ap_add_version_component
authored
53 #define PSGI_HANDLER_NAME "psgi"
54
f61f188 @spiritloose Initial commit
authored
55 #ifdef DEBUG
56 #define TRACE(...) ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, NULL, __VA_ARGS__)
57 #endif
58
59 module AP_MODULE_DECLARE_DATA psgi_module;
60
61 typedef struct {
ace1272 @spiritloose Make PerlInterpreter persistent
authored
62 char *file;
430ccf9 @fujiwara fix PATH_INFO and SCRIPT_NAME
fujiwara authored
63 char *location;
f61f188 @spiritloose Initial commit
authored
64 } psgi_dir_config;
65
c115693 @spiritloose 'do $psgi_app' at post_config
authored
66 static PerlInterpreter *perlinterp = NULL;
67
68 static apr_hash_t *app_mapping = NULL;
69
70 static apr_array_header_t *psgi_apps = NULL;
e284234 @spiritloose Call PERL_SYS_INIT3,PERL_SYS_TERM once per process
authored
71
f61f188 @spiritloose Initial commit
authored
72 static void server_error(request_rec *r, const char *fmt, ...)
73 {
74 va_list argp;
75 const char *msg;
76 va_start(argp, fmt);
77 msg = apr_pvsprintf(r->pool, fmt, argp);
78 va_end(argp);
79 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r->server, "%s", msg);
80 }
81
82 EXTERN_C void xs_init (pTHX);
83
84 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
85
86 XS(ModPSGI_exit);
87 XS(ModPSGI_exit)
88 {
89 dXSARGS;
90 croak("exit");
91 XSRETURN(0);
92 }
93
94 XS(ModPSGI_Input_read);
95 XS(ModPSGI_Input_read)
96 {
97 dXSARGS;
98 SV *self = ST(0);
99 SV *buf = ST(1);
100 request_rec *r = (request_rec *) mg_find(SvRV(self), PERL_MAGIC_ext)->mg_obj;
101 apr_size_t len = SvIV(ST(2));
102 apr_size_t offset = items >= 4 ? SvIV(ST(3)) : 0;
103 apr_status_t rv;
104 apr_bucket_brigade *bb;
105 apr_bucket *bucket;
106 int eos = 0;
107 SV *ret;
108 dXSTARG;
109
110 ret = newSVpv("", 0);
111 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
112 rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES, APR_BLOCK_READ, len);
113 if (rv != APR_SUCCESS) {
114 ST(0) = &PL_sv_undef;
115 XSRETURN(1);
116 }
117
118 for (bucket = APR_BRIGADE_FIRST(bb);
119 bucket != APR_BRIGADE_SENTINEL(bb);
120 bucket = APR_BUCKET_NEXT(bucket)) {
121 const char *bbuf;
122 apr_size_t blen;
123 if (APR_BUCKET_IS_EOS(bucket)) {
124 eos = 1;
125 break;
126 }
127 if (APR_BUCKET_IS_METADATA(bucket)) {
128 continue;
129 }
130 apr_bucket_read(bucket, &bbuf, &blen, APR_BLOCK_READ);
131 sv_catpvn(ret, bbuf, blen);
132 }
133
134 sv_setsv(buf, ret);
135 ST(0) = sv_2mortal(newSViv(SvCUR(buf)));
136 XSRETURN(1);
137 }
138
139 XS(ModPSGI_Errors_print);
140 XS(ModPSGI_Errors_print)
141 {
142 dXSARGS;
143 SV *self = ST(0);
144 SV *msg = ST(1);
145 dXSTARG;
146 request_rec *r = (request_rec *) mg_find(SvRV(self), PERL_MAGIC_ext)->mg_obj;
147 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r->server, "%s", SvPV_nolen(msg));
148 ST(0) = newSViv(1);
149 XSRETURN(1);
150 }
151
152 EXTERN_C void
153 xs_init(pTHX)
154 {
155 char *file = __FILE__;
156 dXSUB_SYS;
157
158 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
159 newXS("ModPSGI::exit", ModPSGI_exit, file);
160 newXSproto("ModPSGI::Input::read", ModPSGI_Input_read, file, "$$$;$");
161 newXSproto("ModPSGI::Errors::print", ModPSGI_Errors_print, file, "$$");
162 }
163
164 static int copy_env(void *rec, const char *key, const char *val)
165 {
fe98cae @spiritloose use dTHX
authored
166 dTHX;
f61f188 @spiritloose Initial commit
authored
167 HV *env = (HV *) rec;
168 hv_store(env, key, strlen(key), newSVpv(val, 0), 0);
169 return 1;
170 }
171
cad20bf @spiritloose Thanks to fujiwara. fixes #4
authored
172 static SV *make_env(request_rec *r, psgi_dir_config *c)
f61f188 @spiritloose Initial commit
authored
173 {
fe98cae @spiritloose use dTHX
authored
174 dTHX;
f61f188 @spiritloose Initial commit
authored
175 HV *env;
176 AV *version;
cad20bf @spiritloose Thanks to fujiwara. fixes #4
authored
177 char *url_scheme, *script_name, *vpath, *path_info;
f61f188 @spiritloose Initial commit
authored
178 SV *input, *errors;
179
180 env = newHV();
181
182 ap_add_cgi_vars(r);
183 ap_add_common_vars(r);
430ccf9 @fujiwara fix PATH_INFO and SCRIPT_NAME
fujiwara authored
184
cad20bf @spiritloose Thanks to fujiwara. fixes #4
authored
185 /* fix SCRIPT_NAME & PATH_INFO */
186 if (c->location == NULL || strcmp(c->location, "/") == 0) {
187 script_name = "";
188 } else {
189 script_name = c->location;
f61f188 @spiritloose Initial commit
authored
190 }
430ccf9 @fujiwara fix PATH_INFO and SCRIPT_NAME
fujiwara authored
191 vpath = apr_pstrcat(r->pool,
cad20bf @spiritloose Thanks to fujiwara. fixes #4
authored
192 apr_table_get(r->subprocess_env, "SCRIPT_NAME"),
193 apr_table_get(r->subprocess_env, "PATH_INFO"),
194 NULL);
195 path_info = &vpath[strlen(script_name)];
430ccf9 @fujiwara fix PATH_INFO and SCRIPT_NAME
fujiwara authored
196 apr_table_set(r->subprocess_env, "PATH_INFO", path_info);
cad20bf @spiritloose Thanks to fujiwara. fixes #4
authored
197 apr_table_set(r->subprocess_env, "SCRIPT_NAME", script_name);
430ccf9 @fujiwara fix PATH_INFO and SCRIPT_NAME
fujiwara authored
198
f61f188 @spiritloose Initial commit
authored
199 apr_table_do(copy_env, env, r->subprocess_env, NULL);
200
201 version = newAV();
202 av_push(version, newSViv(1));
203 av_push(version, newSViv(0));
204 hv_store(env, "psgi.version", 12, newRV_inc((SV *) version), 0);
205
206 url_scheme = apr_table_get(r->subprocess_env, "HTTPS") == NULL ? "http" : "https";
207 hv_store(env, "psgi.url_scheme", 15, newSVpv(url_scheme, 0), 0);
208
209 input = newRV_noinc(newSV(0));
210 sv_magic(SvRV(input), NULL, PERL_MAGIC_ext, NULL, 0);
211 mg_find(SvRV(input), PERL_MAGIC_ext)->mg_obj = (void *) r;
212 sv_bless(input, gv_stashpv("ModPSGI::Input", 1));
213 hv_store(env, "psgi.input", 10, input, 0);
214
215 errors = newRV_noinc(newSV(0));
216 sv_magic(SvRV(errors), NULL, PERL_MAGIC_ext, NULL, 0);
217 mg_find(SvRV(errors), PERL_MAGIC_ext)->mg_obj = (void *) r;
218 sv_bless(errors, gv_stashpv("ModPSGI::Errors", 1));
219 hv_store(env, "psgi.errors", 11, errors, 0);
220
221 hv_store(env, "psgi.multithread", 16, newSViv(0), 0);
222 hv_store(env, "psgi.multiprocess", 17, newSViv(1), 0);
223 hv_store(env, "psgi.run_once", 13, newSViv(1), 0);
3377795 @spiritloose psgi.async -> psgi.nonblocking
authored
224 hv_store(env, "psgi.nonblocking", 16, newSViv(0), 0);
f61f188 @spiritloose Initial commit
authored
225
226 return newRV_inc((SV *) env);
227 }
228
229 static SV *run_app(request_rec *r, SV *app, SV *env)
230 {
fe98cae @spiritloose use dTHX
authored
231 dTHX;
f61f188 @spiritloose Initial commit
authored
232 int count;
233 SV *res;
234 dSP;
235 ENTER;
236 SAVETMPS;
237 PUSHMARK(SP) ;
238 XPUSHs(sv_2mortal(env));
239 PUTBACK;
240
5fc2eef @spiritloose Do not use G_KEEPERR in run_app
authored
241 count = call_sv(app, G_EVAL|G_SCALAR);
f61f188 @spiritloose Initial commit
authored
242 SPAGAIN;
243 if (SvTRUE(ERRSV)) {
244 res = NULL;
245 server_error(r, "%s", SvPV_nolen(ERRSV));
246 POPs;
247 } else if (count > 0) {
248 res = POPs;
249 SvREFCNT_inc(res);
250 } else {
251 res = NULL;
252 }
253 PUTBACK;
254 FREETMPS;
255 LEAVE;
256 return res;
257 }
258
259 static int output_status(request_rec *r, SV *status)
260 {
fe98cae @spiritloose use dTHX
authored
261 dTHX;
f61f188 @spiritloose Initial commit
authored
262 int s = SvIV(status);
263 if (s < 100) {
264 server_error(r, "invalid response status %d", s);
265 return HTTP_INTERNAL_SERVER_ERROR;
266 }
267 r->status = s;
268 return OK;
269 }
270
271 static int output_headers(request_rec *r, AV *headers)
272 {
fe98cae @spiritloose use dTHX
authored
273 dTHX;
f61f188 @spiritloose Initial commit
authored
274 SV *key_sv, *val_sv;
947bdac @spiritloose use ap_set_content_length
authored
275 char *key;
276
277 r->content_type = NULL;
f61f188 @spiritloose Initial commit
authored
278 while (av_len(headers) > -1) {
279 key_sv = av_shift(headers);
280 val_sv = av_shift(headers);
281 if (key_sv == NULL || val_sv == NULL) break;
282 key = SvPV_nolen(key_sv);
aab7f6a @spiritloose Do not check header value
authored
283 if (strcmp(key, "Content-Type") == 0) {
947bdac @spiritloose use ap_set_content_length
authored
284 r->content_type = apr_pstrdup(r->pool, SvPV_nolen(val_sv));
285 } else if (strcmp(key, "Content-Length") == 0) {
286 ap_set_content_length(r, SvIV(val_sv));
f61f188 @spiritloose Initial commit
authored
287 } else if (strcmp(key, "Status") == 0) {
288 server_error(r, "headers must not contain a Status");
289 return HTTP_INTERNAL_SERVER_ERROR;
290 } else {
947bdac @spiritloose use ap_set_content_length
authored
291 apr_table_add(r->headers_out, key, SvPV_nolen(val_sv));
f61f188 @spiritloose Initial commit
authored
292 }
293 }
294 return OK;
295 }
296
297 static int respond_to(SV *obj, const char *method)
298 {
fe98cae @spiritloose use dTHX
authored
299 dTHX;
f61f188 @spiritloose Initial commit
authored
300 int res;
301 dSP;
302 ENTER;
303 SAVETMPS;
304 PUSHMARK(SP);
305 XPUSHs(obj);
306 XPUSHs(sv_2mortal(newSVpv(method, 0)));
307 PUTBACK;
308
309 call_method("can", G_SCALAR);
310 SPAGAIN;
311 res = SvROK(POPs);
312 PUTBACK;
313 FREETMPS;
314 LEAVE;
315 return res;
316 }
317
318 static int output_body_ary(request_rec *r, AV *bodys)
319 {
fe98cae @spiritloose use dTHX
authored
320 dTHX;
f61f188 @spiritloose Initial commit
authored
321 SV **body;
322 I32 i;
323 I32 lastidx;
324 char *buf;
325 STRLEN len;
03fb93e @mattn win32 porting.
mattn authored
326 apr_off_t clen = 0;
f61f188 @spiritloose Initial commit
authored
327
328 lastidx = av_len(bodys);
329 for (i = 0; i <= lastidx; i++) {
330 body = av_fetch(bodys, i, 0);
331 if (SvOK(*body)) {
332 buf = SvPV(*body, len);
333 ap_rwrite(buf, len, r);
334 clen += len;
335 }
336 }
947bdac @spiritloose use ap_set_content_length
authored
337 if (clen > 0) {
338 ap_set_content_length(r, clen);
339 }
f61f188 @spiritloose Initial commit
authored
340 return OK;
341 }
342
343 static int output_body_obj(request_rec *r, SV *obj, int type)
344 {
fe98cae @spiritloose use dTHX
authored
345 dTHX;
f61f188 @spiritloose Initial commit
authored
346 SV *buf_sv, *rs;
347 apr_off_t clen = 0;
348 STRLEN len;
03fb93e @mattn win32 porting.
mattn authored
349 dSP;
f61f188 @spiritloose Initial commit
authored
350 char *buf;
351 int count;
352
353 if (type == SVt_PVMG && !respond_to(obj, "getline")) {
354 server_error(r, "response body object must be able to getline");
355 return HTTP_INTERNAL_SERVER_ERROR;
356 }
357
358 ENTER;
359 SAVETMPS;
360 SAVESPTR(PL_rs);
361 PL_rs = newRV_inc(newSViv(AP_IOBUFSIZE));
362 while (1) {
363 PUSHMARK(SP);
364 XPUSHs(obj);
365 PUTBACK;
366 count = call_method("getline", G_SCALAR);
367 if (count != 1) croak("Big trouble\n");
368 SPAGAIN;
369 buf_sv = POPs;
370 if (SvOK(buf_sv)) {
371 buf = SvPV(buf_sv, len);
372 clen += len;
373 ap_rwrite(buf, len, r);
374 } else {
375 break;
376 }
377 }
947bdac @spiritloose use ap_set_content_length
authored
378 if (len > 0) {
379 ap_set_content_length(r, len);
380 }
f61f188 @spiritloose Initial commit
authored
381 PUSHMARK(SP);
382 XPUSHs(obj);
383 PUTBACK;
384 call_method("close", G_DISCARD);
385 SPAGAIN;
386 PUTBACK;
387 FREETMPS;
388 LEAVE;
389 return OK;
390 }
391
392 static int output_body(request_rec *r, SV *body)
393 {
fe98cae @spiritloose use dTHX
authored
394 dTHX;
f61f188 @spiritloose Initial commit
authored
395 int rc, type;
396 switch (type = SvTYPE(SvRV(body))) {
397 case SVt_PVAV:
398 rc = output_body_ary(r, (AV *) SvRV(body));
399 break;
400 case SVt_PVGV:
401 require_pv("IO/Handle.pm");
402 case SVt_PVMG:
403 rc = output_body_obj(r, body, type);
404 break;
405 default:
406 server_error(r, "response body must be an array reference or object");
407 rc = HTTP_INTERNAL_SERVER_ERROR;
408 break;
409 }
410 return rc;
411 }
412
413 static int output_response(request_rec *r, SV *res)
414 {
fe98cae @spiritloose use dTHX
authored
415 dTHX;
f61f188 @spiritloose Initial commit
authored
416 AV *res_av;
417 SV **status;
418 SV **headers;
419 AV *headers_av;
420 SV **body;
421 int rc;
422
423 if (!SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVAV) {
424 server_error(r, "response must be an array reference");
425 return HTTP_INTERNAL_SERVER_ERROR;
426 }
427 res_av = (AV *) SvRV(res);
428 if (av_len(res_av) != 2) {
429 server_error(r, "response must have 3 elements");
430 return HTTP_INTERNAL_SERVER_ERROR;
431 }
432
433 status = av_fetch(res_av, 0, 0);
434 if (!SvOK(*status)) {
435 server_error(r, "response status must be a scalar value");
436 return HTTP_INTERNAL_SERVER_ERROR;
437 }
438 rc = output_status(r, *status);
439 if (rc != OK) return rc;
440
441 headers = av_fetch(res_av, 1, 0);
442 if (!SvROK(*headers) || SvTYPE(SvRV(*headers)) != SVt_PVAV) {
443 server_error(r, "response headers must be an array reference");
444 return HTTP_INTERNAL_SERVER_ERROR;
445 }
446 headers_av = (AV *) SvRV(*headers);
447 if ((av_len(headers_av) + 1) % 2 != 0) {
448 server_error(r, "num of response headers must be even");
449 return HTTP_INTERNAL_SERVER_ERROR;
450 }
451 rc = output_headers(r, headers_av);
452 if (rc != OK) return rc;
453
454 body = av_fetch(res_av, 2, 0);
455 if (!SvROK(*body)) {
456 server_error(r, "response body must be a reference");
457 return HTTP_INTERNAL_SERVER_ERROR;
458 }
459 rc = output_body(r, *body);
460
461 return rc;
462 }
463
ace1272 @spiritloose Make PerlInterpreter persistent
authored
464 static void init_perl_variables()
f61f188 @spiritloose Initial commit
authored
465 {
fe98cae @spiritloose use dTHX
authored
466 dTHX;
f61f188 @spiritloose Initial commit
authored
467 GV *exit_gv = gv_fetchpv("CORE::GLOBAL::exit", TRUE, SVt_PVCV);
468 GvCV(exit_gv) = get_cv("ModPSGI::exit", TRUE);
469 GvIMPORTED_CV_on(exit_gv);
470 hv_store(GvHV(PL_envgv), "MOD_PSGI", 8, newSVpv(MOD_PSGI_VERSION, 0), 0);
471 }
472
ecd58df @spiritloose Load .psgi from .htaccess
authored
473 static SV *load_psgi(apr_pool_t *pool, const char *file)
474 {
475 dTHX;
476 SV *app;
477 char *code;
478
479 code = apr_psprintf(pool, "do q\"%s\" or die $@",
480 ap_escape_quotes(pool, file));
481 app = eval_pv(code, FALSE);
482
483 if (SvTRUE(ERRSV)) {
484 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, NULL, "%s", SvPV_nolen(ERRSV));
485 return NULL;
486 }
487 if (!SvOK(app) || !SvROK(app) || SvTYPE(SvRV(app)) != SVt_PVCV) {
488 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, NULL,
489 "%s does not return an application code reference", file);
490 return NULL;
491 }
492 return app;
493 }
494
4e00d63 @spiritloose init and destroy in handler
authored
495 static int psgi_handler(request_rec *r)
f61f188 @spiritloose Initial commit
authored
496 {
497 SV *app, *env, *res;
498 psgi_dir_config *c;
499
098e77e @spiritloose ap_add_version_component
authored
500 if (strcmp(r->handler, PSGI_HANDLER_NAME)) {
f61f188 @spiritloose Initial commit
authored
501 return DECLINED;
502 }
503 c = (psgi_dir_config *) ap_get_module_config(r->per_dir_config, &psgi_module);
ace1272 @spiritloose Make PerlInterpreter persistent
authored
504 if (c->file == NULL) {
f61f188 @spiritloose Initial commit
authored
505 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, r->server,
506 "PSGIApp not configured");
507 return DECLINED;
508 }
5b66763 @spiritloose PERL_SET_CONTEXT
authored
509
510 PERL_SET_CONTEXT(perlinterp);
c115693 @spiritloose 'do $psgi_app' at post_config
authored
511 app = apr_hash_get(app_mapping, c->file, APR_HASH_KEY_STRING);
ecd58df @spiritloose Load .psgi from .htaccess
authored
512 if (app == NULL) {
513 app = load_psgi(r->pool, c->file);
514 if (app == NULL) {
515 server_error(r, "%s had compilation errors.", c->file);
516 return HTTP_INTERNAL_SERVER_ERROR;
517 }
518 apr_hash_set(app_mapping, c->file, APR_HASH_KEY_STRING, app);
519 }
cad20bf @spiritloose Thanks to fujiwara. fixes #4
authored
520 env = make_env(r, c);
c115693 @spiritloose 'do $psgi_app' at post_config
authored
521 res = run_app(r, app, env);
f61f188 @spiritloose Initial commit
authored
522 if (res == NULL) {
523 server_error(r, "invalid response");
c115693 @spiritloose 'do $psgi_app' at post_config
authored
524 return HTTP_INTERNAL_SERVER_ERROR;
f61f188 @spiritloose Initial commit
authored
525 }
c115693 @spiritloose 'do $psgi_app' at post_config
authored
526 return output_response(r, res);
f61f188 @spiritloose Initial commit
authored
527 }
528
e284234 @spiritloose Call PERL_SYS_INIT3,PERL_SYS_TERM once per process
authored
529 static apr_status_t psgi_child_exit(void *p)
530 {
03fb93e @mattn win32 porting.
mattn authored
531 if (perlinterp == NULL) {
532 PERL_SET_CONTEXT(perlinterp);
533 PL_perl_destruct_level = 1;
534 perl_destruct(perlinterp);
535 perl_free(perlinterp);
536 PERL_SYS_TERM();
537 perlinterp = NULL;
538 }
e284234 @spiritloose Call PERL_SYS_INIT3,PERL_SYS_TERM once per process
authored
539 return OK;
540 }
541
542 static void psgi_child_init(apr_pool_t *p, server_rec *s)
543 {
544 apr_pool_cleanup_register(p, NULL, psgi_child_exit, psgi_child_exit);
545 }
546
c115693 @spiritloose 'do $psgi_app' at post_config
authored
547 static apr_status_t
548 psgi_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
ace1272 @spiritloose Make PerlInterpreter persistent
authored
549 {
550 int argc = 2;
551 char *argv[] = { "perl", "-e;0", NULL };
552 char **envp = NULL;
553
554 PERL_SYS_INIT3(&argc, (char ***) argv, &envp);
555 perlinterp = perl_alloc();
556 PL_perl_destruct_level = 1;
557 perl_construct(perlinterp);
558 perl_parse(perlinterp, xs_init, argc, argv, envp);
559 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
560 perl_run(perlinterp);
561 init_perl_variables();
c115693 @spiritloose 'do $psgi_app' at post_config
authored
562
563 psgi_apps = apr_array_make(pconf, 10, sizeof(char *));
564 app_mapping = apr_hash_make(pconf);
565
566 return OK;
567 }
568
569 static int
570 psgi_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
571 {
572 dTHX;
573 int i;
574 char *file, **elts;
575 SV *app;
8fd6f10 @spiritloose Ignore first call psgi_post_config
authored
576 void *data;
577 const char *userdata_key = "psgi_post_config";
578
579 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
580 if (data == NULL) {
581 apr_pool_userdata_set((const void *)1, userdata_key,
582 apr_pool_cleanup_null, s->process->pool);
583 return OK;
584 }
c115693 @spiritloose 'do $psgi_app' at post_config
authored
585 elts = (char **) psgi_apps->elts;
586 for (i = 0; i < psgi_apps->nelts; i++) {
587 file = elts[i];
588 app = load_psgi(pconf, file);
589 if (app == NULL) {
590 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, NULL,
591 "%s had compilation errors.", file);
592 return DONE;
593 }
594 apr_hash_set(app_mapping, file, APR_HASH_KEY_STRING, app);
595 }
098e77e @spiritloose ap_add_version_component
authored
596
597 ap_add_version_component(pconf, apr_psprintf(pconf, "mod_psgi/%s", MOD_PSGI_VERSION));
598
ace1272 @spiritloose Make PerlInterpreter persistent
authored
599 return OK;
600 }
601
f61f188 @spiritloose Initial commit
authored
602 static void psgi_register_hooks(apr_pool_t *p)
603 {
03fb93e @mattn win32 porting.
mattn authored
604 ap_hook_pre_config(psgi_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
605 ap_hook_post_config(psgi_post_config, NULL, NULL, APR_HOOK_MIDDLE);
606 ap_hook_child_init(psgi_child_init, NULL, NULL, APR_HOOK_MIDDLE);
607 ap_hook_handler(psgi_handler, NULL, NULL, APR_HOOK_MIDDLE);
f61f188 @spiritloose Initial commit
authored
608 }
609
610 static void *create_dir_config(apr_pool_t *p, char *path)
611 {
612 psgi_dir_config *c = apr_pcalloc(p, sizeof(psgi_dir_config));
ace1272 @spiritloose Make PerlInterpreter persistent
authored
613 c->file = NULL;
430ccf9 @fujiwara fix PATH_INFO and SCRIPT_NAME
fujiwara authored
614 c->location = path;
f61f188 @spiritloose Initial commit
authored
615 return (void *) c;
616 }
617
618 static const char *cmd_psgi_app(cmd_parms *cmd, void *conf, const char *v)
619 {
620 psgi_dir_config *c = (psgi_dir_config *) conf;
ace1272 @spiritloose Make PerlInterpreter persistent
authored
621 c->file = (char *) apr_pstrdup(cmd->pool, v);
c115693 @spiritloose 'do $psgi_app' at post_config
authored
622 *(const char **) apr_array_push(psgi_apps) = c->file;
f61f188 @spiritloose Initial commit
authored
623 return NULL;
624 }
625
626 static const command_rec command_table[] = {
627 AP_INIT_TAKE1("PSGIApp", cmd_psgi_app, NULL,
b991f34 @spiritloose Allow PSGIApp in .htaccess
authored
628 OR_LIMIT, "set PSGI application"),
f61f188 @spiritloose Initial commit
authored
629 { NULL }
630 };
631
632 module AP_MODULE_DECLARE_DATA psgi_module = {
633 STANDARD20_MODULE_STUFF,
634 create_dir_config, /* create per-dir config structures */
635 NULL, /* merge per-dir config structures */
636 NULL, /* create per-server config structures */
637 NULL, /* merge per-server config structures */
638 command_table, /* table of config file commands */
639 psgi_register_hooks /* register hooks */
640 };
641
Something went wrong with that request. Please try again.