3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Manlio Perillo (manlio.perillo@gmail.com)
8 #include <ngx_config.h>
14 ngx_http_upstream_conf_t upstream
;
17 ngx_array_t
*params_len
;
19 ngx_array_t
*params_source
;
21 ngx_hash_t headers_hash
;
22 ngx_uint_t header_params
;
24 ngx_array_t
*scgi_lengths
;
25 ngx_array_t
*scgi_values
;
28 ngx_http_complex_value_t cache_key
;
30 } ngx_http_scgi_loc_conf_t
;
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
);
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
},
252 { ngx_string("scgi_temp_path"),
253 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1234
,
254 ngx_conf_set_path_slot
,
255 NGX_HTTP_LOC_CONF_OFFSET
,
256 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.temp_path
),
259 { ngx_string("scgi_max_temp_file_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
.max_temp_file_size_conf
),
266 { ngx_string("scgi_temp_file_write_size"),
267 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1
,
268 ngx_conf_set_size_slot
,
269 NGX_HTTP_LOC_CONF_OFFSET
,
270 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.temp_file_write_size_conf
),
273 { ngx_string("scgi_next_upstream"),
274 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_1MORE
,
275 ngx_conf_set_bitmask_slot
,
276 NGX_HTTP_LOC_CONF_OFFSET
,
277 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.next_upstream
),
278 &ngx_http_scgi_next_upstream_masks
},
280 { ngx_string("scgi_param"),
281 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE23
,
282 ngx_http_upstream_param_set_slot
,
283 NGX_HTTP_LOC_CONF_OFFSET
,
284 offsetof(ngx_http_scgi_loc_conf_t
, params_source
),
287 { ngx_string("scgi_pass_header"),
288 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1
,
289 ngx_conf_set_str_array_slot
,
290 NGX_HTTP_LOC_CONF_OFFSET
,
291 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.pass_headers
),
294 { ngx_string("scgi_hide_header"),
295 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_TAKE1
,
296 ngx_conf_set_str_array_slot
,
297 NGX_HTTP_LOC_CONF_OFFSET
,
298 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.hide_headers
),
301 { ngx_string("scgi_ignore_headers"),
302 NGX_HTTP_MAIN_CONF
|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF
|NGX_CONF_1MORE
,
303 ngx_conf_set_bitmask_slot
,
304 NGX_HTTP_LOC_CONF_OFFSET
,
305 offsetof(ngx_http_scgi_loc_conf_t
, upstream
.ignore_headers
),
306 &ngx_http_upstream_ignore_headers_masks
},
312 static ngx_http_module_t ngx_http_scgi_module_ctx
= {
313 NULL
, /* preconfiguration */
314 NULL
, /* postconfiguration */
316 NULL
, /* create main configuration */
317 NULL
, /* init main configuration */
319 NULL
, /* create server configuration */
320 NULL
, /* merge server configuration */
322 ngx_http_scgi_create_loc_conf
, /* create location configuration */
323 ngx_http_scgi_merge_loc_conf
/* merge location configuration */
327 ngx_module_t ngx_http_scgi_module
= {
329 &ngx_http_scgi_module_ctx
, /* module context */
330 ngx_http_scgi_commands
, /* module directives */
331 NGX_HTTP_MODULE
, /* module type */
332 NULL
, /* init master */
333 NULL
, /* init module */
334 NULL
, /* init process */
335 NULL
, /* init thread */
336 NULL
, /* exit thread */
337 NULL
, /* exit process */
338 NULL
, /* exit master */
339 NGX_MODULE_V1_PADDING
343 static ngx_str_t ngx_http_scgi_hide_headers
[] = {
344 ngx_string("Status"),
345 ngx_string("X-Accel-Expires"),
346 ngx_string("X-Accel-Redirect"),
347 ngx_string("X-Accel-Limit-Rate"),
348 ngx_string("X-Accel-Buffering"),
349 ngx_string("X-Accel-Charset"),
356 static ngx_keyval_t ngx_http_scgi_cache_headers
[] = {
357 { ngx_string("HTTP_IF_MODIFIED_SINCE"), ngx_string("") },
358 { ngx_string("HTTP_IF_UNMODIFIED_SINCE"), ngx_string("") },
359 { ngx_string("HTTP_IF_NONE_MATCH"), ngx_string("") },
360 { ngx_string("HTTP_IF_MATCH"), ngx_string("") },
361 { ngx_string("HTTP_RANGE"), ngx_string("") },
362 { ngx_string("HTTP_IF_RANGE"), ngx_string("") },
363 { ngx_null_string
, ngx_null_string
}
369 static ngx_path_init_t ngx_http_scgi_temp_path
= {
370 ngx_string(NGX_HTTP_SCGI_TEMP_PATH
), { 1, 2, 0 }
375 ngx_http_scgi_handler(ngx_http_request_t
*r
)
378 ngx_http_status_t
*status
;
379 ngx_http_upstream_t
*u
;
380 ngx_http_scgi_loc_conf_t
*scf
;
382 if (r
->subrequest_in_memory
) {
383 ngx_log_error(NGX_LOG_ALERT
, r
->connection
->log
, 0,
384 "ngx_http_scgi_module does not support "
385 "subrequests in memory");
386 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
389 if (ngx_http_upstream_create(r
) != NGX_OK
) {
390 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
393 status
= ngx_pcalloc(r
->pool
, sizeof(ngx_http_status_t
));
394 if (status
== NULL
) {
395 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
398 ngx_http_set_ctx(r
, status
, ngx_http_scgi_module
);
400 scf
= ngx_http_get_module_loc_conf(r
, ngx_http_scgi_module
);
402 if (scf
->scgi_lengths
) {
403 if (ngx_http_scgi_eval(r
, scf
) != NGX_OK
) {
404 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
410 ngx_str_set(&u
->schema
, "scgi://");
411 u
->output
.tag
= (ngx_buf_tag_t
) &ngx_http_scgi_module
;
413 u
->conf
= &scf
->upstream
;
416 u
->create_key
= ngx_http_scgi_create_key
;
418 u
->create_request
= ngx_http_scgi_create_request
;
419 u
->reinit_request
= ngx_http_scgi_reinit_request
;
420 u
->process_header
= ngx_http_scgi_process_status_line
;
421 u
->abort_request
= ngx_http_scgi_abort_request
;
422 u
->finalize_request
= ngx_http_scgi_finalize_request
;
424 u
->buffering
= scf
->upstream
.buffering
;
426 u
->pipe
= ngx_pcalloc(r
->pool
, sizeof(ngx_event_pipe_t
));
427 if (u
->pipe
== NULL
) {
428 return NGX_HTTP_INTERNAL_SERVER_ERROR
;
431 u
->pipe
->input_filter
= ngx_event_pipe_copy_input_filter
;
432 u
->pipe
->input_ctx
= r
;
434 rc
= ngx_http_read_client_request_body(r
, ngx_http_upstream_init
);
436 if (rc
>= NGX_HTTP_SPECIAL_RESPONSE
) {
445 ngx_http_scgi_eval(ngx_http_request_t
*r
, ngx_http_scgi_loc_conf_t
* scf
)
448 ngx_http_upstream_t
*u
;
450 ngx_memzero(&url
, sizeof(ngx_url_t
));
452 if (ngx_http_script_run(r
, &url
.url
, scf
->scgi_lengths
->elts
, 0,
453 scf
->scgi_values
->elts
)
461 if (ngx_parse_url(r
->pool
, &url
) != NGX_OK
) {
463 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
464 "%s in upstream \"%V\"", url
.err
, &url
.url
);
472 u
->resolved
= ngx_pcalloc(r
->pool
, sizeof(ngx_http_upstream_resolved_t
));
473 if (u
->resolved
== NULL
) {
477 if (url
.addrs
&& url
.addrs
[0].sockaddr
) {
478 u
->resolved
->sockaddr
= url
.addrs
[0].sockaddr
;
479 u
->resolved
->socklen
= url
.addrs
[0].socklen
;
480 u
->resolved
->naddrs
= 1;
481 u
->resolved
->host
= url
.addrs
[0].name
;
484 u
->resolved
->host
= url
.host
;
485 u
->resolved
->port
= url
.port
;
486 u
->resolved
->no_port
= url
.no_port
;
496 ngx_http_scgi_create_key(ngx_http_request_t
*r
)
499 ngx_http_scgi_loc_conf_t
*scf
;
501 key
= ngx_array_push(&r
->cache
->keys
);
506 scf
= ngx_http_get_module_loc_conf(r
, ngx_http_scgi_module
);
508 if (ngx_http_complex_value(r
, &scf
->cache_key
, key
) != NGX_OK
) {
519 ngx_http_scgi_create_request(ngx_http_request_t
*r
)
521 u_char ch
, *key
, *val
, *lowcase_key
;
522 size_t len
, key_len
, val_len
, allocated
;
524 ngx_str_t
*content_length
;
525 ngx_uint_t i
, n
, hash
, skip_empty
, header_params
;
526 ngx_chain_t
*cl
, *body
;
527 ngx_list_part_t
*part
;
528 ngx_table_elt_t
*header
, **ignored
;
529 ngx_http_script_code_pt code
;
530 ngx_http_script_engine_t e
, le
;
531 ngx_http_scgi_loc_conf_t
*scf
;
532 ngx_http_script_len_code_pt lcode
;
533 static ngx_str_t zero
= ngx_string("0");
535 content_length
= r
->headers_in
.content_length
?
536 &r
->headers_in
.content_length
->value
: &zero
;
538 len
= sizeof("CONTENT_LENGTH") + content_length
->len
+ 1;
543 scf
= ngx_http_get_module_loc_conf(r
, ngx_http_scgi_module
);
545 if (scf
->params_len
) {
546 ngx_memzero(&le
, sizeof(ngx_http_script_engine_t
));
548 ngx_http_script_flush_no_cacheable_variables(r
, scf
->flushes
);
551 le
.ip
= scf
->params_len
->elts
;
554 while (*(uintptr_t *) le
.ip
) {
556 lcode
= *(ngx_http_script_len_code_pt
*) le
.ip
;
557 key_len
= lcode(&le
);
559 lcode
= *(ngx_http_script_len_code_pt
*) le
.ip
;
560 skip_empty
= lcode(&le
);
562 for (val_len
= 0; *(uintptr_t *) le
.ip
; val_len
+= lcode(&le
)) {
563 lcode
= *(ngx_http_script_len_code_pt
*) le
.ip
;
565 le
.ip
+= sizeof(uintptr_t);
567 if (skip_empty
&& val_len
== 0) {
571 len
+= key_len
+ val_len
+ 1;
575 if (scf
->upstream
.pass_request_headers
) {
580 if (scf
->header_params
) {
582 part
= &r
->headers_in
.headers
.part
;
589 ignored
= ngx_palloc(r
->pool
, n
* sizeof(void *));
590 if (ignored
== NULL
) {
595 part
= &r
->headers_in
.headers
.part
;
598 for (i
= 0; /* void */; i
++) {
600 if (i
>= part
->nelts
) {
601 if (part
->next
== NULL
) {
610 if (scf
->header_params
) {
611 if (allocated
< header
[i
].key
.len
) {
612 allocated
= header
[i
].key
.len
+ 16;
613 lowcase_key
= ngx_pnalloc(r
->pool
, allocated
);
614 if (lowcase_key
== NULL
) {
621 for (n
= 0; n
< header
[i
].key
.len
; n
++) {
622 ch
= header
[i
].key
.data
[n
];
624 if (ch
>= 'A' && ch
<= 'Z') {
627 } else if (ch
== '-') {
631 hash
= ngx_hash(hash
, ch
);
635 if (ngx_hash_find(&scf
->headers_hash
, hash
, lowcase_key
, n
)) {
636 ignored
[header_params
++] = &header
[i
];
641 len
+= sizeof("HTTP_") - 1 + header
[i
].key
.len
+ 1
642 + header
[i
].value
.len
+ 1;
646 /* netstring: "length:" + packet + "," */
648 b
= ngx_create_temp_buf(r
->pool
, NGX_SIZE_T_LEN
+ 1 + len
+ 1);
653 cl
= ngx_alloc_chain_link(r
->pool
);
660 b
->last
= ngx_snprintf(b
->last
,
661 NGX_SIZE_T_LEN
+ 1 + sizeof("CONTENT_LENGTH")
663 "%ui:CONTENT_LENGTH%Z%V%Z",
664 len
, content_length
);
666 if (scf
->params_len
) {
667 ngx_memzero(&e
, sizeof(ngx_http_script_engine_t
));
669 e
.ip
= scf
->params
->elts
;
674 le
.ip
= scf
->params_len
->elts
;
676 while (*(uintptr_t *) le
.ip
) {
678 lcode
= *(ngx_http_script_len_code_pt
*) le
.ip
;
679 lcode(&le
); /* key length */
681 lcode
= *(ngx_http_script_len_code_pt
*) le
.ip
;
682 skip_empty
= lcode(&le
);
684 for (val_len
= 0; *(uintptr_t *) le
.ip
; val_len
+= lcode(&le
)) {
685 lcode
= *(ngx_http_script_len_code_pt
*) le
.ip
;
687 le
.ip
+= sizeof(uintptr_t);
689 if (skip_empty
&& val_len
== 0) {
692 while (*(uintptr_t *) e
.ip
) {
693 code
= *(ngx_http_script_code_pt
*) e
.ip
;
694 code((ngx_http_script_engine_t
*) &e
);
696 e
.ip
+= sizeof(uintptr_t);
706 code
= *(ngx_http_script_code_pt
*) e
.ip
;
707 code((ngx_http_script_engine_t
*) & e
);
712 while (*(uintptr_t *) e
.ip
) {
713 code
= *(ngx_http_script_code_pt
*) e
.ip
;
714 code((ngx_http_script_engine_t
*) &e
);
717 e
.ip
+= sizeof(uintptr_t);
719 ngx_log_debug2(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
720 "scgi param: \"%s: %s\"", key
, val
);
726 if (scf
->upstream
.pass_request_headers
) {
728 part
= &r
->headers_in
.headers
.part
;
731 for (i
= 0; /* void */; i
++) {
733 if (i
>= part
->nelts
) {
734 if (part
->next
== NULL
) {
743 for (n
= 0; n
< header_params
; n
++) {
744 if (&header
[i
] == ignored
[n
]) {
750 b
->last
= ngx_cpymem(key
, "HTTP_", sizeof("HTTP_") - 1);
752 for (n
= 0; n
< header
[i
].key
.len
; n
++) {
753 ch
= header
[i
].key
.data
[n
];
755 if (ch
>= 'a' && ch
<= 'z') {
758 } else if (ch
== '-') {
765 *b
->last
++ = (u_char
) 0;
768 b
->last
= ngx_copy(val
, header
[i
].value
.data
, header
[i
].value
.len
);
769 *b
->last
++ = (u_char
) 0;
771 ngx_log_debug2(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
772 "scgi param: \"%s: %s\"", key
, val
);
780 *b
->last
++ = (u_char
) ',';
782 if (scf
->upstream
.pass_request_body
) {
783 body
= r
->upstream
->request_bufs
;
784 r
->upstream
->request_bufs
= cl
;
787 b
= ngx_alloc_buf(r
->pool
);
792 ngx_memcpy(b
, body
->buf
, sizeof(ngx_buf_t
));
794 cl
->next
= ngx_alloc_chain_link(r
->pool
);
795 if (cl
->next
== NULL
) {
806 r
->upstream
->request_bufs
= cl
;
816 ngx_http_scgi_reinit_request(ngx_http_request_t
*r
)
818 ngx_http_status_t
*status
;
820 status
= ngx_http_get_module_ctx(r
, ngx_http_scgi_module
);
822 if (status
== NULL
) {
828 status
->start
= NULL
;
831 r
->upstream
->process_header
= ngx_http_scgi_process_status_line
;
838 ngx_http_scgi_process_status_line(ngx_http_request_t
*r
)
842 ngx_http_status_t
*status
;
843 ngx_http_upstream_t
*u
;
845 status
= ngx_http_get_module_ctx(r
, ngx_http_scgi_module
);
847 if (status
== NULL
) {
853 rc
= ngx_http_parse_status_line(r
, &u
->buffer
, status
);
855 if (rc
== NGX_AGAIN
) {
859 if (rc
== NGX_ERROR
) {
861 r
->http_version
= NGX_HTTP_VERSION_9
;
863 u
->process_header
= ngx_http_scgi_process_header
;
865 return ngx_http_scgi_process_header(r
);
869 u
->state
->status
= status
->code
;
872 u
->headers_in
.status_n
= status
->code
;
874 len
= status
->end
- status
->start
;
875 u
->headers_in
.status_line
.len
= len
;
877 u
->headers_in
.status_line
.data
= ngx_pnalloc(r
->pool
, len
);
878 if (u
->headers_in
.status_line
.data
== NULL
) {
882 ngx_memcpy(u
->headers_in
.status_line
.data
, status
->start
, len
);
884 ngx_log_debug2(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
885 "http scgi status %ui \"%V\"",
886 u
->headers_in
.status_n
, &u
->headers_in
.status_line
);
888 u
->process_header
= ngx_http_scgi_process_header
;
890 return ngx_http_scgi_process_header(r
);
895 ngx_http_scgi_process_header(ngx_http_request_t
*r
)
897 ngx_str_t
*status_line
;
898 ngx_int_t rc
, status
;
900 ngx_http_upstream_t
*u
;
901 ngx_http_upstream_header_t
*hh
;
902 ngx_http_upstream_main_conf_t
*umcf
;
904 umcf
= ngx_http_get_module_main_conf(r
, ngx_http_upstream_module
);
908 rc
= ngx_http_parse_header_line(r
, &r
->upstream
->buffer
, 1);
912 /* a header line has been parsed successfully */
914 h
= ngx_list_push(&r
->upstream
->headers_in
.headers
);
919 h
->hash
= r
->header_hash
;
921 h
->key
.len
= r
->header_name_end
- r
->header_name_start
;
922 h
->value
.len
= r
->header_end
- r
->header_start
;
924 h
->key
.data
= ngx_pnalloc(r
->pool
,
925 h
->key
.len
+ 1 + h
->value
.len
+ 1
927 if (h
->key
.data
== NULL
) {
931 h
->value
.data
= h
->key
.data
+ h
->key
.len
+ 1;
932 h
->lowcase_key
= h
->key
.data
+ h
->key
.len
+ 1 + h
->value
.len
+ 1;
934 ngx_cpystrn(h
->key
.data
, r
->header_name_start
, h
->key
.len
+ 1);
935 ngx_cpystrn(h
->value
.data
, r
->header_start
, h
->value
.len
+ 1);
937 if (h
->key
.len
== r
->lowcase_index
) {
938 ngx_memcpy(h
->lowcase_key
, r
->lowcase_header
, h
->key
.len
);
941 ngx_strlow(h
->lowcase_key
, h
->key
.data
, h
->key
.len
);
944 hh
= ngx_hash_find(&umcf
->headers_in_hash
, h
->hash
,
945 h
->lowcase_key
, h
->key
.len
);
947 if (hh
&& hh
->handler(r
, h
, hh
->offset
) != NGX_OK
) {
951 ngx_log_debug2(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
952 "http scgi header: \"%V: %V\"", &h
->key
, &h
->value
);
957 if (rc
== NGX_HTTP_PARSE_HEADER_DONE
) {
959 /* a whole header has been parsed successfully */
961 ngx_log_debug0(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
962 "http scgi header done");
964 if (r
->http_version
> NGX_HTTP_VERSION_9
) {
970 if (u
->headers_in
.status
) {
971 status_line
= &u
->headers_in
.status
->value
;
973 status
= ngx_atoi(status_line
->data
, 3);
974 if (status
== NGX_ERROR
) {
975 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
976 "upstream sent invalid status \"%V\"",
978 return NGX_HTTP_UPSTREAM_INVALID_HEADER
;
981 r
->http_version
= NGX_HTTP_VERSION_10
;
982 u
->headers_in
.status_n
= status
;
983 u
->headers_in
.status_line
= *status_line
;
985 } else if (u
->headers_in
.location
) {
986 r
->http_version
= NGX_HTTP_VERSION_10
;
987 u
->headers_in
.status_n
= 302;
988 ngx_str_set(&u
->headers_in
.status_line
,
989 "302 Moved Temporarily");
992 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
993 "upstream sent neither valid HTTP/1.0 header "
994 "nor \"Status\" header line");
995 u
->headers_in
.status_n
= 200;
996 ngx_str_set(&u
->headers_in
.status_line
, "200 OK");
1000 u
->state
->status
= u
->headers_in
.status_n
;
1006 if (rc
== NGX_AGAIN
) {
1010 /* there was error while a header line parsing */
1012 ngx_log_error(NGX_LOG_ERR
, r
->connection
->log
, 0,
1013 "upstream sent invalid header");
1015 return NGX_HTTP_UPSTREAM_INVALID_HEADER
;
1021 ngx_http_scgi_abort_request(ngx_http_request_t
*r
)
1023 ngx_log_debug0(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
1024 "abort http scgi request");
1031 ngx_http_scgi_finalize_request(ngx_http_request_t
*r
, ngx_int_t rc
)
1033 ngx_log_debug0(NGX_LOG_DEBUG_HTTP
, r
->connection
->log
, 0,
1034 "finalize http scgi request");
1041 ngx_http_scgi_create_loc_conf(ngx_conf_t
*cf
)
1043 ngx_http_scgi_loc_conf_t
*conf
;
1045 conf
= ngx_pcalloc(cf
->pool
, sizeof(ngx_http_scgi_loc_conf_t
));
1050 conf
->upstream
.store
= NGX_CONF_UNSET
;
1051 conf
->upstream
.store_access
= NGX_CONF_UNSET_UINT
;
1052 conf
->upstream
.buffering
= NGX_CONF_UNSET
;
1053 conf
->upstream
.ignore_client_abort
= NGX_CONF_UNSET
;
1055 conf
->upstream
.connect_timeout
= NGX_CONF_UNSET_MSEC
;
1056 conf
->upstream
.send_timeout
= NGX_CONF_UNSET_MSEC
;
1057 conf
->upstream
.read_timeout
= NGX_CONF_UNSET_MSEC
;
1059 conf
->upstream
.send_lowat
= NGX_CONF_UNSET_SIZE
;
1060 conf
->upstream
.buffer_size
= NGX_CONF_UNSET_SIZE
;
1062 conf
->upstream
.busy_buffers_size_conf
= NGX_CONF_UNSET_SIZE
;
1063 conf
->upstream
.max_temp_file_size_conf
= NGX_CONF_UNSET_SIZE
;
1064 conf
->upstream
.temp_file_write_size_conf
= NGX_CONF_UNSET_SIZE
;
1066 conf
->upstream
.pass_request_headers
= NGX_CONF_UNSET
;
1067 conf
->upstream
.pass_request_body
= NGX_CONF_UNSET
;
1069 #if (NGX_HTTP_CACHE)
1070 conf
->upstream
.cache
= NGX_CONF_UNSET_PTR
;
1071 conf
->upstream
.cache_min_uses
= NGX_CONF_UNSET_UINT
;
1072 conf
->upstream
.cache_bypass
= NGX_CONF_UNSET_PTR
;
1073 conf
->upstream
.no_cache
= NGX_CONF_UNSET_PTR
;
1074 conf
->upstream
.cache_valid
= NGX_CONF_UNSET_PTR
;
1077 conf
->upstream
.hide_headers
= NGX_CONF_UNSET_PTR
;
1078 conf
->upstream
.pass_headers
= NGX_CONF_UNSET_PTR
;
1080 conf
->upstream
.intercept_errors
= NGX_CONF_UNSET
;
1082 /* "scgi_cyclic_temp_file" is disabled */
1083 conf
->upstream
.cyclic_temp_file
= 0;
1085 conf
->upstream
.change_buffering
= 1;
1087 ngx_str_set(&conf
->upstream
.module
, "scgi");
1094 ngx_http_scgi_merge_loc_conf(ngx_conf_t
*cf
, void *parent
, void *child
)
1096 ngx_http_scgi_loc_conf_t
*prev
= parent
;
1097 ngx_http_scgi_loc_conf_t
*conf
= child
;
1100 ngx_hash_init_t hash
;
1101 ngx_http_core_loc_conf_t
*clcf
;
1103 if (conf
->upstream
.store
!= 0) {
1104 ngx_conf_merge_value(conf
->upstream
.store
, prev
->upstream
.store
, 0);
1106 if (conf
->upstream
.store_lengths
== NULL
) {
1107 conf
->upstream
.store_lengths
= prev
->upstream
.store_lengths
;
1108 conf
->upstream
.store_values
= prev
->upstream
.store_values
;
1112 ngx_conf_merge_uint_value(conf
->upstream
.store_access
,
1113 prev
->upstream
.store_access
, 0600);
1115 ngx_conf_merge_value(conf
->upstream
.buffering
,
1116 prev
->upstream
.buffering
, 1);
1118 ngx_conf_merge_value(conf
->upstream
.ignore_client_abort
,
1119 prev
->upstream
.ignore_client_abort
, 0);
1121 ngx_conf_merge_msec_value(conf
->upstream
.connect_timeout
,
1122 prev
->upstream
.connect_timeout
, 60000);
1124 ngx_conf_merge_msec_value(conf
->upstream
.send_timeout
,
1125 prev
->upstream
.send_timeout
, 60000);
1127 ngx_conf_merge_msec_value(conf
->upstream
.read_timeout
,
1128 prev
->upstream
.read_timeout
, 60000);
1130 ngx_conf_merge_size_value(conf
->upstream
.send_lowat
,
1131 prev
->upstream
.send_lowat
, 0);
1133 ngx_conf_merge_size_value(conf
->upstream
.buffer_size
,
1134 prev
->upstream
.buffer_size
,
1135 (size_t) ngx_pagesize
);
1138 ngx_conf_merge_bufs_value(conf
->upstream
.bufs
, prev
->upstream
.bufs
,
1141 if (conf
->upstream
.bufs
.num
< 2) {
1142 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1143 "there must be at least 2 \"scgi_buffers\"");
1144 return NGX_CONF_ERROR
;
1148 size
= conf
->upstream
.buffer_size
;
1149 if (size
< conf
->upstream
.bufs
.size
) {
1150 size
= conf
->upstream
.bufs
.size
;
1154 ngx_conf_merge_size_value(conf
->upstream
.busy_buffers_size_conf
,
1155 prev
->upstream
.busy_buffers_size_conf
,
1156 NGX_CONF_UNSET_SIZE
);
1158 if (conf
->upstream
.busy_buffers_size_conf
== NGX_CONF_UNSET_SIZE
) {
1159 conf
->upstream
.busy_buffers_size
= 2 * size
;
1161 conf
->upstream
.busy_buffers_size
=
1162 conf
->upstream
.busy_buffers_size_conf
;
1165 if (conf
->upstream
.busy_buffers_size
< size
) {
1166 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1167 "\"scgi_busy_buffers_size\" must be equal or bigger "
1168 "than maximum of the value of \"scgi_buffer_size\" and "
1169 "one of the \"scgi_buffers\"");
1171 return NGX_CONF_ERROR
;
1174 if (conf
->upstream
.busy_buffers_size
1175 > (conf
->upstream
.bufs
.num
- 1) * conf
->upstream
.bufs
.size
)
1177 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1178 "\"scgi_busy_buffers_size\" must be less than "
1179 "the size of all \"scgi_buffers\" minus one buffer");
1181 return NGX_CONF_ERROR
;
1185 ngx_conf_merge_size_value(conf
->upstream
.temp_file_write_size_conf
,
1186 prev
->upstream
.temp_file_write_size_conf
,
1187 NGX_CONF_UNSET_SIZE
);
1189 if (conf
->upstream
.temp_file_write_size_conf
== NGX_CONF_UNSET_SIZE
) {
1190 conf
->upstream
.temp_file_write_size
= 2 * size
;
1192 conf
->upstream
.temp_file_write_size
=
1193 conf
->upstream
.temp_file_write_size_conf
;
1196 if (conf
->upstream
.temp_file_write_size
< size
) {
1197 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1198 "\"scgi_temp_file_write_size\" must be equal or bigger than "
1199 "maximum of the value of \"scgi_buffer_size\" and "
1200 "one of the \"scgi_buffers\"");
1202 return NGX_CONF_ERROR
;
1206 ngx_conf_merge_size_value(conf
->upstream
.max_temp_file_size_conf
,
1207 prev
->upstream
.max_temp_file_size_conf
,
1208 NGX_CONF_UNSET_SIZE
);
1210 if (conf
->upstream
.max_temp_file_size_conf
== NGX_CONF_UNSET_SIZE
) {
1211 conf
->upstream
.max_temp_file_size
= 1024 * 1024 * 1024;
1213 conf
->upstream
.max_temp_file_size
=
1214 conf
->upstream
.max_temp_file_size_conf
;
1217 if (conf
->upstream
.max_temp_file_size
!= 0
1218 && conf
->upstream
.max_temp_file_size
< size
) {
1219 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1220 "\"scgi_max_temp_file_size\" must be equal to zero to disable "
1221 "the temporary files usage or must be equal or bigger than "
1222 "maximum of the value of \"scgi_buffer_size\" and "
1223 "one of the \"scgi_buffers\"");
1225 return NGX_CONF_ERROR
;
1229 ngx_conf_merge_bitmask_value(conf
->upstream
.ignore_headers
,
1230 prev
->upstream
.ignore_headers
,
1231 NGX_CONF_BITMASK_SET
);
1234 ngx_conf_merge_bitmask_value(conf
->upstream
.next_upstream
,
1235 prev
->upstream
.next_upstream
,
1236 (NGX_CONF_BITMASK_SET
1237 |NGX_HTTP_UPSTREAM_FT_ERROR
1238 |NGX_HTTP_UPSTREAM_FT_TIMEOUT
));
1240 if (conf
->upstream
.next_upstream
& NGX_HTTP_UPSTREAM_FT_OFF
) {
1241 conf
->upstream
.next_upstream
= NGX_CONF_BITMASK_SET
1242 |NGX_HTTP_UPSTREAM_FT_OFF
;
1245 if (ngx_conf_merge_path_value(cf
, &conf
->upstream
.temp_path
,
1246 prev
->upstream
.temp_path
,
1247 &ngx_http_scgi_temp_path
)
1250 return NGX_CONF_ERROR
;
1253 #if (NGX_HTTP_CACHE)
1255 ngx_conf_merge_ptr_value(conf
->upstream
.cache
,
1256 prev
->upstream
.cache
, NULL
);
1258 if (conf
->upstream
.cache
&& conf
->upstream
.cache
->data
== NULL
) {
1259 ngx_shm_zone_t
*shm_zone
;
1261 shm_zone
= conf
->upstream
.cache
;
1263 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1264 "\"scgi_cache\" zone \"%V\" is unknown",
1265 &shm_zone
->shm
.name
);
1267 return NGX_CONF_ERROR
;
1270 ngx_conf_merge_uint_value(conf
->upstream
.cache_min_uses
,
1271 prev
->upstream
.cache_min_uses
, 1);
1273 ngx_conf_merge_bitmask_value(conf
->upstream
.cache_use_stale
,
1274 prev
->upstream
.cache_use_stale
,
1275 (NGX_CONF_BITMASK_SET
1276 |NGX_HTTP_UPSTREAM_FT_OFF
));
1278 if (conf
->upstream
.cache_use_stale
& NGX_HTTP_UPSTREAM_FT_OFF
) {
1279 conf
->upstream
.cache_use_stale
= NGX_CONF_BITMASK_SET
1280 |NGX_HTTP_UPSTREAM_FT_OFF
;
1283 if (conf
->upstream
.cache_methods
== 0) {
1284 conf
->upstream
.cache_methods
= prev
->upstream
.cache_methods
;
1287 conf
->upstream
.cache_methods
|= NGX_HTTP_GET
|NGX_HTTP_HEAD
;
1289 ngx_conf_merge_ptr_value(conf
->upstream
.cache_bypass
,
1290 prev
->upstream
.cache_bypass
, NULL
);
1292 ngx_conf_merge_ptr_value(conf
->upstream
.no_cache
,
1293 prev
->upstream
.no_cache
, NULL
);
1295 ngx_conf_merge_ptr_value(conf
->upstream
.cache_valid
,
1296 prev
->upstream
.cache_valid
, NULL
);
1298 if (conf
->cache_key
.value
.data
== NULL
) {
1299 conf
->cache_key
= prev
->cache_key
;
1304 ngx_conf_merge_value(conf
->upstream
.pass_request_headers
,
1305 prev
->upstream
.pass_request_headers
, 1);
1306 ngx_conf_merge_value(conf
->upstream
.pass_request_body
,
1307 prev
->upstream
.pass_request_body
, 1);
1309 ngx_conf_merge_value(conf
->upstream
.intercept_errors
,
1310 prev
->upstream
.intercept_errors
, 0);
1312 hash
.max_size
= 512;
1313 hash
.bucket_size
= ngx_align(64, ngx_cacheline_size
);
1314 hash
.name
= "scgi_hide_headers_hash";
1316 if (ngx_http_upstream_hide_headers_hash(cf
, &conf
->upstream
,
1317 &prev
->upstream
, ngx_http_scgi_hide_headers
, &hash
)
1320 return NGX_CONF_ERROR
;
1323 if (conf
->upstream
.upstream
== NULL
) {
1324 conf
->upstream
.upstream
= prev
->upstream
.upstream
;
1327 if (conf
->scgi_lengths
== NULL
) {
1328 conf
->scgi_lengths
= prev
->scgi_lengths
;
1329 conf
->scgi_values
= prev
->scgi_values
;
1332 if (conf
->upstream
.upstream
|| conf
->scgi_lengths
) {
1333 clcf
= ngx_http_conf_get_module_loc_conf(cf
, ngx_http_core_module
);
1334 if (clcf
->handler
== NULL
&& clcf
->lmt_excpt
) {
1335 clcf
->handler
= ngx_http_scgi_handler
;
1339 if (ngx_http_scgi_merge_params(cf
, conf
, prev
) != NGX_OK
) {
1340 return NGX_CONF_ERROR
;
1348 ngx_http_scgi_merge_params(ngx_conf_t
*cf
, ngx_http_scgi_loc_conf_t
*conf
,
1349 ngx_http_scgi_loc_conf_t
*prev
)
1355 ngx_array_t headers_names
;
1356 #if (NGX_HTTP_CACHE)
1357 ngx_array_t params_merged
;
1360 ngx_hash_init_t hash
;
1361 ngx_http_upstream_param_t
*src
;
1362 ngx_http_script_compile_t sc
;
1363 ngx_http_script_copy_code_t
*copy
;
1365 if (conf
->params_source
== NULL
) {
1366 conf
->params_source
= prev
->params_source
;
1368 if (prev
->headers_hash
.buckets
1369 #if (NGX_HTTP_CACHE)
1370 && ((conf
->upstream
.cache
== NULL
) == (prev
->upstream
.cache
== NULL
))
1374 conf
->flushes
= prev
->flushes
;
1375 conf
->params_len
= prev
->params_len
;
1376 conf
->params
= prev
->params
;
1377 conf
->headers_hash
= prev
->headers_hash
;
1378 conf
->header_params
= prev
->header_params
;
1384 if (conf
->params_source
== NULL
1385 #if (NGX_HTTP_CACHE)
1386 && (conf
->upstream
.cache
== NULL
)
1390 conf
->headers_hash
.buckets
= (void *) 1;
1394 conf
->params_len
= ngx_array_create(cf
->pool
, 64, 1);
1395 if (conf
->params_len
== NULL
) {
1399 conf
->params
= ngx_array_create(cf
->pool
, 512, 1);
1400 if (conf
->params
== NULL
) {
1404 if (ngx_array_init(&headers_names
, cf
->temp_pool
, 4, sizeof(ngx_hash_key_t
))
1410 if (conf
->params_source
) {
1411 src
= conf
->params_source
->elts
;
1412 nsrc
= conf
->params_source
->nelts
;
1419 #if (NGX_HTTP_CACHE)
1421 if (conf
->upstream
.cache
) {
1423 ngx_http_upstream_param_t
*s
;
1425 if (ngx_array_init(¶ms_merged
, cf
->temp_pool
, 4,
1426 sizeof(ngx_http_upstream_param_t
))
1432 for (i
= 0; i
< nsrc
; i
++) {
1434 s
= ngx_array_push(¶ms_merged
);
1442 h
= ngx_http_scgi_cache_headers
;
1444 while (h
->key
.len
) {
1446 src
= params_merged
.elts
;
1447 nsrc
= params_merged
.nelts
;
1449 for (i
= 0; i
< nsrc
; i
++) {
1450 if (ngx_strcasecmp(h
->key
.data
, src
[i
].key
.data
) == 0) {
1455 s
= ngx_array_push(¶ms_merged
);
1461 s
->value
= h
->value
;
1469 src
= params_merged
.elts
;
1470 nsrc
= params_merged
.nelts
;
1475 for (i
= 0; i
< nsrc
; i
++) {
1477 if (src
[i
].key
.len
> sizeof("HTTP_") - 1
1478 && ngx_strncmp(src
[i
].key
.data
, "HTTP_", sizeof("HTTP_") - 1) == 0)
1480 hk
= ngx_array_push(&headers_names
);
1485 hk
->key
.len
= src
[i
].key
.len
- 5;
1486 hk
->key
.data
= src
[i
].key
.data
+ 5;
1487 hk
->key_hash
= ngx_hash_key_lc(hk
->key
.data
, hk
->key
.len
);
1488 hk
->value
= (void *) 1;
1490 if (src
[i
].value
.len
== 0) {
1495 copy
= ngx_array_push_n(conf
->params_len
,
1496 sizeof(ngx_http_script_copy_code_t
));
1501 copy
->code
= (ngx_http_script_code_pt
) ngx_http_script_copy_len_code
;
1502 copy
->len
= src
[i
].key
.len
+ 1;
1504 copy
= ngx_array_push_n(conf
->params_len
,
1505 sizeof(ngx_http_script_copy_code_t
));
1510 copy
->code
= (ngx_http_script_code_pt
) ngx_http_script_copy_len_code
;
1511 copy
->len
= src
[i
].skip_empty
;
1514 size
= (sizeof(ngx_http_script_copy_code_t
)
1515 + src
[i
].key
.len
+ 1 + sizeof(uintptr_t) - 1)
1516 & ~(sizeof(uintptr_t) - 1);
1518 copy
= ngx_array_push_n(conf
->params
, size
);
1523 copy
->code
= ngx_http_script_copy_code
;
1524 copy
->len
= src
[i
].key
.len
+ 1;
1526 p
= (u_char
*) copy
+ sizeof(ngx_http_script_copy_code_t
);
1527 (void) ngx_cpystrn(p
, src
[i
].key
.data
, src
[i
].key
.len
+ 1);
1530 ngx_memzero(&sc
, sizeof(ngx_http_script_compile_t
));
1533 sc
.source
= &src
[i
].value
;
1534 sc
.flushes
= &conf
->flushes
;
1535 sc
.lengths
= &conf
->params_len
;
1536 sc
.values
= &conf
->params
;
1538 if (ngx_http_script_compile(&sc
) != NGX_OK
) {
1542 code
= ngx_array_push_n(conf
->params_len
, sizeof(uintptr_t));
1547 *code
= (uintptr_t) NULL
;
1550 code
= ngx_array_push_n(conf
->params
, sizeof(uintptr_t));
1555 *code
= (uintptr_t) NULL
;
1558 code
= ngx_array_push_n(conf
->params_len
, sizeof(uintptr_t));
1563 *code
= (uintptr_t) NULL
;
1565 code
= ngx_array_push_n(conf
->params
, sizeof(uintptr_t));
1570 *code
= (uintptr_t) NULL
;
1572 conf
->header_params
= headers_names
.nelts
;
1574 hash
.hash
= &conf
->headers_hash
;
1575 hash
.key
= ngx_hash_key_lc
;
1576 hash
.max_size
= 512;
1577 hash
.bucket_size
= 64;
1578 hash
.name
= "scgi_params_hash";
1579 hash
.pool
= cf
->pool
;
1580 hash
.temp_pool
= NULL
;
1582 return ngx_hash_init(&hash
, headers_names
.elts
, headers_names
.nelts
);
1587 ngx_http_scgi_pass(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
)
1589 ngx_http_scgi_loc_conf_t
*scf
= conf
;
1592 ngx_str_t
*value
, *url
;
1594 ngx_http_core_loc_conf_t
*clcf
;
1595 ngx_http_script_compile_t sc
;
1597 if (scf
->upstream
.upstream
|| scf
->scgi_lengths
) {
1598 return "is duplicate";
1601 clcf
= ngx_http_conf_get_module_loc_conf (cf
, ngx_http_core_module
);
1602 clcf
->handler
= ngx_http_scgi_handler
;
1604 value
= cf
->args
->elts
;
1608 n
= ngx_http_script_variables_count(url
);
1612 ngx_memzero(&sc
, sizeof(ngx_http_script_compile_t
));
1616 sc
.lengths
= &scf
->scgi_lengths
;
1617 sc
.values
= &scf
->scgi_values
;
1619 sc
.complete_lengths
= 1;
1620 sc
.complete_values
= 1;
1622 if (ngx_http_script_compile(&sc
) != NGX_OK
) {
1623 return NGX_CONF_ERROR
;
1629 ngx_memzero(&u
, sizeof(ngx_url_t
));
1634 scf
->upstream
.upstream
= ngx_http_upstream_add(cf
, &u
, 0);
1635 if (scf
->upstream
.upstream
== NULL
) {
1636 return NGX_CONF_ERROR
;
1639 if (clcf
->name
.data
[clcf
->name
.len
- 1] == '/') {
1640 clcf
->auto_redirect
= 1;
1648 ngx_http_scgi_store(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
)
1650 ngx_http_scgi_loc_conf_t
*scf
= conf
;
1653 ngx_http_script_compile_t sc
;
1655 if (scf
->upstream
.store
!= NGX_CONF_UNSET
|| scf
->upstream
.store_lengths
) {
1656 return "is duplicate";
1659 value
= cf
->args
->elts
;
1661 if (ngx_strcmp(value
[1].data
, "off") == 0) {
1662 scf
->upstream
.store
= 0;
1666 #if (NGX_HTTP_CACHE)
1668 if (scf
->upstream
.cache
!= NGX_CONF_UNSET_PTR
1669 && scf
->upstream
.cache
!= NULL
)
1671 return "is incompatible with \"scgi_cache\"";
1676 if (ngx_strcmp(value
[1].data
, "on") == 0) {
1677 scf
->upstream
.store
= 1;
1681 /* include the terminating '\0' into script */
1684 ngx_memzero(&sc
, sizeof(ngx_http_script_compile_t
));
1687 sc
.source
= &value
[1];
1688 sc
.lengths
= &scf
->upstream
.store_lengths
;
1689 sc
.values
= &scf
->upstream
.store_values
;
1690 sc
.variables
= ngx_http_script_variables_count(&value
[1]);;
1691 sc
.complete_lengths
= 1;
1692 sc
.complete_values
= 1;
1694 if (ngx_http_script_compile(&sc
) != NGX_OK
) {
1695 return NGX_CONF_ERROR
;
1702 #if (NGX_HTTP_CACHE)
1705 ngx_http_scgi_cache(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
)
1707 ngx_http_scgi_loc_conf_t
*scf
= conf
;
1711 value
= cf
->args
->elts
;
1713 if (scf
->upstream
.cache
!= NGX_CONF_UNSET_PTR
) {
1714 return "is duplicate";
1717 if (ngx_strcmp(value
[1].data
, "off") == 0) {
1718 scf
->upstream
.cache
= NULL
;
1722 if (scf
->upstream
.store
> 0 || scf
->upstream
.store_lengths
) {
1723 return "is incompatible with \"scgi_store\"";
1726 scf
->upstream
.cache
= ngx_shared_memory_add(cf
, &value
[1], 0,
1727 &ngx_http_scgi_module
);
1728 if (scf
->upstream
.cache
== NULL
) {
1729 return NGX_CONF_ERROR
;
1737 ngx_http_scgi_cache_key(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
)
1739 ngx_http_scgi_loc_conf_t
*scf
= conf
;
1742 ngx_http_compile_complex_value_t ccv
;
1744 value
= cf
->args
->elts
;
1746 if (scf
->cache_key
.value
.len
) {
1747 return "is duplicate";
1750 ngx_memzero(&ccv
, sizeof(ngx_http_compile_complex_value_t
));
1753 ccv
.value
= &value
[1];
1754 ccv
.complex_value
= &scf
->cache_key
;
1756 if (ngx_http_compile_complex_value(&ccv
) != NGX_OK
) {
1757 return NGX_CONF_ERROR
;