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