comparison src/http/modules/ngx_http_scgi_module.c @ 593:4d3e880ce86c NGINX_0_8_42

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