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