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