3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 * Copyright (C) Manlio Perillo (manlio.perillo@gmail.com)
9 #include <ngx_config.h>
15 ngx_http_upstream_conf_t upstream
;
18 ngx_array_t
*params_len
;
20 ngx_array_t
*params_source
;
22 ngx_hash_t headers_hash
;
23 ngx_uint_t header_params
;
25 ngx_array_t
*scgi_lengths
;
26 ngx_array_t
*scgi_values
;
29 ngx_http_complex_value_t cache_key
;
31 } ngx_http_scgi_loc_conf_t
;
34 static ngx_int_t
ngx_http_scgi_eval(ngx_http_request_t
*r
,
35 ngx_http_scgi_loc_conf_t
*scf
);
36 static ngx_int_t
ngx_http_scgi_create_request(ngx_http_request_t
*r
);
37 static ngx_int_t
ngx_http_scgi_reinit_request(ngx_http_request_t
*r
);
38 static ngx_int_t
ngx_http_scgi_process_status_line(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
);
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
,
46 static ngx_int_t
ngx_http_scgi_merge_params(ngx_conf_t
*cf
,
47 ngx_http_scgi_loc_conf_t
*conf
, ngx_http_scgi_loc_conf_t
*prev
);
49 static char *ngx_http_scgi_pass(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
);
50 static char *ngx_http_scgi_store(ngx_conf_t
*cf
, ngx_command_t
*cmd
,
54 static ngx_int_t
ngx_http_scgi_create_key(ngx_http_request_t
*r
);
55 static char *ngx_http_scgi_cache(ngx_conf_t
*cf
, ngx_command_t
*cmd
,
57 static char *ngx_http_scgi_cache_key(ngx_conf_t
*cf
, ngx_command_t
*cmd
,
62 static ngx_conf_bitmask_t ngx_http_scgi_next_upstream_masks
[] = {
63 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR
},
64 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT
},
65 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER
},
66 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500
},
67 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503
},
68 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404
},
69 { ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING
},
70 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF
},
71 { ngx_null_string
, 0 }
75 ngx_module_t ngx_http_scgi_module
;
78 static ngx_command_t ngx_http_scgi_commands
[] = {
80 { ngx_string("scgi_pass"),
81 NGX_HTTP_LOC_CONF
|NGX_HTTP_LIF_CONF
|NGX_CONF_TAKE1
,
83 NGX_HTTP_LOC_CONF_OFFSET
,
87 { ngx_string("scgi_store"),
88 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1
,
90 NGX_HTTP_LOC_CONF_OFFSET
,
94 { ngx_string("scgi_store_access"),
95 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE123
,
96 ngx_conf_set_access_slot
,
97 NGX_HTTP_LOC_CONF_OFFSET
,
98 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.store_access
),
101 { ngx_string("scgi_buffering"),
102 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_FLAG
,
103 ngx_conf_set_flag_slot
,
104 NGX_HTTP_LOC_CONF_OFFSET
,
105 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.buffering
),
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
),
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
),
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
),
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
),
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
),
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
),
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
),
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
),
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
),
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
),
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
),
187 { ngx_string("scgi_cache"),
188 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1
,
190 NGX_HTTP_LOC_CONF_OFFSET
,
194 { ngx_string("scgi_cache_key"),
195 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1
,
196 ngx_http_scgi_cache_key
,
197 NGX_HTTP_LOC_CONF_OFFSET
,
201 { ngx_string("scgi_cache_path"),
202 NGX_HTTP_MAIN_CONF
|NGX_CONF_2MORE
,
203 ngx_http_file_cache_set_slot
,
206 &ngx_http_scgi_module
},
208 { ngx_string("scgi_cache_bypass"),
209 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_1MORE
,
210 ngx_http_set_predicate_slot
,
211 NGX_HTTP_LOC_CONF_OFFSET
,
212 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.cache_bypass
),
215 { ngx_string("scgi_no_cache"),
216 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_1MORE
,
217 ngx_http_set_predicate_slot
,
218 NGX_HTTP_LOC_CONF_OFFSET
,
219 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.no_cache
),
222 { ngx_string("scgi_cache_valid"),
223 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_1MORE
,
224 ngx_http_file_cache_valid_set_slot
,
225 NGX_HTTP_LOC_CONF_OFFSET
,
226 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.cache_valid
),
229 { ngx_string("scgi_cache_min_uses"),
230 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1
,
231 ngx_conf_set_num_slot
,
232 NGX_HTTP_LOC_CONF_OFFSET
,
233 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.cache_min_uses
),
236 { ngx_string("scgi_cache_use_stale"),
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_use_stale
),
241 &ngx_http_scgi_next_upstream_masks
},
243 { ngx_string("scgi_cache_methods"),
244 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_1MORE
,
245 ngx_conf_set_bitmask_slot
,
246 NGX_HTTP_LOC_CONF_OFFSET
,
247 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.cache_methods
),
248 &ngx_http_upstream_cache_method_mask
},
250 { ngx_string("scgi_cache_lock"),
251 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_FLAG
,
252 ngx_conf_set_flag_slot
,
253 NGX_HTTP_LOC_CONF_OFFSET
,
254 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.cache_lock
),
257 { ngx_string("scgi_cache_lock_timeout"),
258 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1
,
259 ngx_conf_set_msec_slot
,
260 NGX_HTTP_LOC_CONF_OFFSET
,
261 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.cache_lock_timeout
),
266 { ngx_string("scgi_temp_path"),
267 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1234
,
268 ngx_conf_set_path_slot
,
269 NGX_HTTP_LOC_CONF_OFFSET
,
270 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.temp_path
),
273 { ngx_string("scgi_max_temp_file_size"),
274 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1
,
275 ngx_conf_set_size_slot
,
276 NGX_HTTP_LOC_CONF_OFFSET
,
277 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.max_temp_file_size_conf
),
280 { ngx_string("scgi_temp_file_write_size"),
281 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1
,
282 ngx_conf_set_size_slot
,
283 NGX_HTTP_LOC_CONF_OFFSET
,
284 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.temp_file_write_size_conf
),
287 { ngx_string("scgi_next_upstream"),
288 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_1MORE
,
289 ngx_conf_set_bitmask_slot
,
290 NGX_HTTP_LOC_CONF_OFFSET
,
291 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.next_upstream
),
292 &ngx_http_scgi_next_upstream_masks
},
294 { ngx_string("scgi_param"),
295 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE23
,
296 ngx_http_upstream_param_set_slot
,
297 NGX_HTTP_LOC_CONF_OFFSET
,
298 offsetof(ngx_http_scgi_loc_conf_t
, params_source
),
301 { ngx_string("scgi_pass_header"),
302 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1
,
303 ngx_conf_set_str_array_slot
,
304 NGX_HTTP_LOC_CONF_OFFSET
,
305 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.pass_headers
),
308 { ngx_string("scgi_hide_header"),
309 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1
,
310 ngx_conf_set_str_array_slot
,
311 NGX_HTTP_LOC_CONF_OFFSET
,
312 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.hide_headers
),
315 { ngx_string("scgi_ignore_headers"),
316 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_1MORE
,
317 ngx_conf_set_bitmask_slot
,
318 NGX_HTTP_LOC_CONF_OFFSET
,
319 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.ignore_headers
),
320 &ngx_http_upstream_ignore_headers_masks
},
326 static ngx_http_module_t ngx_http_scgi_module_ctx
= {
327 NULL
, /* preconfiguration */
328 NULL
, /* postconfiguration */
330 NULL
, /* create main configuration */
331 NULL
, /* init main configuration */
333 NULL
, /* create server configuration */
334 NULL
, /* merge server configuration */
336 ngx_http_scgi_create_loc_conf
, /* create location configuration */
337 ngx_http_scgi_merge_loc_conf
/* merge location configuration */
341 ngx_module_t ngx_http_scgi_module
= {
343 &ngx_http_scgi_module_ctx
, /* module context */
344 ngx_http_scgi_commands
, /* module directives */
345 NGX_HTTP_MODULE
, /* module type */
346 NULL
, /* init master */
347 NULL
, /* init module */
348 NULL
, /* init process */
349 NULL
, /* init thread */
350 NULL
, /* exit thread */
351 NULL
, /* exit process */
352 NULL
, /* exit master */
353 NGX_MODULE_V1_PADDING
357 static ngx_str_t ngx_http_scgi_hide_headers
[] = {
358 ngx_string("Status"),
359 ngx_string("X-Accel-Expires"),
360 ngx_string("X-Accel-Redirect"),
361 ngx_string("X-Accel-Limit-Rate"),
362 ngx_string("X-Accel-Buffering"),
363 ngx_string("X-Accel-Charset"),
370 static ngx_keyval_t ngx_http_scgi_cache_headers
[] = {
371 { ngx_string("HTTP_IF_MODIFIED_SINCE"), ngx_string("") },
372 { ngx_string("HTTP_IF_UNMODIFIED_SINCE"), ngx_string("") },
373 { ngx_string("HTTP_IF_NONE_MATCH"), ngx_string("") },
374 { ngx_string("HTTP_IF_MATCH"), ngx_string("") },
375 { ngx_string("HTTP_RANGE"), ngx_string("") },
376 { ngx_string("HTTP_IF_RANGE"), ngx_string("") },
377 { ngx_null_string
, ngx_null_string
}
383 static ngx_path_init_t ngx_http_scgi_temp_path
= {
384 ngx_string(NGX_HTTP_SCGI_TEMP_PATH
), { 1, 2, 0 }
389 ngx_http_scgi_handler(ngx_http_request_t
*r
)
392 ngx_http_status_t
*status
;
393 ngx_http_upstream_t
*u
;
394 ngx_http_scgi_loc_conf_t
*scf
;
396 if (r
->subrequest_in_memory
) {
397 ngx_log_error(NGX_LOG_ALERT
, r
->connection
->log
, 0,
398 "ngx_http_scgi_module does not support "
399 "subrequests in memory");
400 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
403 if (ngx_http_upstream_create(r
) != NGX_OK
) {
404 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
407 status
= ngx_pcalloc(r
->pool
, sizeof(ngx_http_status_t
));
408 if (status
== NULL
) {
409 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
412 ngx_http_set_ctx(r
, status
, ngx_http_scgi_module
);
414 scf
= ngx_http_get_module_loc_conf(r
, ngx_http_scgi_module
);
416 if (scf
->scgi_lengths
) {
417 if (ngx_http_scgi_eval(r
, scf
) != NGX_OK
) {
418 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
424 ngx_str_set(&u
->schema
, "scgi://");
425 u
->output
.tag
= (ngx_buf_tag_t
) &ngx_http_scgi_module
;
427 u
->conf
= &scf
->upstream
;
430 u
->create_key
= ngx_http_scgi_create_key
;
432 u
->create_request
= ngx_http_scgi_create_request
;
433 u
->reinit_request
= ngx_http_scgi_reinit_request
;
434 u
->process_header
= ngx_http_scgi_process_status_line
;
435 u
->abort_request
= ngx_http_scgi_abort_request
;
436 u
->finalize_request
= ngx_http_scgi_finalize_request
;
439 u
->buffering
= scf
->upstream
.buffering
;
441 u
->pipe
= ngx_pcalloc(r
->pool
, sizeof(ngx_event_pipe_t
));
442 if (u
->pipe
== NULL
) {
443 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
446 u
->pipe
->input_filter
= ngx_event_pipe_copy_input_filter
;
447 u
->pipe
->input_ctx
= r
;
449 rc
= ngx_http_read_client_request_body(r
, ngx_http_upstream_init
);
451 if (rc
>= NGX_HTTP_SPECIAL_RESPONSE
) {
460 ngx_http_scgi_eval(ngx_http_request_t
*r
, ngx_http_scgi_loc_conf_t
* scf
)
463 ngx_http_upstream_t
*u
;
465 ngx_memzero(&url
, sizeof(ngx_url_t
));
467 if (ngx_http_script_run(r
, &url
.url
, scf
->scgi_lengths
->elts
, 0,
468 scf
->scgi_values
->elts
)
476 if (ngx_parse_url(r
->pool
, &url
) != NGX_OK
) {
478 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
479 "%s in upstream \"%V\"", url
.err
, &url
.url
);
487 u
->resolved
= ngx_pcalloc(r
->pool
, sizeof(ngx_http_upstream_resolved_t
));
488 if (u
->resolved
== NULL
) {
492 if (url
.addrs
&& url
.addrs
[0].sockaddr
) {
493 u
->resolved
->sockaddr
= url
.addrs
[0].sockaddr
;
494 u
->resolved
->socklen
= url
.addrs
[0].socklen
;
495 u
->resolved
->naddrs
= 1;
496 u
->resolved
->host
= url
.addrs
[0].name
;
499 u
->resolved
->host
= url
.host
;
500 u
->resolved
->port
= url
.port
;
501 u
->resolved
->no_port
= url
.no_port
;
511 ngx_http_scgi_create_key(ngx_http_request_t
*r
)
514 ngx_http_scgi_loc_conf_t
*scf
;
516 key
= ngx_array_push(&r
->cache
->keys
);
521 scf
= ngx_http_get_module_loc_conf(r
, ngx_http_scgi_module
);
523 if (ngx_http_complex_value(r
, &scf
->cache_key
, key
) != NGX_OK
) {
534 ngx_http_scgi_create_request(ngx_http_request_t
*r
)
536 off_t content_length_n
;
537 u_char ch
, *key
, *val
, *lowcase_key
;
538 size_t len
, key_len
, val_len
, allocated
;
540 ngx_str_t content_length
;
541 ngx_uint_t i
, n
, hash
, skip_empty
, header_params
;
542 ngx_chain_t
*cl
, *body
;
543 ngx_list_part_t
*part
;
544 ngx_table_elt_t
*header
, **ignored
;
545 ngx_http_script_code_pt code
;
546 ngx_http_script_engine_t e
, le
;
547 ngx_http_scgi_loc_conf_t
*scf
;
548 ngx_http_script_len_code_pt lcode
;
549 u_char buffer
[NGX_OFF_T_LEN
];
551 content_length_n
= 0;
552 body
= r
->upstream
->request_bufs
;
555 content_length_n
+= ngx_buf_size(body
->buf
);
559 content_length
.data
= buffer
;
560 content_length
.len
= ngx_sprintf(buffer
, "%O", content_length_n
) - buffer
;
562 len
= sizeof("CONTENT_LENGTH") + content_length
.len
+ 1;
567 scf
= ngx_http_get_module_loc_conf(r
, ngx_http_scgi_module
);
569 if (scf
->params_len
) {
570 ngx_memzero(&le
, sizeof(ngx_http_script_engine_t
));
572 ngx_http_script_flush_no_cacheable_variables(r
, scf
->flushes
);
575 le
.ip
= scf
->params_len
->elts
;
578 while (*(uintptr_t *) le
.ip
) {
580 lcode
= *(ngx_http_script_len_code_pt
*) le
.ip
;
581 key_len
= lcode(&le
);
583 lcode
= *(ngx_http_script_len_code_pt
*) le
.ip
;
584 skip_empty
= lcode(&le
);
586 for (val_len
= 0; *(uintptr_t *) le
.ip
; val_len
+= lcode(&le
)) {
587 lcode
= *(ngx_http_script_len_code_pt
*) le
.ip
;
589 le
.ip
+= sizeof(uintptr_t);
591 if (skip_empty
&& val_len
== 0) {
595 len
+= key_len
+ val_len
+ 1;
599 if (scf
->upstream
.pass_request_headers
) {
604 if (scf
->header_params
) {
606 part
= &r
->headers_in
.headers
.part
;
613 ignored
= ngx_palloc(r
->pool
, n
* sizeof(void *));
614 if (ignored
== NULL
) {
619 part
= &r
->headers_in
.headers
.part
;
622 for (i
= 0; /* void */; i
++) {
624 if (i
>= part
->nelts
) {
625 if (part
->next
== NULL
) {
634 if (scf
->header_params
) {
635 if (allocated
< header
[i
].key
.len
) {
636 allocated
= header
[i
].key
.len
+ 16;
637 lowcase_key
= ngx_pnalloc(r
->pool
, allocated
);
638 if (lowcase_key
== NULL
) {
645 for (n
= 0; n
< header
[i
].key
.len
; n
++) {
646 ch
= header
[i
].key
.data
[n
];
648 if (ch
>= 'A' && ch
<= 'Z') {
651 } else if (ch
== '-') {
655 hash
= ngx_hash(hash
, ch
);
659 if (ngx_hash_find(&scf
->headers_hash
, hash
, lowcase_key
, n
)) {
660 ignored
[header_params
++] = &header
[i
];
665 len
+= sizeof("HTTP_") - 1 + header
[i
].key
.len
+ 1
666 + header
[i
].value
.len
+ 1;
670 /* netstring: "length:" + packet + "," */
672 b
= ngx_create_temp_buf(r
->pool
, NGX_SIZE_T_LEN
+ 1 + len
+ 1);
677 cl
= ngx_alloc_chain_link(r
->pool
);
684 b
->last
= ngx_sprintf(b
->last
, "%ui:CONTENT_LENGTH%Z%V%Z",
685 len
, &content_length
);
687 if (scf
->params_len
) {
688 ngx_memzero(&e
, sizeof(ngx_http_script_engine_t
));
690 e
.ip
= scf
->params
->elts
;
695 le
.ip
= scf
->params_len
->elts
;
697 while (*(uintptr_t *) le
.ip
) {
699 lcode
= *(ngx_http_script_len_code_pt
*) le
.ip
;
700 lcode(&le
); /* key length */
702 lcode
= *(ngx_http_script_len_code_pt
*) le
.ip
;
703 skip_empty
= lcode(&le
);
705 for (val_len
= 0; *(uintptr_t *) le
.ip
; val_len
+= lcode(&le
)) {
706 lcode
= *(ngx_http_script_len_code_pt
*) le
.ip
;
708 le
.ip
+= sizeof(uintptr_t);
710 if (skip_empty
&& val_len
== 0) {
713 while (*(uintptr_t *) e
.ip
) {
714 code
= *(ngx_http_script_code_pt
*) e
.ip
;
715 code((ngx_http_script_engine_t
*) &e
);
717 e
.ip
+= sizeof(uintptr_t);
727 code
= *(ngx_http_script_code_pt
*) e
.ip
;
728 code((ngx_http_script_engine_t
*) & e
);
733 while (*(uintptr_t *) e
.ip
) {
734 code
= *(ngx_http_script_code_pt
*) e
.ip
;
735 code((ngx_http_script_engine_t
*) &e
);
738 e
.ip
+= sizeof(uintptr_t);
740 ngx_log_debug2(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
741 "scgi param: \"%s: %s\"", key
, val
);
747 if (scf
->upstream
.pass_request_headers
) {
749 part
= &r
->headers_in
.headers
.part
;
752 for (i
= 0; /* void */; i
++) {
754 if (i
>= part
->nelts
) {
755 if (part
->next
== NULL
) {
764 for (n
= 0; n
< header_params
; n
++) {
765 if (&header
[i
] == ignored
[n
]) {
771 b
->last
= ngx_cpymem(key
, "HTTP_", sizeof("HTTP_") - 1);
773 for (n
= 0; n
< header
[i
].key
.len
; n
++) {
774 ch
= header
[i
].key
.data
[n
];
776 if (ch
>= 'a' && ch
<= 'z') {
779 } else if (ch
== '-') {
786 *b
->last
++ = (u_char
) 0;
789 b
->last
= ngx_copy(val
, header
[i
].value
.data
, header
[i
].value
.len
);
790 *b
->last
++ = (u_char
) 0;
792 ngx_log_debug2(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
793 "scgi param: \"%s: %s\"", key
, val
);
801 *b
->last
++ = (u_char
) ',';
803 if (scf
->upstream
.pass_request_body
) {
804 body
= r
->upstream
->request_bufs
;
805 r
->upstream
->request_bufs
= cl
;
808 b
= ngx_alloc_buf(r
->pool
);
813 ngx_memcpy(b
, body
->buf
, sizeof(ngx_buf_t
));
815 cl
->next
= ngx_alloc_chain_link(r
->pool
);
816 if (cl
->next
== NULL
) {
827 r
->upstream
->request_bufs
= cl
;
837 ngx_http_scgi_reinit_request(ngx_http_request_t
*r
)
839 ngx_http_status_t
*status
;
841 status
= ngx_http_get_module_ctx(r
, ngx_http_scgi_module
);
843 if (status
== NULL
) {
849 status
->start
= NULL
;
852 r
->upstream
->process_header
= ngx_http_scgi_process_status_line
;
860 ngx_http_scgi_process_status_line(ngx_http_request_t
*r
)
864 ngx_http_status_t
*status
;
865 ngx_http_upstream_t
*u
;
867 status
= ngx_http_get_module_ctx(r
, ngx_http_scgi_module
);
869 if (status
== NULL
) {
875 rc
= ngx_http_parse_status_line(r
, &u
->buffer
, status
);
877 if (rc
== NGX_AGAIN
) {
881 if (rc
== NGX_ERROR
) {
882 u
->process_header
= ngx_http_scgi_process_header
;
883 return ngx_http_scgi_process_header(r
);
887 u
->state
->status
= status
->code
;
890 u
->headers_in
.status_n
= status
->code
;
892 len
= status
->end
- status
->start
;
893 u
->headers_in
.status_line
.len
= len
;
895 u
->headers_in
.status_line
.data
= ngx_pnalloc(r
->pool
, len
);
896 if (u
->headers_in
.status_line
.data
== NULL
) {
900 ngx_memcpy(u
->headers_in
.status_line
.data
, status
->start
, len
);
902 ngx_log_debug2(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
903 "http scgi status %ui \"%V\"",
904 u
->headers_in
.status_n
, &u
->headers_in
.status_line
);
906 u
->process_header
= ngx_http_scgi_process_header
;
908 return ngx_http_scgi_process_header(r
);
913 ngx_http_scgi_process_header(ngx_http_request_t
*r
)
915 ngx_str_t
*status_line
;
916 ngx_int_t rc
, status
;
918 ngx_http_upstream_t
*u
;
919 ngx_http_upstream_header_t
*hh
;
920 ngx_http_upstream_main_conf_t
*umcf
;
922 umcf
= ngx_http_get_module_main_conf(r
, ngx_http_upstream_module
);
926 rc
= ngx_http_parse_header_line(r
, &r
->upstream
->buffer
, 1);
930 /* a header line has been parsed successfully */
932 h
= ngx_list_push(&r
->upstream
->headers_in
.headers
);
937 h
->hash
= r
->header_hash
;
939 h
->key
.len
= r
->header_name_end
- r
->header_name_start
;
940 h
->value
.len
= r
->header_end
- r
->header_start
;
942 h
->key
.data
= ngx_pnalloc(r
->pool
,
943 h
->key
.len
+ 1 + h
->value
.len
+ 1
945 if (h
->key
.data
== NULL
) {
949 h
->value
.data
= h
->key
.data
+ h
->key
.len
+ 1;
950 h
->lowcase_key
= h
->key
.data
+ h
->key
.len
+ 1 + h
->value
.len
+ 1;
952 ngx_memcpy(h
->key
.data
, r
->header_name_start
, h
->key
.len
);
953 h
->key
.data
[h
->key
.len
] = '\0';
954 ngx_memcpy(h
->value
.data
, r
->header_start
, h
->value
.len
);
955 h
->value
.data
[h
->value
.len
] = '\0';
957 if (h
->key
.len
== r
->lowcase_index
) {
958 ngx_memcpy(h
->lowcase_key
, r
->lowcase_header
, h
->key
.len
);
961 ngx_strlow(h
->lowcase_key
, h
->key
.data
, h
->key
.len
);
964 hh
= ngx_hash_find(&umcf
->headers_in_hash
, h
->hash
,
965 h
->lowcase_key
, h
->key
.len
);
967 if (hh
&& hh
->handler(r
, h
, hh
->offset
) != NGX_OK
) {
971 ngx_log_debug2(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
972 "http scgi header: \"%V: %V\"", &h
->key
, &h
->value
);
977 if (rc
== NGX_HTTP_PARSE_HEADER_DONE
) {
979 /* a whole header has been parsed successfully */
981 ngx_log_debug0(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
982 "http scgi header done");
986 if (u
->headers_in
.status_n
) {
990 if (u
->headers_in
.status
) {
991 status_line
= &u
->headers_in
.status
->value
;
993 status
= ngx_atoi(status_line
->data
, 3);
994 if (status
== NGX_ERROR
) {
995 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
996 "upstream sent invalid status \"%V\"",
998 return NGX_HTTP_UPSTREAM_INVALID_HEADER
;
1001 u
->headers_in
.status_n
= status
;
1002 u
->headers_in
.status_line
= *status_line
;
1004 } else if (u
->headers_in
.location
) {
1005 u
->headers_in
.status_n
= 302;
1006 ngx_str_set(&u
->headers_in
.status_line
,
1007 "302 Moved Temporarily");
1010 u
->headers_in
.status_n
= 200;
1011 ngx_str_set(&u
->headers_in
.status_line
, "200 OK");
1015 u
->state
->status
= u
->headers_in
.status_n
;
1020 if (u
->headers_in
.status_n
== NGX_HTTP_SWITCHING_PROTOCOLS
1021 && r
->headers_in
.upgrade
)
1029 if (rc
== NGX_AGAIN
) {
1033 /* there was error while a header line parsing */
1035 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
1036 "upstream sent invalid header");
1038 return NGX_HTTP_UPSTREAM_INVALID_HEADER
;
1044 ngx_http_scgi_abort_request(ngx_http_request_t
*r
)
1046 ngx_log_debug0(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
1047 "abort http scgi request");
1054 ngx_http_scgi_finalize_request(ngx_http_request_t
*r
, ngx_int_t rc
)
1056 ngx_log_debug0(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
1057 "finalize http scgi request");
1064 ngx_http_scgi_create_loc_conf(ngx_conf_t
*cf
)
1066 ngx_http_scgi_loc_conf_t
*conf
;
1068 conf
= ngx_pcalloc(cf
->pool
, sizeof(ngx_http_scgi_loc_conf_t
));
1073 conf
->upstream
.store
= NGX_CONF_UNSET
;
1074 conf
->upstream
.store_access
= NGX_CONF_UNSET_UINT
;
1075 conf
->upstream
.buffering
= NGX_CONF_UNSET
;
1076 conf
->upstream
.ignore_client_abort
= NGX_CONF_UNSET
;
1078 conf
->upstream
.local
= NGX_CONF_UNSET_PTR
;
1080 conf
->upstream
.connect_timeout
= NGX_CONF_UNSET_MSEC
;
1081 conf
->upstream
.send_timeout
= NGX_CONF_UNSET_MSEC
;
1082 conf
->upstream
.read_timeout
= NGX_CONF_UNSET_MSEC
;
1084 conf
->upstream
.send_lowat
= NGX_CONF_UNSET_SIZE
;
1085 conf
->upstream
.buffer_size
= NGX_CONF_UNSET_SIZE
;
1087 conf
->upstream
.busy_buffers_size_conf
= NGX_CONF_UNSET_SIZE
;
1088 conf
->upstream
.max_temp_file_size_conf
= NGX_CONF_UNSET_SIZE
;
1089 conf
->upstream
.temp_file_write_size_conf
= NGX_CONF_UNSET_SIZE
;
1091 conf
->upstream
.pass_request_headers
= NGX_CONF_UNSET
;
1092 conf
->upstream
.pass_request_body
= NGX_CONF_UNSET
;
1094 #if (NGX_HTTP_CACHE)
1095 conf
->upstream
.cache
= NGX_CONF_UNSET_PTR
;
1096 conf
->upstream
.cache_min_uses
= NGX_CONF_UNSET_UINT
;
1097 conf
->upstream
.cache_bypass
= NGX_CONF_UNSET_PTR
;
1098 conf
->upstream
.no_cache
= NGX_CONF_UNSET_PTR
;
1099 conf
->upstream
.cache_valid
= NGX_CONF_UNSET_PTR
;
1100 conf
->upstream
.cache_lock
= NGX_CONF_UNSET
;
1101 conf
->upstream
.cache_lock_timeout
= NGX_CONF_UNSET_MSEC
;
1104 conf
->upstream
.hide_headers
= NGX_CONF_UNSET_PTR
;
1105 conf
->upstream
.pass_headers
= NGX_CONF_UNSET_PTR
;
1107 conf
->upstream
.intercept_errors
= NGX_CONF_UNSET
;
1109 /* "scgi_cyclic_temp_file" is disabled */
1110 conf
->upstream
.cyclic_temp_file
= 0;
1112 conf
->upstream
.change_buffering
= 1;
1114 ngx_str_set(&conf
->upstream
.module
, "scgi");
1121 ngx_http_scgi_merge_loc_conf(ngx_conf_t
*cf
, void *parent
, void *child
)
1123 ngx_http_scgi_loc_conf_t
*prev
= parent
;
1124 ngx_http_scgi_loc_conf_t
*conf
= child
;
1127 ngx_hash_init_t hash
;
1128 ngx_http_core_loc_conf_t
*clcf
;
1130 if (conf
->upstream
.store
!= 0) {
1131 ngx_conf_merge_value(conf
->upstream
.store
, prev
->upstream
.store
, 0);
1133 if (conf
->upstream
.store_lengths
== NULL
) {
1134 conf
->upstream
.store_lengths
= prev
->upstream
.store_lengths
;
1135 conf
->upstream
.store_values
= prev
->upstream
.store_values
;
1139 ngx_conf_merge_uint_value(conf
->upstream
.store_access
,
1140 prev
->upstream
.store_access
, 0600);
1142 ngx_conf_merge_value(conf
->upstream
.buffering
,
1143 prev
->upstream
.buffering
, 1);
1145 ngx_conf_merge_value(conf
->upstream
.ignore_client_abort
,
1146 prev
->upstream
.ignore_client_abort
, 0);
1148 ngx_conf_merge_ptr_value(conf
->upstream
.local
,
1149 prev
->upstream
.local
, NULL
);
1151 ngx_conf_merge_msec_value(conf
->upstream
.connect_timeout
,
1152 prev
->upstream
.connect_timeout
, 60000);
1154 ngx_conf_merge_msec_value(conf
->upstream
.send_timeout
,
1155 prev
->upstream
.send_timeout
, 60000);
1157 ngx_conf_merge_msec_value(conf
->upstream
.read_timeout
,
1158 prev
->upstream
.read_timeout
, 60000);
1160 ngx_conf_merge_size_value(conf
->upstream
.send_lowat
,
1161 prev
->upstream
.send_lowat
, 0);
1163 ngx_conf_merge_size_value(conf
->upstream
.buffer_size
,
1164 prev
->upstream
.buffer_size
,
1165 (size_t) ngx_pagesize
);
1168 ngx_conf_merge_bufs_value(conf
->upstream
.bufs
, prev
->upstream
.bufs
,
1171 if (conf
->upstream
.bufs
.num
< 2) {
1172 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1173 "there must be at least 2 \"scgi_buffers\"");
1174 return NGX_CONF_ERROR
;
1178 size
= conf
->upstream
.buffer_size
;
1179 if (size
< conf
->upstream
.bufs
.size
) {
1180 size
= conf
->upstream
.bufs
.size
;
1184 ngx_conf_merge_size_value(conf
->upstream
.busy_buffers_size_conf
,
1185 prev
->upstream
.busy_buffers_size_conf
,
1186 NGX_CONF_UNSET_SIZE
);
1188 if (conf
->upstream
.busy_buffers_size_conf
== NGX_CONF_UNSET_SIZE
) {
1189 conf
->upstream
.busy_buffers_size
= 2 * size
;
1191 conf
->upstream
.busy_buffers_size
=
1192 conf
->upstream
.busy_buffers_size_conf
;
1195 if (conf
->upstream
.busy_buffers_size
< size
) {
1196 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1197 "\"scgi_busy_buffers_size\" must be equal to or greater "
1198 "than the maximum of the value of \"scgi_buffer_size\" and "
1199 "one of the \"scgi_buffers\"");
1201 return NGX_CONF_ERROR
;
1204 if (conf
->upstream
.busy_buffers_size
1205 > (conf
->upstream
.bufs
.num
- 1) * conf
->upstream
.bufs
.size
)
1207 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1208 "\"scgi_busy_buffers_size\" must be less than "
1209 "the size of all \"scgi_buffers\" minus one buffer");
1211 return NGX_CONF_ERROR
;
1215 ngx_conf_merge_size_value(conf
->upstream
.temp_file_write_size_conf
,
1216 prev
->upstream
.temp_file_write_size_conf
,
1217 NGX_CONF_UNSET_SIZE
);
1219 if (conf
->upstream
.temp_file_write_size_conf
== NGX_CONF_UNSET_SIZE
) {
1220 conf
->upstream
.temp_file_write_size
= 2 * size
;
1222 conf
->upstream
.temp_file_write_size
=
1223 conf
->upstream
.temp_file_write_size_conf
;
1226 if (conf
->upstream
.temp_file_write_size
< size
) {
1227 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1228 "\"scgi_temp_file_write_size\" must be equal to or greater than "
1229 "the maximum of the value of \"scgi_buffer_size\" and "
1230 "one of the \"scgi_buffers\"");
1232 return NGX_CONF_ERROR
;
1236 ngx_conf_merge_size_value(conf
->upstream
.max_temp_file_size_conf
,
1237 prev
->upstream
.max_temp_file_size_conf
,
1238 NGX_CONF_UNSET_SIZE
);
1240 if (conf
->upstream
.max_temp_file_size_conf
== NGX_CONF_UNSET_SIZE
) {
1241 conf
->upstream
.max_temp_file_size
= 1024 * 1024 * 1024;
1243 conf
->upstream
.max_temp_file_size
=
1244 conf
->upstream
.max_temp_file_size_conf
;
1247 if (conf
->upstream
.max_temp_file_size
!= 0
1248 && conf
->upstream
.max_temp_file_size
< size
) {
1249 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1250 "\"scgi_max_temp_file_size\" must be equal to zero to disable "
1251 "temporary files usage or must be equal to or greater than "
1252 "the maximum of the value of \"scgi_buffer_size\" and "
1253 "one of the \"scgi_buffers\"");
1255 return NGX_CONF_ERROR
;
1259 ngx_conf_merge_bitmask_value(conf
->upstream
.ignore_headers
,
1260 prev
->upstream
.ignore_headers
,
1261 NGX_CONF_BITMASK_SET
);
1264 ngx_conf_merge_bitmask_value(conf
->upstream
.next_upstream
,
1265 prev
->upstream
.next_upstream
,
1266 (NGX_CONF_BITMASK_SET
1267 |NGX_HTTP_UPSTREAM_FT_ERROR
1268 |NGX_HTTP_UPSTREAM_FT_TIMEOUT
));
1270 if (conf
->upstream
.next_upstream
& NGX_HTTP_UPSTREAM_FT_OFF
) {
1271 conf
->upstream
.next_upstream
= NGX_CONF_BITMASK_SET
1272 |NGX_HTTP_UPSTREAM_FT_OFF
;
1275 if (ngx_conf_merge_path_value(cf
, &conf
->upstream
.temp_path
,
1276 prev
->upstream
.temp_path
,
1277 &ngx_http_scgi_temp_path
)
1280 return NGX_CONF_ERROR
;
1283 #if (NGX_HTTP_CACHE)
1285 ngx_conf_merge_ptr_value(conf
->upstream
.cache
,
1286 prev
->upstream
.cache
, NULL
);
1288 if (conf
->upstream
.cache
&& conf
->upstream
.cache
->data
== NULL
) {
1289 ngx_shm_zone_t
*shm_zone
;
1291 shm_zone
= conf
->upstream
.cache
;
1293 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1294 "\"scgi_cache\" zone \"%V\" is unknown",
1295 &shm_zone
->shm
.name
);
1297 return NGX_CONF_ERROR
;
1300 ngx_conf_merge_uint_value(conf
->upstream
.cache_min_uses
,
1301 prev
->upstream
.cache_min_uses
, 1);
1303 ngx_conf_merge_bitmask_value(conf
->upstream
.cache_use_stale
,
1304 prev
->upstream
.cache_use_stale
,
1305 (NGX_CONF_BITMASK_SET
1306 |NGX_HTTP_UPSTREAM_FT_OFF
));
1308 if (conf
->upstream
.cache_use_stale
& NGX_HTTP_UPSTREAM_FT_OFF
) {
1309 conf
->upstream
.cache_use_stale
= NGX_CONF_BITMASK_SET
1310 |NGX_HTTP_UPSTREAM_FT_OFF
;
1313 if (conf
->upstream
.cache_use_stale
& NGX_HTTP_UPSTREAM_FT_ERROR
) {
1314 conf
->upstream
.cache_use_stale
|= NGX_HTTP_UPSTREAM_FT_NOLIVE
;
1317 if (conf
->upstream
.cache_methods
== 0) {
1318 conf
->upstream
.cache_methods
= prev
->upstream
.cache_methods
;
1321 conf
->upstream
.cache_methods
|= NGX_HTTP_GET
|NGX_HTTP_HEAD
;
1323 ngx_conf_merge_ptr_value(conf
->upstream
.cache_bypass
,
1324 prev
->upstream
.cache_bypass
, NULL
);
1326 ngx_conf_merge_ptr_value(conf
->upstream
.no_cache
,
1327 prev
->upstream
.no_cache
, NULL
);
1329 ngx_conf_merge_ptr_value(conf
->upstream
.cache_valid
,
1330 prev
->upstream
.cache_valid
, NULL
);
1332 if (conf
->cache_key
.value
.data
== NULL
) {
1333 conf
->cache_key
= prev
->cache_key
;
1336 ngx_conf_merge_value(conf
->upstream
.cache_lock
,
1337 prev
->upstream
.cache_lock
, 0);
1339 ngx_conf_merge_msec_value(conf
->upstream
.cache_lock_timeout
,
1340 prev
->upstream
.cache_lock_timeout
, 5000);
1344 ngx_conf_merge_value(conf
->upstream
.pass_request_headers
,
1345 prev
->upstream
.pass_request_headers
, 1);
1346 ngx_conf_merge_value(conf
->upstream
.pass_request_body
,
1347 prev
->upstream
.pass_request_body
, 1);
1349 ngx_conf_merge_value(conf
->upstream
.intercept_errors
,
1350 prev
->upstream
.intercept_errors
, 0);
1352 hash
.max_size
= 512;
1353 hash
.bucket_size
= ngx_align(64, ngx_cacheline_size
);
1354 hash
.name
= "scgi_hide_headers_hash";
1356 if (ngx_http_upstream_hide_headers_hash(cf
, &conf
->upstream
,
1357 &prev
->upstream
, ngx_http_scgi_hide_headers
, &hash
)
1360 return NGX_CONF_ERROR
;
1363 if (conf
->upstream
.upstream
== NULL
) {
1364 conf
->upstream
.upstream
= prev
->upstream
.upstream
;
1367 if (conf
->scgi_lengths
== NULL
) {
1368 conf
->scgi_lengths
= prev
->scgi_lengths
;
1369 conf
->scgi_values
= prev
->scgi_values
;
1372 if (conf
->upstream
.upstream
|| conf
->scgi_lengths
) {
1373 clcf
= ngx_http_conf_get_module_loc_conf(cf
, ngx_http_core_module
);
1374 if (clcf
->handler
== NULL
&& clcf
->lmt_excpt
) {
1375 clcf
->handler
= ngx_http_scgi_handler
;
1379 if (ngx_http_scgi_merge_params(cf
, conf
, prev
) != NGX_OK
) {
1380 return NGX_CONF_ERROR
;
1388 ngx_http_scgi_merge_params(ngx_conf_t
*cf
, ngx_http_scgi_loc_conf_t
*conf
,
1389 ngx_http_scgi_loc_conf_t
*prev
)
1395 ngx_array_t headers_names
;
1396 #if (NGX_HTTP_CACHE)
1397 ngx_array_t params_merged
;
1400 ngx_hash_init_t hash
;
1401 ngx_http_upstream_param_t
*src
;
1402 ngx_http_script_compile_t sc
;
1403 ngx_http_script_copy_code_t
*copy
;
1405 if (conf
->params_source
== NULL
) {
1406 conf
->params_source
= prev
->params_source
;
1408 if (prev
->headers_hash
.buckets
1409 #if (NGX_HTTP_CACHE)
1410 && ((conf
->upstream
.cache
== NULL
)
1411 == (prev
->upstream
.cache
== NULL
))
1415 conf
->flushes
= prev
->flushes
;
1416 conf
->params_len
= prev
->params_len
;
1417 conf
->params
= prev
->params
;
1418 conf
->headers_hash
= prev
->headers_hash
;
1419 conf
->header_params
= prev
->header_params
;
1425 if (conf
->params_source
== NULL
1426 #if (NGX_HTTP_CACHE)
1427 && (conf
->upstream
.cache
== NULL
)
1431 conf
->headers_hash
.buckets
= (void *) 1;
1435 conf
->params_len
= ngx_array_create(cf
->pool
, 64, 1);
1436 if (conf
->params_len
== NULL
) {
1440 conf
->params
= ngx_array_create(cf
->pool
, 512, 1);
1441 if (conf
->params
== NULL
) {
1445 if (ngx_array_init(&headers_names
, cf
->temp_pool
, 4, sizeof(ngx_hash_key_t
))
1451 if (conf
->params_source
) {
1452 src
= conf
->params_source
->elts
;
1453 nsrc
= conf
->params_source
->nelts
;
1460 #if (NGX_HTTP_CACHE)
1462 if (conf
->upstream
.cache
) {
1464 ngx_http_upstream_param_t
*s
;
1466 if (ngx_array_init(¶ms_merged
, cf
->temp_pool
, 4,
1467 sizeof(ngx_http_upstream_param_t
))
1473 for (i
= 0; i
< nsrc
; i
++) {
1475 s
= ngx_array_push(¶ms_merged
);
1483 h
= ngx_http_scgi_cache_headers
;
1485 while (h
->key
.len
) {
1487 src
= params_merged
.elts
;
1488 nsrc
= params_merged
.nelts
;
1490 for (i
= 0; i
< nsrc
; i
++) {
1491 if (ngx_strcasecmp(h
->key
.data
, src
[i
].key
.data
) == 0) {
1496 s
= ngx_array_push(¶ms_merged
);
1502 s
->value
= h
->value
;
1510 src
= params_merged
.elts
;
1511 nsrc
= params_merged
.nelts
;
1516 for (i
= 0; i
< nsrc
; i
++) {
1518 if (src
[i
].key
.len
> sizeof("HTTP_") - 1
1519 && ngx_strncmp(src
[i
].key
.data
, "HTTP_", sizeof("HTTP_") - 1) == 0)
1521 hk
= ngx_array_push(&headers_names
);
1526 hk
->key
.len
= src
[i
].key
.len
- 5;
1527 hk
->key
.data
= src
[i
].key
.data
+ 5;
1528 hk
->key_hash
= ngx_hash_key_lc(hk
->key
.data
, hk
->key
.len
);
1529 hk
->value
= (void *) 1;
1531 if (src
[i
].value
.len
== 0) {
1536 copy
= ngx_array_push_n(conf
->params_len
,
1537 sizeof(ngx_http_script_copy_code_t
));
1542 copy
->code
= (ngx_http_script_code_pt
) ngx_http_script_copy_len_code
;
1543 copy
->len
= src
[i
].key
.len
+ 1;
1545 copy
= ngx_array_push_n(conf
->params_len
,
1546 sizeof(ngx_http_script_copy_code_t
));
1551 copy
->code
= (ngx_http_script_code_pt
) ngx_http_script_copy_len_code
;
1552 copy
->len
= src
[i
].skip_empty
;
1555 size
= (sizeof(ngx_http_script_copy_code_t
)
1556 + src
[i
].key
.len
+ 1 + sizeof(uintptr_t) - 1)
1557 & ~(sizeof(uintptr_t) - 1);
1559 copy
= ngx_array_push_n(conf
->params
, size
);
1564 copy
->code
= ngx_http_script_copy_code
;
1565 copy
->len
= src
[i
].key
.len
+ 1;
1567 p
= (u_char
*) copy
+ sizeof(ngx_http_script_copy_code_t
);
1568 (void) ngx_cpystrn(p
, src
[i
].key
.data
, src
[i
].key
.len
+ 1);
1571 ngx_memzero(&sc
, sizeof(ngx_http_script_compile_t
));
1574 sc
.source
= &src
[i
].value
;
1575 sc
.flushes
= &conf
->flushes
;
1576 sc
.lengths
= &conf
->params_len
;
1577 sc
.values
= &conf
->params
;
1579 if (ngx_http_script_compile(&sc
) != NGX_OK
) {
1583 code
= ngx_array_push_n(conf
->params_len
, sizeof(uintptr_t));
1588 *code
= (uintptr_t) NULL
;
1591 code
= ngx_array_push_n(conf
->params
, sizeof(uintptr_t));
1596 *code
= (uintptr_t) NULL
;
1599 code
= ngx_array_push_n(conf
->params_len
, sizeof(uintptr_t));
1604 *code
= (uintptr_t) NULL
;
1606 code
= ngx_array_push_n(conf
->params
, sizeof(uintptr_t));
1611 *code
= (uintptr_t) NULL
;
1613 conf
->header_params
= headers_names
.nelts
;
1615 hash
.hash
= &conf
->headers_hash
;
1616 hash
.key
= ngx_hash_key_lc
;
1617 hash
.max_size
= 512;
1618 hash
.bucket_size
= 64;
1619 hash
.name
= "scgi_params_hash";
1620 hash
.pool
= cf
->pool
;
1621 hash
.temp_pool
= NULL
;
1623 return ngx_hash_init(&hash
, headers_names
.elts
, headers_names
.nelts
);
1628 ngx_http_scgi_pass(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
)
1630 ngx_http_scgi_loc_conf_t
*scf
= conf
;
1633 ngx_str_t
*value
, *url
;
1635 ngx_http_core_loc_conf_t
*clcf
;
1636 ngx_http_script_compile_t sc
;
1638 if (scf
->upstream
.upstream
|| scf
->scgi_lengths
) {
1639 return "is duplicate";
1642 clcf
= ngx_http_conf_get_module_loc_conf (cf
, ngx_http_core_module
);
1643 clcf
->handler
= ngx_http_scgi_handler
;
1645 value
= cf
->args
->elts
;
1649 n
= ngx_http_script_variables_count(url
);
1653 ngx_memzero(&sc
, sizeof(ngx_http_script_compile_t
));
1657 sc
.lengths
= &scf
->scgi_lengths
;
1658 sc
.values
= &scf
->scgi_values
;
1660 sc
.complete_lengths
= 1;
1661 sc
.complete_values
= 1;
1663 if (ngx_http_script_compile(&sc
) != NGX_OK
) {
1664 return NGX_CONF_ERROR
;
1670 ngx_memzero(&u
, sizeof(ngx_url_t
));
1675 scf
->upstream
.upstream
= ngx_http_upstream_add(cf
, &u
, 0);
1676 if (scf
->upstream
.upstream
== NULL
) {
1677 return NGX_CONF_ERROR
;
1680 if (clcf
->name
.data
[clcf
->name
.len
- 1] == '/') {
1681 clcf
->auto_redirect
= 1;
1689 ngx_http_scgi_store(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
)
1691 ngx_http_scgi_loc_conf_t
*scf
= conf
;
1694 ngx_http_script_compile_t sc
;
1696 if (scf
->upstream
.store
!= NGX_CONF_UNSET
|| scf
->upstream
.store_lengths
) {
1697 return "is duplicate";
1700 value
= cf
->args
->elts
;
1702 if (ngx_strcmp(value
[1].data
, "off") == 0) {
1703 scf
->upstream
.store
= 0;
1707 #if (NGX_HTTP_CACHE)
1709 if (scf
->upstream
.cache
!= NGX_CONF_UNSET_PTR
1710 && scf
->upstream
.cache
!= NULL
)
1712 return "is incompatible with \"scgi_cache\"";
1717 if (ngx_strcmp(value
[1].data
, "on") == 0) {
1718 scf
->upstream
.store
= 1;
1722 /* include the terminating '\0' into script */
1725 ngx_memzero(&sc
, sizeof(ngx_http_script_compile_t
));
1728 sc
.source
= &value
[1];
1729 sc
.lengths
= &scf
->upstream
.store_lengths
;
1730 sc
.values
= &scf
->upstream
.store_values
;
1731 sc
.variables
= ngx_http_script_variables_count(&value
[1]);;
1732 sc
.complete_lengths
= 1;
1733 sc
.complete_values
= 1;
1735 if (ngx_http_script_compile(&sc
) != NGX_OK
) {
1736 return NGX_CONF_ERROR
;
1743 #if (NGX_HTTP_CACHE)
1746 ngx_http_scgi_cache(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
)
1748 ngx_http_scgi_loc_conf_t
*scf
= conf
;
1752 value
= cf
->args
->elts
;
1754 if (scf
->upstream
.cache
!= NGX_CONF_UNSET_PTR
) {
1755 return "is duplicate";
1758 if (ngx_strcmp(value
[1].data
, "off") == 0) {
1759 scf
->upstream
.cache
= NULL
;
1763 if (scf
->upstream
.store
> 0 || scf
->upstream
.store_lengths
) {
1764 return "is incompatible with \"scgi_store\"";
1767 scf
->upstream
.cache
= ngx_shared_memory_add(cf
, &value
[1], 0,
1768 &ngx_http_scgi_module
);
1769 if (scf
->upstream
.cache
== NULL
) {
1770 return NGX_CONF_ERROR
;
1778 ngx_http_scgi_cache_key(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
)
1780 ngx_http_scgi_loc_conf_t
*scf
= conf
;
1783 ngx_http_compile_complex_value_t ccv
;
1785 value
= cf
->args
->elts
;
1787 if (scf
->cache_key
.value
.data
) {
1788 return "is duplicate";
1791 ngx_memzero(&ccv
, sizeof(ngx_http_compile_complex_value_t
));
1794 ccv
.value
= &value
[1];
1795 ccv
.complex_value
= &scf
->cache_key
;
1797 if (ngx_http_compile_complex_value(&ccv
) != NGX_OK
) {
1798 return NGX_CONF_ERROR
;