Mercurial > hg > nginx
annotate src/http/modules/perl/ngx_http_perl_module.c @ 2820:5eb4e2d2e9fa
test already destroyed request
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 08 May 2009 14:31:59 +0000 |
parents | 8e720b7fe6d2 |
children | c7d57b539248 |
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, |
1899
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
51 HV *nginx, SV *sub, SV **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 |
2715 | 157 static ngx_str_t ngx_null_name = ngx_null_string; |
158 | |
159 static HV *nginx_stash; | |
681 | 160 |
2715 | 161 #if (NGX_HAVE_PERL_MULTIPLICITY) |
162 static ngx_uint_t ngx_perl_term; | |
163 #endif | |
681 | 164 |
633 | 165 |
599 | 166 static void |
167 ngx_http_perl_xs_init(pTHX) | |
168 { | |
169 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__); | |
633 | 170 |
171 nginx_stash = gv_stashpv("nginx", TRUE); | |
599 | 172 } |
173 | |
174 | |
175 static ngx_int_t | |
176 ngx_http_perl_handler(ngx_http_request_t *r) | |
177 { | |
629 | 178 if (r->zero_in_uri) { |
179 return NGX_HTTP_NOT_FOUND; | |
180 } | |
181 | |
681 | 182 ngx_http_perl_handle_request(r); |
183 | |
184 return NGX_DONE; | |
629 | 185 } |
186 | |
187 | |
681 | 188 void |
629 | 189 ngx_http_perl_handle_request(ngx_http_request_t *r) |
190 { | |
681 | 191 SV *sub; |
599 | 192 ngx_int_t rc; |
681 | 193 ngx_str_t uri, args, *handler; |
599 | 194 ngx_http_perl_ctx_t *ctx; |
195 ngx_http_perl_loc_conf_t *plcf; | |
196 ngx_http_perl_main_conf_t *pmcf; | |
197 | |
198 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl handler"); | |
199 | |
200 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
201 | |
202 if (ctx == NULL) { | |
203 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t)); | |
204 if (ctx == NULL) { | |
681 | 205 ngx_http_finalize_request(r, NGX_ERROR); |
1025 | 206 return; |
599 | 207 } |
208 | |
209 ngx_http_set_ctx(r, ctx, ngx_http_perl_module); | |
210 } | |
211 | |
212 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module); | |
213 | |
214 { | |
215 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
216 dTHXa(pmcf->perl); |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
217 PERL_SET_CONTEXT(pmcf->perl); |
599 | 218 |
681 | 219 if (ctx->next == NULL) { |
220 plcf = ngx_http_get_module_loc_conf(r, ngx_http_perl_module); | |
221 sub = plcf->sub; | |
222 handler = &plcf->handler; | |
599 | 223 |
681 | 224 } else { |
225 sub = ctx->next; | |
226 handler = &ngx_null_name; | |
227 ctx->next = NULL; | |
228 } | |
229 | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
230 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
|
231 NULL); |
599 | 232 |
233 } | |
234 | |
1702
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
235 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
|
236 return; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
237 } |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
238 |
599 | 239 if (rc > 600) { |
240 rc = NGX_OK; | |
241 } | |
242 | |
243 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
244 "perl handler done: %i", rc); | |
245 | |
246 if (ctx->redirect_uri.len) { | |
247 uri = ctx->redirect_uri; | |
248 args = ctx->redirect_args; | |
629 | 249 |
250 } else { | |
251 uri.len = 0; | |
599 | 252 } |
253 | |
633 | 254 ctx->filename.data = NULL; |
599 | 255 ctx->redirect_uri.len = 0; |
256 | |
681 | 257 if (ctx->done || ctx->next) { |
258 return; | |
259 } | |
260 | |
599 | 261 if (uri.len) { |
629 | 262 ngx_http_internal_redirect(r, &uri, &args); |
263 return; | |
599 | 264 } |
265 | |
266 if (rc == NGX_OK || rc == NGX_HTTP_OK) { | |
629 | 267 ngx_http_send_special(r, NGX_HTTP_LAST); |
681 | 268 ctx->done = 1; |
599 | 269 } |
270 | |
629 | 271 ngx_http_finalize_request(r, rc); |
599 | 272 } |
273 | |
274 | |
1897 | 275 void |
911 | 276 ngx_http_perl_sleep_handler(ngx_http_request_t *r) |
277 { | |
913
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
278 ngx_event_t *wev; |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
279 |
911 | 280 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
281 "perl sleep handler"); | |
282 | |
913
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
283 wev = r->connection->write; |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
284 |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
285 if (wev->timedout) { |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
286 wev->timedout = 0; |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
287 ngx_http_perl_handle_request(r); |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
288 return; |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
289 } |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
290 |
2388
722b5aff05ae
use "!= NGX_OK" instead of "== NGX_ERROR"
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
291 if (ngx_handle_write_event(wev, 0) != NGX_OK) { |
913
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
292 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
|
293 } |
911 | 294 } |
295 | |
296 | |
599 | 297 static ngx_int_t |
298 ngx_http_perl_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
299 uintptr_t data) | |
300 { | |
301 ngx_http_perl_variable_t *pv = (ngx_http_perl_variable_t *) data; | |
302 | |
303 ngx_int_t rc; | |
304 ngx_str_t value; | |
305 ngx_http_perl_ctx_t *ctx; | |
306 ngx_http_perl_main_conf_t *pmcf; | |
307 | |
308 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
309 "perl variable handler"); | |
310 | |
311 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
312 | |
313 if (ctx == NULL) { | |
314 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t)); | |
315 if (ctx == NULL) { | |
316 return NGX_ERROR; | |
317 } | |
318 | |
319 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
|
320 } |
599 | 321 |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
322 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module); |
599 | 323 |
324 value.data = NULL; | |
325 | |
326 { | |
327 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
328 dTHXa(pmcf->perl); |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
329 PERL_SET_CONTEXT(pmcf->perl); |
599 | 330 |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
331 rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, pv->sub, NULL, |
599 | 332 &pv->handler, &value); |
333 | |
334 } | |
335 | |
336 if (value.data) { | |
337 v->len = value.len; | |
338 v->valid = 1; | |
1565 | 339 v->no_cacheable = 0; |
599 | 340 v->not_found = 0; |
341 v->data = value.data; | |
342 | |
343 } else { | |
344 v->not_found = 1; | |
345 } | |
346 | |
633 | 347 ctx->filename.data = NULL; |
599 | 348 ctx->redirect_uri.len = 0; |
349 | |
871 | 350 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
351 "perl variable done"); | |
352 | |
599 | 353 return rc; |
354 } | |
355 | |
356 | |
617 | 357 #if (NGX_HTTP_SSI) |
358 | |
599 | 359 static ngx_int_t |
360 ngx_http_perl_ssi(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ssi_ctx, | |
361 ngx_str_t **params) | |
362 { | |
1899
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
363 SV *sv, **asv; |
599 | 364 ngx_int_t rc; |
1899
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
365 ngx_str_t *handler, **args; |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
366 ngx_uint_t i; |
599 | 367 ngx_http_perl_ctx_t *ctx; |
368 ngx_http_perl_main_conf_t *pmcf; | |
369 | |
370 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
371 "perl ssi handler"); | |
372 | |
373 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
374 | |
375 if (ctx == NULL) { | |
376 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t)); | |
377 if (ctx == NULL) { | |
378 return NGX_ERROR; | |
379 } | |
380 | |
381 ngx_http_set_ctx(r, ctx, ngx_http_perl_module); | |
382 } | |
383 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
384 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module); |
599 | 385 |
386 ctx->ssi = ssi_ctx; | |
387 | |
388 handler = params[NGX_HTTP_PERL_SSI_SUB]; | |
389 handler->data[handler->len] = '\0'; | |
390 | |
391 { | |
392 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
393 dTHXa(pmcf->perl); |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
394 PERL_SET_CONTEXT(pmcf->perl); |
599 | 395 |
651 | 396 #if 0 |
397 | |
398 /* the code is disabled to force the precompiled perl code using only */ | |
399 | |
400 ngx_http_perl_eval_anon_sub(aTHX_ handler, &sv); | |
401 | |
402 if (sv == &PL_sv_undef) { | |
403 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
404 "eval_pv(\"%V\") failed", handler); | |
405 return NGX_ERROR; | |
406 } | |
407 | |
408 if (sv == NULL) { | |
409 sv = newSVpvn((char *) handler->data, handler->len); | |
410 } | |
411 | |
412 #endif | |
413 | |
599 | 414 sv = newSVpvn((char *) handler->data, handler->len); |
415 | |
1899
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
416 args = ¶ms[NGX_HTTP_PERL_SSI_ARG]; |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
417 |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
418 if (args) { |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
419 |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
420 for (i = 0; args[i]; i++) { /* void */ } |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
421 |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
422 asv = ngx_pcalloc(r->pool, (i + 1) * sizeof(SV *)); |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
423 |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
424 if (asv == NULL) { |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
425 SvREFCNT_dec(sv); |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
426 return NGX_ERROR; |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
427 } |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
428 |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
429 asv[0] = (SV *) i; |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
430 |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
431 for (i = 0; args[i]; i++) { |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
432 asv[i + 1] = newSVpvn((char *) args[i]->data, args[i]->len); |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
433 } |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
434 |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
435 } else { |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
436 asv = NULL; |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
437 } |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
438 |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
439 rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, sv, asv, handler, |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
440 NULL); |
599 | 441 |
442 SvREFCNT_dec(sv); | |
443 | |
444 } | |
445 | |
633 | 446 ctx->filename.data = NULL; |
599 | 447 ctx->redirect_uri.len = 0; |
448 ctx->ssi = NULL; | |
449 | |
871 | 450 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl ssi done"); |
451 | |
599 | 452 return rc; |
453 } | |
454 | |
617 | 455 #endif |
456 | |
599 | 457 |
458 static char * | |
459 ngx_http_perl_init_interpreter(ngx_conf_t *cf, ngx_http_perl_main_conf_t *pmcf) | |
460 { | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
461 #if (NGX_HAVE_PERL_MULTIPLICITY) |
653 | 462 ngx_pool_cleanup_t *cln; |
599 | 463 |
464 cln = ngx_pool_cleanup_add(cf->pool, 0); | |
465 if (cln == NULL) { | |
466 return NGX_CONF_ERROR; | |
467 } | |
468 | |
649 | 469 #else |
470 static PerlInterpreter *perl; | |
471 #endif | |
472 | |
599 | 473 #ifdef NGX_PERL_MODULES |
474 if (pmcf->modules.data == NULL) { | |
475 pmcf->modules.data = NGX_PERL_MODULES; | |
476 } | |
477 #endif | |
478 | |
639 | 479 if (pmcf->modules.data) { |
1352 | 480 if (ngx_conf_full_name(cf->cycle, &pmcf->modules, 0) != NGX_OK) { |
639 | 481 return NGX_CONF_ERROR; |
482 } | |
629 | 483 } |
484 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
485 #if !(NGX_HAVE_PERL_MULTIPLICITY) |
649 | 486 |
487 if (perl) { | |
1165 | 488 |
489 if (ngx_set_environment(cf->cycle, NULL) == NULL) { | |
490 return NGX_CONF_ERROR; | |
491 } | |
492 | |
653 | 493 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log) |
494 != NGX_OK) | |
495 { | |
649 | 496 return NGX_CONF_ERROR; |
497 } | |
498 | |
499 pmcf->perl = perl; | |
1163
8288459f15c9
set nginx_stash after reconfiguration in single interpreter perl
Igor Sysoev <igor@sysoev.ru>
parents:
1069
diff
changeset
|
500 pmcf->nginx = nginx_stash; |
649 | 501 |
502 return NGX_CONF_OK; | |
503 } | |
504 | |
505 #endif | |
506 | |
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
|
507 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
|
508 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
|
509 } |
599 | 510 |
1069 | 511 pmcf->perl = ngx_http_perl_create_interpreter(cf, pmcf); |
599 | 512 |
513 if (pmcf->perl == NULL) { | |
514 return NGX_CONF_ERROR; | |
515 } | |
516 | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
517 pmcf->nginx = nginx_stash; |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
518 |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
519 #if (NGX_HAVE_PERL_MULTIPLICITY) |
649 | 520 |
599 | 521 cln->handler = ngx_http_perl_cleanup_perl; |
522 cln->data = pmcf->perl; | |
523 | |
649 | 524 #else |
525 | |
526 perl = pmcf->perl; | |
527 | |
528 #endif | |
529 | |
599 | 530 return NGX_CONF_OK; |
531 } | |
532 | |
533 | |
534 static PerlInterpreter * | |
1069 | 535 ngx_http_perl_create_interpreter(ngx_conf_t *cf, |
536 ngx_http_perl_main_conf_t *pmcf) | |
599 | 537 { |
538 int n; | |
775 | 539 STRLEN len; |
540 SV *sv; | |
541 char *ver, *embedding[6]; | |
599 | 542 PerlInterpreter *perl; |
543 | |
1069 | 544 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "create perl interpreter"); |
545 | |
546 if (ngx_set_environment(cf->cycle, NULL) == NULL) { | |
547 return NULL; | |
548 } | |
599 | 549 |
550 perl = perl_alloc(); | |
551 if (perl == NULL) { | |
1069 | 552 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_alloc() failed"); |
599 | 553 return NULL; |
554 } | |
555 | |
556 { | |
557 | |
558 dTHXa(perl); | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
559 PERL_SET_CONTEXT(perl); |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
560 |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
561 perl_construct(perl); |
599 | 562 |
563 #ifdef PERL_EXIT_DESTRUCT_END | |
564 PL_exit_flags |= PERL_EXIT_DESTRUCT_END; | |
565 #endif | |
566 | |
567 embedding[0] = ""; | |
568 | |
569 if (pmcf->modules.data) { | |
570 embedding[1] = "-I"; | |
571 embedding[2] = (char *) pmcf->modules.data; | |
572 n = 3; | |
573 | |
574 } else { | |
575 n = 1; | |
576 } | |
577 | |
578 embedding[n++] = "-Mnginx"; | |
579 embedding[n++] = "-e"; | |
580 embedding[n++] = "0"; | |
581 | |
582 n = perl_parse(perl, ngx_http_perl_xs_init, n, embedding, NULL); | |
583 | |
584 if (n != 0) { | |
1069 | 585 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_parse() failed: %d", n); |
599 | 586 goto fail; |
587 } | |
588 | |
775 | 589 sv = get_sv("nginx::VERSION", FALSE); |
590 ver = SvPV(sv, len); | |
591 | |
592 if (ngx_strcmp(ver, NGINX_VERSION) != 0) { | |
1069 | 593 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, |
775 | 594 "version " NGINX_VERSION " of nginx.pm is required, " |
595 "but %s was found", ver); | |
596 goto fail; | |
597 } | |
598 | |
1069 | 599 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log) != NGX_OK) { |
649 | 600 goto fail; |
599 | 601 } |
602 | |
603 } | |
604 | |
605 return perl; | |
606 | |
607 fail: | |
608 | |
609 (void) perl_destruct(perl); | |
610 | |
611 perl_free(perl); | |
612 | |
613 return NULL; | |
614 } | |
615 | |
616 | |
649 | 617 static ngx_int_t |
653 | 618 ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires, ngx_log_t *log) |
649 | 619 { |
620 char **script; | |
621 STRLEN len; | |
622 ngx_str_t err; | |
623 ngx_uint_t i; | |
624 | |
625 script = requires->elts; | |
626 for (i = 0; i < requires->nelts; i++) { | |
627 | |
628 require_pv(script[i]); | |
629 | |
630 if (SvTRUE(ERRSV)) { | |
631 | |
632 err.data = (u_char *) SvPV(ERRSV, len); | |
633 for (len--; err.data[len] == LF || err.data[len] == CR; len--) { | |
634 /* void */ | |
635 } | |
636 err.len = len + 1; | |
637 | |
638 ngx_log_error(NGX_LOG_EMERG, log, 0, | |
639 "require_pv(\"%s\") failed: \"%V\"", script[i], &err); | |
640 | |
641 return NGX_ERROR; | |
642 } | |
643 } | |
644 | |
645 return NGX_OK; | |
646 } | |
647 | |
648 | |
599 | 649 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
|
650 ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r, HV *nginx, SV *sub, |
1899
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
651 SV **args, ngx_str_t *handler, ngx_str_t *rv) |
599 | 652 { |
1702
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
653 SV *sv; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
654 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
|
655 char *line; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
656 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
|
657 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
|
658 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
|
659 ngx_connection_t *c; |
599 | 660 |
661 dSP; | |
662 | |
663 status = 0; | |
664 | |
665 ENTER; | |
666 SAVETMPS; | |
667 | |
668 PUSHMARK(sp); | |
669 | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
670 sv = sv_2mortal(sv_bless(newRV_noinc(newSViv(PTR2IV(r))), nginx)); |
599 | 671 XPUSHs(sv); |
672 | |
673 if (args) { | |
1940
6a4c74bea81c
fix building on 64-bit platforms broken in r1900
Igor Sysoev <igor@sysoev.ru>
parents:
1899
diff
changeset
|
674 EXTEND(sp, (intptr_t) args[0]); |
599 | 675 |
1899
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
676 for (i = 1; i <= (ngx_uint_t) args[0]; i++) { |
d24ef26f1205
pass additional arguments in ngx_http_perl_call_handler() as SV
Igor Sysoev <igor@sysoev.ru>
parents:
1897
diff
changeset
|
677 PUSHs(sv_2mortal(args[i])); |
599 | 678 } |
679 } | |
680 | |
681 PUTBACK; | |
682 | |
1702
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
683 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
|
684 |
599 | 685 n = call_sv(sub, G_EVAL); |
686 | |
687 SPAGAIN; | |
688 | |
1702
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
689 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
|
690 PUTBACK; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
691 |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
692 FREETMPS; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
693 LEAVE; |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
694 |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
695 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
|
696 } |
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
697 |
599 | 698 if (n) { |
699 if (rv == NULL) { | |
700 status = POPi; | |
701 | |
1702
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
702 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
599 | 703 "call_sv: %d", status); |
704 | |
705 } else { | |
619 | 706 line = SvPVx(POPs, n_a); |
599 | 707 rv->len = n_a; |
708 | |
2049 | 709 rv->data = ngx_pnalloc(r->pool, n_a); |
599 | 710 if (rv->data == NULL) { |
711 return NGX_ERROR; | |
712 } | |
713 | |
714 ngx_memcpy(rv->data, line, n_a); | |
715 } | |
716 } | |
717 | |
718 PUTBACK; | |
719 | |
720 FREETMPS; | |
721 LEAVE; | |
722 | |
723 /* check $@ */ | |
724 | |
725 if (SvTRUE(ERRSV)) { | |
726 | |
727 err.data = (u_char *) SvPV(ERRSV, len); | |
728 for (len--; err.data[len] == LF || err.data[len] == CR; len--) { | |
729 /* void */ | |
730 } | |
731 err.len = len + 1; | |
732 | |
1702
86bb52e28ce0
fix segfault when $r->has_request_body() is called with ready body
Igor Sysoev <igor@sysoev.ru>
parents:
1565
diff
changeset
|
733 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
|
734 "call_sv(\"%V\") failed: \"%V\"", handler, &err); |
599 | 735 |
736 if (rv) { | |
737 return NGX_ERROR; | |
738 } | |
739 | |
740 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
741 } | |
742 | |
743 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
|
744 ngx_log_error(NGX_LOG_ALERT, c->log, 0, |
599 | 745 "call_sv(\"%V\") returned %d results", handler, n); |
746 status = NGX_OK; | |
747 } | |
748 | |
749 if (rv) { | |
750 return NGX_OK; | |
751 } | |
752 | |
753 return (ngx_int_t) status; | |
754 } | |
755 | |
756 | |
757 static void | |
758 ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv) | |
759 { | |
623 | 760 u_char *p; |
761 | |
762 for (p = handler->data; *p; p++) { | |
763 if (*p != ' ' && *p != '\t' && *p != CR && *p != LF) { | |
764 break; | |
765 } | |
766 } | |
767 | |
649 | 768 if (ngx_strncmp(p, "sub ", 4) == 0 || ngx_strncmp(p, "use ", 4) == 0) { |
623 | 769 *sv = eval_pv((char *) p, FALSE); |
874 | 770 |
771 /* eval_pv() does not set ERRSV on failure */ | |
772 | |
599 | 773 return; |
774 } | |
775 | |
776 *sv = NULL; | |
777 } | |
778 | |
779 | |
780 static void * | |
781 ngx_http_perl_create_main_conf(ngx_conf_t *cf) | |
782 { | |
783 ngx_http_perl_main_conf_t *pmcf; | |
784 | |
785 pmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_main_conf_t)); | |
786 if (pmcf == NULL) { | |
787 return NGX_CONF_ERROR; | |
788 } | |
789 | |
790 if (ngx_array_init(&pmcf->requires, cf->pool, 1, sizeof(u_char *)) | |
791 != NGX_OK) | |
792 { | |
793 return NULL; | |
794 } | |
795 | |
796 return pmcf; | |
797 } | |
798 | |
799 | |
800 static char * | |
801 ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf) | |
802 { | |
803 ngx_http_perl_main_conf_t *pmcf = conf; | |
804 | |
805 if (pmcf->perl == NULL) { | |
806 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) { | |
807 return NGX_CONF_ERROR; | |
808 } | |
809 } | |
810 | |
811 return NGX_CONF_OK; | |
812 } | |
813 | |
814 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
815 #if (NGX_HAVE_PERL_MULTIPLICITY) |
649 | 816 |
599 | 817 static void |
818 ngx_http_perl_cleanup_perl(void *data) | |
819 { | |
649 | 820 PerlInterpreter *perl = data; |
599 | 821 |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
822 PERL_SET_CONTEXT(perl); |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
823 |
599 | 824 (void) perl_destruct(perl); |
825 | |
826 perl_free(perl); | |
2715 | 827 |
828 if (ngx_perl_term) { | |
829 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0, "perl term"); | |
830 | |
831 PERL_SYS_TERM(); | |
832 } | |
599 | 833 } |
834 | |
649 | 835 #endif |
836 | |
837 | |
599 | 838 static ngx_int_t |
839 ngx_http_perl_preconfiguration(ngx_conf_t *cf) | |
840 { | |
617 | 841 #if (NGX_HTTP_SSI) |
599 | 842 ngx_int_t rc; |
843 ngx_http_ssi_main_conf_t *smcf; | |
844 | |
845 smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module); | |
846 | |
847 rc = ngx_hash_add_key(&smcf->commands, &ngx_http_perl_ssi_command.name, | |
848 &ngx_http_perl_ssi_command, NGX_HASH_READONLY_KEY); | |
849 | |
850 if (rc != NGX_OK) { | |
851 if (rc == NGX_BUSY) { | |
852 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
853 "conflicting SSI command \"%V\"", | |
854 &ngx_http_perl_ssi_command.name); | |
855 } | |
856 | |
857 return NGX_ERROR; | |
858 } | |
617 | 859 #endif |
599 | 860 |
861 return NGX_OK; | |
862 } | |
863 | |
864 | |
865 static void * | |
866 ngx_http_perl_create_loc_conf(ngx_conf_t *cf) | |
867 { | |
868 ngx_http_perl_loc_conf_t *plcf; | |
869 | |
870 plcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_loc_conf_t)); | |
871 if (plcf == NULL) { | |
872 return NGX_CONF_ERROR; | |
873 } | |
874 | |
875 /* | |
876 * set by ngx_pcalloc(): | |
877 * | |
878 * plcf->handler = { 0, NULL }; | |
879 */ | |
880 | |
881 return plcf; | |
882 } | |
883 | |
884 | |
885 static char * | |
886 ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
887 { | |
888 ngx_http_perl_loc_conf_t *prev = parent; | |
889 ngx_http_perl_loc_conf_t *conf = child; | |
890 | |
891 if (conf->sub == NULL) { | |
892 conf->sub = prev->sub; | |
893 conf->handler = prev->handler; | |
894 } | |
895 | |
896 return NGX_CONF_OK; | |
897 } | |
898 | |
899 | |
900 static char * | |
901 ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
902 { | |
903 ngx_http_perl_main_conf_t *pmcf = conf; | |
904 | |
905 u_char **p; | |
906 ngx_str_t *value; | |
907 | |
908 value = cf->args->elts; | |
909 | |
910 p = ngx_array_push(&pmcf->requires); | |
911 | |
912 if (p == NULL) { | |
913 return NGX_CONF_ERROR; | |
914 } | |
915 | |
916 *p = value[1].data; | |
917 | |
918 return NGX_CONF_OK; | |
919 } | |
920 | |
921 | |
922 static char * | |
923 ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
924 { | |
925 ngx_http_perl_loc_conf_t *plcf = conf; | |
926 | |
927 ngx_str_t *value; | |
928 ngx_http_core_loc_conf_t *clcf; | |
929 ngx_http_perl_main_conf_t *pmcf; | |
930 | |
931 value = cf->args->elts; | |
932 | |
933 if (plcf->handler.data) { | |
934 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
935 "duplicate perl handler \"%V\"", &value[1]); | |
936 return NGX_CONF_ERROR; | |
937 } | |
938 | |
939 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module); | |
940 | |
941 if (pmcf->perl == NULL) { | |
942 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) { | |
943 return NGX_CONF_ERROR; | |
944 } | |
945 } | |
946 | |
947 plcf->handler = value[1]; | |
948 | |
949 { | |
950 | |
951 dTHXa(pmcf->perl); | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
952 PERL_SET_CONTEXT(pmcf->perl); |
599 | 953 |
954 ngx_http_perl_eval_anon_sub(aTHX_ &value[1], &plcf->sub); | |
955 | |
956 if (plcf->sub == &PL_sv_undef) { | |
957 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, | |
958 "eval_pv(\"%V\") failed", &value[1]); | |
959 return NGX_CONF_ERROR; | |
960 } | |
961 | |
962 if (plcf->sub == NULL) { | |
963 plcf->sub = newSVpvn((char *) value[1].data, value[1].len); | |
964 } | |
965 | |
966 } | |
967 | |
968 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
969 clcf->handler = ngx_http_perl_handler; | |
970 | |
971 return NGX_CONF_OK; | |
972 } | |
973 | |
974 | |
975 static char * | |
976 ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
977 { | |
978 ngx_int_t index; | |
979 ngx_str_t *value; | |
980 ngx_http_variable_t *v; | |
981 ngx_http_perl_variable_t *pv; | |
982 ngx_http_perl_main_conf_t *pmcf; | |
983 | |
984 value = cf->args->elts; | |
985 | |
986 if (value[1].data[0] != '$') { | |
987 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
988 "invalid variable name \"%V\"", &value[1]); | |
989 return NGX_CONF_ERROR; | |
990 } | |
991 | |
992 value[1].len--; | |
993 value[1].data++; | |
994 | |
1565 | 995 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE); |
599 | 996 if (v == NULL) { |
997 return NGX_CONF_ERROR; | |
998 } | |
999 | |
1000 pv = ngx_palloc(cf->pool, sizeof(ngx_http_perl_variable_t)); | |
1001 if (pv == NULL) { | |
1002 return NGX_CONF_ERROR; | |
1003 } | |
1004 | |
1005 index = ngx_http_get_variable_index(cf, &value[1]); | |
1006 if (index == NGX_ERROR) { | |
1007 return NGX_CONF_ERROR; | |
1008 } | |
1009 | |
1010 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module); | |
1011 | |
1012 if (pmcf->perl == NULL) { | |
1013 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) { | |
1014 return NGX_CONF_ERROR; | |
1015 } | |
1016 } | |
1017 | |
1018 pv->handler = value[2]; | |
1019 | |
1020 { | |
1021 | |
1022 dTHXa(pmcf->perl); | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
1023 PERL_SET_CONTEXT(pmcf->perl); |
599 | 1024 |
1025 ngx_http_perl_eval_anon_sub(aTHX_ &value[2], &pv->sub); | |
1026 | |
1027 if (pv->sub == &PL_sv_undef) { | |
1028 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, | |
1029 "eval_pv(\"%V\") failed", &value[2]); | |
1030 return NGX_CONF_ERROR; | |
1031 } | |
1032 | |
1033 if (pv->sub == NULL) { | |
1034 pv->sub = newSVpvn((char *) value[2].data, value[2].len); | |
1035 } | |
1036 | |
1037 } | |
1038 | |
637 | 1039 v->get_handler = ngx_http_perl_variable; |
599 | 1040 v->data = (uintptr_t) pv; |
1041 | |
1042 return NGX_CONF_OK; | |
1043 } | |
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
|
1044 |
aa653367028e
PERL_SYS_TERM() should be called once on exit only, this fixes the message
Igor Sysoev <igor@sysoev.ru>
parents:
1165
diff
changeset
|
1045 |
1257 | 1046 static ngx_int_t |
1047 ngx_http_perl_init_worker(ngx_cycle_t *cycle) | |
1048 { | |
1258
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1049 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
|
1050 |
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1051 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
|
1052 |
2713
b4d8aef4a1ad
fix segfault if no http section is defined in confguraiton,
Igor Sysoev <igor@sysoev.ru>
parents:
2388
diff
changeset
|
1053 if (pmcf) { |
b4d8aef4a1ad
fix segfault if no http section is defined in confguraiton,
Igor Sysoev <igor@sysoev.ru>
parents:
2388
diff
changeset
|
1054 dTHXa(pmcf->perl); |
b4d8aef4a1ad
fix segfault if no http section is defined in confguraiton,
Igor Sysoev <igor@sysoev.ru>
parents:
2388
diff
changeset
|
1055 PERL_SET_CONTEXT(pmcf->perl); |
1258
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1056 |
2713
b4d8aef4a1ad
fix segfault if no http section is defined in confguraiton,
Igor Sysoev <igor@sysoev.ru>
parents:
2388
diff
changeset
|
1057 /* set worker's $$ */ |
1257 | 1058 |
2713
b4d8aef4a1ad
fix segfault if no http section is defined in confguraiton,
Igor Sysoev <igor@sysoev.ru>
parents:
2388
diff
changeset
|
1059 sv_setiv(GvSV(gv_fetchpv("$", TRUE, SVt_PV)), (I32) ngx_pid); |
1258
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1060 } |
5076d97b9cf0
fix building on threaded or multiplicity interpreter perl,
Igor Sysoev <igor@sysoev.ru>
parents:
1257
diff
changeset
|
1061 |
1257 | 1062 return NGX_OK; |
1063 } | |
1064 | |
1941 | 1065 |
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
|
1066 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
|
1067 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
|
1068 { |
2715 | 1069 #if (NGX_HAVE_PERL_MULTIPLICITY) |
1070 | |
1071 ngx_perl_term = 1; | |
1072 | |
1073 #else | |
1946
171a283af56b
some perl builds require my_perl for PERL_SYS_TERM()
Igor Sysoev <igor@sysoev.ru>
parents:
1941
diff
changeset
|
1074 ngx_http_perl_main_conf_t *pmcf; |
171a283af56b
some perl builds require my_perl for PERL_SYS_TERM()
Igor Sysoev <igor@sysoev.ru>
parents:
1941
diff
changeset
|
1075 |
171a283af56b
some perl builds require my_perl for PERL_SYS_TERM()
Igor Sysoev <igor@sysoev.ru>
parents:
1941
diff
changeset
|
1076 pmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_perl_module); |
171a283af56b
some perl builds require my_perl for PERL_SYS_TERM()
Igor Sysoev <igor@sysoev.ru>
parents:
1941
diff
changeset
|
1077 |
2715 | 1078 if (pmcf && nginx_stash) { |
1079 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cycle->log, 0, "perl term"); | |
1080 | |
1081 (void) perl_destruct(pmcf->perl); | |
1082 | |
1083 perl_free(pmcf->perl); | |
1946
171a283af56b
some perl builds require my_perl for PERL_SYS_TERM()
Igor Sysoev <igor@sysoev.ru>
parents:
1941
diff
changeset
|
1084 |
2714
4dd1773990db
fix segfault on exit if no http section is defined in confguraiton,
Igor Sysoev <igor@sysoev.ru>
parents:
2713
diff
changeset
|
1085 PERL_SYS_TERM(); |
1946
171a283af56b
some perl builds require my_perl for PERL_SYS_TERM()
Igor Sysoev <igor@sysoev.ru>
parents:
1941
diff
changeset
|
1086 } |
2715 | 1087 |
1088 #endif | |
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
|
1089 } |