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