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