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