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