comparison src/http/modules/perl/ngx_http_perl_module.c @ 599:869b6444d234 release-0.3.21

nginx-0.3.21-RELEASE import *) Feature: the ngx_http_perl_module. *) Change: the "valid_referers" directive allows the referreres without URI part.
author Igor Sysoev <igor@sysoev.ru>
date Mon, 16 Jan 2006 14:56:53 +0000
parents
children 51b27717f140
comparison
equal deleted inserted replaced
598:bfa12c280dec 599:869b6444d234
1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
10 #include <ngx_http_perl_module.h>
11
12
13 typedef struct {
14 PerlInterpreter **free_perls;
15 ngx_uint_t interp;
16 ngx_uint_t nalloc;
17 ngx_uint_t interp_max;
18
19 PerlInterpreter *perl;
20 ngx_str_t modules;
21 ngx_array_t requires;
22 } ngx_http_perl_main_conf_t;
23
24
25 typedef struct {
26 SV *sub;
27 ngx_str_t handler;
28 } ngx_http_perl_loc_conf_t;
29
30
31 typedef struct {
32 SV *sub;
33 ngx_str_t handler;
34 } ngx_http_perl_variable_t;
35
36
37 static ngx_int_t ngx_http_perl_ssi(ngx_http_request_t *r,
38 ngx_http_ssi_ctx_t *ssi_ctx, ngx_str_t **params);
39 static ngx_int_t
40 ngx_http_perl_get_interpreter(ngx_http_perl_main_conf_t *pmcf,
41 PerlInterpreter **perl, ngx_log_t *log);
42 static ngx_inline void
43 ngx_http_perl_free_interpreter(ngx_http_perl_main_conf_t *pmcf,
44 PerlInterpreter *perl);
45 static char *ngx_http_perl_init_interpreter(ngx_conf_t *cf,
46 ngx_http_perl_main_conf_t *pmcf);
47 static PerlInterpreter *
48 ngx_http_perl_create_interpreter(ngx_http_perl_main_conf_t *pmcf,
49 ngx_log_t *log);
50 static ngx_int_t ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r,
51 SV *sub, ngx_str_t **args, ngx_str_t *handler, ngx_str_t *rv);
52 static void ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv);
53
54 static ngx_int_t ngx_http_perl_preconfiguration(ngx_conf_t *cf);
55 static void *ngx_http_perl_create_main_conf(ngx_conf_t *cf);
56 static char *ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf);
57 static void *ngx_http_perl_create_loc_conf(ngx_conf_t *cf);
58 static char *ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent,
59 void *child);
60 static char *ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd,
61 void *conf);
62 static char *ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
63 static char *ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
64 static char *ngx_http_perl_interp_max_unsupported(ngx_conf_t *cf, void *post,
65 void *data);
66 static void ngx_http_perl_cleanup_perl(void *data);
67
68
69 static ngx_conf_post_handler_pt ngx_http_perl_interp_max_p =
70 ngx_http_perl_interp_max_unsupported;
71
72
73 static ngx_command_t ngx_http_perl_commands[] = {
74
75 { ngx_string("perl_modules"),
76 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
77 ngx_conf_set_str_slot,
78 NGX_HTTP_MAIN_CONF_OFFSET,
79 offsetof(ngx_http_perl_main_conf_t, modules),
80 NULL },
81
82 { ngx_string("perl_require"),
83 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
84 ngx_http_perl_require,
85 NGX_HTTP_MAIN_CONF_OFFSET,
86 0,
87 NULL },
88
89 { ngx_string("perl_interp_max"),
90 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
91 ngx_conf_set_num_slot,
92 NGX_HTTP_MAIN_CONF_OFFSET,
93 offsetof(ngx_http_perl_main_conf_t, interp_max),
94 &ngx_http_perl_interp_max_p },
95
96 { ngx_string("perl"),
97 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
98 ngx_http_perl,
99 NGX_HTTP_LOC_CONF_OFFSET,
100 0,
101 NULL },
102
103 { ngx_string("perl_set"),
104 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2,
105 ngx_http_perl_set,
106 NGX_HTTP_LOC_CONF_OFFSET,
107 0,
108 NULL },
109
110 ngx_null_command
111 };
112
113
114 static ngx_http_module_t ngx_http_perl_module_ctx = {
115 ngx_http_perl_preconfiguration, /* preconfiguration */
116 NULL, /* postconfiguration */
117
118 ngx_http_perl_create_main_conf, /* create main configuration */
119 ngx_http_perl_init_main_conf, /* init main configuration */
120
121 NULL, /* create server configuration */
122 NULL, /* merge server configuration */
123
124 ngx_http_perl_create_loc_conf, /* create location configuration */
125 ngx_http_perl_merge_loc_conf /* merge location configuration */
126 };
127
128
129 ngx_module_t ngx_http_perl_module = {
130 NGX_MODULE_V1,
131 &ngx_http_perl_module_ctx, /* module context */
132 ngx_http_perl_commands, /* module directives */
133 NGX_HTTP_MODULE, /* module type */
134 NULL, /* init master */
135 NULL, /* init module */
136 NULL, /* init process */
137 NULL, /* init thread */
138 NULL, /* exit thread */
139 NULL, /* exit process */
140 NULL, /* exit master */
141 NGX_MODULE_V1_PADDING
142 };
143
144
145 #define NGX_HTTP_PERL_SSI_SUB 0
146 #define NGX_HTTP_PERL_SSI_ARG 1
147
148
149 static ngx_http_ssi_param_t ngx_http_perl_ssi_params[] = {
150 { ngx_string("sub"), NGX_HTTP_PERL_SSI_SUB, 1, 0 },
151 { ngx_string("arg"), NGX_HTTP_PERL_SSI_ARG, 0, 1 },
152 { ngx_null_string, 0, 0, 0 }
153 };
154
155
156 static ngx_http_ssi_command_t ngx_http_perl_ssi_command = {
157 ngx_string("perl"), ngx_http_perl_ssi, ngx_http_perl_ssi_params, 0, 1
158 };
159
160
161 static void
162 ngx_http_perl_xs_init(pTHX)
163 {
164 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__);
165 }
166
167
168 static ngx_int_t
169 ngx_http_perl_handler(ngx_http_request_t *r)
170 {
171 ngx_int_t rc;
172 ngx_str_t uri, args;
173 ngx_http_perl_ctx_t *ctx;
174 ngx_http_perl_loc_conf_t *plcf;
175 ngx_http_perl_main_conf_t *pmcf;
176
177 /* TODO: Win32 */
178 if (r->zero_in_uri) {
179 return NGX_HTTP_NOT_FOUND;
180 }
181
182 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl handler");
183
184 /* mod_perl's content handler assumes that content type was already set */
185
186 if (ngx_http_set_content_type(r) != NGX_OK) {
187 return NGX_HTTP_INTERNAL_SERVER_ERROR;
188 }
189
190 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
191
192 if (ctx == NULL) {
193 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t));
194 if (ctx == NULL) {
195 return NGX_ERROR;
196 }
197
198 ngx_http_set_ctx(r, ctx, ngx_http_perl_module);
199 }
200
201 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
202
203 rc = ngx_http_perl_get_interpreter(pmcf, &ctx->perl, r->connection->log);
204
205 if (rc != NGX_OK) {
206 return rc;
207 }
208
209 {
210
211 dTHXa(ctx->perl);
212
213 plcf = ngx_http_get_module_loc_conf(r, ngx_http_perl_module);
214
215 rc = ngx_http_perl_call_handler(aTHX_ r, plcf->sub, NULL,
216 &plcf->handler, NULL);
217
218 }
219
220 ngx_http_perl_free_interpreter(pmcf, ctx->perl);
221
222 if (rc > 600) {
223 rc = NGX_OK;
224 }
225
226 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
227 "perl handler done: %i", rc);
228
229 if (ctx->redirect_uri.len) {
230 uri = ctx->redirect_uri;
231 args = ctx->redirect_args;
232 }
233
234 ctx->filename = NULL;
235 ctx->redirect_uri.len = 0;
236
237 if (uri.len) {
238 return ngx_http_internal_redirect(r, &uri, &args);
239 }
240
241 if (rc == NGX_OK || rc == NGX_HTTP_OK) {
242 return ngx_http_send_special(r, NGX_HTTP_LAST);
243 }
244
245 return rc;
246 }
247
248
249 static ngx_int_t
250 ngx_http_perl_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
251 uintptr_t data)
252 {
253 ngx_http_perl_variable_t *pv = (ngx_http_perl_variable_t *) data;
254
255 ngx_int_t rc;
256 ngx_str_t value;
257 ngx_http_perl_ctx_t *ctx;
258 ngx_http_perl_main_conf_t *pmcf;
259
260 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
261 "perl variable handler");
262
263 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
264
265 if (ctx == NULL) {
266 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t));
267 if (ctx == NULL) {
268 return NGX_ERROR;
269 }
270
271 ngx_http_set_ctx(r, ctx, ngx_http_perl_module);
272 }
273
274 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
275
276 rc = ngx_http_perl_get_interpreter(pmcf, &ctx->perl, r->connection->log);
277
278 if (rc != NGX_OK) {
279 return rc;
280 }
281
282 value.data = NULL;
283
284 {
285
286 dTHXa(ctx->perl);
287
288 rc = ngx_http_perl_call_handler(aTHX_ r, pv->sub, NULL,
289 &pv->handler, &value);
290
291 }
292
293 ngx_http_perl_free_interpreter(pmcf, ctx->perl);
294
295 if (value.data) {
296 v->len = value.len;
297 v->valid = 1;
298 v->no_cachable = 0;
299 v->not_found = 0;
300 v->data = value.data;
301
302 } else {
303 v->not_found = 1;
304 }
305
306 ctx->filename = NULL;
307 ctx->redirect_uri.len = 0;
308
309 return rc;
310 }
311
312
313 static ngx_int_t
314 ngx_http_perl_ssi(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ssi_ctx,
315 ngx_str_t **params)
316 {
317 SV *sv;
318 ngx_int_t rc;
319 ngx_str_t *handler;
320 ngx_http_perl_ctx_t *ctx;
321 ngx_http_perl_main_conf_t *pmcf;
322
323 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
324 "perl ssi handler");
325
326 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
327
328 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
329
330 if (ctx == NULL) {
331 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t));
332 if (ctx == NULL) {
333 return NGX_ERROR;
334 }
335
336 ngx_http_set_ctx(r, ctx, ngx_http_perl_module);
337 }
338
339 rc = ngx_http_perl_get_interpreter(pmcf, &ctx->perl, r->connection->log);
340
341 if (rc != NGX_OK) {
342 return rc;
343 }
344
345 ctx->ssi = ssi_ctx;
346
347 handler = params[NGX_HTTP_PERL_SSI_SUB];
348 handler->data[handler->len] = '\0';
349
350 {
351
352 dTHXa(ctx->perl);
353
354 #if 0
355
356 ngx_http_perl_eval_anon_sub(aTHX_ handler, &sv);
357
358 if (sv == &PL_sv_undef) {
359 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
360 "eval_pv(\"%V\") failed", handler);
361 return NGX_ERROR;
362 }
363
364 if (sv == NULL) {
365 sv = newSVpvn((char *) handler->data, handler->len);
366 }
367
368 #endif
369
370 sv = newSVpvn((char *) handler->data, handler->len);
371
372 rc = ngx_http_perl_call_handler(aTHX_ r, sv, &params[NGX_HTTP_PERL_SSI_ARG],
373 handler, NULL);
374
375 SvREFCNT_dec(sv);
376
377 }
378
379 ngx_http_perl_free_interpreter(pmcf, ctx->perl);
380
381 ctx->filename = NULL;
382 ctx->redirect_uri.len = 0;
383 ctx->ssi = NULL;
384
385 return rc;
386 }
387
388
389 static ngx_int_t
390 ngx_http_perl_get_interpreter(ngx_http_perl_main_conf_t *pmcf,
391 PerlInterpreter **perl, ngx_log_t *log)
392 {
393 if (pmcf->interp) {
394 pmcf->interp--;
395
396 *perl = pmcf->free_perls[pmcf->interp];
397
398 return NGX_OK;
399 }
400
401 if (pmcf->nalloc < pmcf->interp_max) {
402 *perl = ngx_http_perl_create_interpreter(pmcf, log);
403
404 if (*perl) {
405 return NGX_OK;
406 }
407
408 return NGX_HTTP_INTERNAL_SERVER_ERROR;
409 }
410
411 ngx_log_error(NGX_LOG_ALERT, log, 0, "no free perl interpreter");
412
413 return NGX_HTTP_SERVICE_UNAVAILABLE;
414 }
415
416
417 static ngx_inline void
418 ngx_http_perl_free_interpreter(ngx_http_perl_main_conf_t *pmcf,
419 PerlInterpreter *perl)
420 {
421 pmcf->free_perls[pmcf->interp++] = perl;
422 }
423
424
425 static char *
426 ngx_http_perl_init_interpreter(ngx_conf_t *cf, ngx_http_perl_main_conf_t *pmcf)
427 {
428 ngx_pool_cleanup_t *cln;
429
430 cln = ngx_pool_cleanup_add(cf->pool, 0);
431 if (cln == NULL) {
432 return NGX_CONF_ERROR;
433 }
434
435 #ifdef NGX_PERL_MODULES
436 if (pmcf->modules.data == NULL) {
437 pmcf->modules.data = NGX_PERL_MODULES;
438 }
439 #endif
440
441 PERL_SYS_INIT(&ngx_argc, &ngx_argv);
442
443 pmcf->perl = ngx_http_perl_create_interpreter(pmcf, cf->log);
444
445 if (pmcf->perl == NULL) {
446 PERL_SYS_TERM();
447 return NGX_CONF_ERROR;
448 }
449
450 cln->handler = ngx_http_perl_cleanup_perl;
451 cln->data = pmcf->perl;
452
453 return NGX_CONF_OK;
454 }
455
456
457 static PerlInterpreter *
458 ngx_http_perl_create_interpreter(ngx_http_perl_main_conf_t *pmcf,
459 ngx_log_t *log)
460 {
461 int n;
462 char *embedding[6];
463 char **script;
464 STRLEN len;
465 ngx_str_t err;
466 ngx_uint_t i;
467 PerlInterpreter *perl;
468
469 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "create perl interpreter");
470
471 #if (NGX_HAVE_PERL_CLONE)
472
473 if (pmcf->perl) {
474
475 perl = perl_clone(pmcf->perl, CLONEf_KEEP_PTR_TABLE);
476 if (perl == NULL) {
477 ngx_log_error(NGX_LOG_ALERT, log, 0, "perl_clone() failed");
478 return NULL;
479 }
480
481 {
482
483 dTHXa(perl);
484
485 ptr_table_free(PL_ptr_table);
486 PL_ptr_table = NULL;
487
488 }
489
490 pmcf->nalloc++;
491
492 return perl;
493 }
494
495 #endif
496
497 perl = perl_alloc();
498 if (perl == NULL) {
499 ngx_log_error(NGX_LOG_ALERT, log, 0, "perl_alloc() failed");
500 return NULL;
501 }
502
503 perl_construct(perl);
504
505 {
506
507 dTHXa(perl);
508
509 #ifdef PERL_EXIT_DESTRUCT_END
510 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
511 #endif
512
513 embedding[0] = "";
514
515 if (pmcf->modules.data) {
516 embedding[1] = "-I";
517 embedding[2] = (char *) pmcf->modules.data;
518 n = 3;
519
520 } else {
521 n = 1;
522 }
523
524 embedding[n++] = "-Mnginx";
525 embedding[n++] = "-e";
526 embedding[n++] = "0";
527
528 n = perl_parse(perl, ngx_http_perl_xs_init, n, embedding, NULL);
529
530 if (n != 0) {
531 ngx_log_error(NGX_LOG_ALERT, log, 0, "perl_parse() failed: %d", n);
532 goto fail;
533 }
534
535 script = pmcf->requires.elts;
536 for (i = 0; i < pmcf->requires.nelts; i++) {
537 require_pv(script[i]);
538
539 if (SvTRUE(ERRSV)) {
540
541 err.data = (u_char *) SvPV(ERRSV, len);
542 for (len--; err.data[len] == LF || err.data[len] == CR; len--) {
543 /* void */
544 }
545 err.len = len + 1;
546
547 ngx_log_error(NGX_LOG_EMERG, log, 0,
548 "require_pv(\"%s\") failed: \"%V\"", script[i], &err);
549 goto fail;
550 }
551 }
552
553 }
554
555 pmcf->nalloc++;
556
557 return perl;
558
559 fail:
560
561 (void) perl_destruct(perl);
562
563 perl_free(perl);
564
565 return NULL;
566 }
567
568
569 #if (__INTEL_COMPILER)
570 /*
571 * disable 'declaration hides parameter "my_perl"' warning for ENTER and LEAVE
572 */
573 #pragma warning(disable:1599)
574 #endif
575
576
577 static ngx_int_t
578 ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r, SV *sub,
579 ngx_str_t **args, ngx_str_t *handler, ngx_str_t *rv)
580 {
581 SV *sv;
582 int n, status;
583 char *line;
584 STRLEN len, n_a;
585 ngx_str_t err;
586 ngx_uint_t i;
587
588 dSP;
589
590 status = 0;
591
592 ENTER;
593 SAVETMPS;
594
595 PUSHMARK(sp);
596
597 sv = sv_newmortal();
598 sv_setref_pv(sv, "nginx", r);
599 XPUSHs(sv);
600
601 if (args) {
602 for (i = 0; args[i]; i++) { /* void */ }
603
604 EXTEND(sp, (int) i);
605
606 for (i = 0; args[i]; i++) {
607 PUSHs(sv_2mortal(newSVpvn((char *) args[i]->data, args[i]->len)));
608 }
609 }
610
611 PUTBACK;
612
613 n = call_sv(sub, G_EVAL);
614
615 SPAGAIN;
616
617 if (n) {
618 if (rv == NULL) {
619 status = POPi;
620
621 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
622 "call_sv: %d", status);
623
624 } else {
625 line = POPpx;
626 rv->len = n_a;
627
628 rv->data = ngx_palloc(r->pool, n_a);
629 if (rv->data == NULL) {
630 return NGX_ERROR;
631 }
632
633 ngx_memcpy(rv->data, line, n_a);
634 }
635 }
636
637 PUTBACK;
638
639 FREETMPS;
640 LEAVE;
641
642 /* check $@ */
643
644 if (SvTRUE(ERRSV)) {
645
646 err.data = (u_char *) SvPV(ERRSV, len);
647 for (len--; err.data[len] == LF || err.data[len] == CR; len--) {
648 /* void */
649 }
650 err.len = len + 1;
651
652 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
653 "call_sv(\"%V\") failed: \"%V\"",
654 handler, &err);
655
656 if (rv) {
657 return NGX_ERROR;
658 }
659
660 return NGX_HTTP_INTERNAL_SERVER_ERROR;
661 }
662
663 if (n != 1) {
664 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
665 "call_sv(\"%V\") returned %d results", handler, n);
666 status = NGX_OK;
667 }
668
669 if (rv) {
670 return NGX_OK;
671 }
672
673 return (ngx_int_t) status;
674 }
675
676
677 #if (__INTEL_COMPILER)
678 #pragma warning(default:1599)
679 #endif
680
681
682 static void
683 ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv)
684 {
685 if (ngx_strncmp(handler->data, "sub ", 4) == 0
686 || ngx_strncmp(handler->data, "use ", 4) == 0)
687 {
688 *sv = eval_pv((char *) handler->data, FALSE);
689
690 return;
691 }
692
693 *sv = NULL;
694 }
695
696
697 static void *
698 ngx_http_perl_create_main_conf(ngx_conf_t *cf)
699 {
700 ngx_http_perl_main_conf_t *pmcf;
701
702 pmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_main_conf_t));
703 if (pmcf == NULL) {
704 return NGX_CONF_ERROR;
705 }
706
707 pmcf->interp_max = NGX_CONF_UNSET_UINT;
708
709 if (ngx_array_init(&pmcf->requires, cf->pool, 1, sizeof(u_char *))
710 != NGX_OK)
711 {
712 return NULL;
713 }
714
715 return pmcf;
716 }
717
718
719 static char *
720 ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf)
721 {
722 ngx_http_perl_main_conf_t *pmcf = conf;
723
724 #if (NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
725 ngx_conf_init_unsigned_value(pmcf->interp_max, 10);
726 #else
727 ngx_conf_init_unsigned_value(pmcf->interp_max, 1);
728 #endif
729
730 pmcf->free_perls = ngx_pcalloc(cf->pool,
731 pmcf->interp_max * sizeof(PerlInterpreter *));
732 if (pmcf->free_perls == NULL) {
733 return NGX_CONF_ERROR;
734 }
735
736 if (pmcf->perl == NULL) {
737 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
738 return NGX_CONF_ERROR;
739 }
740 }
741
742 #if !(NGX_HAVE_PERL_CLONE)
743 ngx_http_perl_free_interpreter(pmcf, pmcf->perl);
744 #endif
745
746 return NGX_CONF_OK;
747 }
748
749
750 static void
751 ngx_http_perl_cleanup_perl(void *data)
752 {
753 PerlInterpreter *perl = data;
754
755 (void) perl_destruct(perl);
756
757 perl_free(perl);
758
759 PERL_SYS_TERM();
760 }
761
762
763 static ngx_int_t
764 ngx_http_perl_preconfiguration(ngx_conf_t *cf)
765 {
766 ngx_int_t rc;
767 ngx_http_ssi_main_conf_t *smcf;
768
769 smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module);
770
771 rc = ngx_hash_add_key(&smcf->commands, &ngx_http_perl_ssi_command.name,
772 &ngx_http_perl_ssi_command, NGX_HASH_READONLY_KEY);
773
774 if (rc != NGX_OK) {
775 if (rc == NGX_BUSY) {
776 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
777 "conflicting SSI command \"%V\"",
778 &ngx_http_perl_ssi_command.name);
779 }
780
781 return NGX_ERROR;
782 }
783
784 return NGX_OK;
785 }
786
787
788 static void *
789 ngx_http_perl_create_loc_conf(ngx_conf_t *cf)
790 {
791 ngx_http_perl_loc_conf_t *plcf;
792
793 plcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_loc_conf_t));
794 if (plcf == NULL) {
795 return NGX_CONF_ERROR;
796 }
797
798 /*
799 * set by ngx_pcalloc():
800 *
801 * plcf->handler = { 0, NULL };
802 */
803
804 return plcf;
805 }
806
807
808 static char *
809 ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
810 {
811 ngx_http_perl_loc_conf_t *prev = parent;
812 ngx_http_perl_loc_conf_t *conf = child;
813
814 if (conf->sub == NULL) {
815 conf->sub = prev->sub;
816 conf->handler = prev->handler;
817 }
818
819 return NGX_CONF_OK;
820 }
821
822
823 static char *
824 ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
825 {
826 ngx_http_perl_main_conf_t *pmcf = conf;
827
828 u_char **p;
829 ngx_str_t *value;
830
831 value = cf->args->elts;
832
833 p = ngx_array_push(&pmcf->requires);
834
835 if (p == NULL) {
836 return NGX_CONF_ERROR;
837 }
838
839 *p = value[1].data;
840
841 return NGX_CONF_OK;
842 }
843
844
845 static char *
846 ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
847 {
848 ngx_http_perl_loc_conf_t *plcf = conf;
849
850 ngx_str_t *value;
851 ngx_http_core_loc_conf_t *clcf;
852 ngx_http_perl_main_conf_t *pmcf;
853
854 value = cf->args->elts;
855
856 if (plcf->handler.data) {
857 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
858 "duplicate perl handler \"%V\"", &value[1]);
859 return NGX_CONF_ERROR;
860 }
861
862 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module);
863
864 if (pmcf->perl == NULL) {
865 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
866 return NGX_CONF_ERROR;
867 }
868 }
869
870 plcf->handler = value[1];
871
872 {
873
874 dTHXa(pmcf->perl);
875
876 ngx_http_perl_eval_anon_sub(aTHX_ &value[1], &plcf->sub);
877
878 if (plcf->sub == &PL_sv_undef) {
879 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
880 "eval_pv(\"%V\") failed", &value[1]);
881 return NGX_CONF_ERROR;
882 }
883
884 if (plcf->sub == NULL) {
885 plcf->sub = newSVpvn((char *) value[1].data, value[1].len);
886 }
887
888 }
889
890 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
891 clcf->handler = ngx_http_perl_handler;
892
893 return NGX_CONF_OK;
894 }
895
896
897 static char *
898 ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
899 {
900 ngx_int_t index;
901 ngx_str_t *value;
902 ngx_http_variable_t *v;
903 ngx_http_perl_variable_t *pv;
904 ngx_http_perl_main_conf_t *pmcf;
905
906 value = cf->args->elts;
907
908 if (value[1].data[0] != '$') {
909 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
910 "invalid variable name \"%V\"", &value[1]);
911 return NGX_CONF_ERROR;
912 }
913
914 value[1].len--;
915 value[1].data++;
916
917 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGABLE);
918 if (v == NULL) {
919 return NGX_CONF_ERROR;
920 }
921
922 pv = ngx_palloc(cf->pool, sizeof(ngx_http_perl_variable_t));
923 if (pv == NULL) {
924 return NGX_CONF_ERROR;
925 }
926
927 index = ngx_http_get_variable_index(cf, &value[1]);
928 if (index == NGX_ERROR) {
929 return NGX_CONF_ERROR;
930 }
931
932 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module);
933
934 if (pmcf->perl == NULL) {
935 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
936 return NGX_CONF_ERROR;
937 }
938 }
939
940 pv->handler = value[2];
941
942 {
943
944 dTHXa(pmcf->perl);
945
946 ngx_http_perl_eval_anon_sub(aTHX_ &value[2], &pv->sub);
947
948 if (pv->sub == &PL_sv_undef) {
949 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
950 "eval_pv(\"%V\") failed", &value[2]);
951 return NGX_CONF_ERROR;
952 }
953
954 if (pv->sub == NULL) {
955 pv->sub = newSVpvn((char *) value[2].data, value[2].len);
956 }
957
958 }
959
960 v->handler = ngx_http_perl_variable;
961 v->data = (uintptr_t) pv;
962
963 return NGX_CONF_OK;
964 }
965
966
967 static char *
968 ngx_http_perl_interp_max_unsupported(ngx_conf_t *cf, void *post, void *data)
969 {
970 #if (NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
971
972 return NGX_CONF_OK;
973
974 #else
975
976 return "to use perl_interp_max you have to build perl with "
977 "-Dusemultiplicity or -Dusethreads options";
978
979 #endif
980 }