Mercurial > hg > nginx
annotate src/http/modules/ngx_http_scgi_module.c @ 3668:1d4c7aaed5d2
update the previous commit
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 02 Jul 2010 09:28:50 +0000 |
parents | 12bd9e26fadb |
children | 2728c4e4a9ae |
rev | line source |
---|---|
3637 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 * Copyright (C) Manlio Perillo (manlio.perillo@gmail.com) | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_http.h> | |
11 | |
12 | |
13 typedef struct { | |
14 ngx_http_upstream_conf_t upstream; | |
15 | |
16 ngx_array_t *flushes; | |
17 ngx_array_t *params_len; | |
18 ngx_array_t *params; | |
19 ngx_array_t *params_source; | |
20 | |
21 ngx_hash_t headers_hash; | |
22 ngx_uint_t header_params; | |
23 | |
24 ngx_array_t *scgi_lengths; | |
25 ngx_array_t *scgi_values; | |
26 | |
27 #if (NGX_HTTP_CACHE) | |
28 ngx_http_complex_value_t cache_key; | |
29 #endif | |
30 } ngx_http_scgi_loc_conf_t; | |
31 | |
32 | |
33 static ngx_int_t ngx_http_scgi_eval(ngx_http_request_t *r, | |
34 ngx_http_scgi_loc_conf_t *scf); | |
35 static ngx_int_t ngx_http_scgi_create_request(ngx_http_request_t *r); | |
36 static ngx_int_t ngx_http_scgi_reinit_request(ngx_http_request_t *r); | |
37 static ngx_int_t ngx_http_scgi_process_status_line(ngx_http_request_t *r); | |
38 static ngx_int_t ngx_http_scgi_process_header(ngx_http_request_t *r); | |
39 static ngx_int_t ngx_http_scgi_process_header(ngx_http_request_t *r); | |
40 static void ngx_http_scgi_abort_request(ngx_http_request_t *r); | |
41 static void ngx_http_scgi_finalize_request(ngx_http_request_t *r, ngx_int_t rc); | |
42 | |
43 static void *ngx_http_scgi_create_loc_conf(ngx_conf_t *cf); | |
44 static char *ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, | |
45 void *child); | |
46 | |
47 static char *ngx_http_scgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
48 static char *ngx_http_scgi_store(ngx_conf_t *cf, ngx_command_t *cmd, | |
49 void *conf); | |
50 | |
51 #if (NGX_HTTP_CACHE) | |
52 static ngx_int_t ngx_http_scgi_create_key(ngx_http_request_t *r); | |
53 static char *ngx_http_scgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, | |
54 void *conf); | |
55 static char *ngx_http_scgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, | |
56 void *conf); | |
57 #endif | |
58 | |
59 | |
60 static ngx_conf_bitmask_t ngx_http_scgi_next_upstream_masks[] = { | |
61 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR }, | |
62 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT }, | |
63 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER }, | |
64 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 }, | |
65 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 }, | |
66 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 }, | |
67 { ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING }, | |
68 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF }, | |
69 { ngx_null_string, 0 } | |
70 }; | |
71 | |
72 | |
73 ngx_module_t ngx_http_scgi_module; | |
74 | |
75 | |
76 static ngx_command_t ngx_http_scgi_commands[] = { | |
77 | |
78 { ngx_string("scgi_pass"), | |
79 NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1, | |
80 ngx_http_scgi_pass, | |
81 NGX_HTTP_LOC_CONF_OFFSET, | |
82 0, | |
83 NULL }, | |
84 | |
85 { ngx_string("scgi_store"), | |
86 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
87 ngx_http_scgi_store, | |
88 NGX_HTTP_LOC_CONF_OFFSET, | |
89 0, | |
90 NULL }, | |
91 | |
92 { ngx_string("scgi_store_access"), | |
93 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123, | |
94 ngx_conf_set_access_slot, | |
95 NGX_HTTP_LOC_CONF_OFFSET, | |
96 offsetof(ngx_http_scgi_loc_conf_t, upstream.store_access), | |
97 NULL }, | |
98 | |
99 { ngx_string("scgi_ignore_client_abort"), | |
100 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
101 ngx_conf_set_flag_slot, | |
102 NGX_HTTP_LOC_CONF_OFFSET, | |
103 offsetof(ngx_http_scgi_loc_conf_t, upstream.ignore_client_abort), | |
104 NULL }, | |
105 | |
106 { ngx_string("scgi_bind"), | |
107 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
108 ngx_http_upstream_bind_set_slot, | |
109 NGX_HTTP_LOC_CONF_OFFSET, | |
110 offsetof(ngx_http_scgi_loc_conf_t, upstream.local), | |
111 NULL }, | |
112 | |
113 { ngx_string("scgi_connect_timeout"), | |
114 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
115 ngx_conf_set_msec_slot, | |
116 NGX_HTTP_LOC_CONF_OFFSET, | |
117 offsetof(ngx_http_scgi_loc_conf_t, upstream.connect_timeout), | |
118 NULL }, | |
119 | |
120 { ngx_string("scgi_send_timeout"), | |
121 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
122 ngx_conf_set_msec_slot, | |
123 NGX_HTTP_LOC_CONF_OFFSET, | |
124 offsetof(ngx_http_scgi_loc_conf_t, upstream.send_timeout), | |
125 NULL }, | |
126 | |
127 { ngx_string("scgi_buffer_size"), | |
128 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
129 ngx_conf_set_size_slot, | |
130 NGX_HTTP_LOC_CONF_OFFSET, | |
131 offsetof(ngx_http_scgi_loc_conf_t, upstream.buffer_size), | |
132 NULL }, | |
133 | |
134 { ngx_string("scgi_pass_request_headers"), | |
135 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
136 ngx_conf_set_flag_slot, | |
137 NGX_HTTP_LOC_CONF_OFFSET, | |
138 offsetof(ngx_http_scgi_loc_conf_t, upstream.pass_request_headers), | |
139 NULL }, | |
140 | |
141 { ngx_string("scgi_pass_request_body"), | |
142 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
143 ngx_conf_set_flag_slot, | |
144 NGX_HTTP_LOC_CONF_OFFSET, | |
145 offsetof(ngx_http_scgi_loc_conf_t, upstream.pass_request_body), | |
146 NULL }, | |
147 | |
148 { ngx_string("scgi_intercept_errors"), | |
149 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
150 ngx_conf_set_flag_slot, | |
151 NGX_HTTP_LOC_CONF_OFFSET, | |
152 offsetof(ngx_http_scgi_loc_conf_t, upstream.intercept_errors), | |
153 NULL }, | |
154 | |
155 { ngx_string("scgi_read_timeout"), | |
156 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
157 ngx_conf_set_msec_slot, | |
158 NGX_HTTP_LOC_CONF_OFFSET, | |
159 offsetof(ngx_http_scgi_loc_conf_t, upstream.read_timeout), | |
160 NULL }, | |
161 | |
162 { ngx_string("scgi_buffers"), | |
163 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, | |
164 ngx_conf_set_bufs_slot, | |
165 NGX_HTTP_LOC_CONF_OFFSET, | |
166 offsetof(ngx_http_scgi_loc_conf_t, upstream.bufs), | |
167 NULL }, | |
168 | |
169 { ngx_string("scgi_busy_buffers_size"), | |
170 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
171 ngx_conf_set_size_slot, | |
172 NGX_HTTP_LOC_CONF_OFFSET, | |
173 offsetof(ngx_http_scgi_loc_conf_t, upstream.busy_buffers_size_conf), | |
174 NULL }, | |
175 | |
176 #if (NGX_HTTP_CACHE) | |
177 | |
178 { ngx_string("scgi_cache"), | |
179 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
180 ngx_http_scgi_cache, | |
181 NGX_HTTP_LOC_CONF_OFFSET, | |
182 0, | |
183 NULL }, | |
184 | |
185 { ngx_string("scgi_cache_key"), | |
186 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
187 ngx_http_scgi_cache_key, | |
188 NGX_HTTP_LOC_CONF_OFFSET, | |
189 0, | |
190 NULL }, | |
191 | |
192 { ngx_string("scgi_cache_path"), | |
193 NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE, | |
194 ngx_http_file_cache_set_slot, | |
195 0, | |
196 0, | |
197 &ngx_http_scgi_module }, | |
198 | |
199 { ngx_string("scgi_no_cache"), | |
200 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
201 ngx_http_no_cache_set_slot, | |
202 NGX_HTTP_LOC_CONF_OFFSET, | |
203 offsetof(ngx_http_scgi_loc_conf_t, upstream.no_cache), | |
204 NULL }, | |
205 | |
206 { ngx_string("scgi_cache_valid"), | |
207 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
208 ngx_http_file_cache_valid_set_slot, | |
209 NGX_HTTP_LOC_CONF_OFFSET, | |
210 offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_valid), | |
211 NULL }, | |
212 | |
213 { ngx_string("scgi_cache_min_uses"), | |
214 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
215 ngx_conf_set_num_slot, | |
216 NGX_HTTP_LOC_CONF_OFFSET, | |
217 offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_min_uses), | |
218 NULL }, | |
219 | |
220 { ngx_string("scgi_cache_use_stale"), | |
221 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
222 ngx_conf_set_bitmask_slot, | |
223 NGX_HTTP_LOC_CONF_OFFSET, | |
224 offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_use_stale), | |
225 &ngx_http_scgi_next_upstream_masks }, | |
226 | |
227 { ngx_string("scgi_cache_methods"), | |
228 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
229 ngx_conf_set_bitmask_slot, | |
230 NGX_HTTP_LOC_CONF_OFFSET, | |
231 offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_methods), | |
232 &ngx_http_upstream_cache_method_mask }, | |
233 | |
234 #endif | |
235 | |
236 { ngx_string("scgi_temp_path"), | |
237 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234, | |
238 ngx_conf_set_path_slot, | |
239 NGX_HTTP_LOC_CONF_OFFSET, | |
240 offsetof(ngx_http_scgi_loc_conf_t, upstream.temp_path), | |
241 NULL }, | |
242 | |
243 { ngx_string("scgi_max_temp_file_size"), | |
244 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
245 ngx_conf_set_size_slot, | |
246 NGX_HTTP_LOC_CONF_OFFSET, | |
247 offsetof(ngx_http_scgi_loc_conf_t, upstream.max_temp_file_size_conf), | |
248 NULL }, | |
249 | |
250 { ngx_string("scgi_temp_file_write_size"), | |
251 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
252 ngx_conf_set_size_slot, | |
253 NGX_HTTP_LOC_CONF_OFFSET, | |
254 offsetof(ngx_http_scgi_loc_conf_t, upstream.temp_file_write_size_conf), | |
255 NULL }, | |
256 | |
257 { ngx_string("scgi_next_upstream"), | |
258 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
259 ngx_conf_set_bitmask_slot, | |
260 NGX_HTTP_LOC_CONF_OFFSET, | |
261 offsetof(ngx_http_scgi_loc_conf_t, upstream.next_upstream), | |
262 &ngx_http_scgi_next_upstream_masks }, | |
263 | |
264 { ngx_string("scgi_param"), | |
265 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, | |
266 ngx_conf_set_keyval_slot, | |
267 NGX_HTTP_LOC_CONF_OFFSET, | |
268 offsetof(ngx_http_scgi_loc_conf_t, params_source), | |
269 NULL }, | |
270 | |
271 { ngx_string("scgi_pass_header"), | |
272 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
273 ngx_conf_set_str_array_slot, | |
274 NGX_HTTP_LOC_CONF_OFFSET, | |
275 offsetof(ngx_http_scgi_loc_conf_t, upstream.pass_headers), | |
276 NULL }, | |
277 | |
278 { ngx_string("scgi_hide_header"), | |
279 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
280 ngx_conf_set_str_array_slot, | |
281 NGX_HTTP_LOC_CONF_OFFSET, | |
282 offsetof(ngx_http_scgi_loc_conf_t, upstream.hide_headers), | |
283 NULL }, | |
284 | |
285 { ngx_string("scgi_ignore_headers"), | |
286 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
287 ngx_conf_set_bitmask_slot, | |
288 NGX_HTTP_LOC_CONF_OFFSET, | |
289 offsetof(ngx_http_scgi_loc_conf_t, upstream.ignore_headers), | |
3667
12bd9e26fadb
use shared ngx_http_upstream_ignore_headers_masks[]
Igor Sysoev <igor@sysoev.ru>
parents:
3637
diff
changeset
|
290 &ngx_http_upstream_ignore_headers_masks }, |
3637 | 291 |
292 ngx_null_command | |
293 }; | |
294 | |
295 | |
296 static ngx_http_module_t ngx_http_scgi_module_ctx = { | |
297 NULL, /* preconfiguration */ | |
298 NULL, /* postconfiguration */ | |
299 | |
300 NULL, /* create main configuration */ | |
301 NULL, /* init main configuration */ | |
302 | |
303 NULL, /* create server configuration */ | |
304 NULL, /* merge server configuration */ | |
305 | |
306 ngx_http_scgi_create_loc_conf, /* create location configuration */ | |
307 ngx_http_scgi_merge_loc_conf /* merge location configuration */ | |
308 }; | |
309 | |
310 | |
311 ngx_module_t ngx_http_scgi_module = { | |
312 NGX_MODULE_V1, | |
313 &ngx_http_scgi_module_ctx, /* module context */ | |
314 ngx_http_scgi_commands, /* module directives */ | |
315 NGX_HTTP_MODULE, /* module type */ | |
316 NULL, /* init master */ | |
317 NULL, /* init module */ | |
318 NULL, /* init process */ | |
319 NULL, /* init thread */ | |
320 NULL, /* exit thread */ | |
321 NULL, /* exit process */ | |
322 NULL, /* exit master */ | |
323 NGX_MODULE_V1_PADDING | |
324 }; | |
325 | |
326 | |
327 static ngx_str_t ngx_http_scgi_hide_headers[] = { | |
328 ngx_string("Status"), | |
329 ngx_string("X-Accel-Expires"), | |
330 ngx_string("X-Accel-Redirect"), | |
331 ngx_string("X-Accel-Limit-Rate"), | |
332 ngx_string("X-Accel-Buffering"), | |
333 ngx_string("X-Accel-Charset"), | |
334 ngx_null_string | |
335 }; | |
336 | |
337 | |
338 #if (NGX_HTTP_CACHE) | |
339 | |
340 static ngx_str_t ngx_http_scgi_hide_cache_headers[] = { | |
341 ngx_string("Status"), | |
342 ngx_string("X-Accel-Expires"), | |
343 ngx_string("X-Accel-Redirect"), | |
344 ngx_string("X-Accel-Limit-Rate"), | |
345 ngx_string("X-Accel-Buffering"), | |
346 ngx_string("X-Accel-Charset"), | |
347 ngx_string("Set-Cookie"), | |
348 ngx_string("P3P"), | |
349 ngx_null_string | |
350 }; | |
351 | |
352 | |
353 static ngx_keyval_t ngx_http_scgi_cache_headers[] = { | |
354 { ngx_string("HTTP_IF_MODIFIED_SINCE"), ngx_string("") }, | |
355 { ngx_string("HTTP_IF_UNMODIFIED_SINCE"), ngx_string("") }, | |
356 { ngx_string("HTTP_IF_NONE_MATCH"), ngx_string("") }, | |
357 { ngx_string("HTTP_IF_MATCH"), ngx_string("") }, | |
358 { ngx_string("HTTP_RANGE"), ngx_string("") }, | |
359 { ngx_string("HTTP_IF_RANGE"), ngx_string("") }, | |
360 { ngx_null_string, ngx_null_string } | |
361 }; | |
362 | |
363 #endif | |
364 | |
365 | |
366 static ngx_path_init_t ngx_http_scgi_temp_path = { | |
367 ngx_string(NGX_HTTP_SCGI_TEMP_PATH), { 1, 2, 0 } | |
368 }; | |
369 | |
370 | |
371 static ngx_int_t | |
372 ngx_http_scgi_handler(ngx_http_request_t *r) | |
373 { | |
374 ngx_int_t rc; | |
375 ngx_http_status_t *status; | |
376 ngx_http_upstream_t *u; | |
377 ngx_http_scgi_loc_conf_t *scf; | |
378 | |
379 if (r->subrequest_in_memory) { | |
380 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
381 "ngx_http_scgi_module does not support " | |
382 "subrequests in memory"); | |
383 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
384 } | |
385 | |
386 if (ngx_http_upstream_create(r) != NGX_OK) { | |
387 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
388 } | |
389 | |
390 status = ngx_pcalloc(r->pool, sizeof(ngx_http_status_t)); | |
391 if (status == NULL) { | |
392 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
393 } | |
394 | |
395 ngx_http_set_ctx(r, status, ngx_http_scgi_module); | |
396 | |
397 scf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module); | |
398 | |
399 if (scf->scgi_lengths) { | |
400 if (ngx_http_scgi_eval(r, scf) != NGX_OK) { | |
401 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
402 } | |
403 } | |
404 | |
405 u = r->upstream; | |
406 | |
407 ngx_str_set(&u->schema, "scgi://"); | |
408 u->output.tag = (ngx_buf_tag_t) &ngx_http_scgi_module; | |
409 | |
410 u->conf = &scf->upstream; | |
411 | |
412 #if (NGX_HTTP_CACHE) | |
413 u->create_key = ngx_http_scgi_create_key; | |
414 #endif | |
415 u->create_request = ngx_http_scgi_create_request; | |
416 u->reinit_request = ngx_http_scgi_reinit_request; | |
417 u->process_header = ngx_http_scgi_process_status_line; | |
418 u->abort_request = ngx_http_scgi_abort_request; | |
419 u->finalize_request = ngx_http_scgi_finalize_request; | |
420 | |
421 u->buffering = 1; | |
422 | |
423 u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t)); | |
424 if (u->pipe == NULL) { | |
425 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
426 } | |
427 | |
428 u->pipe->input_filter = ngx_event_pipe_copy_input_filter; | |
429 u->pipe->input_ctx = r; | |
430 | |
431 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init); | |
432 | |
433 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { | |
434 return rc; | |
435 } | |
436 | |
437 return NGX_DONE; | |
438 } | |
439 | |
440 | |
441 static ngx_int_t | |
442 ngx_http_scgi_eval(ngx_http_request_t *r, ngx_http_scgi_loc_conf_t * scf) | |
443 { | |
444 ngx_url_t url; | |
445 ngx_http_upstream_t *u; | |
446 | |
447 ngx_memzero(&url, sizeof(ngx_url_t)); | |
448 | |
449 if (ngx_http_script_run(r, &url.url, scf->scgi_lengths->elts, 0, | |
450 scf->scgi_values->elts) | |
451 == NULL) | |
452 { | |
453 return NGX_ERROR; | |
454 } | |
455 | |
456 url.no_resolve = 1; | |
457 | |
458 if (ngx_parse_url(r->pool, &url) != NGX_OK) { | |
459 if (url.err) { | |
460 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
461 "%s in upstream \"%V\"", url.err, &url.url); | |
462 } | |
463 | |
464 return NGX_ERROR; | |
465 } | |
466 | |
467 if (url.no_port) { | |
468 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
469 "no port in upstream \"%V\"", &url.url); | |
470 return NGX_ERROR; | |
471 } | |
472 | |
473 u = r->upstream; | |
474 | |
475 u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t)); | |
476 if (u->resolved == NULL) { | |
477 return NGX_ERROR; | |
478 } | |
479 | |
480 if (url.addrs && url.addrs[0].sockaddr) { | |
481 u->resolved->sockaddr = url.addrs[0].sockaddr; | |
482 u->resolved->socklen = url.addrs[0].socklen; | |
483 u->resolved->naddrs = 1; | |
484 u->resolved->host = url.addrs[0].name; | |
485 | |
486 } else { | |
487 u->resolved->host = url.host; | |
488 u->resolved->port = url.port; | |
489 } | |
490 | |
491 return NGX_OK; | |
492 } | |
493 | |
494 | |
495 #if (NGX_HTTP_CACHE) | |
496 | |
497 static ngx_int_t | |
498 ngx_http_scgi_create_key(ngx_http_request_t *r) | |
499 { | |
500 ngx_str_t *key; | |
501 ngx_http_scgi_loc_conf_t *scf; | |
502 | |
503 key = ngx_array_push(&r->cache->keys); | |
504 if (key == NULL) { | |
505 return NGX_ERROR; | |
506 } | |
507 | |
508 scf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module); | |
509 | |
510 if (ngx_http_complex_value(r, &scf->cache_key, key) != NGX_OK) { | |
511 return NGX_ERROR; | |
512 } | |
513 | |
514 return NGX_OK; | |
515 } | |
516 | |
517 #endif | |
518 | |
519 | |
520 static ngx_int_t | |
521 ngx_http_scgi_create_request(ngx_http_request_t *r) | |
522 { | |
523 u_char ch, *key, *val, *lowcase_key; | |
524 size_t len, allocated; | |
525 ngx_buf_t *b; | |
526 ngx_str_t *content_length; | |
527 ngx_uint_t i, n, hash, header_params; | |
528 ngx_chain_t *cl, *body; | |
529 ngx_list_part_t *part; | |
530 ngx_table_elt_t *header, **ignored; | |
531 ngx_http_script_code_pt code; | |
532 ngx_http_script_engine_t e, le; | |
533 ngx_http_scgi_loc_conf_t *scf; | |
534 ngx_http_script_len_code_pt lcode; | |
535 static ngx_str_t zero = ngx_string("0"); | |
536 | |
537 content_length = r->headers_in.content_length ? | |
538 &r->headers_in.content_length->value : &zero; | |
539 | |
540 len = sizeof("CONTENT_LENGTH") + content_length->len + 1; | |
541 | |
542 header_params = 0; | |
543 ignored = NULL; | |
544 | |
545 scf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module); | |
546 | |
547 if (scf->params_len) { | |
548 ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); | |
549 | |
550 ngx_http_script_flush_no_cacheable_variables(r, scf->flushes); | |
551 le.flushed = 1; | |
552 | |
553 le.ip = scf->params_len->elts; | |
554 le.request = r; | |
555 | |
556 while (*(uintptr_t *) le.ip) { | |
557 | |
558 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
559 len += lcode(&le); | |
560 | |
561 while (*(uintptr_t *) le.ip) { | |
562 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
563 len += lcode(&le) + 1; | |
564 } | |
565 le.ip += sizeof(uintptr_t); | |
566 } | |
567 } | |
568 | |
569 if (scf->upstream.pass_request_headers) { | |
570 | |
571 allocated = 0; | |
572 lowcase_key = NULL; | |
573 | |
574 if (scf->header_params) { | |
575 ignored = ngx_palloc(r->pool, scf->header_params * sizeof(void *)); | |
576 if (ignored == NULL) { | |
577 return NGX_ERROR; | |
578 } | |
579 } | |
580 | |
581 part = &r->headers_in.headers.part; | |
582 header = part->elts; | |
583 | |
584 for (i = 0; /* void */; i++) { | |
585 | |
586 if (i >= part->nelts) { | |
587 if (part->next == NULL) { | |
588 break; | |
589 } | |
590 | |
591 part = part->next; | |
592 header = part->elts; | |
593 i = 0; | |
594 } | |
595 | |
596 if (scf->header_params) { | |
597 if (allocated < header[i].key.len) { | |
598 allocated = header[i].key.len + 16; | |
599 lowcase_key = ngx_pnalloc(r->pool, allocated); | |
600 if (lowcase_key == NULL) { | |
601 return NGX_ERROR; | |
602 } | |
603 } | |
604 | |
605 hash = 0; | |
606 | |
607 for (n = 0; n < header[i].key.len; n++) { | |
608 ch = header[i].key.data[n]; | |
609 | |
610 if (ch >= 'A' && ch <= 'Z') { | |
611 ch |= 0x20; | |
612 | |
613 } else if (ch == '-') { | |
614 ch = '_'; | |
615 } | |
616 | |
617 hash = ngx_hash(hash, ch); | |
618 lowcase_key[n] = ch; | |
619 } | |
620 | |
621 if (ngx_hash_find(&scf->headers_hash, hash, lowcase_key, n)) { | |
622 ignored[header_params++] = &header[i]; | |
623 continue; | |
624 } | |
625 } | |
626 | |
627 len += sizeof("HTTP_") - 1 + header[i].key.len + 1 | |
628 + header[i].value.len + 1; | |
629 } | |
630 } | |
631 | |
632 /* netstring: "length:" + packet + "," */ | |
633 | |
634 b = ngx_create_temp_buf(r->pool, NGX_SIZE_T_LEN + 1 + len + 1); | |
635 if (b == NULL) { | |
636 return NGX_ERROR; | |
637 } | |
638 | |
639 cl = ngx_alloc_chain_link(r->pool); | |
640 if (cl == NULL) { | |
641 return NGX_ERROR; | |
642 } | |
643 | |
644 cl->buf = b; | |
645 | |
646 b->last = ngx_snprintf(b->last, | |
647 NGX_SIZE_T_LEN + 1 + sizeof("CONTENT_LENGTH") | |
648 + NGX_OFF_T_LEN + 1, | |
649 "%ui:CONTENT_LENGTH%Z%V%Z", | |
650 len, content_length); | |
651 | |
652 if (scf->params_len) { | |
653 ngx_memzero(&e, sizeof(ngx_http_script_engine_t)); | |
654 | |
655 e.ip = scf->params->elts; | |
656 e.pos = b->last; | |
657 e.request = r; | |
658 e.flushed = 1; | |
659 | |
660 while (*(uintptr_t *) e.ip) { | |
661 | |
662 #if (NGX_DEBUG) | |
663 key = e.pos; | |
664 #endif | |
665 code = *(ngx_http_script_code_pt *) e.ip; | |
666 code((ngx_http_script_engine_t *) & e); | |
667 | |
668 #if (NGX_DEBUG) | |
669 val = e.pos; | |
670 #endif | |
671 while (*(uintptr_t *) e.ip) { | |
672 code = *(ngx_http_script_code_pt *) e.ip; | |
673 code((ngx_http_script_engine_t *) &e); | |
674 } | |
675 *e.pos++ = '\0'; | |
676 e.ip += sizeof(uintptr_t); | |
677 | |
678 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
679 "scgi param: \"%s: %s\"", key, val); | |
680 } | |
681 | |
682 b->last = e.pos; | |
683 } | |
684 | |
685 if (scf->upstream.pass_request_headers) { | |
686 | |
687 part = &r->headers_in.headers.part; | |
688 header = part->elts; | |
689 | |
690 for (i = 0; /* void */; i++) { | |
691 | |
692 if (i >= part->nelts) { | |
693 if (part->next == NULL) { | |
694 break; | |
695 } | |
696 | |
697 part = part->next; | |
698 header = part->elts; | |
699 i = 0; | |
700 } | |
701 | |
702 for (n = 0; n < header_params; n++) { | |
703 if (&header[i] == ignored[n]) { | |
704 goto next; | |
705 } | |
706 } | |
707 | |
708 key = b->last; | |
709 b->last = ngx_cpymem(key, "HTTP_", sizeof("HTTP_") - 1); | |
710 | |
711 for (n = 0; n < header[i].key.len; n++) { | |
712 ch = header[i].key.data[n]; | |
713 | |
714 if (ch >= 'a' && ch <= 'z') { | |
715 ch &= ~0x20; | |
716 | |
717 } else if (ch == '-') { | |
718 ch = '_'; | |
719 } | |
720 | |
721 *b->last++ = ch; | |
722 } | |
723 | |
724 *b->last++ = (u_char) 0; | |
725 | |
726 val = b->last; | |
727 b->last = ngx_copy(val, header[i].value.data, header[i].value.len); | |
728 *b->last++ = (u_char) 0; | |
729 | |
730 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
731 "scgi param: \"%s: %s\"", key, val); | |
732 | |
733 next: | |
734 | |
735 continue; | |
736 } | |
737 } | |
738 | |
739 *b->last++ = (u_char) ','; | |
740 | |
741 if (scf->upstream.pass_request_body) { | |
742 body = r->upstream->request_bufs; | |
743 r->upstream->request_bufs = cl; | |
744 | |
745 while (body) { | |
746 b = ngx_alloc_buf(r->pool); | |
747 if (b == NULL) { | |
748 return NGX_ERROR; | |
749 } | |
750 | |
751 ngx_memcpy(b, body->buf, sizeof(ngx_buf_t)); | |
752 | |
753 cl->next = ngx_alloc_chain_link(r->pool); | |
754 if (cl->next == NULL) { | |
755 return NGX_ERROR; | |
756 } | |
757 | |
758 cl = cl->next; | |
759 cl->buf = b; | |
760 | |
761 body = body->next; | |
762 } | |
763 | |
764 } else { | |
765 r->upstream->request_bufs = cl; | |
766 } | |
767 | |
768 cl->next = NULL; | |
769 | |
770 return NGX_OK; | |
771 } | |
772 | |
773 | |
774 static ngx_int_t | |
775 ngx_http_scgi_reinit_request(ngx_http_request_t *r) | |
776 { | |
777 ngx_http_status_t *status; | |
778 | |
779 status = ngx_http_get_module_ctx(r, ngx_http_scgi_module); | |
780 | |
781 if (status == NULL) { | |
782 return NGX_OK; | |
783 } | |
784 | |
785 status->code = 0; | |
786 status->count = 0; | |
787 status->start = NULL; | |
788 status->end = NULL; | |
789 | |
790 r->upstream->process_header = ngx_http_scgi_process_status_line; | |
791 | |
792 return NGX_OK; | |
793 } | |
794 | |
795 | |
796 static ngx_int_t | |
797 ngx_http_scgi_process_status_line(ngx_http_request_t *r) | |
798 { | |
799 size_t len; | |
800 ngx_int_t rc; | |
801 ngx_http_status_t *status; | |
802 ngx_http_upstream_t *u; | |
803 | |
804 status = ngx_http_get_module_ctx(r, ngx_http_scgi_module); | |
805 | |
806 if (status == NULL) { | |
807 return NGX_ERROR; | |
808 } | |
809 | |
810 u = r->upstream; | |
811 | |
812 rc = ngx_http_parse_status_line(r, &u->buffer, status); | |
813 | |
814 if (rc == NGX_AGAIN) { | |
815 return rc; | |
816 } | |
817 | |
818 if (rc == NGX_ERROR) { | |
819 | |
820 r->http_version = NGX_HTTP_VERSION_9; | |
821 | |
822 u->process_header = ngx_http_scgi_process_header; | |
823 | |
824 return ngx_http_scgi_process_header(r); | |
825 } | |
826 | |
827 if (u->state) { | |
828 u->state->status = status->code; | |
829 } | |
830 | |
831 u->headers_in.status_n = status->code; | |
832 | |
833 len = status->end - status->start; | |
834 u->headers_in.status_line.len = len; | |
835 | |
836 u->headers_in.status_line.data = ngx_pnalloc(r->pool, len); | |
837 if (u->headers_in.status_line.data == NULL) { | |
838 return NGX_ERROR; | |
839 } | |
840 | |
841 ngx_memcpy(u->headers_in.status_line.data, status->start, len); | |
842 | |
843 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
844 "http scgi status %ui \"%V\"", | |
845 u->headers_in.status_n, &u->headers_in.status_line); | |
846 | |
847 u->process_header = ngx_http_scgi_process_header; | |
848 | |
849 return ngx_http_scgi_process_header(r); | |
850 } | |
851 | |
852 | |
853 static ngx_int_t | |
854 ngx_http_scgi_process_header(ngx_http_request_t *r) | |
855 { | |
856 ngx_str_t *status_line; | |
857 ngx_int_t rc, status; | |
858 ngx_table_elt_t *h; | |
859 ngx_http_upstream_t *u; | |
860 ngx_http_upstream_header_t *hh; | |
861 ngx_http_upstream_main_conf_t *umcf; | |
862 | |
863 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module); | |
864 | |
865 for ( ;; ) { | |
866 | |
867 rc = ngx_http_parse_header_line(r, &r->upstream->buffer, 1); | |
868 | |
869 if (rc == NGX_OK) { | |
870 | |
871 /* a header line has been parsed successfully */ | |
872 | |
873 h = ngx_list_push(&r->upstream->headers_in.headers); | |
874 if (h == NULL) { | |
875 return NGX_ERROR; | |
876 } | |
877 | |
878 h->hash = r->header_hash; | |
879 | |
880 h->key.len = r->header_name_end - r->header_name_start; | |
881 h->value.len = r->header_end - r->header_start; | |
882 | |
883 h->key.data = ngx_pnalloc(r->pool, | |
884 h->key.len + 1 + h->value.len + 1 | |
885 + h->key.len); | |
886 if (h->key.data == NULL) { | |
887 return NGX_ERROR; | |
888 } | |
889 | |
890 h->value.data = h->key.data + h->key.len + 1; | |
891 h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1; | |
892 | |
893 ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1); | |
894 ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1); | |
895 | |
896 if (h->key.len == r->lowcase_index) { | |
897 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len); | |
898 | |
899 } else { | |
900 ngx_strlow(h->lowcase_key, h->key.data, h->key.len); | |
901 } | |
902 | |
903 hh = ngx_hash_find(&umcf->headers_in_hash, h->hash, | |
904 h->lowcase_key, h->key.len); | |
905 | |
906 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) { | |
907 return NGX_ERROR; | |
908 } | |
909 | |
910 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
911 "http scgi header: \"%V: %V\"", &h->key, &h->value); | |
912 | |
913 continue; | |
914 } | |
915 | |
916 if (rc == NGX_HTTP_PARSE_HEADER_DONE) { | |
917 | |
918 /* a whole header has been parsed successfully */ | |
919 | |
920 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
921 "http scgi header done"); | |
922 | |
923 if (r->http_version > NGX_HTTP_VERSION_9) { | |
924 return NGX_OK; | |
925 } | |
926 | |
927 u = r->upstream; | |
928 | |
929 if (u->headers_in.status) { | |
930 status_line = &u->headers_in.status->value; | |
931 | |
932 status = ngx_atoi(status_line->data, 3); | |
933 if (status == NGX_ERROR) { | |
934 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
935 "upstream sent invalid status \"%V\"", | |
936 status_line); | |
937 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
938 } | |
939 | |
940 r->http_version = NGX_HTTP_VERSION_10; | |
941 u->headers_in.status_n = status; | |
942 u->headers_in.status_line = *status_line; | |
943 | |
944 } else if (u->headers_in.location) { | |
945 r->http_version = NGX_HTTP_VERSION_10; | |
946 u->headers_in.status_n = 302; | |
947 ngx_str_set(&u->headers_in.status_line, | |
948 "302 Moved Temporarily"); | |
949 | |
950 } else { | |
951 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
952 "upstream sent neither valid HTTP/1.0 header " | |
953 "nor \"Status\" header line"); | |
954 u->headers_in.status_n = 200; | |
955 ngx_str_set(&u->headers_in.status_line, "200 OK"); | |
956 } | |
957 | |
958 if (u->state) { | |
959 u->state->status = u->headers_in.status_n; | |
960 } | |
961 | |
962 return NGX_OK; | |
963 } | |
964 | |
965 if (rc == NGX_AGAIN) { | |
966 return NGX_AGAIN; | |
967 } | |
968 | |
969 /* there was error while a header line parsing */ | |
970 | |
971 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
972 "upstream sent invalid header"); | |
973 | |
974 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
975 } | |
976 } | |
977 | |
978 | |
979 static void | |
980 ngx_http_scgi_abort_request(ngx_http_request_t *r) | |
981 { | |
982 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
983 "abort http scgi request"); | |
984 | |
985 return; | |
986 } | |
987 | |
988 | |
989 static void | |
990 ngx_http_scgi_finalize_request(ngx_http_request_t *r, ngx_int_t rc) | |
991 { | |
992 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
993 "finalize http scgi request"); | |
994 | |
995 return; | |
996 } | |
997 | |
998 | |
999 static void * | |
1000 ngx_http_scgi_create_loc_conf(ngx_conf_t *cf) | |
1001 { | |
1002 ngx_http_scgi_loc_conf_t *conf; | |
1003 | |
1004 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_scgi_loc_conf_t)); | |
1005 if (conf == NULL) { | |
1006 return NULL; | |
1007 } | |
1008 | |
1009 conf->upstream.store = NGX_CONF_UNSET; | |
1010 conf->upstream.store_access = NGX_CONF_UNSET_UINT; | |
1011 conf->upstream.buffering = NGX_CONF_UNSET; | |
1012 conf->upstream.ignore_client_abort = NGX_CONF_UNSET; | |
1013 | |
1014 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; | |
1015 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; | |
1016 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC; | |
1017 | |
1018 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE; | |
1019 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE; | |
1020 | |
1021 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE; | |
1022 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE; | |
1023 conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE; | |
1024 | |
1025 conf->upstream.pass_request_headers = NGX_CONF_UNSET; | |
1026 conf->upstream.pass_request_body = NGX_CONF_UNSET; | |
1027 | |
1028 #if (NGX_HTTP_CACHE) | |
1029 conf->upstream.cache = NGX_CONF_UNSET_PTR; | |
1030 conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT; | |
1031 conf->upstream.cache_valid = NGX_CONF_UNSET_PTR; | |
1032 #endif | |
1033 | |
1034 conf->upstream.hide_headers = NGX_CONF_UNSET_PTR; | |
1035 conf->upstream.pass_headers = NGX_CONF_UNSET_PTR; | |
1036 | |
1037 conf->upstream.intercept_errors = NGX_CONF_UNSET; | |
1038 | |
1039 /* "scgi_cyclic_temp_file" is disabled */ | |
1040 conf->upstream.cyclic_temp_file = 0; | |
1041 | |
1042 return conf; | |
1043 } | |
1044 | |
1045 | |
1046 static char * | |
1047 ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
1048 { | |
1049 ngx_http_scgi_loc_conf_t *prev = parent; | |
1050 ngx_http_scgi_loc_conf_t *conf = child; | |
1051 | |
1052 u_char *p; | |
1053 size_t size; | |
1054 uintptr_t *code; | |
1055 ngx_str_t *hide; | |
1056 ngx_uint_t i; | |
1057 ngx_array_t headers_names; | |
1058 ngx_keyval_t *src; | |
1059 ngx_hash_key_t *hk; | |
1060 ngx_hash_init_t hash; | |
1061 ngx_http_script_compile_t sc; | |
1062 ngx_http_script_copy_code_t *copy; | |
1063 | |
1064 if (conf->upstream.store != 0) { | |
1065 ngx_conf_merge_value(conf->upstream.store, prev->upstream.store, 0); | |
1066 | |
1067 if (conf->upstream.store_lengths == NULL) { | |
1068 conf->upstream.store_lengths = prev->upstream.store_lengths; | |
1069 conf->upstream.store_values = prev->upstream.store_values; | |
1070 } | |
1071 } | |
1072 | |
1073 ngx_conf_merge_uint_value(conf->upstream.store_access, | |
1074 prev->upstream.store_access, 0600); | |
1075 | |
1076 ngx_conf_merge_value(conf->upstream.buffering, | |
1077 prev->upstream.buffering, 1); | |
1078 | |
1079 ngx_conf_merge_value(conf->upstream.ignore_client_abort, | |
1080 prev->upstream.ignore_client_abort, 0); | |
1081 | |
1082 ngx_conf_merge_msec_value(conf->upstream.connect_timeout, | |
1083 prev->upstream.connect_timeout, 60000); | |
1084 | |
1085 ngx_conf_merge_msec_value(conf->upstream.send_timeout, | |
1086 prev->upstream.send_timeout, 60000); | |
1087 | |
1088 ngx_conf_merge_msec_value(conf->upstream.read_timeout, | |
1089 prev->upstream.read_timeout, 60000); | |
1090 | |
1091 ngx_conf_merge_size_value(conf->upstream.send_lowat, | |
1092 prev->upstream.send_lowat, 0); | |
1093 | |
1094 ngx_conf_merge_size_value(conf->upstream.buffer_size, | |
1095 prev->upstream.buffer_size, | |
1096 (size_t) ngx_pagesize); | |
1097 | |
1098 | |
1099 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs, | |
1100 8, ngx_pagesize); | |
1101 | |
1102 if (conf->upstream.bufs.num < 2) { | |
1103 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1104 "there must be at least 2 \"scgi_buffers\""); | |
1105 return NGX_CONF_ERROR; | |
1106 } | |
1107 | |
1108 | |
1109 size = conf->upstream.buffer_size; | |
1110 if (size < conf->upstream.bufs.size) { | |
1111 size = conf->upstream.bufs.size; | |
1112 } | |
1113 | |
1114 | |
1115 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf, | |
1116 prev->upstream.busy_buffers_size_conf, | |
1117 NGX_CONF_UNSET_SIZE); | |
1118 | |
1119 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) { | |
1120 conf->upstream.busy_buffers_size = 2 * size; | |
1121 } else { | |
1122 conf->upstream.busy_buffers_size = | |
1123 conf->upstream.busy_buffers_size_conf; | |
1124 } | |
1125 | |
1126 if (conf->upstream.busy_buffers_size < size) { | |
1127 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1128 "\"scgi_busy_buffers_size\" must be equal or bigger " | |
1129 "than maximum of the value of \"scgi_buffer_size\" and " | |
1130 "one of the \"scgi_buffers\""); | |
1131 | |
1132 return NGX_CONF_ERROR; | |
1133 } | |
1134 | |
1135 if (conf->upstream.busy_buffers_size | |
1136 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size) | |
1137 { | |
1138 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1139 "\"scgi_busy_buffers_size\" must be less than " | |
1140 "the size of all \"scgi_buffers\" minus one buffer"); | |
1141 | |
1142 return NGX_CONF_ERROR; | |
1143 } | |
1144 | |
1145 | |
1146 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf, | |
1147 prev->upstream.temp_file_write_size_conf, | |
1148 NGX_CONF_UNSET_SIZE); | |
1149 | |
1150 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) { | |
1151 conf->upstream.temp_file_write_size = 2 * size; | |
1152 } else { | |
1153 conf->upstream.temp_file_write_size = | |
1154 conf->upstream.temp_file_write_size_conf; | |
1155 } | |
1156 | |
1157 if (conf->upstream.temp_file_write_size < size) { | |
1158 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1159 "\"scgi_temp_file_write_size\" must be equal or bigger than " | |
1160 "maximum of the value of \"scgi_buffer_size\" and " | |
1161 "one of the \"scgi_buffers\""); | |
1162 | |
1163 return NGX_CONF_ERROR; | |
1164 } | |
1165 | |
1166 | |
1167 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf, | |
1168 prev->upstream.max_temp_file_size_conf, | |
1169 NGX_CONF_UNSET_SIZE); | |
1170 | |
1171 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) { | |
1172 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024; | |
1173 } else { | |
1174 conf->upstream.max_temp_file_size = | |
1175 conf->upstream.max_temp_file_size_conf; | |
1176 } | |
1177 | |
1178 if (conf->upstream.max_temp_file_size != 0 | |
1179 && conf->upstream.max_temp_file_size < size) { | |
1180 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1181 "\"scgi_max_temp_file_size\" must be equal to zero to disable " | |
1182 "the temporary files usage or must be equal or bigger than " | |
1183 "maximum of the value of \"scgi_buffer_size\" and " | |
1184 "one of the \"scgi_buffers\""); | |
1185 | |
1186 return NGX_CONF_ERROR; | |
1187 } | |
1188 | |
1189 | |
1190 ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers, | |
1191 prev->upstream.ignore_headers, | |
1192 NGX_CONF_BITMASK_SET); | |
1193 | |
1194 | |
1195 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream, | |
1196 prev->upstream.next_upstream, | |
1197 (NGX_CONF_BITMASK_SET | |
1198 |NGX_HTTP_UPSTREAM_FT_ERROR | |
1199 |NGX_HTTP_UPSTREAM_FT_TIMEOUT)); | |
1200 | |
1201 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) { | |
1202 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET | |
1203 |NGX_HTTP_UPSTREAM_FT_OFF; | |
1204 } | |
1205 | |
1206 if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path, | |
1207 prev->upstream.temp_path, | |
1208 &ngx_http_scgi_temp_path) | |
1209 != NGX_OK) | |
1210 { | |
1211 return NGX_CONF_ERROR; | |
1212 } | |
1213 | |
1214 #if (NGX_HTTP_CACHE) | |
1215 | |
1216 ngx_conf_merge_ptr_value(conf->upstream.cache, | |
1217 prev->upstream.cache, NULL); | |
1218 | |
1219 if (conf->upstream.cache && conf->upstream.cache->data == NULL) { | |
1220 ngx_shm_zone_t *shm_zone; | |
1221 | |
1222 shm_zone = conf->upstream.cache; | |
1223 | |
1224 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1225 "\"scgi_cache\" zone \"%V\" is unknown", | |
1226 &shm_zone->shm.name); | |
1227 | |
1228 return NGX_CONF_ERROR; | |
1229 } | |
1230 | |
1231 ngx_conf_merge_uint_value(conf->upstream.cache_min_uses, | |
1232 prev->upstream.cache_min_uses, 1); | |
1233 | |
1234 ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale, | |
1235 prev->upstream.cache_use_stale, | |
1236 (NGX_CONF_BITMASK_SET | |
1237 |NGX_HTTP_UPSTREAM_FT_OFF)); | |
1238 | |
1239 if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) { | |
1240 conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET | |
1241 |NGX_HTTP_UPSTREAM_FT_OFF; | |
1242 } | |
1243 | |
1244 if (conf->upstream.cache_methods == 0) { | |
1245 conf->upstream.cache_methods = prev->upstream.cache_methods; | |
1246 } | |
1247 | |
1248 conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD; | |
1249 | |
1250 ngx_conf_merge_ptr_value(conf->upstream.no_cache, | |
1251 prev->upstream.no_cache, NULL); | |
1252 | |
1253 ngx_conf_merge_ptr_value(conf->upstream.cache_valid, | |
1254 prev->upstream.cache_valid, NULL); | |
1255 | |
1256 if (conf->cache_key.value.data == NULL) { | |
1257 conf->cache_key = prev->cache_key; | |
1258 } | |
1259 | |
1260 #endif | |
1261 | |
1262 ngx_conf_merge_value(conf->upstream.pass_request_headers, | |
1263 prev->upstream.pass_request_headers, 1); | |
1264 ngx_conf_merge_value(conf->upstream.pass_request_body, | |
1265 prev->upstream.pass_request_body, 1); | |
1266 | |
1267 ngx_conf_merge_value(conf->upstream.intercept_errors, | |
1268 prev->upstream.intercept_errors, 0); | |
1269 | |
1270 hash.max_size = 512; | |
1271 hash.bucket_size = ngx_align(64, ngx_cacheline_size); | |
1272 hash.name = "scgi_hide_headers_hash"; | |
1273 | |
1274 #if (NGX_HTTP_CACHE) | |
1275 | |
1276 hide = conf->upstream.cache ? ngx_http_scgi_hide_cache_headers: | |
1277 ngx_http_scgi_hide_headers; | |
1278 #else | |
1279 | |
1280 hide = ngx_http_scgi_hide_headers; | |
1281 | |
1282 #endif | |
1283 | |
1284 if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream, | |
1285 &prev->upstream, hide, &hash) | |
1286 != NGX_OK) | |
1287 { | |
1288 return NGX_CONF_ERROR; | |
1289 } | |
1290 | |
1291 if (conf->upstream.upstream == NULL) { | |
1292 conf->upstream.upstream = prev->upstream.upstream; | |
1293 } | |
1294 | |
1295 if (conf->scgi_lengths == NULL) { | |
1296 conf->scgi_lengths = prev->scgi_lengths; | |
1297 conf->scgi_values = prev->scgi_values; | |
1298 } | |
1299 | |
1300 if (conf->params_source == NULL) { | |
1301 conf->flushes = prev->flushes; | |
1302 conf->params_len = prev->params_len; | |
1303 conf->params = prev->params; | |
1304 conf->params_source = prev->params_source; | |
1305 conf->headers_hash = prev->headers_hash; | |
1306 | |
1307 #if (NGX_HTTP_CACHE) | |
1308 | |
1309 if (conf->params_source == NULL) { | |
1310 | |
1311 if ((conf->upstream.cache == NULL) | |
1312 == (prev->upstream.cache == NULL)) | |
1313 { | |
1314 return NGX_CONF_OK; | |
1315 } | |
1316 | |
1317 /* 6 is a number of ngx_http_scgi_cache_headers entries */ | |
1318 conf->params_source = ngx_array_create(cf->pool, 6, | |
1319 sizeof(ngx_keyval_t)); | |
1320 if (conf->params_source == NULL) { | |
1321 return NGX_CONF_ERROR; | |
1322 } | |
1323 } | |
1324 #else | |
1325 | |
1326 if (conf->params_source == NULL) { | |
1327 return NGX_CONF_OK; | |
1328 } | |
1329 | |
1330 #endif | |
1331 } | |
1332 | |
1333 conf->params_len = ngx_array_create(cf->pool, 64, 1); | |
1334 if (conf->params_len == NULL) { | |
1335 return NGX_CONF_ERROR; | |
1336 } | |
1337 | |
1338 conf->params = ngx_array_create(cf->pool, 512, 1); | |
1339 if (conf->params == NULL) { | |
1340 return NGX_CONF_ERROR; | |
1341 } | |
1342 | |
1343 if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t)) | |
1344 != NGX_OK) | |
1345 { | |
1346 return NGX_CONF_ERROR; | |
1347 } | |
1348 | |
1349 src = conf->params_source->elts; | |
1350 | |
1351 #if (NGX_HTTP_CACHE) | |
1352 | |
1353 if (conf->upstream.cache) { | |
1354 ngx_keyval_t *h, *s; | |
1355 | |
1356 for (h = ngx_http_scgi_cache_headers; h->key.len; h++) { | |
1357 | |
1358 for (i = 0; i < conf->params_source->nelts; i++) { | |
1359 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) { | |
1360 goto next; | |
1361 } | |
1362 } | |
1363 | |
1364 s = ngx_array_push(conf->params_source); | |
1365 if (s == NULL) { | |
1366 return NGX_CONF_ERROR; | |
1367 } | |
1368 | |
1369 *s = *h; | |
1370 | |
1371 src = conf->params_source->elts; | |
1372 | |
1373 next: | |
1374 | |
1375 h++; | |
1376 } | |
1377 } | |
1378 | |
1379 #endif | |
1380 | |
1381 for (i = 0; i < conf->params_source->nelts; i++) { | |
1382 | |
1383 if (src[i].key.len > sizeof("HTTP_") - 1 | |
1384 && ngx_strncmp(src[i].key.data, "HTTP_", sizeof("HTTP_") - 1) == 0) | |
1385 { | |
1386 hk = ngx_array_push(&headers_names); | |
1387 if (hk == NULL) { | |
1388 return NGX_CONF_ERROR; | |
1389 } | |
1390 | |
1391 hk->key.len = src[i].key.len - 5; | |
1392 hk->key.data = src[i].key.data + 5; | |
1393 hk->key_hash = ngx_hash_key_lc(hk->key.data, hk->key.len); | |
1394 hk->value = (void *) 1; | |
1395 | |
1396 if (src[i].value.len == 0) { | |
1397 continue; | |
1398 } | |
1399 } | |
1400 | |
1401 copy = ngx_array_push_n(conf->params_len, | |
1402 sizeof(ngx_http_script_copy_code_t)); | |
1403 if (copy == NULL) { | |
1404 return NGX_CONF_ERROR; | |
1405 } | |
1406 | |
1407 copy->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code; | |
1408 copy->len = src[i].key.len + 1; | |
1409 | |
1410 | |
1411 size = (sizeof(ngx_http_script_copy_code_t) | |
1412 + src[i].key.len + 1 + sizeof(uintptr_t) - 1) | |
1413 & ~(sizeof(uintptr_t) - 1); | |
1414 | |
1415 copy = ngx_array_push_n(conf->params, size); | |
1416 if (copy == NULL) { | |
1417 return NGX_CONF_ERROR; | |
1418 } | |
1419 | |
1420 copy->code = ngx_http_script_copy_code; | |
1421 copy->len = src[i].key.len + 1; | |
1422 | |
1423 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t); | |
1424 (void) ngx_cpystrn(p, src[i].key.data, src[i].key.len + 1); | |
1425 | |
1426 | |
1427 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
1428 | |
1429 sc.cf = cf; | |
1430 sc.source = &src[i].value; | |
1431 sc.flushes = &conf->flushes; | |
1432 sc.lengths = &conf->params_len; | |
1433 sc.values = &conf->params; | |
1434 | |
1435 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
1436 return NGX_CONF_ERROR; | |
1437 } | |
1438 | |
1439 code = ngx_array_push_n(conf->params_len, sizeof(uintptr_t)); | |
1440 if (code == NULL) { | |
1441 return NGX_CONF_ERROR; | |
1442 } | |
1443 | |
1444 *code = (uintptr_t) NULL; | |
1445 | |
1446 | |
1447 code = ngx_array_push_n(conf->params, sizeof(uintptr_t)); | |
1448 if (code == NULL) { | |
1449 return NGX_CONF_ERROR; | |
1450 } | |
1451 | |
1452 *code = (uintptr_t) NULL; | |
1453 } | |
1454 | |
1455 code = ngx_array_push_n(conf->params_len, sizeof(uintptr_t)); | |
1456 if (code == NULL) { | |
1457 return NGX_CONF_ERROR; | |
1458 } | |
1459 | |
1460 *code = (uintptr_t) NULL; | |
1461 | |
1462 code = ngx_array_push_n(conf->params, sizeof(uintptr_t)); | |
1463 if (code == NULL) { | |
1464 return NGX_CONF_ERROR; | |
1465 } | |
1466 | |
1467 *code = (uintptr_t) NULL; | |
1468 | |
1469 conf->header_params = headers_names.nelts; | |
1470 | |
1471 hash.hash = &conf->headers_hash; | |
1472 hash.key = ngx_hash_key_lc; | |
1473 hash.max_size = 512; | |
1474 hash.bucket_size = 64; | |
1475 hash.name = "scgi_params_hash"; | |
1476 hash.pool = cf->pool; | |
1477 hash.temp_pool = NULL; | |
1478 | |
1479 if (ngx_hash_init(&hash, headers_names.elts, headers_names.nelts) != NGX_OK) | |
1480 { | |
1481 return NGX_CONF_ERROR; | |
1482 } | |
1483 | |
1484 return NGX_CONF_OK; | |
1485 } | |
1486 | |
1487 | |
1488 static char * | |
1489 ngx_http_scgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1490 { | |
1491 ngx_http_scgi_loc_conf_t *scf = conf; | |
1492 | |
1493 ngx_url_t u; | |
1494 ngx_str_t *value, *url; | |
1495 ngx_uint_t n; | |
1496 ngx_http_core_loc_conf_t *clcf; | |
1497 ngx_http_script_compile_t sc; | |
1498 | |
1499 if (scf->upstream.upstream || scf->scgi_lengths) { | |
1500 return "is duplicate"; | |
1501 } | |
1502 | |
1503 clcf = ngx_http_conf_get_module_loc_conf (cf, ngx_http_core_module); | |
1504 clcf->handler = ngx_http_scgi_handler; | |
1505 | |
1506 value = cf->args->elts; | |
1507 | |
1508 url = &value[1]; | |
1509 | |
1510 n = ngx_http_script_variables_count(url); | |
1511 | |
1512 if (n) { | |
1513 | |
1514 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
1515 | |
1516 sc.cf = cf; | |
1517 sc.source = url; | |
1518 sc.lengths = &scf->scgi_lengths; | |
1519 sc.values = &scf->scgi_values; | |
1520 sc.variables = n; | |
1521 sc.complete_lengths = 1; | |
1522 sc.complete_values = 1; | |
1523 | |
1524 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
1525 return NGX_CONF_ERROR; | |
1526 } | |
1527 | |
1528 return NGX_CONF_OK; | |
1529 } | |
1530 | |
1531 ngx_memzero(&u, sizeof(ngx_url_t)); | |
1532 | |
1533 u.url = value[1]; | |
1534 u.no_resolve = 1; | |
1535 | |
1536 scf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0); | |
1537 if (scf->upstream.upstream == NULL) { | |
1538 return NGX_CONF_ERROR; | |
1539 } | |
1540 | |
1541 if (clcf->name.data[clcf->name.len - 1] == '/') { | |
1542 clcf->auto_redirect = 1; | |
1543 } | |
1544 | |
1545 return NGX_CONF_OK; | |
1546 } | |
1547 | |
1548 | |
1549 static char * | |
1550 ngx_http_scgi_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1551 { | |
1552 ngx_http_scgi_loc_conf_t *scf = conf; | |
1553 | |
1554 ngx_str_t *value; | |
1555 ngx_http_script_compile_t sc; | |
1556 | |
1557 if (scf->upstream.store != NGX_CONF_UNSET || scf->upstream.store_lengths) { | |
1558 return "is duplicate"; | |
1559 } | |
1560 | |
1561 value = cf->args->elts; | |
1562 | |
1563 if (ngx_strcmp(value[1].data, "off") == 0) { | |
1564 scf->upstream.store = 0; | |
1565 return NGX_CONF_OK; | |
1566 } | |
1567 | |
1568 #if (NGX_HTTP_CACHE) | |
1569 | |
1570 if (scf->upstream.cache != NGX_CONF_UNSET_PTR | |
1571 && scf->upstream.cache != NULL) | |
1572 { | |
1573 return "is incompatible with \"scgi_cache\""; | |
1574 } | |
1575 | |
1576 #endif | |
1577 | |
1578 if (ngx_strcmp(value[1].data, "on") == 0) { | |
1579 scf->upstream.store = 1; | |
1580 return NGX_CONF_OK; | |
1581 } | |
1582 | |
1583 /* include the terminating '\0' into script */ | |
1584 value[1].len++; | |
1585 | |
1586 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
1587 | |
1588 sc.cf = cf; | |
1589 sc.source = &value[1]; | |
1590 sc.lengths = &scf->upstream.store_lengths; | |
1591 sc.values = &scf->upstream.store_values; | |
1592 sc.variables = ngx_http_script_variables_count(&value[1]);; | |
1593 sc.complete_lengths = 1; | |
1594 sc.complete_values = 1; | |
1595 | |
1596 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
1597 return NGX_CONF_ERROR; | |
1598 } | |
1599 | |
1600 return NGX_CONF_OK; | |
1601 } | |
1602 | |
1603 | |
1604 #if (NGX_HTTP_CACHE) | |
1605 | |
1606 static char * | |
1607 ngx_http_scgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1608 { | |
1609 ngx_http_scgi_loc_conf_t *scf = conf; | |
1610 | |
1611 ngx_str_t *value; | |
1612 | |
1613 value = cf->args->elts; | |
1614 | |
1615 if (scf->upstream.cache != NGX_CONF_UNSET_PTR) { | |
1616 return "is duplicate"; | |
1617 } | |
1618 | |
1619 if (ngx_strcmp(value[1].data, "off") == 0) { | |
1620 scf->upstream.cache = NULL; | |
1621 return NGX_CONF_OK; | |
1622 } | |
1623 | |
1624 if (scf->upstream.store > 0 || scf->upstream.store_lengths) { | |
1625 return "is incompatible with \"scgi_store\""; | |
1626 } | |
1627 | |
1628 scf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0, | |
1629 &ngx_http_scgi_module); | |
1630 if (scf->upstream.cache == NULL) { | |
1631 return NGX_CONF_ERROR; | |
1632 } | |
1633 | |
1634 return NGX_CONF_OK; | |
1635 } | |
1636 | |
1637 | |
1638 static char * | |
1639 ngx_http_scgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1640 { | |
1641 ngx_http_scgi_loc_conf_t *scf = conf; | |
1642 | |
1643 ngx_str_t *value; | |
1644 ngx_http_compile_complex_value_t ccv; | |
1645 | |
1646 value = cf->args->elts; | |
1647 | |
1648 if (scf->cache_key.value.len) { | |
1649 return "is duplicate"; | |
1650 } | |
1651 | |
1652 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); | |
1653 | |
1654 ccv.cf = cf; | |
1655 ccv.value = &value[1]; | |
1656 ccv.complex_value = &scf->cache_key; | |
1657 | |
1658 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { | |
1659 return NGX_CONF_ERROR; | |
1660 } | |
1661 | |
1662 return NGX_CONF_OK; | |
1663 } | |
1664 | |
1665 #endif |