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