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