Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.11 ]​[ nginx-1.14.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  * Copyright (C) Manlio Perillo (manlio.perillo@gmail.com)
0006  */
0007 
0008 
0009 #include <ngx_config.h>
0010 #include <ngx_core.h>
0011 #include <ngx_http.h>
0012 
0013 
0014 typedef struct {
0015     ngx_array_t                caches;  /* ngx_http_file_cache_t * */
0016 } ngx_http_scgi_main_conf_t;
0017 
0018 
0019 typedef struct {
0020     ngx_array_t               *flushes;
0021     ngx_array_t               *lengths;
0022     ngx_array_t               *values;
0023     ngx_uint_t                 number;
0024     ngx_hash_t                 hash;
0025 } ngx_http_scgi_params_t;
0026 
0027 
0028 typedef struct {
0029     ngx_http_upstream_conf_t   upstream;
0030 
0031     ngx_http_scgi_params_t     params;
0032 #if (NGX_HTTP_CACHE)
0033     ngx_http_scgi_params_t     params_cache;
0034 #endif
0035     ngx_array_t               *params_source;
0036 
0037     ngx_array_t               *scgi_lengths;
0038     ngx_array_t               *scgi_values;
0039 
0040 #if (NGX_HTTP_CACHE)
0041     ngx_http_complex_value_t   cache_key;
0042 #endif
0043 } ngx_http_scgi_loc_conf_t;
0044 
0045 
0046 static ngx_int_t ngx_http_scgi_eval(ngx_http_request_t *r,
0047     ngx_http_scgi_loc_conf_t *scf);
0048 static ngx_int_t ngx_http_scgi_create_request(ngx_http_request_t *r);
0049 static ngx_int_t ngx_http_scgi_reinit_request(ngx_http_request_t *r);
0050 static ngx_int_t ngx_http_scgi_process_status_line(ngx_http_request_t *r);
0051 static ngx_int_t ngx_http_scgi_process_header(ngx_http_request_t *r);
0052 static void ngx_http_scgi_abort_request(ngx_http_request_t *r);
0053 static void ngx_http_scgi_finalize_request(ngx_http_request_t *r, ngx_int_t rc);
0054 
0055 static void *ngx_http_scgi_create_main_conf(ngx_conf_t *cf);
0056 static void *ngx_http_scgi_create_loc_conf(ngx_conf_t *cf);
0057 static char *ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent,
0058     void *child);
0059 static ngx_int_t ngx_http_scgi_init_params(ngx_conf_t *cf,
0060     ngx_http_scgi_loc_conf_t *conf, ngx_http_scgi_params_t *params,
0061     ngx_keyval_t *default_params);
0062 
0063 static char *ngx_http_scgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
0064 static char *ngx_http_scgi_store(ngx_conf_t *cf, ngx_command_t *cmd,
0065     void *conf);
0066 
0067 #if (NGX_HTTP_CACHE)
0068 static ngx_int_t ngx_http_scgi_create_key(ngx_http_request_t *r);
0069 static char *ngx_http_scgi_cache(ngx_conf_t *cf, ngx_command_t *cmd,
0070     void *conf);
0071 static char *ngx_http_scgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd,
0072     void *conf);
0073 #endif
0074 
0075 
0076 static ngx_conf_bitmask_t ngx_http_scgi_next_upstream_masks[] = {
0077     { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
0078     { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
0079     { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
0080     { ngx_string("non_idempotent"), NGX_HTTP_UPSTREAM_FT_NON_IDEMPOTENT },
0081     { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
0082     { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
0083     { ngx_string("http_403"), NGX_HTTP_UPSTREAM_FT_HTTP_403 },
0084     { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
0085     { ngx_string("http_429"), NGX_HTTP_UPSTREAM_FT_HTTP_429 },
0086     { ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING },
0087     { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
0088     { ngx_null_string, 0 }
0089 };
0090 
0091 
0092 ngx_module_t  ngx_http_scgi_module;
0093 
0094 
0095 static ngx_command_t ngx_http_scgi_commands[] = {
0096 
0097     { ngx_string("scgi_pass"),
0098       NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
0099       ngx_http_scgi_pass,
0100       NGX_HTTP_LOC_CONF_OFFSET,
0101       0,
0102       NULL },
0103 
0104     { ngx_string("scgi_store"),
0105       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0106       ngx_http_scgi_store,
0107       NGX_HTTP_LOC_CONF_OFFSET,
0108       0,
0109       NULL },
0110 
0111     { ngx_string("scgi_store_access"),
0112       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
0113       ngx_conf_set_access_slot,
0114       NGX_HTTP_LOC_CONF_OFFSET,
0115       offsetof(ngx_http_scgi_loc_conf_t, upstream.store_access),
0116       NULL },
0117 
0118     { ngx_string("scgi_buffering"),
0119       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0120       ngx_conf_set_flag_slot,
0121       NGX_HTTP_LOC_CONF_OFFSET,
0122       offsetof(ngx_http_scgi_loc_conf_t, upstream.buffering),
0123       NULL },
0124 
0125     { ngx_string("scgi_request_buffering"),
0126       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0127       ngx_conf_set_flag_slot,
0128       NGX_HTTP_LOC_CONF_OFFSET,
0129       offsetof(ngx_http_scgi_loc_conf_t, upstream.request_buffering),
0130       NULL },
0131 
0132     { ngx_string("scgi_ignore_client_abort"),
0133       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0134       ngx_conf_set_flag_slot,
0135       NGX_HTTP_LOC_CONF_OFFSET,
0136       offsetof(ngx_http_scgi_loc_conf_t, upstream.ignore_client_abort),
0137       NULL },
0138 
0139     { ngx_string("scgi_bind"),
0140       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
0141       ngx_http_upstream_bind_set_slot,
0142       NGX_HTTP_LOC_CONF_OFFSET,
0143       offsetof(ngx_http_scgi_loc_conf_t, upstream.local),
0144       NULL },
0145 
0146     { ngx_string("scgi_socket_keepalive"),
0147       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0148       ngx_conf_set_flag_slot,
0149       NGX_HTTP_LOC_CONF_OFFSET,
0150       offsetof(ngx_http_scgi_loc_conf_t, upstream.socket_keepalive),
0151       NULL },
0152 
0153     { ngx_string("scgi_connect_timeout"),
0154       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0155       ngx_conf_set_msec_slot,
0156       NGX_HTTP_LOC_CONF_OFFSET,
0157       offsetof(ngx_http_scgi_loc_conf_t, upstream.connect_timeout),
0158       NULL },
0159 
0160     { ngx_string("scgi_send_timeout"),
0161       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0162       ngx_conf_set_msec_slot,
0163       NGX_HTTP_LOC_CONF_OFFSET,
0164       offsetof(ngx_http_scgi_loc_conf_t, upstream.send_timeout),
0165       NULL },
0166 
0167     { ngx_string("scgi_buffer_size"),
0168       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0169       ngx_conf_set_size_slot,
0170       NGX_HTTP_LOC_CONF_OFFSET,
0171       offsetof(ngx_http_scgi_loc_conf_t, upstream.buffer_size),
0172       NULL },
0173 
0174     { ngx_string("scgi_pass_request_headers"),
0175       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0176       ngx_conf_set_flag_slot,
0177       NGX_HTTP_LOC_CONF_OFFSET,
0178       offsetof(ngx_http_scgi_loc_conf_t, upstream.pass_request_headers),
0179       NULL },
0180 
0181     { ngx_string("scgi_pass_request_body"),
0182       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0183       ngx_conf_set_flag_slot,
0184       NGX_HTTP_LOC_CONF_OFFSET,
0185       offsetof(ngx_http_scgi_loc_conf_t, upstream.pass_request_body),
0186       NULL },
0187 
0188     { ngx_string("scgi_intercept_errors"),
0189       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0190       ngx_conf_set_flag_slot,
0191       NGX_HTTP_LOC_CONF_OFFSET,
0192       offsetof(ngx_http_scgi_loc_conf_t, upstream.intercept_errors),
0193       NULL },
0194 
0195     { ngx_string("scgi_read_timeout"),
0196       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0197       ngx_conf_set_msec_slot,
0198       NGX_HTTP_LOC_CONF_OFFSET,
0199       offsetof(ngx_http_scgi_loc_conf_t, upstream.read_timeout),
0200       NULL },
0201 
0202     { ngx_string("scgi_buffers"),
0203       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
0204       ngx_conf_set_bufs_slot,
0205       NGX_HTTP_LOC_CONF_OFFSET,
0206       offsetof(ngx_http_scgi_loc_conf_t, upstream.bufs),
0207       NULL },
0208 
0209     { ngx_string("scgi_busy_buffers_size"),
0210       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0211       ngx_conf_set_size_slot,
0212       NGX_HTTP_LOC_CONF_OFFSET,
0213       offsetof(ngx_http_scgi_loc_conf_t, upstream.busy_buffers_size_conf),
0214       NULL },
0215 
0216     { ngx_string("scgi_force_ranges"),
0217       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0218       ngx_conf_set_flag_slot,
0219       NGX_HTTP_LOC_CONF_OFFSET,
0220       offsetof(ngx_http_scgi_loc_conf_t, upstream.force_ranges),
0221       NULL },
0222 
0223     { ngx_string("scgi_limit_rate"),
0224       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0225       ngx_conf_set_size_slot,
0226       NGX_HTTP_LOC_CONF_OFFSET,
0227       offsetof(ngx_http_scgi_loc_conf_t, upstream.limit_rate),
0228       NULL },
0229 
0230 #if (NGX_HTTP_CACHE)
0231 
0232     { ngx_string("scgi_cache"),
0233       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0234       ngx_http_scgi_cache,
0235       NGX_HTTP_LOC_CONF_OFFSET,
0236       0,
0237       NULL },
0238 
0239     { ngx_string("scgi_cache_key"),
0240       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0241       ngx_http_scgi_cache_key,
0242       NGX_HTTP_LOC_CONF_OFFSET,
0243       0,
0244       NULL },
0245 
0246     { ngx_string("scgi_cache_path"),
0247       NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
0248       ngx_http_file_cache_set_slot,
0249       NGX_HTTP_MAIN_CONF_OFFSET,
0250       offsetof(ngx_http_scgi_main_conf_t, caches),
0251       &ngx_http_scgi_module },
0252 
0253     { ngx_string("scgi_cache_bypass"),
0254       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0255       ngx_http_set_predicate_slot,
0256       NGX_HTTP_LOC_CONF_OFFSET,
0257       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_bypass),
0258       NULL },
0259 
0260     { ngx_string("scgi_no_cache"),
0261       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0262       ngx_http_set_predicate_slot,
0263       NGX_HTTP_LOC_CONF_OFFSET,
0264       offsetof(ngx_http_scgi_loc_conf_t, upstream.no_cache),
0265       NULL },
0266 
0267     { ngx_string("scgi_cache_valid"),
0268       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0269       ngx_http_file_cache_valid_set_slot,
0270       NGX_HTTP_LOC_CONF_OFFSET,
0271       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_valid),
0272       NULL },
0273 
0274     { ngx_string("scgi_cache_min_uses"),
0275       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0276       ngx_conf_set_num_slot,
0277       NGX_HTTP_LOC_CONF_OFFSET,
0278       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_min_uses),
0279       NULL },
0280 
0281     { ngx_string("scgi_cache_max_range_offset"),
0282       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0283       ngx_conf_set_off_slot,
0284       NGX_HTTP_LOC_CONF_OFFSET,
0285       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_max_range_offset),
0286       NULL },
0287 
0288     { ngx_string("scgi_cache_use_stale"),
0289       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0290       ngx_conf_set_bitmask_slot,
0291       NGX_HTTP_LOC_CONF_OFFSET,
0292       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_use_stale),
0293       &ngx_http_scgi_next_upstream_masks },
0294 
0295     { ngx_string("scgi_cache_methods"),
0296       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0297       ngx_conf_set_bitmask_slot,
0298       NGX_HTTP_LOC_CONF_OFFSET,
0299       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_methods),
0300       &ngx_http_upstream_cache_method_mask },
0301 
0302     { ngx_string("scgi_cache_lock"),
0303       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0304       ngx_conf_set_flag_slot,
0305       NGX_HTTP_LOC_CONF_OFFSET,
0306       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_lock),
0307       NULL },
0308 
0309     { ngx_string("scgi_cache_lock_timeout"),
0310       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0311       ngx_conf_set_msec_slot,
0312       NGX_HTTP_LOC_CONF_OFFSET,
0313       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_lock_timeout),
0314       NULL },
0315 
0316     { ngx_string("scgi_cache_lock_age"),
0317       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0318       ngx_conf_set_msec_slot,
0319       NGX_HTTP_LOC_CONF_OFFSET,
0320       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_lock_age),
0321       NULL },
0322 
0323     { ngx_string("scgi_cache_revalidate"),
0324       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0325       ngx_conf_set_flag_slot,
0326       NGX_HTTP_LOC_CONF_OFFSET,
0327       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_revalidate),
0328       NULL },
0329 
0330     { ngx_string("scgi_cache_background_update"),
0331       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0332       ngx_conf_set_flag_slot,
0333       NGX_HTTP_LOC_CONF_OFFSET,
0334       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_background_update),
0335       NULL },
0336 
0337 #endif
0338 
0339     { ngx_string("scgi_temp_path"),
0340       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
0341       ngx_conf_set_path_slot,
0342       NGX_HTTP_LOC_CONF_OFFSET,
0343       offsetof(ngx_http_scgi_loc_conf_t, upstream.temp_path),
0344       NULL },
0345 
0346     { ngx_string("scgi_max_temp_file_size"),
0347       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0348       ngx_conf_set_size_slot,
0349       NGX_HTTP_LOC_CONF_OFFSET,
0350       offsetof(ngx_http_scgi_loc_conf_t, upstream.max_temp_file_size_conf),
0351       NULL },
0352 
0353     { ngx_string("scgi_temp_file_write_size"),
0354       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0355       ngx_conf_set_size_slot,
0356       NGX_HTTP_LOC_CONF_OFFSET,
0357       offsetof(ngx_http_scgi_loc_conf_t, upstream.temp_file_write_size_conf),
0358       NULL },
0359 
0360     { ngx_string("scgi_next_upstream"),
0361       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0362       ngx_conf_set_bitmask_slot,
0363       NGX_HTTP_LOC_CONF_OFFSET,
0364       offsetof(ngx_http_scgi_loc_conf_t, upstream.next_upstream),
0365       &ngx_http_scgi_next_upstream_masks },
0366 
0367     { ngx_string("scgi_next_upstream_tries"),
0368       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0369       ngx_conf_set_num_slot,
0370       NGX_HTTP_LOC_CONF_OFFSET,
0371       offsetof(ngx_http_scgi_loc_conf_t, upstream.next_upstream_tries),
0372       NULL },
0373 
0374     { ngx_string("scgi_next_upstream_timeout"),
0375       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0376       ngx_conf_set_msec_slot,
0377       NGX_HTTP_LOC_CONF_OFFSET,
0378       offsetof(ngx_http_scgi_loc_conf_t, upstream.next_upstream_timeout),
0379       NULL },
0380 
0381     { ngx_string("scgi_param"),
0382       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE23,
0383       ngx_http_upstream_param_set_slot,
0384       NGX_HTTP_LOC_CONF_OFFSET,
0385       offsetof(ngx_http_scgi_loc_conf_t, params_source),
0386       NULL },
0387 
0388     { ngx_string("scgi_pass_header"),
0389       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0390       ngx_conf_set_str_array_slot,
0391       NGX_HTTP_LOC_CONF_OFFSET,
0392       offsetof(ngx_http_scgi_loc_conf_t, upstream.pass_headers),
0393       NULL },
0394 
0395     { ngx_string("scgi_hide_header"),
0396       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0397       ngx_conf_set_str_array_slot,
0398       NGX_HTTP_LOC_CONF_OFFSET,
0399       offsetof(ngx_http_scgi_loc_conf_t, upstream.hide_headers),
0400       NULL },
0401 
0402     { ngx_string("scgi_ignore_headers"),
0403       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0404       ngx_conf_set_bitmask_slot,
0405       NGX_HTTP_LOC_CONF_OFFSET,
0406       offsetof(ngx_http_scgi_loc_conf_t, upstream.ignore_headers),
0407       &ngx_http_upstream_ignore_headers_masks },
0408 
0409       ngx_null_command
0410 };
0411 
0412 
0413 static ngx_http_module_t ngx_http_scgi_module_ctx = {
0414     NULL,                                  /* preconfiguration */
0415     NULL,                                  /* postconfiguration */
0416 
0417     ngx_http_scgi_create_main_conf,        /* create main configuration */
0418     NULL,                                  /* init main configuration */
0419 
0420     NULL,                                  /* create server configuration */
0421     NULL,                                  /* merge server configuration */
0422 
0423     ngx_http_scgi_create_loc_conf,         /* create location configuration */
0424     ngx_http_scgi_merge_loc_conf           /* merge location configuration */
0425 };
0426 
0427 
0428 ngx_module_t ngx_http_scgi_module = {
0429     NGX_MODULE_V1,
0430     &ngx_http_scgi_module_ctx,             /* module context */
0431     ngx_http_scgi_commands,                /* module directives */
0432     NGX_HTTP_MODULE,                       /* module type */
0433     NULL,                                  /* init master */
0434     NULL,                                  /* init module */
0435     NULL,                                  /* init process */
0436     NULL,                                  /* init thread */
0437     NULL,                                  /* exit thread */
0438     NULL,                                  /* exit process */
0439     NULL,                                  /* exit master */
0440     NGX_MODULE_V1_PADDING
0441 };
0442 
0443 
0444 static ngx_str_t ngx_http_scgi_hide_headers[] = {
0445     ngx_string("Status"),
0446     ngx_string("X-Accel-Expires"),
0447     ngx_string("X-Accel-Redirect"),
0448     ngx_string("X-Accel-Limit-Rate"),
0449     ngx_string("X-Accel-Buffering"),
0450     ngx_string("X-Accel-Charset"),
0451     ngx_null_string
0452 };
0453 
0454 
0455 #if (NGX_HTTP_CACHE)
0456 
0457 static ngx_keyval_t  ngx_http_scgi_cache_headers[] = {
0458     { ngx_string("HTTP_IF_MODIFIED_SINCE"),
0459       ngx_string("$upstream_cache_last_modified") },
0460     { ngx_string("HTTP_IF_UNMODIFIED_SINCE"), ngx_string("") },
0461     { ngx_string("HTTP_IF_NONE_MATCH"), ngx_string("$upstream_cache_etag") },
0462     { ngx_string("HTTP_IF_MATCH"), ngx_string("") },
0463     { ngx_string("HTTP_RANGE"), ngx_string("") },
0464     { ngx_string("HTTP_IF_RANGE"), ngx_string("") },
0465     { ngx_null_string, ngx_null_string }
0466 };
0467 
0468 #endif
0469 
0470 
0471 static ngx_path_init_t ngx_http_scgi_temp_path = {
0472     ngx_string(NGX_HTTP_SCGI_TEMP_PATH), { 1, 2, 0 }
0473 };
0474 
0475 
0476 static ngx_int_t
0477 ngx_http_scgi_handler(ngx_http_request_t *r)
0478 {
0479     ngx_int_t                   rc;
0480     ngx_http_status_t          *status;
0481     ngx_http_upstream_t        *u;
0482     ngx_http_scgi_loc_conf_t   *scf;
0483 #if (NGX_HTTP_CACHE)
0484     ngx_http_scgi_main_conf_t  *smcf;
0485 #endif
0486 
0487     if (ngx_http_upstream_create(r) != NGX_OK) {
0488         return NGX_HTTP_INTERNAL_SERVER_ERROR;
0489     }
0490 
0491     status = ngx_pcalloc(r->pool, sizeof(ngx_http_status_t));
0492     if (status == NULL) {
0493         return NGX_HTTP_INTERNAL_SERVER_ERROR;
0494     }
0495 
0496     ngx_http_set_ctx(r, status, ngx_http_scgi_module);
0497 
0498     scf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module);
0499 
0500     if (scf->scgi_lengths) {
0501         if (ngx_http_scgi_eval(r, scf) != NGX_OK) {
0502             return NGX_HTTP_INTERNAL_SERVER_ERROR;
0503         }
0504     }
0505 
0506     u = r->upstream;
0507 
0508     ngx_str_set(&u->schema, "scgi://");
0509     u->output.tag = (ngx_buf_tag_t) &ngx_http_scgi_module;
0510 
0511     u->conf = &scf->upstream;
0512 
0513 #if (NGX_HTTP_CACHE)
0514     smcf = ngx_http_get_module_main_conf(r, ngx_http_scgi_module);
0515 
0516     u->caches = &smcf->caches;
0517     u->create_key = ngx_http_scgi_create_key;
0518 #endif
0519 
0520     u->create_request = ngx_http_scgi_create_request;
0521     u->reinit_request = ngx_http_scgi_reinit_request;
0522     u->process_header = ngx_http_scgi_process_status_line;
0523     u->abort_request = ngx_http_scgi_abort_request;
0524     u->finalize_request = ngx_http_scgi_finalize_request;
0525     r->state = 0;
0526 
0527     u->buffering = scf->upstream.buffering;
0528 
0529     u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
0530     if (u->pipe == NULL) {
0531         return NGX_HTTP_INTERNAL_SERVER_ERROR;
0532     }
0533 
0534     u->pipe->input_filter = ngx_event_pipe_copy_input_filter;
0535     u->pipe->input_ctx = r;
0536 
0537     if (!scf->upstream.request_buffering
0538         && scf->upstream.pass_request_body
0539         && !r->headers_in.chunked)
0540     {
0541         r->request_body_no_buffering = 1;
0542     }
0543 
0544     rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
0545 
0546     if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
0547         return rc;
0548     }
0549 
0550     return NGX_DONE;
0551 }
0552 
0553 
0554 static ngx_int_t
0555 ngx_http_scgi_eval(ngx_http_request_t *r, ngx_http_scgi_loc_conf_t * scf)
0556 {
0557     ngx_url_t             url;
0558     ngx_http_upstream_t  *u;
0559 
0560     ngx_memzero(&url, sizeof(ngx_url_t));
0561 
0562     if (ngx_http_script_run(r, &url.url, scf->scgi_lengths->elts, 0,
0563                             scf->scgi_values->elts)
0564         == NULL)
0565     {
0566         return NGX_ERROR;
0567     }
0568 
0569     url.no_resolve = 1;
0570 
0571     if (ngx_parse_url(r->pool, &url) != NGX_OK) {
0572         if (url.err) {
0573             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
0574                           "%s in upstream \"%V\"", url.err, &url.url);
0575         }
0576 
0577         return NGX_ERROR;
0578     }
0579 
0580     u = r->upstream;
0581 
0582     u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
0583     if (u->resolved == NULL) {
0584         return NGX_ERROR;
0585     }
0586 
0587     if (url.addrs) {
0588         u->resolved->sockaddr = url.addrs[0].sockaddr;
0589         u->resolved->socklen = url.addrs[0].socklen;
0590         u->resolved->name = url.addrs[0].name;
0591         u->resolved->naddrs = 1;
0592     }
0593 
0594     u->resolved->host = url.host;
0595     u->resolved->port = url.port;
0596     u->resolved->no_port = url.no_port;
0597 
0598     return NGX_OK;
0599 }
0600 
0601 
0602 #if (NGX_HTTP_CACHE)
0603 
0604 static ngx_int_t
0605 ngx_http_scgi_create_key(ngx_http_request_t *r)
0606 {
0607     ngx_str_t                 *key;
0608     ngx_http_scgi_loc_conf_t  *scf;
0609 
0610     key = ngx_array_push(&r->cache->keys);
0611     if (key == NULL) {
0612         return NGX_ERROR;
0613     }
0614 
0615     scf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module);
0616 
0617     if (ngx_http_complex_value(r, &scf->cache_key, key) != NGX_OK) {
0618         return NGX_ERROR;
0619     }
0620 
0621     return NGX_OK;
0622 }
0623 
0624 #endif
0625 
0626 
0627 static ngx_int_t
0628 ngx_http_scgi_create_request(ngx_http_request_t *r)
0629 {
0630     off_t                         content_length_n;
0631     u_char                        ch, *key, *val, *lowcase_key;
0632     size_t                        len, key_len, val_len, allocated;
0633     ngx_buf_t                    *b;
0634     ngx_str_t                     content_length;
0635     ngx_uint_t                    i, n, hash, skip_empty, header_params;
0636     ngx_chain_t                  *cl, *body;
0637     ngx_list_part_t              *part;
0638     ngx_table_elt_t              *header, **ignored;
0639     ngx_http_scgi_params_t       *params;
0640     ngx_http_script_code_pt       code;
0641     ngx_http_script_engine_t      e, le;
0642     ngx_http_scgi_loc_conf_t     *scf;
0643     ngx_http_script_len_code_pt   lcode;
0644     u_char                        buffer[NGX_OFF_T_LEN];
0645 
0646     content_length_n = 0;
0647     body = r->upstream->request_bufs;
0648 
0649     while (body) {
0650         content_length_n += ngx_buf_size(body->buf);
0651         body = body->next;
0652     }
0653 
0654     content_length.data = buffer;
0655     content_length.len = ngx_sprintf(buffer, "%O", content_length_n) - buffer;
0656 
0657     len = sizeof("CONTENT_LENGTH") + content_length.len + 1;
0658 
0659     header_params = 0;
0660     ignored = NULL;
0661 
0662     scf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module);
0663 
0664 #if (NGX_HTTP_CACHE)
0665     params = r->upstream->cacheable ? &scf->params_cache : &scf->params;
0666 #else
0667     params = &scf->params;
0668 #endif
0669 
0670     if (params->lengths) {
0671         ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
0672 
0673         ngx_http_script_flush_no_cacheable_variables(r, params->flushes);
0674         le.flushed = 1;
0675 
0676         le.ip = params->lengths->elts;
0677         le.request = r;
0678 
0679         while (*(uintptr_t *) le.ip) {
0680 
0681             lcode = *(ngx_http_script_len_code_pt *) le.ip;
0682             key_len = lcode(&le);
0683 
0684             lcode = *(ngx_http_script_len_code_pt *) le.ip;
0685             skip_empty = lcode(&le);
0686 
0687             for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
0688                 lcode = *(ngx_http_script_len_code_pt *) le.ip;
0689             }
0690             le.ip += sizeof(uintptr_t);
0691 
0692             if (skip_empty && val_len == 0) {
0693                 continue;
0694             }
0695 
0696             len += key_len + val_len + 1;
0697         }
0698     }
0699 
0700     if (scf->upstream.pass_request_headers) {
0701 
0702         allocated = 0;
0703         lowcase_key = NULL;
0704 
0705         if (params->number) {
0706             n = 0;
0707             part = &r->headers_in.headers.part;
0708 
0709             while (part) {
0710                 n += part->nelts;
0711                 part = part->next;
0712             }
0713 
0714             ignored = ngx_palloc(r->pool, n * sizeof(void *));
0715             if (ignored == NULL) {
0716                 return NGX_ERROR;
0717             }
0718         }
0719 
0720         part = &r->headers_in.headers.part;
0721         header = part->elts;
0722 
0723         for (i = 0; /* void */; i++) {
0724 
0725             if (i >= part->nelts) {
0726                 if (part->next == NULL) {
0727                     break;
0728                 }
0729 
0730                 part = part->next;
0731                 header = part->elts;
0732                 i = 0;
0733             }
0734 
0735             if (params->number) {
0736                 if (allocated < header[i].key.len) {
0737                     allocated = header[i].key.len + 16;
0738                     lowcase_key = ngx_pnalloc(r->pool, allocated);
0739                     if (lowcase_key == NULL) {
0740                         return NGX_ERROR;
0741                     }
0742                 }
0743 
0744                 hash = 0;
0745 
0746                 for (n = 0; n < header[i].key.len; n++) {
0747                     ch = header[i].key.data[n];
0748 
0749                     if (ch >= 'A' && ch <= 'Z') {
0750                         ch |= 0x20;
0751 
0752                     } else if (ch == '-') {
0753                         ch = '_';
0754                     }
0755 
0756                     hash = ngx_hash(hash, ch);
0757                     lowcase_key[n] = ch;
0758                 }
0759 
0760                 if (ngx_hash_find(&params->hash, hash, lowcase_key, n)) {
0761                     ignored[header_params++] = &header[i];
0762                     continue;
0763                 }
0764             }
0765 
0766             len += sizeof("HTTP_") - 1 + header[i].key.len + 1
0767                 + header[i].value.len + 1;
0768         }
0769     }
0770 
0771     /* netstring: "length:" + packet + "," */
0772 
0773     b = ngx_create_temp_buf(r->pool, NGX_SIZE_T_LEN + 1 + len + 1);
0774     if (b == NULL) {
0775         return NGX_ERROR;
0776     }
0777 
0778     cl = ngx_alloc_chain_link(r->pool);
0779     if (cl == NULL) {
0780         return NGX_ERROR;
0781     }
0782 
0783     cl->buf = b;
0784 
0785     b->last = ngx_sprintf(b->last, "%ui:CONTENT_LENGTH%Z%V%Z",
0786                           len, &content_length);
0787 
0788     if (params->lengths) {
0789         ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
0790 
0791         e.ip = params->values->elts;
0792         e.pos = b->last;
0793         e.request = r;
0794         e.flushed = 1;
0795 
0796         le.ip = params->lengths->elts;
0797 
0798         while (*(uintptr_t *) le.ip) {
0799 
0800             lcode = *(ngx_http_script_len_code_pt *) le.ip;
0801             lcode(&le); /* key length */
0802 
0803             lcode = *(ngx_http_script_len_code_pt *) le.ip;
0804             skip_empty = lcode(&le);
0805 
0806             for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
0807                 lcode = *(ngx_http_script_len_code_pt *) le.ip;
0808             }
0809             le.ip += sizeof(uintptr_t);
0810 
0811             if (skip_empty && val_len == 0) {
0812                 e.skip = 1;
0813 
0814                 while (*(uintptr_t *) e.ip) {
0815                     code = *(ngx_http_script_code_pt *) e.ip;
0816                     code((ngx_http_script_engine_t *) &e);
0817                 }
0818                 e.ip += sizeof(uintptr_t);
0819 
0820                 e.skip = 0;
0821 
0822                 continue;
0823             }
0824 
0825 #if (NGX_DEBUG)
0826             key = e.pos;
0827 #endif
0828             code = *(ngx_http_script_code_pt *) e.ip;
0829             code((ngx_http_script_engine_t *) &e);
0830 
0831 #if (NGX_DEBUG)
0832             val = e.pos;
0833 #endif
0834             while (*(uintptr_t *) e.ip) {
0835                 code = *(ngx_http_script_code_pt *) e.ip;
0836                 code((ngx_http_script_engine_t *) &e);
0837             }
0838             *e.pos++ = '\0';
0839             e.ip += sizeof(uintptr_t);
0840 
0841             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0842                            "scgi param: \"%s: %s\"", key, val);
0843         }
0844 
0845         b->last = e.pos;
0846     }
0847 
0848     if (scf->upstream.pass_request_headers) {
0849 
0850         part = &r->headers_in.headers.part;
0851         header = part->elts;
0852 
0853         for (i = 0; /* void */; i++) {
0854 
0855             if (i >= part->nelts) {
0856                 if (part->next == NULL) {
0857                     break;
0858                 }
0859 
0860                 part = part->next;
0861                 header = part->elts;
0862                 i = 0;
0863             }
0864 
0865             for (n = 0; n < header_params; n++) {
0866                 if (&header[i] == ignored[n]) {
0867                     goto next;
0868                 }
0869             }
0870 
0871             key = b->last;
0872             b->last = ngx_cpymem(key, "HTTP_", sizeof("HTTP_") - 1);
0873 
0874             for (n = 0; n < header[i].key.len; n++) {
0875                 ch = header[i].key.data[n];
0876 
0877                 if (ch >= 'a' && ch <= 'z') {
0878                     ch &= ~0x20;
0879 
0880                 } else if (ch == '-') {
0881                     ch = '_';
0882                 }
0883 
0884                 *b->last++ = ch;
0885             }
0886 
0887             *b->last++ = (u_char) 0;
0888 
0889             val = b->last;
0890             b->last = ngx_copy(val, header[i].value.data, header[i].value.len);
0891             *b->last++ = (u_char) 0;
0892 
0893             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0894                            "scgi param: \"%s: %s\"", key, val);
0895 
0896         next:
0897 
0898             continue;
0899         }
0900     }
0901 
0902     *b->last++ = (u_char) ',';
0903 
0904     if (r->request_body_no_buffering) {
0905         r->upstream->request_bufs = cl;
0906 
0907     } else if (scf->upstream.pass_request_body) {
0908         body = r->upstream->request_bufs;
0909         r->upstream->request_bufs = cl;
0910 
0911         while (body) {
0912             b = ngx_alloc_buf(r->pool);
0913             if (b == NULL) {
0914                 return NGX_ERROR;
0915             }
0916 
0917             ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
0918 
0919             cl->next = ngx_alloc_chain_link(r->pool);
0920             if (cl->next == NULL) {
0921                 return NGX_ERROR;
0922             }
0923 
0924             cl = cl->next;
0925             cl->buf = b;
0926 
0927             body = body->next;
0928         }
0929 
0930     } else {
0931         r->upstream->request_bufs = cl;
0932     }
0933 
0934     cl->next = NULL;
0935 
0936     return NGX_OK;
0937 }
0938 
0939 
0940 static ngx_int_t
0941 ngx_http_scgi_reinit_request(ngx_http_request_t *r)
0942 {
0943     ngx_http_status_t  *status;
0944 
0945     status = ngx_http_get_module_ctx(r, ngx_http_scgi_module);
0946 
0947     if (status == NULL) {
0948         return NGX_OK;
0949     }
0950 
0951     status->code = 0;
0952     status->count = 0;
0953     status->start = NULL;
0954     status->end = NULL;
0955 
0956     r->upstream->process_header = ngx_http_scgi_process_status_line;
0957     r->state = 0;
0958 
0959     return NGX_OK;
0960 }
0961 
0962 
0963 static ngx_int_t
0964 ngx_http_scgi_process_status_line(ngx_http_request_t *r)
0965 {
0966     size_t                len;
0967     ngx_int_t             rc;
0968     ngx_http_status_t    *status;
0969     ngx_http_upstream_t  *u;
0970 
0971     status = ngx_http_get_module_ctx(r, ngx_http_scgi_module);
0972 
0973     if (status == NULL) {
0974         return NGX_ERROR;
0975     }
0976 
0977     u = r->upstream;
0978 
0979     rc = ngx_http_parse_status_line(r, &u->buffer, status);
0980 
0981     if (rc == NGX_AGAIN) {
0982         return rc;
0983     }
0984 
0985     if (rc == NGX_ERROR) {
0986         u->process_header = ngx_http_scgi_process_header;
0987         return ngx_http_scgi_process_header(r);
0988     }
0989 
0990     if (u->state && u->state->status == 0) {
0991         u->state->status = status->code;
0992     }
0993 
0994     u->headers_in.status_n = status->code;
0995 
0996     len = status->end - status->start;
0997     u->headers_in.status_line.len = len;
0998 
0999     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
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
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
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 *
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 *
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 *
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
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(&params_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(&params_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(&params_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 = &params->flushes;
1774         sc.lengths = &params->lengths;
1775         sc.values = &params->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 = &params->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 *
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 *
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 *
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 *
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