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