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