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
|
266
|
44 static void ngx_http_perl_sleep_handler(ngx_http_request_t *r);
|
148
|
45 static char *ngx_http_perl_init_interpreter(ngx_conf_t *cf,
|
|
46 ngx_http_perl_main_conf_t *pmcf);
|
278
|
47 static PerlInterpreter *ngx_http_perl_create_interpreter(ngx_conf_t *cf,
|
|
48 ngx_http_perl_main_conf_t *pmcf);
|
202
|
49 static ngx_int_t ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires,
|
198
|
50 ngx_log_t *log);
|
148
|
51 static ngx_int_t ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r,
|
258
|
52 HV *nginx, SV *sub, ngx_str_t **args, ngx_str_t *handler, ngx_str_t *rv);
|
148
|
53 static void ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv);
|
|
54
|
|
55 static ngx_int_t ngx_http_perl_preconfiguration(ngx_conf_t *cf);
|
|
56 static void *ngx_http_perl_create_main_conf(ngx_conf_t *cf);
|
|
57 static char *ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf);
|
|
58 static void *ngx_http_perl_create_loc_conf(ngx_conf_t *cf);
|
|
59 static char *ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent,
|
|
60 void *child);
|
|
61 static char *ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
62 void *conf);
|
|
63 static char *ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
64 static char *ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
258
|
65
|
|
66 #if (NGX_HAVE_PERL_MULTIPLICITY)
|
148
|
67 static void ngx_http_perl_cleanup_perl(void *data);
|
198
|
68 #endif
|
148
|
69
|
|
70
|
|
71 static ngx_command_t ngx_http_perl_commands[] = {
|
|
72
|
|
73 { ngx_string("perl_modules"),
|
|
74 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
|
|
75 ngx_conf_set_str_slot,
|
|
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,
|
|
82 ngx_http_perl_require,
|
|
83 NGX_HTTP_MAIN_CONF_OFFSET,
|
|
84 0,
|
|
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 */
|
|
127 NULL, /* init process */
|
|
128 NULL, /* init thread */
|
|
129 NULL, /* exit thread */
|
|
130 NULL, /* exit process */
|
|
131 NULL, /* exit master */
|
|
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
|
230
|
155 static ngx_str_t ngx_null_name = ngx_null_string;
|
|
156
|
|
157
|
182
|
158 static HV *nginx_stash;
|
|
159
|
148
|
160 static void
|
|
161 ngx_http_perl_xs_init(pTHX)
|
|
162 {
|
|
163 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__);
|
182
|
164
|
|
165 nginx_stash = gv_stashpv("nginx", TRUE);
|
148
|
166 }
|
|
167
|
|
168
|
|
169 static ngx_int_t
|
|
170 ngx_http_perl_handler(ngx_http_request_t *r)
|
|
171 {
|
178
|
172 /* TODO: Win32 */
|
|
173 if (r->zero_in_uri) {
|
|
174 return NGX_HTTP_NOT_FOUND;
|
|
175 }
|
|
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
|
|
230 if (rc > 600) {
|
|
231 rc = NGX_OK;
|
|
232 }
|
|
233
|
|
234 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
235 "perl handler done: %i", rc);
|
|
236
|
|
237 if (ctx->redirect_uri.len) {
|
|
238 uri = ctx->redirect_uri;
|
|
239 args = ctx->redirect_args;
|
178
|
240
|
|
241 } else {
|
|
242 uri.len = 0;
|
148
|
243 }
|
|
244
|
182
|
245 ctx->filename.data = NULL;
|
148
|
246 ctx->redirect_uri.len = 0;
|
|
247
|
266
|
248 if (ctx->sleep) {
|
|
249 ngx_add_timer(r->connection->write, (ngx_msec_t) ctx->sleep);
|
|
250 r->write_event_handler = ngx_http_perl_sleep_handler;
|
|
251 ctx->sleep = 0;
|
|
252 }
|
|
253
|
230
|
254 if (ctx->done || ctx->next) {
|
|
255 return;
|
|
256 }
|
|
257
|
148
|
258 if (uri.len) {
|
178
|
259 ngx_http_internal_redirect(r, &uri, &args);
|
|
260 return;
|
148
|
261 }
|
|
262
|
|
263 if (rc == NGX_OK || rc == NGX_HTTP_OK) {
|
178
|
264 ngx_http_send_special(r, NGX_HTTP_LAST);
|
230
|
265 ctx->done = 1;
|
148
|
266 }
|
|
267
|
178
|
268 ngx_http_finalize_request(r, rc);
|
148
|
269 }
|
|
270
|
|
271
|
266
|
272 static void
|
|
273 ngx_http_perl_sleep_handler(ngx_http_request_t *r)
|
|
274 {
|
|
275 ngx_event_t *wev;
|
|
276
|
|
277 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
278 "perl sleep handler");
|
|
279
|
|
280 wev = r->connection->write;
|
|
281
|
|
282 if (wev->timedout) {
|
|
283 wev->timedout = 0;
|
|
284 ngx_http_perl_handle_request(r);
|
|
285 return;
|
|
286 }
|
|
287
|
|
288 if (ngx_handle_write_event(wev, 0) == NGX_ERROR) {
|
|
289 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
290 }
|
|
291 }
|
|
292
|
|
293
|
148
|
294 static ngx_int_t
|
|
295 ngx_http_perl_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
296 uintptr_t data)
|
|
297 {
|
|
298 ngx_http_perl_variable_t *pv = (ngx_http_perl_variable_t *) data;
|
|
299
|
|
300 ngx_int_t rc;
|
|
301 ngx_str_t value;
|
|
302 ngx_http_perl_ctx_t *ctx;
|
|
303 ngx_http_perl_main_conf_t *pmcf;
|
|
304
|
|
305 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
306 "perl variable handler");
|
|
307
|
|
308 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
|
|
309
|
|
310 if (ctx == NULL) {
|
|
311 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t));
|
|
312 if (ctx == NULL) {
|
|
313 return NGX_ERROR;
|
|
314 }
|
|
315
|
|
316 ngx_http_set_ctx(r, ctx, ngx_http_perl_module);
|
|
317 }
|
|
318
|
|
319 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
|
|
320
|
|
321 value.data = NULL;
|
|
322
|
|
323 {
|
|
324
|
258
|
325 dTHXa(pmcf->perl);
|
|
326 PERL_SET_CONTEXT(pmcf->perl);
|
148
|
327
|
258
|
328 rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, pv->sub, NULL,
|
148
|
329 &pv->handler, &value);
|
|
330
|
|
331 }
|
|
332
|
|
333 if (value.data) {
|
|
334 v->len = value.len;
|
|
335 v->valid = 1;
|
|
336 v->no_cachable = 0;
|
|
337 v->not_found = 0;
|
|
338 v->data = value.data;
|
|
339
|
|
340 } else {
|
|
341 v->not_found = 1;
|
|
342 }
|
|
343
|
182
|
344 ctx->filename.data = NULL;
|
148
|
345 ctx->redirect_uri.len = 0;
|
|
346
|
258
|
347 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
348 "perl variable done");
|
|
349
|
148
|
350 return rc;
|
|
351 }
|
|
352
|
|
353
|
166
|
354 #if (NGX_HTTP_SSI)
|
|
355
|
148
|
356 static ngx_int_t
|
|
357 ngx_http_perl_ssi(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ssi_ctx,
|
|
358 ngx_str_t **params)
|
|
359 {
|
|
360 SV *sv;
|
|
361 ngx_int_t rc;
|
|
362 ngx_str_t *handler;
|
|
363 ngx_http_perl_ctx_t *ctx;
|
|
364 ngx_http_perl_main_conf_t *pmcf;
|
|
365
|
|
366 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
367 "perl ssi handler");
|
|
368
|
|
369 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
|
|
370
|
|
371 if (ctx == NULL) {
|
|
372 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t));
|
|
373 if (ctx == NULL) {
|
|
374 return NGX_ERROR;
|
|
375 }
|
|
376
|
|
377 ngx_http_set_ctx(r, ctx, ngx_http_perl_module);
|
|
378 }
|
|
379
|
258
|
380 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
|
148
|
381
|
|
382 ctx->ssi = ssi_ctx;
|
|
383
|
|
384 handler = params[NGX_HTTP_PERL_SSI_SUB];
|
|
385 handler->data[handler->len] = '\0';
|
|
386
|
|
387 {
|
|
388
|
258
|
389 dTHXa(pmcf->perl);
|
|
390 PERL_SET_CONTEXT(pmcf->perl);
|
148
|
391
|
200
|
392 #if 0
|
|
393
|
|
394 /* the code is disabled to force the precompiled perl code using only */
|
|
395
|
|
396 ngx_http_perl_eval_anon_sub(aTHX_ handler, &sv);
|
|
397
|
|
398 if (sv == &PL_sv_undef) {
|
|
399 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
400 "eval_pv(\"%V\") failed", handler);
|
|
401 return NGX_ERROR;
|
|
402 }
|
|
403
|
|
404 if (sv == NULL) {
|
|
405 sv = newSVpvn((char *) handler->data, handler->len);
|
|
406 }
|
|
407
|
|
408 #endif
|
|
409
|
148
|
410 sv = newSVpvn((char *) handler->data, handler->len);
|
|
411
|
258
|
412 rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, sv,
|
|
413 ¶ms[NGX_HTTP_PERL_SSI_ARG],
|
148
|
414 handler, NULL);
|
|
415
|
|
416 SvREFCNT_dec(sv);
|
|
417
|
|
418 }
|
|
419
|
182
|
420 ctx->filename.data = NULL;
|
148
|
421 ctx->redirect_uri.len = 0;
|
|
422 ctx->ssi = NULL;
|
|
423
|
258
|
424 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl ssi done");
|
|
425
|
148
|
426 return rc;
|
|
427 }
|
|
428
|
166
|
429 #endif
|
|
430
|
148
|
431
|
|
432 static char *
|
|
433 ngx_http_perl_init_interpreter(ngx_conf_t *cf, ngx_http_perl_main_conf_t *pmcf)
|
|
434 {
|
258
|
435 #if (NGX_HAVE_PERL_MULTIPLICITY)
|
202
|
436 ngx_pool_cleanup_t *cln;
|
148
|
437
|
|
438 cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
439 if (cln == NULL) {
|
|
440 return NGX_CONF_ERROR;
|
|
441 }
|
|
442
|
198
|
443 #else
|
|
444 static PerlInterpreter *perl;
|
|
445 #endif
|
|
446
|
148
|
447 #ifdef NGX_PERL_MODULES
|
|
448 if (pmcf->modules.data == NULL) {
|
|
449 pmcf->modules.data = NGX_PERL_MODULES;
|
|
450 }
|
|
451 #endif
|
|
452
|
188
|
453 if (pmcf->modules.data) {
|
|
454 if (ngx_conf_full_name(cf->cycle, &pmcf->modules) != NGX_OK) {
|
|
455 return NGX_CONF_ERROR;
|
|
456 }
|
178
|
457 }
|
|
458
|
258
|
459 #if !(NGX_HAVE_PERL_MULTIPLICITY)
|
198
|
460
|
|
461 if (perl) {
|
296
|
462
|
|
463 if (ngx_set_environment(cf->cycle, NULL) == NULL) {
|
|
464 return NGX_CONF_ERROR;
|
|
465 }
|
|
466
|
202
|
467 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log)
|
|
468 != NGX_OK)
|
|
469 {
|
198
|
470 return NGX_CONF_ERROR;
|
|
471 }
|
|
472
|
|
473 pmcf->perl = perl;
|
296
|
474 pmcf->nginx = nginx_stash;
|
198
|
475
|
|
476 return NGX_CONF_OK;
|
|
477 }
|
|
478
|
|
479 #endif
|
|
480
|
148
|
481 PERL_SYS_INIT(&ngx_argc, &ngx_argv);
|
|
482
|
278
|
483 pmcf->perl = ngx_http_perl_create_interpreter(cf, pmcf);
|
148
|
484
|
|
485 if (pmcf->perl == NULL) {
|
|
486 PERL_SYS_TERM();
|
|
487 return NGX_CONF_ERROR;
|
|
488 }
|
|
489
|
258
|
490 pmcf->nginx = nginx_stash;
|
|
491
|
|
492 #if (NGX_HAVE_PERL_MULTIPLICITY)
|
198
|
493
|
148
|
494 cln->handler = ngx_http_perl_cleanup_perl;
|
|
495 cln->data = pmcf->perl;
|
|
496
|
198
|
497 #else
|
|
498
|
|
499 perl = pmcf->perl;
|
|
500
|
|
501 #endif
|
|
502
|
148
|
503 return NGX_CONF_OK;
|
|
504 }
|
|
505
|
|
506
|
|
507 static PerlInterpreter *
|
278
|
508 ngx_http_perl_create_interpreter(ngx_conf_t *cf,
|
|
509 ngx_http_perl_main_conf_t *pmcf)
|
148
|
510 {
|
|
511 int n;
|
248
|
512 STRLEN len;
|
|
513 SV *sv;
|
|
514 char *ver, *embedding[6];
|
148
|
515 PerlInterpreter *perl;
|
|
516
|
278
|
517 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "create perl interpreter");
|
|
518
|
|
519 if (ngx_set_environment(cf->cycle, NULL) == NULL) {
|
|
520 return NULL;
|
|
521 }
|
148
|
522
|
|
523 perl = perl_alloc();
|
|
524 if (perl == NULL) {
|
278
|
525 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_alloc() failed");
|
148
|
526 return NULL;
|
|
527 }
|
|
528
|
|
529 {
|
|
530
|
|
531 dTHXa(perl);
|
258
|
532 PERL_SET_CONTEXT(perl);
|
|
533
|
|
534 perl_construct(perl);
|
148
|
535
|
|
536 #ifdef PERL_EXIT_DESTRUCT_END
|
|
537 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
|
|
538 #endif
|
|
539
|
|
540 embedding[0] = "";
|
|
541
|
|
542 if (pmcf->modules.data) {
|
|
543 embedding[1] = "-I";
|
|
544 embedding[2] = (char *) pmcf->modules.data;
|
|
545 n = 3;
|
|
546
|
|
547 } else {
|
|
548 n = 1;
|
|
549 }
|
|
550
|
|
551 embedding[n++] = "-Mnginx";
|
|
552 embedding[n++] = "-e";
|
|
553 embedding[n++] = "0";
|
|
554
|
|
555 n = perl_parse(perl, ngx_http_perl_xs_init, n, embedding, NULL);
|
|
556
|
|
557 if (n != 0) {
|
278
|
558 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_parse() failed: %d", n);
|
148
|
559 goto fail;
|
|
560 }
|
|
561
|
248
|
562 sv = get_sv("nginx::VERSION", FALSE);
|
|
563 ver = SvPV(sv, len);
|
|
564
|
|
565 if (ngx_strcmp(ver, NGINX_VERSION) != 0) {
|
278
|
566 ngx_log_error(NGX_LOG_ALERT, cf->log, 0,
|
248
|
567 "version " NGINX_VERSION " of nginx.pm is required, "
|
|
568 "but %s was found", ver);
|
|
569 goto fail;
|
|
570 }
|
|
571
|
278
|
572 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log) != NGX_OK) {
|
198
|
573 goto fail;
|
148
|
574 }
|
|
575
|
|
576 }
|
|
577
|
|
578 return perl;
|
|
579
|
|
580 fail:
|
|
581
|
|
582 (void) perl_destruct(perl);
|
|
583
|
|
584 perl_free(perl);
|
|
585
|
|
586 return NULL;
|
|
587 }
|
|
588
|
|
589
|
198
|
590 static ngx_int_t
|
202
|
591 ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires, ngx_log_t *log)
|
198
|
592 {
|
|
593 char **script;
|
|
594 STRLEN len;
|
|
595 ngx_str_t err;
|
|
596 ngx_uint_t i;
|
|
597
|
|
598 script = requires->elts;
|
|
599 for (i = 0; i < requires->nelts; i++) {
|
|
600
|
|
601 require_pv(script[i]);
|
|
602
|
|
603 if (SvTRUE(ERRSV)) {
|
|
604
|
|
605 err.data = (u_char *) SvPV(ERRSV, len);
|
|
606 for (len--; err.data[len] == LF || err.data[len] == CR; len--) {
|
|
607 /* void */
|
|
608 }
|
|
609 err.len = len + 1;
|
|
610
|
|
611 ngx_log_error(NGX_LOG_EMERG, log, 0,
|
|
612 "require_pv(\"%s\") failed: \"%V\"", script[i], &err);
|
|
613
|
|
614 return NGX_ERROR;
|
|
615 }
|
|
616 }
|
|
617
|
|
618 return NGX_OK;
|
|
619 }
|
|
620
|
|
621
|
148
|
622 static ngx_int_t
|
258
|
623 ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r, HV *nginx, SV *sub,
|
148
|
624 ngx_str_t **args, ngx_str_t *handler, ngx_str_t *rv)
|
|
625 {
|
|
626 SV *sv;
|
|
627 int n, status;
|
|
628 char *line;
|
|
629 STRLEN len, n_a;
|
|
630 ngx_str_t err;
|
|
631 ngx_uint_t i;
|
|
632
|
|
633 dSP;
|
|
634
|
|
635 status = 0;
|
|
636
|
|
637 ENTER;
|
|
638 SAVETMPS;
|
|
639
|
|
640 PUSHMARK(sp);
|
|
641
|
258
|
642 sv = sv_2mortal(sv_bless(newRV_noinc(newSViv(PTR2IV(r))), nginx));
|
148
|
643 XPUSHs(sv);
|
|
644
|
|
645 if (args) {
|
|
646 for (i = 0; args[i]; i++) { /* void */ }
|
|
647
|
|
648 EXTEND(sp, (int) i);
|
|
649
|
|
650 for (i = 0; args[i]; i++) {
|
|
651 PUSHs(sv_2mortal(newSVpvn((char *) args[i]->data, args[i]->len)));
|
|
652 }
|
|
653 }
|
|
654
|
|
655 PUTBACK;
|
|
656
|
|
657 n = call_sv(sub, G_EVAL);
|
|
658
|
|
659 SPAGAIN;
|
|
660
|
|
661 if (n) {
|
|
662 if (rv == NULL) {
|
|
663 status = POPi;
|
|
664
|
|
665 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
666 "call_sv: %d", status);
|
|
667
|
|
668 } else {
|
168
|
669 line = SvPVx(POPs, n_a);
|
148
|
670 rv->len = n_a;
|
|
671
|
|
672 rv->data = ngx_palloc(r->pool, n_a);
|
|
673 if (rv->data == NULL) {
|
|
674 return NGX_ERROR;
|
|
675 }
|
|
676
|
|
677 ngx_memcpy(rv->data, line, n_a);
|
|
678 }
|
|
679 }
|
|
680
|
|
681 PUTBACK;
|
|
682
|
|
683 FREETMPS;
|
|
684 LEAVE;
|
|
685
|
|
686 /* check $@ */
|
|
687
|
|
688 if (SvTRUE(ERRSV)) {
|
|
689
|
|
690 err.data = (u_char *) SvPV(ERRSV, len);
|
|
691 for (len--; err.data[len] == LF || err.data[len] == CR; len--) {
|
|
692 /* void */
|
|
693 }
|
|
694 err.len = len + 1;
|
|
695
|
|
696 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
697 "call_sv(\"%V\") failed: \"%V\"",
|
|
698 handler, &err);
|
|
699
|
|
700 if (rv) {
|
|
701 return NGX_ERROR;
|
|
702 }
|
|
703
|
|
704 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
705 }
|
|
706
|
|
707 if (n != 1) {
|
|
708 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
709 "call_sv(\"%V\") returned %d results", handler, n);
|
|
710 status = NGX_OK;
|
|
711 }
|
|
712
|
|
713 if (rv) {
|
|
714 return NGX_OK;
|
|
715 }
|
|
716
|
|
717 return (ngx_int_t) status;
|
|
718 }
|
|
719
|
|
720
|
|
721 static void
|
|
722 ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv)
|
|
723 {
|
172
|
724 u_char *p;
|
|
725
|
|
726 for (p = handler->data; *p; p++) {
|
|
727 if (*p != ' ' && *p != '\t' && *p != CR && *p != LF) {
|
|
728 break;
|
|
729 }
|
|
730 }
|
|
731
|
198
|
732 if (ngx_strncmp(p, "sub ", 4) == 0 || ngx_strncmp(p, "use ", 4) == 0) {
|
172
|
733 *sv = eval_pv((char *) p, FALSE);
|
258
|
734
|
|
735 /* eval_pv() does not set ERRSV on failure */
|
|
736
|
148
|
737 return;
|
|
738 }
|
|
739
|
|
740 *sv = NULL;
|
|
741 }
|
|
742
|
|
743
|
|
744 static void *
|
|
745 ngx_http_perl_create_main_conf(ngx_conf_t *cf)
|
|
746 {
|
|
747 ngx_http_perl_main_conf_t *pmcf;
|
|
748
|
|
749 pmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_main_conf_t));
|
|
750 if (pmcf == NULL) {
|
|
751 return NGX_CONF_ERROR;
|
|
752 }
|
|
753
|
|
754 if (ngx_array_init(&pmcf->requires, cf->pool, 1, sizeof(u_char *))
|
|
755 != NGX_OK)
|
|
756 {
|
|
757 return NULL;
|
|
758 }
|
|
759
|
|
760 return pmcf;
|
|
761 }
|
|
762
|
|
763
|
|
764 static char *
|
|
765 ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf)
|
|
766 {
|
|
767 ngx_http_perl_main_conf_t *pmcf = conf;
|
|
768
|
|
769 if (pmcf->perl == NULL) {
|
|
770 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
|
|
771 return NGX_CONF_ERROR;
|
|
772 }
|
|
773 }
|
|
774
|
|
775 return NGX_CONF_OK;
|
|
776 }
|
|
777
|
|
778
|
258
|
779 #if (NGX_HAVE_PERL_MULTIPLICITY)
|
198
|
780
|
148
|
781 static void
|
|
782 ngx_http_perl_cleanup_perl(void *data)
|
|
783 {
|
198
|
784 PerlInterpreter *perl = data;
|
148
|
785
|
258
|
786 PERL_SET_CONTEXT(perl);
|
|
787
|
148
|
788 (void) perl_destruct(perl);
|
|
789
|
|
790 perl_free(perl);
|
|
791
|
|
792 PERL_SYS_TERM();
|
|
793 }
|
|
794
|
198
|
795 #endif
|
|
796
|
|
797
|
148
|
798 static ngx_int_t
|
|
799 ngx_http_perl_preconfiguration(ngx_conf_t *cf)
|
|
800 {
|
166
|
801 #if (NGX_HTTP_SSI)
|
148
|
802 ngx_int_t rc;
|
|
803 ngx_http_ssi_main_conf_t *smcf;
|
|
804
|
|
805 smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module);
|
|
806
|
|
807 rc = ngx_hash_add_key(&smcf->commands, &ngx_http_perl_ssi_command.name,
|
|
808 &ngx_http_perl_ssi_command, NGX_HASH_READONLY_KEY);
|
|
809
|
|
810 if (rc != NGX_OK) {
|
|
811 if (rc == NGX_BUSY) {
|
|
812 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
813 "conflicting SSI command \"%V\"",
|
|
814 &ngx_http_perl_ssi_command.name);
|
|
815 }
|
|
816
|
|
817 return NGX_ERROR;
|
|
818 }
|
166
|
819 #endif
|
148
|
820
|
|
821 return NGX_OK;
|
|
822 }
|
|
823
|
|
824
|
|
825 static void *
|
|
826 ngx_http_perl_create_loc_conf(ngx_conf_t *cf)
|
|
827 {
|
|
828 ngx_http_perl_loc_conf_t *plcf;
|
|
829
|
|
830 plcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_loc_conf_t));
|
|
831 if (plcf == NULL) {
|
|
832 return NGX_CONF_ERROR;
|
|
833 }
|
|
834
|
|
835 /*
|
|
836 * set by ngx_pcalloc():
|
|
837 *
|
|
838 * plcf->handler = { 0, NULL };
|
|
839 */
|
|
840
|
|
841 return plcf;
|
|
842 }
|
|
843
|
|
844
|
|
845 static char *
|
|
846 ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
847 {
|
|
848 ngx_http_perl_loc_conf_t *prev = parent;
|
|
849 ngx_http_perl_loc_conf_t *conf = child;
|
|
850
|
|
851 if (conf->sub == NULL) {
|
|
852 conf->sub = prev->sub;
|
|
853 conf->handler = prev->handler;
|
|
854 }
|
|
855
|
|
856 return NGX_CONF_OK;
|
|
857 }
|
|
858
|
|
859
|
|
860 static char *
|
|
861 ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
862 {
|
|
863 ngx_http_perl_main_conf_t *pmcf = conf;
|
|
864
|
|
865 u_char **p;
|
|
866 ngx_str_t *value;
|
|
867
|
|
868 value = cf->args->elts;
|
|
869
|
|
870 p = ngx_array_push(&pmcf->requires);
|
|
871
|
|
872 if (p == NULL) {
|
|
873 return NGX_CONF_ERROR;
|
|
874 }
|
|
875
|
|
876 *p = value[1].data;
|
|
877
|
|
878 return NGX_CONF_OK;
|
|
879 }
|
|
880
|
|
881
|
|
882 static char *
|
|
883 ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
884 {
|
|
885 ngx_http_perl_loc_conf_t *plcf = conf;
|
|
886
|
|
887 ngx_str_t *value;
|
|
888 ngx_http_core_loc_conf_t *clcf;
|
|
889 ngx_http_perl_main_conf_t *pmcf;
|
|
890
|
|
891 value = cf->args->elts;
|
|
892
|
|
893 if (plcf->handler.data) {
|
|
894 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
895 "duplicate perl handler \"%V\"", &value[1]);
|
|
896 return NGX_CONF_ERROR;
|
|
897 }
|
|
898
|
|
899 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module);
|
|
900
|
|
901 if (pmcf->perl == NULL) {
|
|
902 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
|
|
903 return NGX_CONF_ERROR;
|
|
904 }
|
|
905 }
|
|
906
|
|
907 plcf->handler = value[1];
|
|
908
|
|
909 {
|
|
910
|
|
911 dTHXa(pmcf->perl);
|
258
|
912 PERL_SET_CONTEXT(pmcf->perl);
|
148
|
913
|
|
914 ngx_http_perl_eval_anon_sub(aTHX_ &value[1], &plcf->sub);
|
|
915
|
|
916 if (plcf->sub == &PL_sv_undef) {
|
|
917 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
|
|
918 "eval_pv(\"%V\") failed", &value[1]);
|
|
919 return NGX_CONF_ERROR;
|
|
920 }
|
|
921
|
|
922 if (plcf->sub == NULL) {
|
|
923 plcf->sub = newSVpvn((char *) value[1].data, value[1].len);
|
|
924 }
|
|
925
|
|
926 }
|
|
927
|
|
928 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
929 clcf->handler = ngx_http_perl_handler;
|
|
930
|
|
931 return NGX_CONF_OK;
|
|
932 }
|
|
933
|
|
934
|
|
935 static char *
|
|
936 ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
937 {
|
|
938 ngx_int_t index;
|
|
939 ngx_str_t *value;
|
|
940 ngx_http_variable_t *v;
|
|
941 ngx_http_perl_variable_t *pv;
|
|
942 ngx_http_perl_main_conf_t *pmcf;
|
|
943
|
|
944 value = cf->args->elts;
|
|
945
|
|
946 if (value[1].data[0] != '$') {
|
|
947 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
948 "invalid variable name \"%V\"", &value[1]);
|
|
949 return NGX_CONF_ERROR;
|
|
950 }
|
|
951
|
|
952 value[1].len--;
|
|
953 value[1].data++;
|
|
954
|
|
955 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGABLE);
|
|
956 if (v == NULL) {
|
|
957 return NGX_CONF_ERROR;
|
|
958 }
|
|
959
|
|
960 pv = ngx_palloc(cf->pool, sizeof(ngx_http_perl_variable_t));
|
|
961 if (pv == NULL) {
|
|
962 return NGX_CONF_ERROR;
|
|
963 }
|
|
964
|
|
965 index = ngx_http_get_variable_index(cf, &value[1]);
|
|
966 if (index == NGX_ERROR) {
|
|
967 return NGX_CONF_ERROR;
|
|
968 }
|
|
969
|
|
970 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module);
|
|
971
|
|
972 if (pmcf->perl == NULL) {
|
|
973 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
|
|
974 return NGX_CONF_ERROR;
|
|
975 }
|
|
976 }
|
|
977
|
|
978 pv->handler = value[2];
|
|
979
|
|
980 {
|
|
981
|
|
982 dTHXa(pmcf->perl);
|
258
|
983 PERL_SET_CONTEXT(pmcf->perl);
|
148
|
984
|
|
985 ngx_http_perl_eval_anon_sub(aTHX_ &value[2], &pv->sub);
|
|
986
|
|
987 if (pv->sub == &PL_sv_undef) {
|
|
988 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
|
|
989 "eval_pv(\"%V\") failed", &value[2]);
|
|
990 return NGX_CONF_ERROR;
|
|
991 }
|
|
992
|
|
993 if (pv->sub == NULL) {
|
|
994 pv->sub = newSVpvn((char *) value[2].data, value[2].len);
|
|
995 }
|
|
996
|
|
997 }
|
|
998
|
186
|
999 v->get_handler = ngx_http_perl_variable;
|
148
|
1000 v->data = (uintptr_t) pv;
|
|
1001
|
|
1002 return NGX_CONF_OK;
|
|
1003 }
|