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;
|
575
|
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,
|
575
|
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,
|
575
|
76 ngx_conf_set_str_array_slot,
|
148
|
77 NGX_HTTP_MAIN_CONF_OFFSET,
|
575
|
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
|
593
|
149 static ngx_str_t ngx_null_name = ngx_null_string;
|
|
150 static HV *nginx_stash;
|
230
|
151
|
520
|
152 #if (NGX_HAVE_PERL_MULTIPLICITY)
|
593
|
153 static ngx_uint_t ngx_perl_term;
|
|
154 #else
|
|
155 static PerlInterpreter *perl;
|
520
|
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
|
564
|
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
|
564
|
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) {
|
566
|
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) {
|
566
|
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);
|
566
|
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
|
459
|
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 {
|
593
|
459 ngx_str_t *m;
|
|
460 ngx_uint_t i;
|
258
|
461 #if (NGX_HAVE_PERL_MULTIPLICITY)
|
593
|
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
|
575
|
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
|
613
|
484 ngx_str_set(m, NGX_PERL_MODULES);
|
148
|
485 }
|
|
486 #endif
|
|
487
|
575
|
488 if (pmcf->modules != NGX_CONF_UNSET_PTR) {
|
|
489 m = pmcf->modules->elts;
|
|
490 for (i = 0; i < pmcf->modules->nelts; i++) {
|
|
491 if (ngx_conf_full_name(cf->cycle, &m[i], 0) != NGX_OK) {
|
|
492 return NGX_CONF_ERROR;
|
|
493 }
|
188
|
494 }
|
178
|
495 }
|
|
496
|
258
|
497 #if !(NGX_HAVE_PERL_MULTIPLICITY)
|
198
|
498
|
|
499 if (perl) {
|
296
|
500
|
|
501 if (ngx_set_environment(cf->cycle, NULL) == NULL) {
|
|
502 return NGX_CONF_ERROR;
|
|
503 }
|
|
504
|
575
|
505 if (ngx_http_perl_run_requires(aTHX_ pmcf->requires, cf->log)
|
202
|
506 != NGX_OK)
|
|
507 {
|
198
|
508 return NGX_CONF_ERROR;
|
|
509 }
|
|
510
|
|
511 pmcf->perl = perl;
|
296
|
512 pmcf->nginx = nginx_stash;
|
198
|
513
|
|
514 return NGX_CONF_OK;
|
|
515 }
|
|
516
|
|
517 #endif
|
|
518
|
306
|
519 if (nginx_stash == NULL) {
|
|
520 PERL_SYS_INIT(&ngx_argc, &ngx_argv);
|
|
521 }
|
148
|
522
|
278
|
523 pmcf->perl = ngx_http_perl_create_interpreter(cf, pmcf);
|
148
|
524
|
|
525 if (pmcf->perl == NULL) {
|
|
526 return NGX_CONF_ERROR;
|
|
527 }
|
|
528
|
258
|
529 pmcf->nginx = nginx_stash;
|
|
530
|
|
531 #if (NGX_HAVE_PERL_MULTIPLICITY)
|
198
|
532
|
148
|
533 cln->handler = ngx_http_perl_cleanup_perl;
|
|
534 cln->data = pmcf->perl;
|
|
535
|
198
|
536 #else
|
|
537
|
|
538 perl = pmcf->perl;
|
|
539
|
|
540 #endif
|
|
541
|
148
|
542 return NGX_CONF_OK;
|
|
543 }
|
|
544
|
|
545
|
|
546 static PerlInterpreter *
|
278
|
547 ngx_http_perl_create_interpreter(ngx_conf_t *cf,
|
|
548 ngx_http_perl_main_conf_t *pmcf)
|
148
|
549 {
|
|
550 int n;
|
248
|
551 STRLEN len;
|
|
552 SV *sv;
|
575
|
553 char *ver, **embedding;
|
|
554 ngx_str_t *m;
|
|
555 ngx_uint_t i;
|
148
|
556 PerlInterpreter *perl;
|
|
557
|
278
|
558 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "create perl interpreter");
|
|
559
|
|
560 if (ngx_set_environment(cf->cycle, NULL) == NULL) {
|
|
561 return NULL;
|
|
562 }
|
148
|
563
|
|
564 perl = perl_alloc();
|
|
565 if (perl == NULL) {
|
278
|
566 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_alloc() failed");
|
148
|
567 return NULL;
|
|
568 }
|
|
569
|
|
570 {
|
|
571
|
|
572 dTHXa(perl);
|
258
|
573 PERL_SET_CONTEXT(perl);
|
|
574
|
|
575 perl_construct(perl);
|
148
|
576
|
|
577 #ifdef PERL_EXIT_DESTRUCT_END
|
|
578 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
|
|
579 #endif
|
|
580
|
575
|
581 n = (pmcf->modules != NGX_CONF_UNSET_PTR) ? pmcf->modules->nelts * 2 : 0;
|
|
582
|
|
583 embedding = ngx_palloc(cf->pool, (4 + n) * sizeof(char *));
|
|
584 if (embedding == NULL) {
|
|
585 goto fail;
|
|
586 }
|
|
587
|
148
|
588 embedding[0] = "";
|
|
589
|
575
|
590 if (n++) {
|
|
591 m = pmcf->modules->elts;
|
|
592 for (i = 0; i < pmcf->modules->nelts; i++) {
|
|
593 embedding[2 * i + 1] = "-I";
|
|
594 embedding[2 * i + 2] = (char *) m[i].data;
|
|
595 }
|
148
|
596 }
|
|
597
|
|
598 embedding[n++] = "-Mnginx";
|
|
599 embedding[n++] = "-e";
|
|
600 embedding[n++] = "0";
|
|
601
|
|
602 n = perl_parse(perl, ngx_http_perl_xs_init, n, embedding, NULL);
|
|
603
|
|
604 if (n != 0) {
|
278
|
605 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_parse() failed: %d", n);
|
148
|
606 goto fail;
|
|
607 }
|
|
608
|
248
|
609 sv = get_sv("nginx::VERSION", FALSE);
|
|
610 ver = SvPV(sv, len);
|
|
611
|
|
612 if (ngx_strcmp(ver, NGINX_VERSION) != 0) {
|
278
|
613 ngx_log_error(NGX_LOG_ALERT, cf->log, 0,
|
248
|
614 "version " NGINX_VERSION " of nginx.pm is required, "
|
|
615 "but %s was found", ver);
|
|
616 goto fail;
|
|
617 }
|
|
618
|
575
|
619 if (ngx_http_perl_run_requires(aTHX_ pmcf->requires, cf->log) != NGX_OK) {
|
198
|
620 goto fail;
|
148
|
621 }
|
|
622
|
|
623 }
|
|
624
|
|
625 return perl;
|
|
626
|
|
627 fail:
|
|
628
|
|
629 (void) perl_destruct(perl);
|
|
630
|
|
631 perl_free(perl);
|
|
632
|
|
633 return NULL;
|
|
634 }
|
|
635
|
|
636
|
198
|
637 static ngx_int_t
|
202
|
638 ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires, ngx_log_t *log)
|
198
|
639 {
|
575
|
640 u_char *err;
|
198
|
641 STRLEN len;
|
575
|
642 ngx_str_t *script;
|
198
|
643 ngx_uint_t i;
|
|
644
|
575
|
645 if (requires == NGX_CONF_UNSET_PTR) {
|
|
646 return NGX_OK;
|
|
647 }
|
|
648
|
198
|
649 script = requires->elts;
|
|
650 for (i = 0; i < requires->nelts; i++) {
|
|
651
|
575
|
652 require_pv((char *) script[i].data);
|
198
|
653
|
|
654 if (SvTRUE(ERRSV)) {
|
|
655
|
575
|
656 err = (u_char *) SvPV(ERRSV, len);
|
|
657 while (--len && (err[len] == CR || err[len] == LF)) { /* void */ }
|
198
|
658
|
|
659 ngx_log_error(NGX_LOG_EMERG, log, 0,
|
575
|
660 "require_pv(\"%s\") failed: \"%*s\"",
|
|
661 script[i].data, len + 1, err);
|
198
|
662
|
|
663 return NGX_ERROR;
|
|
664 }
|
|
665 }
|
|
666
|
|
667 return NGX_OK;
|
|
668 }
|
|
669
|
|
670
|
148
|
671 static ngx_int_t
|
258
|
672 ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r, HV *nginx, SV *sub,
|
366
|
673 SV **args, ngx_str_t *handler, ngx_str_t *rv)
|
148
|
674 {
|
356
|
675 SV *sv;
|
|
676 int n, status;
|
|
677 char *line;
|
575
|
678 u_char *err;
|
356
|
679 STRLEN len, n_a;
|
|
680 ngx_uint_t i;
|
|
681 ngx_connection_t *c;
|
148
|
682
|
|
683 dSP;
|
|
684
|
|
685 status = 0;
|
|
686
|
|
687 ENTER;
|
|
688 SAVETMPS;
|
|
689
|
|
690 PUSHMARK(sp);
|
|
691
|
258
|
692 sv = sv_2mortal(sv_bless(newRV_noinc(newSViv(PTR2IV(r))), nginx));
|
148
|
693 XPUSHs(sv);
|
|
694
|
|
695 if (args) {
|
370
|
696 EXTEND(sp, (intptr_t) args[0]);
|
148
|
697
|
366
|
698 for (i = 1; i <= (ngx_uint_t) args[0]; i++) {
|
|
699 PUSHs(sv_2mortal(args[i]));
|
148
|
700 }
|
|
701 }
|
|
702
|
|
703 PUTBACK;
|
|
704
|
356
|
705 c = r->connection;
|
|
706
|
148
|
707 n = call_sv(sub, G_EVAL);
|
|
708
|
|
709 SPAGAIN;
|
|
710
|
|
711 if (n) {
|
|
712 if (rv == NULL) {
|
|
713 status = POPi;
|
|
714
|
356
|
715 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
148
|
716 "call_sv: %d", status);
|
|
717
|
|
718 } else {
|
168
|
719 line = SvPVx(POPs, n_a);
|
148
|
720 rv->len = n_a;
|
|
721
|
382
|
722 rv->data = ngx_pnalloc(r->pool, n_a);
|
148
|
723 if (rv->data == NULL) {
|
|
724 return NGX_ERROR;
|
|
725 }
|
|
726
|
|
727 ngx_memcpy(rv->data, line, n_a);
|
|
728 }
|
|
729 }
|
|
730
|
|
731 PUTBACK;
|
|
732
|
|
733 FREETMPS;
|
|
734 LEAVE;
|
|
735
|
|
736 /* check $@ */
|
|
737
|
|
738 if (SvTRUE(ERRSV)) {
|
|
739
|
575
|
740 err = (u_char *) SvPV(ERRSV, len);
|
|
741 while (--len && (err[len] == CR || err[len] == LF)) { /* void */ }
|
148
|
742
|
356
|
743 ngx_log_error(NGX_LOG_ERR, c->log, 0,
|
575
|
744 "call_sv(\"%V\") failed: \"%*s\"", handler, len + 1, err);
|
148
|
745
|
|
746 if (rv) {
|
|
747 return NGX_ERROR;
|
|
748 }
|
|
749
|
|
750 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
751 }
|
|
752
|
|
753 if (n != 1) {
|
356
|
754 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
|
148
|
755 "call_sv(\"%V\") returned %d results", handler, n);
|
|
756 status = NGX_OK;
|
|
757 }
|
|
758
|
|
759 if (rv) {
|
|
760 return NGX_OK;
|
|
761 }
|
|
762
|
|
763 return (ngx_int_t) status;
|
|
764 }
|
|
765
|
|
766
|
|
767 static void
|
|
768 ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv)
|
|
769 {
|
172
|
770 u_char *p;
|
|
771
|
|
772 for (p = handler->data; *p; p++) {
|
|
773 if (*p != ' ' && *p != '\t' && *p != CR && *p != LF) {
|
|
774 break;
|
|
775 }
|
|
776 }
|
|
777
|
568
|
778 if (ngx_strncmp(p, "sub ", 4) == 0
|
|
779 || ngx_strncmp(p, "sub{", 4) == 0
|
|
780 || ngx_strncmp(p, "use ", 4) == 0)
|
|
781 {
|
172
|
782 *sv = eval_pv((char *) p, FALSE);
|
258
|
783
|
|
784 /* eval_pv() does not set ERRSV on failure */
|
|
785
|
148
|
786 return;
|
|
787 }
|
|
788
|
|
789 *sv = NULL;
|
|
790 }
|
|
791
|
|
792
|
|
793 static void *
|
|
794 ngx_http_perl_create_main_conf(ngx_conf_t *cf)
|
|
795 {
|
|
796 ngx_http_perl_main_conf_t *pmcf;
|
|
797
|
|
798 pmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_main_conf_t));
|
|
799 if (pmcf == NULL) {
|
536
|
800 return NULL;
|
148
|
801 }
|
|
802
|
575
|
803 pmcf->modules = NGX_CONF_UNSET_PTR;
|
|
804 pmcf->requires = NGX_CONF_UNSET_PTR;
|
148
|
805
|
|
806 return pmcf;
|
|
807 }
|
|
808
|
|
809
|
|
810 static char *
|
|
811 ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf)
|
|
812 {
|
|
813 ngx_http_perl_main_conf_t *pmcf = conf;
|
|
814
|
|
815 if (pmcf->perl == NULL) {
|
|
816 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
|
|
817 return NGX_CONF_ERROR;
|
|
818 }
|
|
819 }
|
|
820
|
|
821 return NGX_CONF_OK;
|
|
822 }
|
|
823
|
|
824
|
258
|
825 #if (NGX_HAVE_PERL_MULTIPLICITY)
|
198
|
826
|
148
|
827 static void
|
|
828 ngx_http_perl_cleanup_perl(void *data)
|
|
829 {
|
198
|
830 PerlInterpreter *perl = data;
|
148
|
831
|
258
|
832 PERL_SET_CONTEXT(perl);
|
|
833
|
148
|
834 (void) perl_destruct(perl);
|
|
835
|
|
836 perl_free(perl);
|
520
|
837
|
|
838 if (ngx_perl_term) {
|
|
839 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0, "perl term");
|
|
840
|
|
841 PERL_SYS_TERM();
|
|
842 }
|
148
|
843 }
|
|
844
|
198
|
845 #endif
|
|
846
|
|
847
|
148
|
848 static ngx_int_t
|
|
849 ngx_http_perl_preconfiguration(ngx_conf_t *cf)
|
|
850 {
|
166
|
851 #if (NGX_HTTP_SSI)
|
148
|
852 ngx_int_t rc;
|
|
853 ngx_http_ssi_main_conf_t *smcf;
|
|
854
|
|
855 smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module);
|
|
856
|
|
857 rc = ngx_hash_add_key(&smcf->commands, &ngx_http_perl_ssi_command.name,
|
|
858 &ngx_http_perl_ssi_command, NGX_HASH_READONLY_KEY);
|
|
859
|
|
860 if (rc != NGX_OK) {
|
|
861 if (rc == NGX_BUSY) {
|
|
862 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
863 "conflicting SSI command \"%V\"",
|
|
864 &ngx_http_perl_ssi_command.name);
|
|
865 }
|
|
866
|
|
867 return NGX_ERROR;
|
|
868 }
|
166
|
869 #endif
|
148
|
870
|
|
871 return NGX_OK;
|
|
872 }
|
|
873
|
|
874
|
|
875 static void *
|
|
876 ngx_http_perl_create_loc_conf(ngx_conf_t *cf)
|
|
877 {
|
|
878 ngx_http_perl_loc_conf_t *plcf;
|
|
879
|
|
880 plcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_loc_conf_t));
|
|
881 if (plcf == NULL) {
|
536
|
882 return NULL;
|
148
|
883 }
|
|
884
|
|
885 /*
|
|
886 * set by ngx_pcalloc():
|
|
887 *
|
|
888 * plcf->handler = { 0, NULL };
|
|
889 */
|
|
890
|
|
891 return plcf;
|
|
892 }
|
|
893
|
|
894
|
|
895 static char *
|
|
896 ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
897 {
|
|
898 ngx_http_perl_loc_conf_t *prev = parent;
|
|
899 ngx_http_perl_loc_conf_t *conf = child;
|
|
900
|
|
901 if (conf->sub == NULL) {
|
|
902 conf->sub = prev->sub;
|
|
903 conf->handler = prev->handler;
|
|
904 }
|
|
905
|
|
906 return NGX_CONF_OK;
|
|
907 }
|
|
908
|
|
909
|
|
910 static char *
|
|
911 ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
912 {
|
|
913 ngx_http_perl_loc_conf_t *plcf = conf;
|
|
914
|
|
915 ngx_str_t *value;
|
|
916 ngx_http_core_loc_conf_t *clcf;
|
|
917 ngx_http_perl_main_conf_t *pmcf;
|
|
918
|
|
919 value = cf->args->elts;
|
|
920
|
|
921 if (plcf->handler.data) {
|
|
922 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
923 "duplicate perl handler \"%V\"", &value[1]);
|
|
924 return NGX_CONF_ERROR;
|
|
925 }
|
|
926
|
|
927 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module);
|
|
928
|
|
929 if (pmcf->perl == NULL) {
|
|
930 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
|
|
931 return NGX_CONF_ERROR;
|
|
932 }
|
|
933 }
|
|
934
|
|
935 plcf->handler = value[1];
|
|
936
|
|
937 {
|
|
938
|
|
939 dTHXa(pmcf->perl);
|
258
|
940 PERL_SET_CONTEXT(pmcf->perl);
|
148
|
941
|
|
942 ngx_http_perl_eval_anon_sub(aTHX_ &value[1], &plcf->sub);
|
|
943
|
|
944 if (plcf->sub == &PL_sv_undef) {
|
|
945 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
|
|
946 "eval_pv(\"%V\") failed", &value[1]);
|
|
947 return NGX_CONF_ERROR;
|
|
948 }
|
|
949
|
|
950 if (plcf->sub == NULL) {
|
|
951 plcf->sub = newSVpvn((char *) value[1].data, value[1].len);
|
|
952 }
|
|
953
|
|
954 }
|
|
955
|
|
956 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
957 clcf->handler = ngx_http_perl_handler;
|
|
958
|
|
959 return NGX_CONF_OK;
|
|
960 }
|
|
961
|
|
962
|
|
963 static char *
|
|
964 ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
965 {
|
|
966 ngx_int_t index;
|
|
967 ngx_str_t *value;
|
|
968 ngx_http_variable_t *v;
|
|
969 ngx_http_perl_variable_t *pv;
|
|
970 ngx_http_perl_main_conf_t *pmcf;
|
|
971
|
|
972 value = cf->args->elts;
|
|
973
|
|
974 if (value[1].data[0] != '$') {
|
|
975 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
976 "invalid variable name \"%V\"", &value[1]);
|
|
977 return NGX_CONF_ERROR;
|
|
978 }
|
|
979
|
|
980 value[1].len--;
|
|
981 value[1].data++;
|
|
982
|
340
|
983 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE);
|
148
|
984 if (v == NULL) {
|
|
985 return NGX_CONF_ERROR;
|
|
986 }
|
|
987
|
|
988 pv = ngx_palloc(cf->pool, sizeof(ngx_http_perl_variable_t));
|
|
989 if (pv == NULL) {
|
|
990 return NGX_CONF_ERROR;
|
|
991 }
|
|
992
|
|
993 index = ngx_http_get_variable_index(cf, &value[1]);
|
|
994 if (index == NGX_ERROR) {
|
|
995 return NGX_CONF_ERROR;
|
|
996 }
|
|
997
|
|
998 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module);
|
|
999
|
|
1000 if (pmcf->perl == NULL) {
|
|
1001 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
|
|
1002 return NGX_CONF_ERROR;
|
|
1003 }
|
|
1004 }
|
|
1005
|
|
1006 pv->handler = value[2];
|
|
1007
|
|
1008 {
|
|
1009
|
|
1010 dTHXa(pmcf->perl);
|
258
|
1011 PERL_SET_CONTEXT(pmcf->perl);
|
148
|
1012
|
|
1013 ngx_http_perl_eval_anon_sub(aTHX_ &value[2], &pv->sub);
|
|
1014
|
|
1015 if (pv->sub == &PL_sv_undef) {
|
|
1016 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
|
|
1017 "eval_pv(\"%V\") failed", &value[2]);
|
|
1018 return NGX_CONF_ERROR;
|
|
1019 }
|
|
1020
|
|
1021 if (pv->sub == NULL) {
|
|
1022 pv->sub = newSVpvn((char *) value[2].data, value[2].len);
|
|
1023 }
|
|
1024
|
|
1025 }
|
|
1026
|
186
|
1027 v->get_handler = ngx_http_perl_variable;
|
148
|
1028 v->data = (uintptr_t) pv;
|
|
1029
|
|
1030 return NGX_CONF_OK;
|
|
1031 }
|
306
|
1032
|
|
1033
|
314
|
1034 static ngx_int_t
|
|
1035 ngx_http_perl_init_worker(ngx_cycle_t *cycle)
|
|
1036 {
|
|
1037 ngx_http_perl_main_conf_t *pmcf;
|
|
1038
|
|
1039 pmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_perl_module);
|
|
1040
|
520
|
1041 if (pmcf) {
|
|
1042 dTHXa(pmcf->perl);
|
|
1043 PERL_SET_CONTEXT(pmcf->perl);
|
314
|
1044
|
520
|
1045 /* set worker's $$ */
|
314
|
1046
|
520
|
1047 sv_setiv(GvSV(gv_fetchpv("$", TRUE, SVt_PV)), (I32) ngx_pid);
|
314
|
1048 }
|
|
1049
|
|
1050 return NGX_OK;
|
|
1051 }
|
|
1052
|
370
|
1053
|
306
|
1054 static void
|
|
1055 ngx_http_perl_exit(ngx_cycle_t *cycle)
|
|
1056 {
|
520
|
1057 #if (NGX_HAVE_PERL_MULTIPLICITY)
|
|
1058
|
593
|
1059 /*
|
|
1060 * the master exit hook is run before global pool cleanup,
|
|
1061 * therefore just set flag here
|
|
1062 */
|
|
1063
|
520
|
1064 ngx_perl_term = 1;
|
|
1065
|
|
1066 #else
|
370
|
1067
|
593
|
1068 if (nginx_stash) {
|
520
|
1069 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cycle->log, 0, "perl term");
|
370
|
1070
|
593
|
1071 (void) perl_destruct(perl);
|
370
|
1072
|
593
|
1073 perl_free(perl);
|
370
|
1074
|
520
|
1075 PERL_SYS_TERM();
|
370
|
1076 }
|
520
|
1077
|
|
1078 #endif
|
306
|
1079 }
|