comparison src/http/modules/ngx_http_scgi_module.c @ 635:e67b227c8dbb default tip

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