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