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