3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
10 #include <ngx_http_perl_module.h>
14 PerlInterpreter *perl;
18 } ngx_http_perl_main_conf_t;
24 } ngx_http_perl_loc_conf_t;
30 } ngx_http_perl_variable_t;
35 PerlInterpreter *perl;
36 } ngx_http_perl_cleanup_t;
40 static ngx_int_t ngx_http_perl_ssi(ngx_http_request_t *r,
41 ngx_http_ssi_ctx_t *ssi_ctx, ngx_str_t **params);
44 static char *ngx_http_perl_init_interpreter(ngx_conf_t *cf,
45 ngx_http_perl_main_conf_t *pmcf);
46 static PerlInterpreter *ngx_http_perl_create_interpreter(ngx_conf_t *cf,
47 ngx_http_perl_main_conf_t *pmcf);
48 static ngx_int_t ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires,
50 static ngx_int_t ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r,
51 HV *nginx, SV *sub, SV **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);
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,
60 static char *ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd,
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);
65 #if (NGX_HAVE_PERL_MULTIPLICITY)
66 static void ngx_http_perl_cleanup_perl(void *data);
69 static ngx_int_t ngx_http_perl_init_worker(ngx_cycle_t *cycle);
70 static void ngx_http_perl_exit(ngx_cycle_t *cycle);
73 static ngx_command_t ngx_http_perl_commands[] = {
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),
82 { ngx_string("perl_require"),
83 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
84 ngx_http_perl_require,
85 NGX_HTTP_MAIN_CONF_OFFSET,
90 NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1,
92 NGX_HTTP_LOC_CONF_OFFSET,
96 { ngx_string("perl_set"),
97 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2,
99 NGX_HTTP_LOC_CONF_OFFSET,
107 static ngx_http_module_t ngx_http_perl_module_ctx = {
108 ngx_http_perl_preconfiguration, /* preconfiguration */
109 NULL, /* postconfiguration */
111 ngx_http_perl_create_main_conf, /* create main configuration */
112 ngx_http_perl_init_main_conf, /* init main configuration */
114 NULL, /* create server configuration */
115 NULL, /* merge server configuration */
117 ngx_http_perl_create_loc_conf, /* create location configuration */
118 ngx_http_perl_merge_loc_conf /* merge location configuration */
122 ngx_module_t ngx_http_perl_module = {
124 &ngx_http_perl_module_ctx, /* module context */
125 ngx_http_perl_commands, /* module directives */
126 NGX_HTTP_MODULE, /* module type */
127 NULL, /* init master */
128 NULL, /* init module */
129 ngx_http_perl_init_worker, /* init process */
130 NULL, /* init thread */
131 NULL, /* exit thread */
132 NULL, /* exit process */
133 ngx_http_perl_exit, /* exit master */
134 NGX_MODULE_V1_PADDING
140 #define NGX_HTTP_PERL_SSI_SUB 0
141 #define NGX_HTTP_PERL_SSI_ARG 1
144 static ngx_http_ssi_param_t ngx_http_perl_ssi_params[] = {
145 { ngx_string("sub"), NGX_HTTP_PERL_SSI_SUB, 1, 0 },
146 { ngx_string("arg"), NGX_HTTP_PERL_SSI_ARG, 0, 1 },
147 { ngx_null_string, 0, 0, 0 }
150 static ngx_http_ssi_command_t ngx_http_perl_ssi_command = {
151 ngx_string("perl"), ngx_http_perl_ssi, ngx_http_perl_ssi_params, 0, 0, 1
157 static ngx_str_t ngx_null_name = ngx_null_string;
160 static HV *nginx_stash;
163 ngx_http_perl_xs_init(pTHX)
165 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__);
167 nginx_stash = gv_stashpv("nginx", TRUE);
172 ngx_http_perl_handler(ngx_http_request_t *r)
175 if (r->zero_in_uri) {
176 return NGX_HTTP_NOT_FOUND;
179 ngx_http_perl_handle_request(r);
186 ngx_http_perl_handle_request(ngx_http_request_t *r)
190 ngx_str_t uri, args, *handler;
191 ngx_http_perl_ctx_t *ctx;
192 ngx_http_perl_loc_conf_t *plcf;
193 ngx_http_perl_main_conf_t *pmcf;
195 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl handler");
197 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
200 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t));
202 ngx_http_finalize_request(r, NGX_ERROR);
206 ngx_http_set_ctx(r, ctx, ngx_http_perl_module);
209 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
214 PERL_SET_CONTEXT(pmcf->perl);
216 if (ctx->next == NULL) {
217 plcf = ngx_http_get_module_loc_conf(r, ngx_http_perl_module);
219 handler = &plcf->handler;
223 handler = &ngx_null_name;
227 rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, sub, NULL, handler,
232 if (rc == NGX_DONE) {
240 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
241 "perl handler done: %i", rc);
243 if (ctx->redirect_uri.len) {
244 uri = ctx->redirect_uri;
245 args = ctx->redirect_args;
251 ctx->filename.data = NULL;
252 ctx->redirect_uri.len = 0;
254 if (ctx->done || ctx->next) {
259 ngx_http_internal_redirect(r, &uri, &args);
263 if (rc == NGX_OK || rc == NGX_HTTP_OK) {
264 ngx_http_send_special(r, NGX_HTTP_LAST);
268 ngx_http_finalize_request(r, rc);
273 ngx_http_perl_sleep_handler(ngx_http_request_t *r)
277 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
278 "perl sleep handler");
280 wev = r->connection->write;
284 ngx_http_perl_handle_request(r);
288 if (ngx_handle_write_event(wev, 0) != NGX_OK) {
289 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
295 ngx_http_perl_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
298 ngx_http_perl_variable_t *pv = (ngx_http_perl_variable_t *) data;
302 ngx_http_perl_ctx_t *ctx;
303 ngx_http_perl_main_conf_t *pmcf;
305 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
306 "perl variable handler");
308 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
311 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t));
316 ngx_http_set_ctx(r, ctx, ngx_http_perl_module);
319 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
326 PERL_SET_CONTEXT(pmcf->perl);
328 rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, pv->sub, NULL,
329 &pv->handler, &value);
338 v->data = value.data;
344 ctx->filename.data = NULL;
345 ctx->redirect_uri.len = 0;
347 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
348 "perl variable done");
357 ngx_http_perl_ssi(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ssi_ctx,
362 ngx_str_t *handler, **args;
364 ngx_http_perl_ctx_t *ctx;
365 ngx_http_perl_main_conf_t *pmcf;
367 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
370 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
373 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t));
378 ngx_http_set_ctx(r, ctx, ngx_http_perl_module);
381 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
385 handler = params[NGX_HTTP_PERL_SSI_SUB];
386 handler->data[handler->len] = '\0';
391 PERL_SET_CONTEXT(pmcf->perl);
395 /* the code is disabled to force the precompiled perl code using only */
397 ngx_http_perl_eval_anon_sub(aTHX_ handler, &sv);
399 if (sv == &PL_sv_undef) {
400 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
401 "eval_pv(\"%V\") failed", handler);
406 sv = newSVpvn((char *) handler->data, handler->len);
411 sv = newSVpvn((char *) handler->data, handler->len);
413 args = ¶ms[NGX_HTTP_PERL_SSI_ARG];
417 for (i = 0; args[i]; i++) { /* void */ }
419 asv = ngx_pcalloc(r->pool, (i + 1) * sizeof(SV *));
428 for (i = 0; args[i]; i++) {
429 asv[i + 1] = newSVpvn((char *) args[i]->data, args[i]->len);
436 rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, sv, asv, handler,
443 ctx->filename.data = NULL;
444 ctx->redirect_uri.len = 0;
447 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl ssi done");
456 ngx_http_perl_init_interpreter(ngx_conf_t *cf, ngx_http_perl_main_conf_t *pmcf)
458 #if (NGX_HAVE_PERL_MULTIPLICITY)
459 ngx_pool_cleanup_t *cln;
461 cln = ngx_pool_cleanup_add(cf->pool, 0);
463 return NGX_CONF_ERROR;
467 static PerlInterpreter *perl;
470 #ifdef NGX_PERL_MODULES
471 if (pmcf->modules.data == NULL) {
472 pmcf->modules.data = NGX_PERL_MODULES;
476 if (pmcf->modules.data) {
477 if (ngx_conf_full_name(cf->cycle, &pmcf->modules, 0) != NGX_OK) {
478 return NGX_CONF_ERROR;
482 #if !(NGX_HAVE_PERL_MULTIPLICITY)
486 if (ngx_set_environment(cf->cycle, NULL) == NULL) {
487 return NGX_CONF_ERROR;
490 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log)
493 return NGX_CONF_ERROR;
497 pmcf->nginx = nginx_stash;
504 if (nginx_stash == NULL) {
505 PERL_SYS_INIT(&ngx_argc, &ngx_argv);
508 pmcf->perl = ngx_http_perl_create_interpreter(cf, pmcf);
510 if (pmcf->perl == NULL) {
511 return NGX_CONF_ERROR;
514 pmcf->nginx = nginx_stash;
516 #if (NGX_HAVE_PERL_MULTIPLICITY)
518 cln->handler = ngx_http_perl_cleanup_perl;
519 cln->data = pmcf->perl;
531 static PerlInterpreter *
532 ngx_http_perl_create_interpreter(ngx_conf_t *cf,
533 ngx_http_perl_main_conf_t *pmcf)
538 char *ver, *embedding[6];
539 PerlInterpreter *perl;
541 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "create perl interpreter");
543 if (ngx_set_environment(cf->cycle, NULL) == NULL) {
549 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_alloc() failed");
556 PERL_SET_CONTEXT(perl);
558 perl_construct(perl);
560 #ifdef PERL_EXIT_DESTRUCT_END
561 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
566 if (pmcf->modules.data) {
568 embedding[2] = (char *) pmcf->modules.data;
575 embedding[n++] = "-Mnginx";
576 embedding[n++] = "-e";
577 embedding[n++] = "0";
579 n = perl_parse(perl, ngx_http_perl_xs_init, n, embedding, NULL);
582 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_parse() failed: %d", n);
586 sv = get_sv("nginx::VERSION", FALSE);
589 if (ngx_strcmp(ver, NGINX_VERSION) != 0) {
590 ngx_log_error(NGX_LOG_ALERT, cf->log, 0,
591 "version " NGINX_VERSION " of nginx.pm is required, "
592 "but %s was found", ver);
596 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log) != NGX_OK) {
606 (void) perl_destruct(perl);
615 ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires, ngx_log_t *log)
622 script = requires->elts;
623 for (i = 0; i < requires->nelts; i++) {
625 require_pv(script[i]);
629 err.data = (u_char *) SvPV(ERRSV, len);
630 for (len--; err.data[len] == LF || err.data[len] == CR; len--) {
635 ngx_log_error(NGX_LOG_EMERG, log, 0,
636 "require_pv(\"%s\") failed: \"%V\"", script[i], &err);
647 ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r, HV *nginx, SV *sub,
648 SV **args, ngx_str_t *handler, ngx_str_t *rv)
667 sv = sv_2mortal(sv_bless(newRV_noinc(newSViv(PTR2IV(r))), nginx));
671 EXTEND(sp, (intptr_t) args[0]);
673 for (i = 1; i <= (ngx_uint_t) args[0]; i++) {
674 PUSHs(sv_2mortal(args[i]));
682 n = call_sv(sub, G_EVAL);
699 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
700 "call_sv: %d", status);
703 line = SvPVx(POPs, n_a);
706 rv->data = ngx_pnalloc(r->pool, n_a);
707 if (rv->data == NULL) {
711 ngx_memcpy(rv->data, line, n_a);
724 err.data = (u_char *) SvPV(ERRSV, len);
725 for (len--; err.data[len] == LF || err.data[len] == CR; len--) {
730 ngx_log_error(NGX_LOG_ERR, c->log, 0,
731 "call_sv(\"%V\") failed: \"%V\"", handler, &err);
737 return NGX_HTTP_INTERNAL_SERVER_ERROR;
741 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
742 "call_sv(\"%V\") returned %d results", handler, n);
750 return (ngx_int_t) status;
755 ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv)
759 for (p = handler->data; *p; p++) {
760 if (*p != ' ' && *p != '\t' && *p != CR && *p != LF) {
765 if (ngx_strncmp(p, "sub ", 4) == 0 || ngx_strncmp(p, "use ", 4) == 0) {
766 *sv = eval_pv((char *) p, FALSE);
768 /* eval_pv() does not set ERRSV on failure */
778 ngx_http_perl_create_main_conf(ngx_conf_t *cf)
780 ngx_http_perl_main_conf_t *pmcf;
782 pmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_main_conf_t));
784 return NGX_CONF_ERROR;
787 if (ngx_array_init(&pmcf->requires, cf->pool, 1, sizeof(u_char *))
798 ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf)
800 ngx_http_perl_main_conf_t *pmcf = conf;
802 if (pmcf->perl == NULL) {
803 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
804 return NGX_CONF_ERROR;
812 #if (NGX_HAVE_PERL_MULTIPLICITY)
815 ngx_http_perl_cleanup_perl(void *data)
817 PerlInterpreter *perl = data;
819 PERL_SET_CONTEXT(perl);
821 (void) perl_destruct(perl);
830 ngx_http_perl_preconfiguration(ngx_conf_t *cf)
834 ngx_http_ssi_main_conf_t *smcf;
836 smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module);
838 rc = ngx_hash_add_key(&smcf->commands, &ngx_http_perl_ssi_command.name,
839 &ngx_http_perl_ssi_command, NGX_HASH_READONLY_KEY);
842 if (rc == NGX_BUSY) {
843 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
844 "conflicting SSI command \"%V\"",
845 &ngx_http_perl_ssi_command.name);
857 ngx_http_perl_create_loc_conf(ngx_conf_t *cf)
859 ngx_http_perl_loc_conf_t *plcf;
861 plcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_loc_conf_t));
863 return NGX_CONF_ERROR;
867 * set by ngx_pcalloc():
869 * plcf->handler = { 0, NULL };
877 ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
879 ngx_http_perl_loc_conf_t *prev = parent;
880 ngx_http_perl_loc_conf_t *conf = child;
882 if (conf->sub == NULL) {
883 conf->sub = prev->sub;
884 conf->handler = prev->handler;
892 ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
894 ngx_http_perl_main_conf_t *pmcf = conf;
899 value = cf->args->elts;
901 p = ngx_array_push(&pmcf->requires);
904 return NGX_CONF_ERROR;
914 ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
916 ngx_http_perl_loc_conf_t *plcf = conf;
919 ngx_http_core_loc_conf_t *clcf;
920 ngx_http_perl_main_conf_t *pmcf;
922 value = cf->args->elts;
924 if (plcf->handler.data) {
925 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
926 "duplicate perl handler \"%V\"", &value[1]);
927 return NGX_CONF_ERROR;
930 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module);
932 if (pmcf->perl == NULL) {
933 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
934 return NGX_CONF_ERROR;
938 plcf->handler = value[1];
943 PERL_SET_CONTEXT(pmcf->perl);
945 ngx_http_perl_eval_anon_sub(aTHX_ &value[1], &plcf->sub);
947 if (plcf->sub == &PL_sv_undef) {
948 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
949 "eval_pv(\"%V\") failed", &value[1]);
950 return NGX_CONF_ERROR;
953 if (plcf->sub == NULL) {
954 plcf->sub = newSVpvn((char *) value[1].data, value[1].len);
959 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
960 clcf->handler = ngx_http_perl_handler;
967 ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
971 ngx_http_variable_t *v;
972 ngx_http_perl_variable_t *pv;
973 ngx_http_perl_main_conf_t *pmcf;
975 value = cf->args->elts;
977 if (value[1].data[0] != '$') {
978 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
979 "invalid variable name \"%V\"", &value[1]);
980 return NGX_CONF_ERROR;
986 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE);
988 return NGX_CONF_ERROR;
991 pv = ngx_palloc(cf->pool, sizeof(ngx_http_perl_variable_t));
993 return NGX_CONF_ERROR;
996 index = ngx_http_get_variable_index(cf, &value[1]);
997 if (index == NGX_ERROR) {
998 return NGX_CONF_ERROR;
1001 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module);
1003 if (pmcf->perl == NULL) {
1004 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
1005 return NGX_CONF_ERROR;
1009 pv->handler = value[2];
1014 PERL_SET_CONTEXT(pmcf->perl);
1016 ngx_http_perl_eval_anon_sub(aTHX_ &value[2], &pv->sub);
1018 if (pv->sub == &PL_sv_undef) {
1019 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
1020 "eval_pv(\"%V\") failed", &value[2]);
1021 return NGX_CONF_ERROR;
1024 if (pv->sub == NULL) {
1025 pv->sub = newSVpvn((char *) value[2].data, value[2].len);
1030 v->get_handler = ngx_http_perl_variable;
1031 v->data = (uintptr_t) pv;
1038 ngx_http_perl_init_worker(ngx_cycle_t *cycle)
1040 ngx_http_perl_main_conf_t *pmcf;
1042 pmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_perl_module);
1047 PERL_SET_CONTEXT(pmcf->perl);
1049 /* set worker's $$ */
1051 sv_setiv(GvSV(gv_fetchpv("$", TRUE, SVt_PV)), (I32) ngx_pid);
1060 ngx_http_perl_exit(ngx_cycle_t *cycle)
1062 ngx_http_perl_main_conf_t *pmcf;
1064 pmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_perl_module);
1069 PERL_SET_CONTEXT(pmcf->perl);