Mercurial > hg > nginx-quic
annotate src/http/modules/perl/ngx_http_perl_module.c @ 1897:b43f244d0430
optimize $r->sleep
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Sat, 16 Feb 2008 14:23:14 +0000 |
parents | 86bb52e28ce0 |
children | d24ef26f1205 |
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 | |
599 | 44 static char *ngx_http_perl_init_interpreter(ngx_conf_t *cf, |
45 ngx_http_perl_main_conf_t *pmcf); | |
1069 | 46 static PerlInterpreter *ngx_http_perl_create_interpreter(ngx_conf_t *cf, |
47 ngx_http_perl_main_conf_t *pmcf); | |
653 | 48 static ngx_int_t ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires, |
649 | 49 ngx_log_t *log); |
599 | 50 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
|
51 HV *nginx, SV *sub, ngx_str_t **args, ngx_str_t *handler, ngx_str_t *rv); |
599 | 52 static void ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv); |
53 | |
54 static ngx_int_t ngx_http_perl_preconfiguration(ngx_conf_t *cf); | |
55 static void *ngx_http_perl_create_main_conf(ngx_conf_t *cf); | |
56 static char *ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf); | |
57 static void *ngx_http_perl_create_loc_conf(ngx_conf_t *cf); | |
58 static char *ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, | |
59 void *child); | |
60 static char *ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd, | |
61 void *conf); | |
62 static char *ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
63 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
|
64 |
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
65 #if (NGX_HAVE_PERL_MULTIPLICITY) |
599 | 66 static void ngx_http_perl_cleanup_perl(void *data); |
649 | 67 #endif |
599 | 68 |
1257 | 69 static ngx_int_t ngx_http_perl_init_worker(ngx_cycle_t *cycle); |
1221
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
70 static void ngx_http_perl_exit(ngx_cycle_t *cycle); |
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
71 |
599 | 72 |
73 static ngx_command_t ngx_http_perl_commands[] = { | |
74 | |
75 { ngx_string("perl_modules"), | |
76 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, | |
77 ngx_conf_set_str_slot, | |
78 NGX_HTTP_MAIN_CONF_OFFSET, | |
79 offsetof(ngx_http_perl_main_conf_t, modules), | |
80 NULL }, | |
81 | |
82 { ngx_string("perl_require"), | |
83 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, | |
84 ngx_http_perl_require, | |
85 NGX_HTTP_MAIN_CONF_OFFSET, | |
86 0, | |
87 NULL }, | |
88 | |
89 { ngx_string("perl"), | |
922
a5b9cdfe3e19
allow "perl" inside "limit_except"
Igor Sysoev <igor@sysoev.ru>
parents:
913
diff
changeset
|
90 NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1, |
599 | 91 ngx_http_perl, |
92 NGX_HTTP_LOC_CONF_OFFSET, | |
93 0, | |
94 NULL }, | |
95 | |
96 { ngx_string("perl_set"), | |
97 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2, | |
98 ngx_http_perl_set, | |
99 NGX_HTTP_LOC_CONF_OFFSET, | |
100 0, | |
101 NULL }, | |
102 | |
103 ngx_null_command | |
104 }; | |
105 | |
106 | |
107 static ngx_http_module_t ngx_http_perl_module_ctx = { | |
108 ngx_http_perl_preconfiguration, /* preconfiguration */ | |
109 NULL, /* postconfiguration */ | |
110 | |
111 ngx_http_perl_create_main_conf, /* create main configuration */ | |
112 ngx_http_perl_init_main_conf, /* init main configuration */ | |
113 | |
114 NULL, /* create server configuration */ | |
115 NULL, /* merge server configuration */ | |
116 | |
117 ngx_http_perl_create_loc_conf, /* create location configuration */ | |
118 ngx_http_perl_merge_loc_conf /* merge location configuration */ | |
119 }; | |
120 | |
121 | |
122 ngx_module_t ngx_http_perl_module = { | |
123 NGX_MODULE_V1, | |
124 &ngx_http_perl_module_ctx, /* module context */ | |
125 ngx_http_perl_commands, /* module directives */ | |
126 NGX_HTTP_MODULE, /* module type */ | |
127 NULL, /* init master */ | |
128 NULL, /* init module */ | |
1257 | 129 ngx_http_perl_init_worker, /* init process */ |
599 | 130 NULL, /* init thread */ |
131 NULL, /* exit thread */ | |
132 NULL, /* exit process */ | |
1221
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
133 ngx_http_perl_exit, /* exit master */ |
599 | 134 NGX_MODULE_V1_PADDING |
135 }; | |
136 | |
137 | |
617 | 138 #if (NGX_HTTP_SSI) |
139 | |
599 | 140 #define NGX_HTTP_PERL_SSI_SUB 0 |
141 #define NGX_HTTP_PERL_SSI_ARG 1 | |
142 | |
143 | |
144 static ngx_http_ssi_param_t ngx_http_perl_ssi_params[] = { | |
145 { ngx_string("sub"), NGX_HTTP_PERL_SSI_SUB, 1, 0 }, | |
146 { ngx_string("arg"), NGX_HTTP_PERL_SSI_ARG, 0, 1 }, | |
147 { ngx_null_string, 0, 0, 0 } | |
148 }; | |
149 | |
150 static ngx_http_ssi_command_t ngx_http_perl_ssi_command = { | |
667 | 151 ngx_string("perl"), ngx_http_perl_ssi, ngx_http_perl_ssi_params, 0, 0, 1 |
599 | 152 }; |
153 | |
617 | 154 #endif |
155 | |
599 | 156 |
681 | 157 static ngx_str_t ngx_null_name = ngx_null_string; |
158 | |
159 | |
633 | 160 static HV *nginx_stash; |
161 | |
599 | 162 static void |
163 ngx_http_perl_xs_init(pTHX) | |
164 { | |
165 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__); | |
633 | 166 |
167 nginx_stash = gv_stashpv("nginx", TRUE); | |
599 | 168 } |
169 | |
170 | |
171 static ngx_int_t | |
172 ngx_http_perl_handler(ngx_http_request_t *r) | |
173 { | |
629 | 174 /* TODO: Win32 */ |
175 if (r->zero_in_uri) { | |
176 return NGX_HTTP_NOT_FOUND; | |
177 } | |
178 | |
681 | 179 ngx_http_perl_handle_request(r); |
180 | |
181 return NGX_DONE; | |
629 | 182 } |
183 | |
184 | |
681 | 185 void |
629 | 186 ngx_http_perl_handle_request(ngx_http_request_t *r) |
187 { | |
681 | 188 SV *sub; |
599 | 189 ngx_int_t rc; |
681 | 190 ngx_str_t uri, args, *handler; |
599 | 191 ngx_http_perl_ctx_t *ctx; |
192 ngx_http_perl_loc_conf_t *plcf; | |
193 ngx_http_perl_main_conf_t *pmcf; | |
194 | |
195 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl handler"); | |
196 | |
197 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
198 | |
199 if (ctx == NULL) { | |
200 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t)); | |
201 if (ctx == NULL) { | |
681 | 202 ngx_http_finalize_request(r, NGX_ERROR); |
1025 | 203 return; |
599 | 204 } |
205 | |
206 ngx_http_set_ctx(r, ctx, ngx_http_perl_module); | |
207 } | |
208 | |
209 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module); | |
210 | |
211 { | |
212 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
213 dTHXa(pmcf->perl); |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
214 PERL_SET_CONTEXT(pmcf->perl); |
599 | 215 |
681 | 216 if (ctx->next == NULL) { |
217 plcf = ngx_http_get_module_loc_conf(r, ngx_http_perl_module); | |
218 sub = plcf->sub; | |
219 handler = &plcf->handler; | |
599 | 220 |
681 | 221 } else { |
222 sub = ctx->next; | |
223 handler = &ngx_null_name; | |
224 ctx->next = NULL; | |
225 } | |
226 | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
227 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
|
228 NULL); |
599 | 229 |
230 } | |
231 | |
1702
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
232 if (rc == NGX_DONE) { |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
233 return; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
234 } |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
235 |
599 | 236 if (rc > 600) { |
237 rc = NGX_OK; | |
238 } | |
239 | |
240 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
241 "perl handler done: %i", rc); | |
242 | |
243 if (ctx->redirect_uri.len) { | |
244 uri = ctx->redirect_uri; | |
245 args = ctx->redirect_args; | |
629 | 246 |
247 } else { | |
248 uri.len = 0; | |
599 | 249 } |
250 | |
633 | 251 ctx->filename.data = NULL; |
599 | 252 ctx->redirect_uri.len = 0; |
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 | |
1897 | 272 void |
911 | 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; | |
1565 | 336 v->no_cacheable = 0; |
599 | 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) { |
1352 | 454 if (ngx_conf_full_name(cf->cycle, &pmcf->modules, 0) != NGX_OK) { |
639 | 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) { | |
1165 | 462 |
463 if (ngx_set_environment(cf->cycle, NULL) == NULL) { | |
464 return NGX_CONF_ERROR; | |
465 } | |
466 | |
653 | 467 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log) |
468 != NGX_OK) | |
469 { | |
649 | 470 return NGX_CONF_ERROR; |
471 } | |
472 | |
473 pmcf->perl = perl; | |
1163
8288459f15c9
set nginx_stash after reconfiguration in single interpreter perl
Igor Sysoev <igor@sysoev.ru>
parents:
1069
diff
changeset
|
474 pmcf->nginx = nginx_stash; |
649 | 475 |
476 return NGX_CONF_OK; | |
477 } | |
478 | |
479 #endif | |
480 | |
1221
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
481 if (nginx_stash == NULL) { |
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
482 PERL_SYS_INIT(&ngx_argc, &ngx_argv); |
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
483 } |
599 | 484 |
1069 | 485 pmcf->perl = ngx_http_perl_create_interpreter(cf, pmcf); |
599 | 486 |
487 if (pmcf->perl == NULL) { | |
488 return NGX_CONF_ERROR; | |
489 } | |
490 | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
491 pmcf->nginx = nginx_stash; |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
492 |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
493 #if (NGX_HAVE_PERL_MULTIPLICITY) |
649 | 494 |
599 | 495 cln->handler = ngx_http_perl_cleanup_perl; |
496 cln->data = pmcf->perl; | |
497 | |
649 | 498 #else |
499 | |
500 perl = pmcf->perl; | |
501 | |
502 #endif | |
503 | |
599 | 504 return NGX_CONF_OK; |
505 } | |
506 | |
507 | |
508 static PerlInterpreter * | |
1069 | 509 ngx_http_perl_create_interpreter(ngx_conf_t *cf, |
510 ngx_http_perl_main_conf_t *pmcf) | |
599 | 511 { |
512 int n; | |
775 | 513 STRLEN len; |
514 SV *sv; | |
515 char *ver, *embedding[6]; | |
599 | 516 PerlInterpreter *perl; |
517 | |
1069 | 518 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "create perl interpreter"); |
519 | |
520 if (ngx_set_environment(cf->cycle, NULL) == NULL) { | |
521 return NULL; | |
522 } | |
599 | 523 |
524 perl = perl_alloc(); | |
525 if (perl == NULL) { | |
1069 | 526 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_alloc() failed"); |
599 | 527 return NULL; |
528 } | |
529 | |
530 { | |
531 | |
532 dTHXa(perl); | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
533 PERL_SET_CONTEXT(perl); |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
534 |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
535 perl_construct(perl); |
599 | 536 |
537 #ifdef PERL_EXIT_DESTRUCT_END | |
538 PL_exit_flags |= PERL_EXIT_DESTRUCT_END; | |
539 #endif | |
540 | |
541 embedding[0] = ""; | |
542 | |
543 if (pmcf->modules.data) { | |
544 embedding[1] = "-I"; | |
545 embedding[2] = (char *) pmcf->modules.data; | |
546 n = 3; | |
547 | |
548 } else { | |
549 n = 1; | |
550 } | |
551 | |
552 embedding[n++] = "-Mnginx"; | |
553 embedding[n++] = "-e"; | |
554 embedding[n++] = "0"; | |
555 | |
556 n = perl_parse(perl, ngx_http_perl_xs_init, n, embedding, NULL); | |
557 | |
558 if (n != 0) { | |
1069 | 559 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_parse() failed: %d", n); |
599 | 560 goto fail; |
561 } | |
562 | |
775 | 563 sv = get_sv("nginx::VERSION", FALSE); |
564 ver = SvPV(sv, len); | |
565 | |
566 if (ngx_strcmp(ver, NGINX_VERSION) != 0) { | |
1069 | 567 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, |
775 | 568 "version " NGINX_VERSION " of nginx.pm is required, " |
569 "but %s was found", ver); | |
570 goto fail; | |
571 } | |
572 | |
1069 | 573 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log) != NGX_OK) { |
649 | 574 goto fail; |
599 | 575 } |
576 | |
577 } | |
578 | |
579 return perl; | |
580 | |
581 fail: | |
582 | |
583 (void) perl_destruct(perl); | |
584 | |
585 perl_free(perl); | |
586 | |
587 return NULL; | |
588 } | |
589 | |
590 | |
649 | 591 static ngx_int_t |
653 | 592 ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires, ngx_log_t *log) |
649 | 593 { |
594 char **script; | |
595 STRLEN len; | |
596 ngx_str_t err; | |
597 ngx_uint_t i; | |
598 | |
599 script = requires->elts; | |
600 for (i = 0; i < requires->nelts; i++) { | |
601 | |
602 require_pv(script[i]); | |
603 | |
604 if (SvTRUE(ERRSV)) { | |
605 | |
606 err.data = (u_char *) SvPV(ERRSV, len); | |
607 for (len--; err.data[len] == LF || err.data[len] == CR; len--) { | |
608 /* void */ | |
609 } | |
610 err.len = len + 1; | |
611 | |
612 ngx_log_error(NGX_LOG_EMERG, log, 0, | |
613 "require_pv(\"%s\") failed: \"%V\"", script[i], &err); | |
614 | |
615 return NGX_ERROR; | |
616 } | |
617 } | |
618 | |
619 return NGX_OK; | |
620 } | |
621 | |
622 | |
599 | 623 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
|
624 ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r, HV *nginx, SV *sub, |
599 | 625 ngx_str_t **args, ngx_str_t *handler, ngx_str_t *rv) |
626 { | |
1702
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
627 SV *sv; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
628 int n, status; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
629 char *line; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
630 STRLEN len, n_a; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
631 ngx_str_t err; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
632 ngx_uint_t i; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
633 ngx_connection_t *c; |
599 | 634 |
635 dSP; | |
636 | |
637 status = 0; | |
638 | |
639 ENTER; | |
640 SAVETMPS; | |
641 | |
642 PUSHMARK(sp); | |
643 | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
644 sv = sv_2mortal(sv_bless(newRV_noinc(newSViv(PTR2IV(r))), nginx)); |
599 | 645 XPUSHs(sv); |
646 | |
647 if (args) { | |
648 for (i = 0; args[i]; i++) { /* void */ } | |
649 | |
650 EXTEND(sp, (int) i); | |
651 | |
652 for (i = 0; args[i]; i++) { | |
653 PUSHs(sv_2mortal(newSVpvn((char *) args[i]->data, args[i]->len))); | |
654 } | |
655 } | |
656 | |
657 PUTBACK; | |
658 | |
1702
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
659 c = r->connection; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
660 |
599 | 661 n = call_sv(sub, G_EVAL); |
662 | |
663 SPAGAIN; | |
664 | |
1702
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
665 if (c->destroyed) { |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
666 PUTBACK; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
667 |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
668 FREETMPS; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
669 LEAVE; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
670 |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
671 return NGX_DONE; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
672 } |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
673 |
599 | 674 if (n) { |
675 if (rv == NULL) { | |
676 status = POPi; | |
677 | |
1702
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
678 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
599 | 679 "call_sv: %d", status); |
680 | |
681 } else { | |
619 | 682 line = SvPVx(POPs, n_a); |
599 | 683 rv->len = n_a; |
684 | |
685 rv->data = ngx_palloc(r->pool, n_a); | |
686 if (rv->data == NULL) { | |
687 return NGX_ERROR; | |
688 } | |
689 | |
690 ngx_memcpy(rv->data, line, n_a); | |
691 } | |
692 } | |
693 | |
694 PUTBACK; | |
695 | |
696 FREETMPS; | |
697 LEAVE; | |
698 | |
699 /* check $@ */ | |
700 | |
701 if (SvTRUE(ERRSV)) { | |
702 | |
703 err.data = (u_char *) SvPV(ERRSV, len); | |
704 for (len--; err.data[len] == LF || err.data[len] == CR; len--) { | |
705 /* void */ | |
706 } | |
707 err.len = len + 1; | |
708 | |
1702
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
709 ngx_log_error(NGX_LOG_ERR, c->log, 0, |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
710 "call_sv(\"%V\") failed: \"%V\"", handler, &err); |
599 | 711 |
712 if (rv) { | |
713 return NGX_ERROR; | |
714 } | |
715 | |
716 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
717 } | |
718 | |
719 if (n != 1) { | |
1702
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
720 ngx_log_error(NGX_LOG_ALERT, c->log, 0, |
599 | 721 "call_sv(\"%V\") returned %d results", handler, n); |
722 status = NGX_OK; | |
723 } | |
724 | |
725 if (rv) { | |
726 return NGX_OK; | |
727 } | |
728 | |
729 return (ngx_int_t) status; | |
730 } | |
731 | |
732 | |
733 static void | |
734 ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv) | |
735 { | |
623 | 736 u_char *p; |
737 | |
738 for (p = handler->data; *p; p++) { | |
739 if (*p != ' ' && *p != '\t' && *p != CR && *p != LF) { | |
740 break; | |
741 } | |
742 } | |
743 | |
649 | 744 if (ngx_strncmp(p, "sub ", 4) == 0 || ngx_strncmp(p, "use ", 4) == 0) { |
623 | 745 *sv = eval_pv((char *) p, FALSE); |
874 | 746 |
747 /* eval_pv() does not set ERRSV on failure */ | |
748 | |
599 | 749 return; |
750 } | |
751 | |
752 *sv = NULL; | |
753 } | |
754 | |
755 | |
756 static void * | |
757 ngx_http_perl_create_main_conf(ngx_conf_t *cf) | |
758 { | |
759 ngx_http_perl_main_conf_t *pmcf; | |
760 | |
761 pmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_main_conf_t)); | |
762 if (pmcf == NULL) { | |
763 return NGX_CONF_ERROR; | |
764 } | |
765 | |
766 if (ngx_array_init(&pmcf->requires, cf->pool, 1, sizeof(u_char *)) | |
767 != NGX_OK) | |
768 { | |
769 return NULL; | |
770 } | |
771 | |
772 return pmcf; | |
773 } | |
774 | |
775 | |
776 static char * | |
777 ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf) | |
778 { | |
779 ngx_http_perl_main_conf_t *pmcf = conf; | |
780 | |
781 if (pmcf->perl == NULL) { | |
782 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) { | |
783 return NGX_CONF_ERROR; | |
784 } | |
785 } | |
786 | |
787 return NGX_CONF_OK; | |
788 } | |
789 | |
790 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
791 #if (NGX_HAVE_PERL_MULTIPLICITY) |
649 | 792 |
599 | 793 static void |
794 ngx_http_perl_cleanup_perl(void *data) | |
795 { | |
649 | 796 PerlInterpreter *perl = data; |
599 | 797 |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
798 PERL_SET_CONTEXT(perl); |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
799 |
599 | 800 (void) perl_destruct(perl); |
801 | |
802 perl_free(perl); | |
803 } | |
804 | |
649 | 805 #endif |
806 | |
807 | |
599 | 808 static ngx_int_t |
809 ngx_http_perl_preconfiguration(ngx_conf_t *cf) | |
810 { | |
617 | 811 #if (NGX_HTTP_SSI) |
599 | 812 ngx_int_t rc; |
813 ngx_http_ssi_main_conf_t *smcf; | |
814 | |
815 smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module); | |
816 | |
817 rc = ngx_hash_add_key(&smcf->commands, &ngx_http_perl_ssi_command.name, | |
818 &ngx_http_perl_ssi_command, NGX_HASH_READONLY_KEY); | |
819 | |
820 if (rc != NGX_OK) { | |
821 if (rc == NGX_BUSY) { | |
822 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
823 "conflicting SSI command \"%V\"", | |
824 &ngx_http_perl_ssi_command.name); | |
825 } | |
826 | |
827 return NGX_ERROR; | |
828 } | |
617 | 829 #endif |
599 | 830 |
831 return NGX_OK; | |
832 } | |
833 | |
834 | |
835 static void * | |
836 ngx_http_perl_create_loc_conf(ngx_conf_t *cf) | |
837 { | |
838 ngx_http_perl_loc_conf_t *plcf; | |
839 | |
840 plcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_loc_conf_t)); | |
841 if (plcf == NULL) { | |
842 return NGX_CONF_ERROR; | |
843 } | |
844 | |
845 /* | |
846 * set by ngx_pcalloc(): | |
847 * | |
848 * plcf->handler = { 0, NULL }; | |
849 */ | |
850 | |
851 return plcf; | |
852 } | |
853 | |
854 | |
855 static char * | |
856 ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
857 { | |
858 ngx_http_perl_loc_conf_t *prev = parent; | |
859 ngx_http_perl_loc_conf_t *conf = child; | |
860 | |
861 if (conf->sub == NULL) { | |
862 conf->sub = prev->sub; | |
863 conf->handler = prev->handler; | |
864 } | |
865 | |
866 return NGX_CONF_OK; | |
867 } | |
868 | |
869 | |
870 static char * | |
871 ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
872 { | |
873 ngx_http_perl_main_conf_t *pmcf = conf; | |
874 | |
875 u_char **p; | |
876 ngx_str_t *value; | |
877 | |
878 value = cf->args->elts; | |
879 | |
880 p = ngx_array_push(&pmcf->requires); | |
881 | |
882 if (p == NULL) { | |
883 return NGX_CONF_ERROR; | |
884 } | |
885 | |
886 *p = value[1].data; | |
887 | |
888 return NGX_CONF_OK; | |
889 } | |
890 | |
891 | |
892 static char * | |
893 ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
894 { | |
895 ngx_http_perl_loc_conf_t *plcf = conf; | |
896 | |
897 ngx_str_t *value; | |
898 ngx_http_core_loc_conf_t *clcf; | |
899 ngx_http_perl_main_conf_t *pmcf; | |
900 | |
901 value = cf->args->elts; | |
902 | |
903 if (plcf->handler.data) { | |
904 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
905 "duplicate perl handler \"%V\"", &value[1]); | |
906 return NGX_CONF_ERROR; | |
907 } | |
908 | |
909 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module); | |
910 | |
911 if (pmcf->perl == NULL) { | |
912 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) { | |
913 return NGX_CONF_ERROR; | |
914 } | |
915 } | |
916 | |
917 plcf->handler = value[1]; | |
918 | |
919 { | |
920 | |
921 dTHXa(pmcf->perl); | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
922 PERL_SET_CONTEXT(pmcf->perl); |
599 | 923 |
924 ngx_http_perl_eval_anon_sub(aTHX_ &value[1], &plcf->sub); | |
925 | |
926 if (plcf->sub == &PL_sv_undef) { | |
927 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, | |
928 "eval_pv(\"%V\") failed", &value[1]); | |
929 return NGX_CONF_ERROR; | |
930 } | |
931 | |
932 if (plcf->sub == NULL) { | |
933 plcf->sub = newSVpvn((char *) value[1].data, value[1].len); | |
934 } | |
935 | |
936 } | |
937 | |
938 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
939 clcf->handler = ngx_http_perl_handler; | |
940 | |
941 return NGX_CONF_OK; | |
942 } | |
943 | |
944 | |
945 static char * | |
946 ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
947 { | |
948 ngx_int_t index; | |
949 ngx_str_t *value; | |
950 ngx_http_variable_t *v; | |
951 ngx_http_perl_variable_t *pv; | |
952 ngx_http_perl_main_conf_t *pmcf; | |
953 | |
954 value = cf->args->elts; | |
955 | |
956 if (value[1].data[0] != '$') { | |
957 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
958 "invalid variable name \"%V\"", &value[1]); | |
959 return NGX_CONF_ERROR; | |
960 } | |
961 | |
962 value[1].len--; | |
963 value[1].data++; | |
964 | |
1565 | 965 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE); |
599 | 966 if (v == NULL) { |
967 return NGX_CONF_ERROR; | |
968 } | |
969 | |
970 pv = ngx_palloc(cf->pool, sizeof(ngx_http_perl_variable_t)); | |
971 if (pv == NULL) { | |
972 return NGX_CONF_ERROR; | |
973 } | |
974 | |
975 index = ngx_http_get_variable_index(cf, &value[1]); | |
976 if (index == NGX_ERROR) { | |
977 return NGX_CONF_ERROR; | |
978 } | |
979 | |
980 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module); | |
981 | |
982 if (pmcf->perl == NULL) { | |
983 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) { | |
984 return NGX_CONF_ERROR; | |
985 } | |
986 } | |
987 | |
988 pv->handler = value[2]; | |
989 | |
990 { | |
991 | |
992 dTHXa(pmcf->perl); | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
993 PERL_SET_CONTEXT(pmcf->perl); |
599 | 994 |
995 ngx_http_perl_eval_anon_sub(aTHX_ &value[2], &pv->sub); | |
996 | |
997 if (pv->sub == &PL_sv_undef) { | |
998 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, | |
999 "eval_pv(\"%V\") failed", &value[2]); | |
1000 return NGX_CONF_ERROR; | |
1001 } | |
1002 | |
1003 if (pv->sub == NULL) { | |
1004 pv->sub = newSVpvn((char *) value[2].data, value[2].len); | |
1005 } | |
1006 | |
1007 } | |
1008 | |
637 | 1009 v->get_handler = ngx_http_perl_variable; |
599 | 1010 v->data = (uintptr_t) pv; |
1011 | |
1012 return NGX_CONF_OK; | |
1013 } | |
1221
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
1014 |
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
1015 |
1257 | 1016 static ngx_int_t |
1017 ngx_http_perl_init_worker(ngx_cycle_t *cycle) | |
1018 { | |
1258
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1019 ngx_http_perl_main_conf_t *pmcf; |
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1020 |
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1021 pmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_perl_module); |
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1022 |
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1023 { |
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1024 |
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1025 dTHXa(pmcf->perl); |
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1026 PERL_SET_CONTEXT(pmcf->perl); |
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1027 |
1257 | 1028 /* set worker's $$ */ |
1029 | |
1030 sv_setiv(GvSV(gv_fetchpv("$", TRUE, SVt_PV)), (I32) ngx_pid); | |
1031 | |
1258
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1032 } |
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1033 |
1257 | 1034 return NGX_OK; |
1035 } | |
1036 | |
1221
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
1037 static void |
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
1038 ngx_http_perl_exit(ngx_cycle_t *cycle) |
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
1039 { |
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
1040 PERL_SYS_TERM(); |
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
1041 } |