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