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