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