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