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 = {
|
|
173 ngx_string("perl"), ngx_http_perl_ssi, ngx_http_perl_ssi_params, 0, 1
|
|
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 /* mod_perl's content handler assumes that content type was already set */
|
|
230
|
|
231 if (ngx_http_set_content_type(r) != NGX_OK) {
|
178
|
232 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
233 return;
|
148
|
234 }
|
|
235
|
|
236 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
|
|
237
|
|
238 if (ctx == NULL) {
|
|
239 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t));
|
|
240 if (ctx == NULL) {
|
178
|
241 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
242 return;
|
148
|
243 }
|
|
244
|
|
245 ngx_http_set_ctx(r, ctx, ngx_http_perl_module);
|
|
246 }
|
|
247
|
|
248 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
|
|
249
|
|
250 rc = ngx_http_perl_get_interpreter(pmcf, &ctx->perl, r->connection->log);
|
|
251
|
|
252 if (rc != NGX_OK) {
|
178
|
253 ngx_http_finalize_request(r, rc);
|
|
254 return;
|
148
|
255 }
|
|
256
|
|
257 {
|
|
258
|
|
259 dTHXa(ctx->perl);
|
|
260
|
|
261 plcf = ngx_http_get_module_loc_conf(r, ngx_http_perl_module);
|
|
262
|
|
263 rc = ngx_http_perl_call_handler(aTHX_ r, plcf->sub, NULL,
|
|
264 &plcf->handler, NULL);
|
|
265
|
|
266 }
|
|
267
|
|
268 ngx_http_perl_free_interpreter(pmcf, ctx->perl);
|
|
269
|
|
270 if (rc > 600) {
|
|
271 rc = NGX_OK;
|
|
272 }
|
|
273
|
|
274 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
275 "perl handler done: %i", rc);
|
|
276
|
|
277 if (ctx->redirect_uri.len) {
|
|
278 uri = ctx->redirect_uri;
|
|
279 args = ctx->redirect_args;
|
178
|
280
|
|
281 } else {
|
|
282 uri.len = 0;
|
148
|
283 }
|
|
284
|
182
|
285 ctx->filename.data = NULL;
|
148
|
286 ctx->redirect_uri.len = 0;
|
|
287
|
|
288 if (uri.len) {
|
178
|
289 ngx_http_internal_redirect(r, &uri, &args);
|
|
290 return;
|
148
|
291 }
|
|
292
|
|
293 if (rc == NGX_OK || rc == NGX_HTTP_OK) {
|
178
|
294 ngx_http_send_special(r, NGX_HTTP_LAST);
|
148
|
295 }
|
|
296
|
178
|
297 ngx_http_finalize_request(r, rc);
|
148
|
298 }
|
|
299
|
|
300
|
|
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 rc = ngx_http_perl_get_interpreter(pmcf, &ctx->perl, r->connection->log);
|
|
329
|
|
330 if (rc != NGX_OK) {
|
|
331 return rc;
|
|
332 }
|
|
333
|
|
334 value.data = NULL;
|
|
335
|
|
336 {
|
|
337
|
|
338 dTHXa(ctx->perl);
|
|
339
|
|
340 rc = ngx_http_perl_call_handler(aTHX_ r, pv->sub, NULL,
|
|
341 &pv->handler, &value);
|
|
342
|
|
343 }
|
|
344
|
|
345 ngx_http_perl_free_interpreter(pmcf, ctx->perl);
|
|
346
|
|
347 if (value.data) {
|
|
348 v->len = value.len;
|
|
349 v->valid = 1;
|
|
350 v->no_cachable = 0;
|
|
351 v->not_found = 0;
|
|
352 v->data = value.data;
|
|
353
|
|
354 } else {
|
|
355 v->not_found = 1;
|
|
356 }
|
|
357
|
182
|
358 ctx->filename.data = NULL;
|
148
|
359 ctx->redirect_uri.len = 0;
|
|
360
|
|
361 return rc;
|
|
362 }
|
|
363
|
|
364
|
166
|
365 #if (NGX_HTTP_SSI)
|
|
366
|
148
|
367 static ngx_int_t
|
|
368 ngx_http_perl_ssi(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ssi_ctx,
|
|
369 ngx_str_t **params)
|
|
370 {
|
|
371 SV *sv;
|
|
372 ngx_int_t rc;
|
|
373 ngx_str_t *handler;
|
|
374 ngx_http_perl_ctx_t *ctx;
|
|
375 ngx_http_perl_main_conf_t *pmcf;
|
|
376
|
|
377 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
378 "perl ssi handler");
|
|
379
|
|
380 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
|
|
381
|
|
382 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
|
|
383
|
|
384 if (ctx == NULL) {
|
|
385 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t));
|
|
386 if (ctx == NULL) {
|
|
387 return NGX_ERROR;
|
|
388 }
|
|
389
|
|
390 ngx_http_set_ctx(r, ctx, ngx_http_perl_module);
|
|
391 }
|
|
392
|
|
393 rc = ngx_http_perl_get_interpreter(pmcf, &ctx->perl, r->connection->log);
|
|
394
|
|
395 if (rc != NGX_OK) {
|
|
396 return rc;
|
|
397 }
|
|
398
|
|
399 ctx->ssi = ssi_ctx;
|
|
400
|
|
401 handler = params[NGX_HTTP_PERL_SSI_SUB];
|
|
402 handler->data[handler->len] = '\0';
|
|
403
|
|
404 {
|
|
405
|
|
406 dTHXa(ctx->perl);
|
|
407
|
200
|
408 #if 0
|
|
409
|
|
410 /* the code is disabled to force the precompiled perl code using only */
|
|
411
|
|
412 ngx_http_perl_eval_anon_sub(aTHX_ handler, &sv);
|
|
413
|
|
414 if (sv == &PL_sv_undef) {
|
|
415 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
416 "eval_pv(\"%V\") failed", handler);
|
|
417 return NGX_ERROR;
|
|
418 }
|
|
419
|
|
420 if (sv == NULL) {
|
|
421 sv = newSVpvn((char *) handler->data, handler->len);
|
|
422 }
|
|
423
|
|
424 #endif
|
|
425
|
148
|
426 sv = newSVpvn((char *) handler->data, handler->len);
|
|
427
|
|
428 rc = ngx_http_perl_call_handler(aTHX_ r, sv, ¶ms[NGX_HTTP_PERL_SSI_ARG],
|
|
429 handler, NULL);
|
|
430
|
|
431 SvREFCNT_dec(sv);
|
|
432
|
|
433 }
|
|
434
|
|
435 ngx_http_perl_free_interpreter(pmcf, ctx->perl);
|
|
436
|
182
|
437 ctx->filename.data = NULL;
|
148
|
438 ctx->redirect_uri.len = 0;
|
|
439 ctx->ssi = NULL;
|
|
440
|
|
441 return rc;
|
|
442 }
|
|
443
|
166
|
444 #endif
|
|
445
|
148
|
446
|
|
447 static ngx_int_t
|
|
448 ngx_http_perl_get_interpreter(ngx_http_perl_main_conf_t *pmcf,
|
|
449 PerlInterpreter **perl, ngx_log_t *log)
|
|
450 {
|
|
451 if (pmcf->interp) {
|
|
452 pmcf->interp--;
|
|
453
|
|
454 *perl = pmcf->free_perls[pmcf->interp];
|
|
455
|
|
456 return NGX_OK;
|
|
457 }
|
|
458
|
|
459 if (pmcf->nalloc < pmcf->interp_max) {
|
|
460 *perl = ngx_http_perl_create_interpreter(pmcf, log);
|
|
461
|
|
462 if (*perl) {
|
|
463 return NGX_OK;
|
|
464 }
|
|
465
|
|
466 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
467 }
|
|
468
|
|
469 ngx_log_error(NGX_LOG_ALERT, log, 0, "no free perl interpreter");
|
|
470
|
|
471 return NGX_HTTP_SERVICE_UNAVAILABLE;
|
|
472 }
|
|
473
|
|
474
|
|
475 static ngx_inline void
|
|
476 ngx_http_perl_free_interpreter(ngx_http_perl_main_conf_t *pmcf,
|
|
477 PerlInterpreter *perl)
|
|
478 {
|
|
479 pmcf->free_perls[pmcf->interp++] = perl;
|
|
480 }
|
|
481
|
|
482
|
|
483 static char *
|
|
484 ngx_http_perl_init_interpreter(ngx_conf_t *cf, ngx_http_perl_main_conf_t *pmcf)
|
|
485 {
|
198
|
486 #if (NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
|
202
|
487 ngx_pool_cleanup_t *cln;
|
148
|
488
|
|
489 cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
490 if (cln == NULL) {
|
|
491 return NGX_CONF_ERROR;
|
|
492 }
|
|
493
|
198
|
494 #else
|
|
495 static PerlInterpreter *perl;
|
|
496 #endif
|
|
497
|
148
|
498 #ifdef NGX_PERL_MODULES
|
|
499 if (pmcf->modules.data == NULL) {
|
|
500 pmcf->modules.data = NGX_PERL_MODULES;
|
|
501 }
|
|
502 #endif
|
|
503
|
188
|
504 if (pmcf->modules.data) {
|
|
505 if (ngx_conf_full_name(cf->cycle, &pmcf->modules) != NGX_OK) {
|
|
506 return NGX_CONF_ERROR;
|
|
507 }
|
178
|
508 }
|
|
509
|
198
|
510 #if !(NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
|
|
511
|
|
512 if (perl) {
|
202
|
513 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log)
|
|
514 != NGX_OK)
|
|
515 {
|
198
|
516 return NGX_CONF_ERROR;
|
|
517 }
|
|
518
|
|
519 pmcf->perl = perl;
|
|
520
|
|
521 return NGX_CONF_OK;
|
|
522 }
|
|
523
|
|
524 #endif
|
|
525
|
148
|
526 PERL_SYS_INIT(&ngx_argc, &ngx_argv);
|
|
527
|
|
528 pmcf->perl = ngx_http_perl_create_interpreter(pmcf, cf->log);
|
|
529
|
|
530 if (pmcf->perl == NULL) {
|
|
531 PERL_SYS_TERM();
|
|
532 return NGX_CONF_ERROR;
|
|
533 }
|
|
534
|
198
|
535 #if (NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
|
|
536
|
148
|
537 cln->handler = ngx_http_perl_cleanup_perl;
|
|
538 cln->data = pmcf->perl;
|
|
539
|
198
|
540 #else
|
|
541
|
|
542 perl = pmcf->perl;
|
|
543
|
|
544 #endif
|
|
545
|
148
|
546 return NGX_CONF_OK;
|
|
547 }
|
|
548
|
|
549
|
|
550 static PerlInterpreter *
|
|
551 ngx_http_perl_create_interpreter(ngx_http_perl_main_conf_t *pmcf,
|
|
552 ngx_log_t *log)
|
|
553 {
|
|
554 int n;
|
|
555 char *embedding[6];
|
|
556 PerlInterpreter *perl;
|
|
557
|
|
558 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "create perl interpreter");
|
|
559
|
|
560 #if (NGX_HAVE_PERL_CLONE)
|
|
561
|
|
562 if (pmcf->perl) {
|
|
563
|
|
564 perl = perl_clone(pmcf->perl, CLONEf_KEEP_PTR_TABLE);
|
|
565 if (perl == NULL) {
|
|
566 ngx_log_error(NGX_LOG_ALERT, log, 0, "perl_clone() failed");
|
|
567 return NULL;
|
|
568 }
|
|
569
|
|
570 {
|
|
571
|
|
572 dTHXa(perl);
|
|
573
|
|
574 ptr_table_free(PL_ptr_table);
|
|
575 PL_ptr_table = NULL;
|
|
576
|
|
577 }
|
|
578
|
|
579 pmcf->nalloc++;
|
|
580
|
|
581 return perl;
|
|
582 }
|
|
583
|
|
584 #endif
|
|
585
|
|
586 perl = perl_alloc();
|
|
587 if (perl == NULL) {
|
|
588 ngx_log_error(NGX_LOG_ALERT, log, 0, "perl_alloc() failed");
|
|
589 return NULL;
|
|
590 }
|
|
591
|
|
592 perl_construct(perl);
|
|
593
|
|
594 {
|
|
595
|
|
596 dTHXa(perl);
|
|
597
|
|
598 #ifdef PERL_EXIT_DESTRUCT_END
|
|
599 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
|
|
600 #endif
|
|
601
|
|
602 embedding[0] = "";
|
|
603
|
|
604 if (pmcf->modules.data) {
|
|
605 embedding[1] = "-I";
|
|
606 embedding[2] = (char *) pmcf->modules.data;
|
|
607 n = 3;
|
|
608
|
|
609 } else {
|
|
610 n = 1;
|
|
611 }
|
|
612
|
|
613 embedding[n++] = "-Mnginx";
|
|
614 embedding[n++] = "-e";
|
|
615 embedding[n++] = "0";
|
|
616
|
|
617 n = perl_parse(perl, ngx_http_perl_xs_init, n, embedding, NULL);
|
|
618
|
|
619 if (n != 0) {
|
|
620 ngx_log_error(NGX_LOG_ALERT, log, 0, "perl_parse() failed: %d", n);
|
|
621 goto fail;
|
|
622 }
|
|
623
|
202
|
624 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, log) != NGX_OK) {
|
198
|
625 goto fail;
|
148
|
626 }
|
|
627
|
|
628 }
|
|
629
|
|
630 pmcf->nalloc++;
|
|
631
|
|
632 return perl;
|
|
633
|
|
634 fail:
|
|
635
|
|
636 (void) perl_destruct(perl);
|
|
637
|
|
638 perl_free(perl);
|
|
639
|
|
640 return NULL;
|
|
641 }
|
|
642
|
|
643
|
198
|
644 static ngx_int_t
|
202
|
645 ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires, ngx_log_t *log)
|
198
|
646 {
|
|
647 char **script;
|
|
648 STRLEN len;
|
|
649 ngx_str_t err;
|
|
650 ngx_uint_t i;
|
|
651
|
|
652 script = requires->elts;
|
|
653 for (i = 0; i < requires->nelts; i++) {
|
|
654
|
|
655 require_pv(script[i]);
|
|
656
|
|
657 if (SvTRUE(ERRSV)) {
|
|
658
|
|
659 err.data = (u_char *) SvPV(ERRSV, len);
|
|
660 for (len--; err.data[len] == LF || err.data[len] == CR; len--) {
|
|
661 /* void */
|
|
662 }
|
|
663 err.len = len + 1;
|
|
664
|
|
665 ngx_log_error(NGX_LOG_EMERG, log, 0,
|
|
666 "require_pv(\"%s\") failed: \"%V\"", script[i], &err);
|
|
667
|
|
668 return NGX_ERROR;
|
|
669 }
|
|
670 }
|
|
671
|
|
672 return NGX_OK;
|
|
673 }
|
|
674
|
|
675
|
148
|
676 #if (__INTEL_COMPILER)
|
|
677 /*
|
|
678 * disable 'declaration hides parameter "my_perl"' warning for ENTER and LEAVE
|
|
679 */
|
|
680 #pragma warning(disable:1599)
|
|
681 #endif
|
|
682
|
|
683
|
|
684 static ngx_int_t
|
|
685 ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r, SV *sub,
|
|
686 ngx_str_t **args, ngx_str_t *handler, ngx_str_t *rv)
|
|
687 {
|
|
688 SV *sv;
|
|
689 int n, status;
|
|
690 char *line;
|
|
691 STRLEN len, n_a;
|
|
692 ngx_str_t err;
|
|
693 ngx_uint_t i;
|
|
694
|
|
695 dSP;
|
|
696
|
|
697 status = 0;
|
|
698
|
|
699 ENTER;
|
|
700 SAVETMPS;
|
|
701
|
|
702 PUSHMARK(sp);
|
|
703
|
182
|
704 sv = sv_2mortal(sv_bless(newRV_noinc(newSViv(PTR2IV(r))), nginx_stash));
|
148
|
705 XPUSHs(sv);
|
|
706
|
|
707 if (args) {
|
|
708 for (i = 0; args[i]; i++) { /* void */ }
|
|
709
|
|
710 EXTEND(sp, (int) i);
|
|
711
|
|
712 for (i = 0; args[i]; i++) {
|
|
713 PUSHs(sv_2mortal(newSVpvn((char *) args[i]->data, args[i]->len)));
|
|
714 }
|
|
715 }
|
|
716
|
|
717 PUTBACK;
|
|
718
|
|
719 n = call_sv(sub, G_EVAL);
|
|
720
|
|
721 SPAGAIN;
|
|
722
|
|
723 if (n) {
|
|
724 if (rv == NULL) {
|
|
725 status = POPi;
|
|
726
|
|
727 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
728 "call_sv: %d", status);
|
|
729
|
|
730 } else {
|
168
|
731 line = SvPVx(POPs, n_a);
|
148
|
732 rv->len = n_a;
|
|
733
|
|
734 rv->data = ngx_palloc(r->pool, n_a);
|
|
735 if (rv->data == NULL) {
|
|
736 return NGX_ERROR;
|
|
737 }
|
|
738
|
|
739 ngx_memcpy(rv->data, line, n_a);
|
|
740 }
|
|
741 }
|
|
742
|
|
743 PUTBACK;
|
|
744
|
|
745 FREETMPS;
|
|
746 LEAVE;
|
|
747
|
|
748 /* check $@ */
|
|
749
|
|
750 if (SvTRUE(ERRSV)) {
|
|
751
|
|
752 err.data = (u_char *) SvPV(ERRSV, len);
|
|
753 for (len--; err.data[len] == LF || err.data[len] == CR; len--) {
|
|
754 /* void */
|
|
755 }
|
|
756 err.len = len + 1;
|
|
757
|
|
758 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
759 "call_sv(\"%V\") failed: \"%V\"",
|
|
760 handler, &err);
|
|
761
|
|
762 if (rv) {
|
|
763 return NGX_ERROR;
|
|
764 }
|
|
765
|
|
766 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
767 }
|
|
768
|
|
769 if (n != 1) {
|
|
770 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
771 "call_sv(\"%V\") returned %d results", handler, n);
|
|
772 status = NGX_OK;
|
|
773 }
|
|
774
|
|
775 if (rv) {
|
|
776 return NGX_OK;
|
|
777 }
|
|
778
|
|
779 return (ngx_int_t) status;
|
|
780 }
|
|
781
|
|
782
|
|
783 #if (__INTEL_COMPILER)
|
|
784 #pragma warning(default:1599)
|
|
785 #endif
|
|
786
|
|
787
|
|
788 static void
|
|
789 ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv)
|
|
790 {
|
172
|
791 u_char *p;
|
|
792
|
|
793 for (p = handler->data; *p; p++) {
|
|
794 if (*p != ' ' && *p != '\t' && *p != CR && *p != LF) {
|
|
795 break;
|
|
796 }
|
|
797 }
|
|
798
|
198
|
799 if (ngx_strncmp(p, "sub ", 4) == 0 || ngx_strncmp(p, "use ", 4) == 0) {
|
172
|
800 *sv = eval_pv((char *) p, FALSE);
|
148
|
801 return;
|
|
802 }
|
|
803
|
|
804 *sv = NULL;
|
|
805 }
|
|
806
|
|
807
|
|
808 static void *
|
|
809 ngx_http_perl_create_main_conf(ngx_conf_t *cf)
|
|
810 {
|
|
811 ngx_http_perl_main_conf_t *pmcf;
|
|
812
|
|
813 pmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_main_conf_t));
|
|
814 if (pmcf == NULL) {
|
|
815 return NGX_CONF_ERROR;
|
|
816 }
|
|
817
|
|
818 pmcf->interp_max = NGX_CONF_UNSET_UINT;
|
|
819
|
|
820 if (ngx_array_init(&pmcf->requires, cf->pool, 1, sizeof(u_char *))
|
|
821 != NGX_OK)
|
|
822 {
|
|
823 return NULL;
|
|
824 }
|
|
825
|
|
826 return pmcf;
|
|
827 }
|
|
828
|
|
829
|
|
830 static char *
|
|
831 ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf)
|
|
832 {
|
|
833 ngx_http_perl_main_conf_t *pmcf = conf;
|
|
834
|
|
835 #if (NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
|
|
836 ngx_conf_init_unsigned_value(pmcf->interp_max, 10);
|
|
837 #else
|
|
838 ngx_conf_init_unsigned_value(pmcf->interp_max, 1);
|
|
839 #endif
|
|
840
|
|
841 pmcf->free_perls = ngx_pcalloc(cf->pool,
|
|
842 pmcf->interp_max * sizeof(PerlInterpreter *));
|
|
843 if (pmcf->free_perls == NULL) {
|
|
844 return NGX_CONF_ERROR;
|
|
845 }
|
|
846
|
|
847 if (pmcf->perl == NULL) {
|
|
848 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
|
|
849 return NGX_CONF_ERROR;
|
|
850 }
|
|
851 }
|
|
852
|
|
853 #if !(NGX_HAVE_PERL_CLONE)
|
|
854 ngx_http_perl_free_interpreter(pmcf, pmcf->perl);
|
|
855 #endif
|
|
856
|
|
857 return NGX_CONF_OK;
|
|
858 }
|
|
859
|
|
860
|
198
|
861 #if (NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
|
|
862
|
148
|
863 static void
|
|
864 ngx_http_perl_cleanup_perl(void *data)
|
|
865 {
|
198
|
866 PerlInterpreter *perl = data;
|
148
|
867
|
|
868 (void) perl_destruct(perl);
|
|
869
|
|
870 perl_free(perl);
|
|
871
|
|
872 PERL_SYS_TERM();
|
|
873 }
|
|
874
|
198
|
875 #endif
|
|
876
|
|
877
|
|
878 static void
|
|
879 ngx_http_perl_cleanup_sv(void *data)
|
|
880 {
|
202
|
881 ngx_http_perl_cleanup_t *cln = data;
|
198
|
882
|
202
|
883 dTHXa(cln->perl);
|
|
884
|
|
885 SvREFCNT_dec(cln->sv);
|
198
|
886 }
|
|
887
|
148
|
888
|
|
889 static ngx_int_t
|
|
890 ngx_http_perl_preconfiguration(ngx_conf_t *cf)
|
|
891 {
|
166
|
892 #if (NGX_HTTP_SSI)
|
148
|
893 ngx_int_t rc;
|
|
894 ngx_http_ssi_main_conf_t *smcf;
|
|
895
|
|
896 smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module);
|
|
897
|
|
898 rc = ngx_hash_add_key(&smcf->commands, &ngx_http_perl_ssi_command.name,
|
|
899 &ngx_http_perl_ssi_command, NGX_HASH_READONLY_KEY);
|
|
900
|
|
901 if (rc != NGX_OK) {
|
|
902 if (rc == NGX_BUSY) {
|
|
903 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
904 "conflicting SSI command \"%V\"",
|
|
905 &ngx_http_perl_ssi_command.name);
|
|
906 }
|
|
907
|
|
908 return NGX_ERROR;
|
|
909 }
|
166
|
910 #endif
|
148
|
911
|
|
912 return NGX_OK;
|
|
913 }
|
|
914
|
|
915
|
|
916 static void *
|
|
917 ngx_http_perl_create_loc_conf(ngx_conf_t *cf)
|
|
918 {
|
|
919 ngx_http_perl_loc_conf_t *plcf;
|
|
920
|
|
921 plcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_loc_conf_t));
|
|
922 if (plcf == NULL) {
|
|
923 return NGX_CONF_ERROR;
|
|
924 }
|
|
925
|
|
926 /*
|
|
927 * set by ngx_pcalloc():
|
|
928 *
|
|
929 * plcf->handler = { 0, NULL };
|
|
930 */
|
|
931
|
|
932 return plcf;
|
|
933 }
|
|
934
|
|
935
|
|
936 static char *
|
|
937 ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
938 {
|
|
939 ngx_http_perl_loc_conf_t *prev = parent;
|
|
940 ngx_http_perl_loc_conf_t *conf = child;
|
|
941
|
|
942 if (conf->sub == NULL) {
|
|
943 conf->sub = prev->sub;
|
|
944 conf->handler = prev->handler;
|
|
945 }
|
|
946
|
|
947 return NGX_CONF_OK;
|
|
948 }
|
|
949
|
|
950
|
|
951 static char *
|
|
952 ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
953 {
|
|
954 ngx_http_perl_main_conf_t *pmcf = conf;
|
|
955
|
|
956 u_char **p;
|
|
957 ngx_str_t *value;
|
|
958
|
|
959 value = cf->args->elts;
|
|
960
|
|
961 p = ngx_array_push(&pmcf->requires);
|
|
962
|
|
963 if (p == NULL) {
|
|
964 return NGX_CONF_ERROR;
|
|
965 }
|
|
966
|
|
967 *p = value[1].data;
|
|
968
|
|
969 return NGX_CONF_OK;
|
|
970 }
|
|
971
|
|
972
|
|
973 static char *
|
|
974 ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
975 {
|
|
976 ngx_http_perl_loc_conf_t *plcf = conf;
|
|
977
|
|
978 ngx_str_t *value;
|
198
|
979 ngx_pool_cleanup_t *cln;
|
202
|
980 ngx_http_perl_cleanup_t *pcln;
|
148
|
981 ngx_http_core_loc_conf_t *clcf;
|
|
982 ngx_http_perl_main_conf_t *pmcf;
|
|
983
|
|
984 value = cf->args->elts;
|
|
985
|
|
986 if (plcf->handler.data) {
|
|
987 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
988 "duplicate perl handler \"%V\"", &value[1]);
|
|
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
|
202
|
1000 cln = ngx_pool_cleanup_add(cf->pool, sizeof(ngx_http_perl_cleanup_t));
|
198
|
1001 if (cln == NULL) {
|
|
1002 return NGX_CONF_ERROR;
|
|
1003 }
|
|
1004
|
148
|
1005 plcf->handler = value[1];
|
|
1006
|
|
1007 {
|
|
1008
|
|
1009 dTHXa(pmcf->perl);
|
|
1010
|
|
1011 ngx_http_perl_eval_anon_sub(aTHX_ &value[1], &plcf->sub);
|
|
1012
|
|
1013 if (plcf->sub == &PL_sv_undef) {
|
|
1014 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
|
|
1015 "eval_pv(\"%V\") failed", &value[1]);
|
|
1016 return NGX_CONF_ERROR;
|
|
1017 }
|
|
1018
|
|
1019 if (plcf->sub == NULL) {
|
|
1020 plcf->sub = newSVpvn((char *) value[1].data, value[1].len);
|
|
1021 }
|
|
1022
|
|
1023 }
|
|
1024
|
198
|
1025 cln->handler = ngx_http_perl_cleanup_sv;
|
202
|
1026 pcln = cln->data;
|
|
1027 pcln->sv = plcf->sub;
|
|
1028 pcln->perl = pmcf->perl;
|
198
|
1029
|
148
|
1030 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
1031 clcf->handler = ngx_http_perl_handler;
|
|
1032
|
|
1033 return NGX_CONF_OK;
|
|
1034 }
|
|
1035
|
|
1036
|
|
1037 static char *
|
|
1038 ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1039 {
|
|
1040 ngx_int_t index;
|
|
1041 ngx_str_t *value;
|
198
|
1042 ngx_pool_cleanup_t *cln;
|
148
|
1043 ngx_http_variable_t *v;
|
202
|
1044 ngx_http_perl_cleanup_t *pcln;
|
148
|
1045 ngx_http_perl_variable_t *pv;
|
|
1046 ngx_http_perl_main_conf_t *pmcf;
|
|
1047
|
|
1048 value = cf->args->elts;
|
|
1049
|
|
1050 if (value[1].data[0] != '$') {
|
|
1051 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1052 "invalid variable name \"%V\"", &value[1]);
|
|
1053 return NGX_CONF_ERROR;
|
|
1054 }
|
|
1055
|
|
1056 value[1].len--;
|
|
1057 value[1].data++;
|
|
1058
|
|
1059 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGABLE);
|
|
1060 if (v == NULL) {
|
|
1061 return NGX_CONF_ERROR;
|
|
1062 }
|
|
1063
|
|
1064 pv = ngx_palloc(cf->pool, sizeof(ngx_http_perl_variable_t));
|
|
1065 if (pv == NULL) {
|
|
1066 return NGX_CONF_ERROR;
|
|
1067 }
|
|
1068
|
|
1069 index = ngx_http_get_variable_index(cf, &value[1]);
|
|
1070 if (index == NGX_ERROR) {
|
|
1071 return NGX_CONF_ERROR;
|
|
1072 }
|
|
1073
|
|
1074 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module);
|
|
1075
|
|
1076 if (pmcf->perl == NULL) {
|
|
1077 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
|
|
1078 return NGX_CONF_ERROR;
|
|
1079 }
|
|
1080 }
|
|
1081
|
202
|
1082 cln = ngx_pool_cleanup_add(cf->pool, sizeof(ngx_http_perl_cleanup_t));
|
198
|
1083 if (cln == NULL) {
|
|
1084 return NGX_CONF_ERROR;
|
|
1085 }
|
|
1086
|
148
|
1087 pv->handler = value[2];
|
|
1088
|
|
1089 {
|
|
1090
|
|
1091 dTHXa(pmcf->perl);
|
|
1092
|
|
1093 ngx_http_perl_eval_anon_sub(aTHX_ &value[2], &pv->sub);
|
|
1094
|
|
1095 if (pv->sub == &PL_sv_undef) {
|
|
1096 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
|
|
1097 "eval_pv(\"%V\") failed", &value[2]);
|
|
1098 return NGX_CONF_ERROR;
|
|
1099 }
|
|
1100
|
|
1101 if (pv->sub == NULL) {
|
|
1102 pv->sub = newSVpvn((char *) value[2].data, value[2].len);
|
|
1103 }
|
|
1104
|
|
1105 }
|
|
1106
|
198
|
1107 cln->handler = ngx_http_perl_cleanup_sv;
|
202
|
1108 pcln = cln->data;
|
|
1109 pcln->sv = pv->sub;
|
|
1110 pcln->perl = pmcf->perl;
|
198
|
1111
|
186
|
1112 v->get_handler = ngx_http_perl_variable;
|
148
|
1113 v->data = (uintptr_t) pv;
|
|
1114
|
|
1115 return NGX_CONF_OK;
|
|
1116 }
|
|
1117
|
|
1118
|
|
1119 static char *
|
|
1120 ngx_http_perl_interp_max_unsupported(ngx_conf_t *cf, void *post, void *data)
|
|
1121 {
|
|
1122 #if (NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
|
|
1123
|
|
1124 return NGX_CONF_OK;
|
|
1125
|
|
1126 #else
|
|
1127
|
|
1128 return "to use perl_interp_max you have to build perl with "
|
|
1129 "-Dusemultiplicity or -Dusethreads options";
|
|
1130
|
|
1131 #endif
|
|
1132 }
|