Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Unbit S.a.s. 2009-2010
0004  * Copyright (C) 2008 Manlio Perillo (manlio.perillo@gmail.com)
0005  * Copyright (C) Igor Sysoev
0006  * Copyright (C) Nginx, Inc.
0007  */
0008 
0009 
0010 #include <ngx_config.h>
0011 #include <ngx_core.h>
0012 #include <ngx_http.h>
0013 
0014 
0015 typedef struct {
0016     ngx_array_t                caches;  /* ngx_http_file_cache_t * */
0017 } ngx_http_uwsgi_main_conf_t;
0018 
0019 
0020 typedef struct {
0021     ngx_array_t               *flushes;
0022     ngx_array_t               *lengths;
0023     ngx_array_t               *values;
0024     ngx_uint_t                 number;
0025     ngx_hash_t                 hash;
0026 } ngx_http_uwsgi_params_t;
0027 
0028 
0029 typedef struct {
0030     ngx_http_upstream_conf_t   upstream;
0031 
0032     ngx_http_uwsgi_params_t    params;
0033 #if (NGX_HTTP_CACHE)
0034     ngx_http_uwsgi_params_t    params_cache;
0035 #endif
0036     ngx_array_t               *params_source;
0037 
0038     ngx_array_t               *uwsgi_lengths;
0039     ngx_array_t               *uwsgi_values;
0040 
0041 #if (NGX_HTTP_CACHE)
0042     ngx_http_complex_value_t   cache_key;
0043 #endif
0044 
0045     ngx_str_t                  uwsgi_string;
0046 
0047     ngx_uint_t                 modifier1;
0048     ngx_uint_t                 modifier2;
0049 
0050 #if (NGX_HTTP_SSL)
0051     ngx_uint_t                 ssl;
0052     ngx_uint_t                 ssl_protocols;
0053     ngx_str_t                  ssl_ciphers;
0054     ngx_uint_t                 ssl_verify_depth;
0055     ngx_str_t                  ssl_trusted_certificate;
0056     ngx_str_t                  ssl_crl;
0057     ngx_str_t                  ssl_certificate;
0058     ngx_str_t                  ssl_certificate_key;
0059     ngx_array_t               *ssl_passwords;
0060 #endif
0061 } ngx_http_uwsgi_loc_conf_t;
0062 
0063 
0064 static ngx_int_t ngx_http_uwsgi_eval(ngx_http_request_t *r,
0065     ngx_http_uwsgi_loc_conf_t *uwcf);
0066 static ngx_int_t ngx_http_uwsgi_create_request(ngx_http_request_t *r);
0067 static ngx_int_t ngx_http_uwsgi_reinit_request(ngx_http_request_t *r);
0068 static ngx_int_t ngx_http_uwsgi_process_status_line(ngx_http_request_t *r);
0069 static ngx_int_t ngx_http_uwsgi_process_header(ngx_http_request_t *r);
0070 static void ngx_http_uwsgi_abort_request(ngx_http_request_t *r);
0071 static void ngx_http_uwsgi_finalize_request(ngx_http_request_t *r,
0072     ngx_int_t rc);
0073 
0074 static void *ngx_http_uwsgi_create_main_conf(ngx_conf_t *cf);
0075 static void *ngx_http_uwsgi_create_loc_conf(ngx_conf_t *cf);
0076 static char *ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent,
0077     void *child);
0078 static ngx_int_t ngx_http_uwsgi_init_params(ngx_conf_t *cf,
0079     ngx_http_uwsgi_loc_conf_t *conf, ngx_http_uwsgi_params_t *params,
0080     ngx_keyval_t *default_params);
0081 
0082 static char *ngx_http_uwsgi_pass(ngx_conf_t *cf, ngx_command_t *cmd,
0083     void *conf);
0084 static char *ngx_http_uwsgi_store(ngx_conf_t *cf, ngx_command_t *cmd,
0085     void *conf);
0086 
0087 #if (NGX_HTTP_CACHE)
0088 static ngx_int_t ngx_http_uwsgi_create_key(ngx_http_request_t *r);
0089 static char *ngx_http_uwsgi_cache(ngx_conf_t *cf, ngx_command_t *cmd,
0090     void *conf);
0091 static char *ngx_http_uwsgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd,
0092     void *conf);
0093 #endif
0094 
0095 #if (NGX_HTTP_SSL)
0096 static char *ngx_http_uwsgi_ssl_password_file(ngx_conf_t *cf,
0097     ngx_command_t *cmd, void *conf);
0098 static ngx_int_t ngx_http_uwsgi_set_ssl(ngx_conf_t *cf,
0099     ngx_http_uwsgi_loc_conf_t *uwcf);
0100 #endif
0101 
0102 
0103 static ngx_conf_num_bounds_t  ngx_http_uwsgi_modifier_bounds = {
0104     ngx_conf_check_num_bounds, 0, 255
0105 };
0106 
0107 
0108 static ngx_conf_bitmask_t ngx_http_uwsgi_next_upstream_masks[] = {
0109     { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
0110     { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
0111     { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
0112     { ngx_string("non_idempotent"), NGX_HTTP_UPSTREAM_FT_NON_IDEMPOTENT },
0113     { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
0114     { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
0115     { ngx_string("http_403"), NGX_HTTP_UPSTREAM_FT_HTTP_403 },
0116     { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
0117     { ngx_string("http_429"), NGX_HTTP_UPSTREAM_FT_HTTP_429 },
0118     { ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING },
0119     { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
0120     { ngx_null_string, 0 }
0121 };
0122 
0123 
0124 #if (NGX_HTTP_SSL)
0125 
0126 static ngx_conf_bitmask_t  ngx_http_uwsgi_ssl_protocols[] = {
0127     { ngx_string("SSLv2"), NGX_SSL_SSLv2 },
0128     { ngx_string("SSLv3"), NGX_SSL_SSLv3 },
0129     { ngx_string("TLSv1"), NGX_SSL_TLSv1 },
0130     { ngx_string("TLSv1.1"), NGX_SSL_TLSv1_1 },
0131     { ngx_string("TLSv1.2"), NGX_SSL_TLSv1_2 },
0132     { ngx_string("TLSv1.3"), NGX_SSL_TLSv1_3 },
0133     { ngx_null_string, 0 }
0134 };
0135 
0136 #endif
0137 
0138 
0139 ngx_module_t  ngx_http_uwsgi_module;
0140 
0141 
0142 static ngx_command_t ngx_http_uwsgi_commands[] = {
0143 
0144     { ngx_string("uwsgi_pass"),
0145       NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
0146       ngx_http_uwsgi_pass,
0147       NGX_HTTP_LOC_CONF_OFFSET,
0148       0,
0149       NULL },
0150 
0151     { ngx_string("uwsgi_modifier1"),
0152       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0153       ngx_conf_set_num_slot,
0154       NGX_HTTP_LOC_CONF_OFFSET,
0155       offsetof(ngx_http_uwsgi_loc_conf_t, modifier1),
0156       &ngx_http_uwsgi_modifier_bounds },
0157 
0158     { ngx_string("uwsgi_modifier2"),
0159       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0160       ngx_conf_set_num_slot,
0161       NGX_HTTP_LOC_CONF_OFFSET,
0162       offsetof(ngx_http_uwsgi_loc_conf_t, modifier2),
0163       &ngx_http_uwsgi_modifier_bounds },
0164 
0165     { ngx_string("uwsgi_store"),
0166       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0167       ngx_http_uwsgi_store,
0168       NGX_HTTP_LOC_CONF_OFFSET,
0169       0,
0170       NULL },
0171 
0172     { ngx_string("uwsgi_store_access"),
0173       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
0174       ngx_conf_set_access_slot,
0175       NGX_HTTP_LOC_CONF_OFFSET,
0176       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.store_access),
0177       NULL },
0178 
0179     { ngx_string("uwsgi_buffering"),
0180       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0181       ngx_conf_set_flag_slot,
0182       NGX_HTTP_LOC_CONF_OFFSET,
0183       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.buffering),
0184       NULL },
0185 
0186     { ngx_string("uwsgi_request_buffering"),
0187       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0188       ngx_conf_set_flag_slot,
0189       NGX_HTTP_LOC_CONF_OFFSET,
0190       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.request_buffering),
0191       NULL },
0192 
0193     { ngx_string("uwsgi_ignore_client_abort"),
0194       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0195       ngx_conf_set_flag_slot,
0196       NGX_HTTP_LOC_CONF_OFFSET,
0197       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ignore_client_abort),
0198       NULL },
0199 
0200     { ngx_string("uwsgi_bind"),
0201       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
0202       ngx_http_upstream_bind_set_slot,
0203       NGX_HTTP_LOC_CONF_OFFSET,
0204       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.local),
0205       NULL },
0206 
0207     { ngx_string("uwsgi_connect_timeout"),
0208       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0209       ngx_conf_set_msec_slot,
0210       NGX_HTTP_LOC_CONF_OFFSET,
0211       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.connect_timeout),
0212       NULL },
0213 
0214     { ngx_string("uwsgi_send_timeout"),
0215       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0216       ngx_conf_set_msec_slot,
0217       NGX_HTTP_LOC_CONF_OFFSET,
0218       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.send_timeout),
0219       NULL },
0220 
0221     { ngx_string("uwsgi_buffer_size"),
0222       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0223       ngx_conf_set_size_slot,
0224       NGX_HTTP_LOC_CONF_OFFSET,
0225       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.buffer_size),
0226       NULL },
0227 
0228     { ngx_string("uwsgi_pass_request_headers"),
0229       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0230       ngx_conf_set_flag_slot,
0231       NGX_HTTP_LOC_CONF_OFFSET,
0232       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.pass_request_headers),
0233       NULL },
0234 
0235     { ngx_string("uwsgi_pass_request_body"),
0236       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0237       ngx_conf_set_flag_slot,
0238       NGX_HTTP_LOC_CONF_OFFSET,
0239       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.pass_request_body),
0240       NULL },
0241 
0242     { ngx_string("uwsgi_intercept_errors"),
0243       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0244       ngx_conf_set_flag_slot,
0245       NGX_HTTP_LOC_CONF_OFFSET,
0246       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.intercept_errors),
0247       NULL },
0248 
0249     { ngx_string("uwsgi_read_timeout"),
0250       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0251       ngx_conf_set_msec_slot,
0252       NGX_HTTP_LOC_CONF_OFFSET,
0253       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.read_timeout),
0254       NULL },
0255 
0256     { ngx_string("uwsgi_buffers"),
0257       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
0258       ngx_conf_set_bufs_slot,
0259       NGX_HTTP_LOC_CONF_OFFSET,
0260       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.bufs),
0261       NULL },
0262 
0263     { ngx_string("uwsgi_busy_buffers_size"),
0264       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0265       ngx_conf_set_size_slot,
0266       NGX_HTTP_LOC_CONF_OFFSET,
0267       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.busy_buffers_size_conf),
0268       NULL },
0269 
0270     { ngx_string("uwsgi_force_ranges"),
0271       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0272       ngx_conf_set_flag_slot,
0273       NGX_HTTP_LOC_CONF_OFFSET,
0274       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.force_ranges),
0275       NULL },
0276 
0277     { ngx_string("uwsgi_limit_rate"),
0278       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0279       ngx_conf_set_size_slot,
0280       NGX_HTTP_LOC_CONF_OFFSET,
0281       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.limit_rate),
0282       NULL },
0283 
0284 #if (NGX_HTTP_CACHE)
0285 
0286     { ngx_string("uwsgi_cache"),
0287       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0288       ngx_http_uwsgi_cache,
0289       NGX_HTTP_LOC_CONF_OFFSET,
0290       0,
0291       NULL },
0292 
0293     { ngx_string("uwsgi_cache_key"),
0294       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0295       ngx_http_uwsgi_cache_key,
0296       NGX_HTTP_LOC_CONF_OFFSET,
0297       0,
0298       NULL },
0299 
0300     { ngx_string("uwsgi_cache_path"),
0301       NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
0302       ngx_http_file_cache_set_slot,
0303       NGX_HTTP_MAIN_CONF_OFFSET,
0304       offsetof(ngx_http_uwsgi_main_conf_t, caches),
0305       &ngx_http_uwsgi_module },
0306 
0307     { ngx_string("uwsgi_cache_bypass"),
0308       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0309       ngx_http_set_predicate_slot,
0310       NGX_HTTP_LOC_CONF_OFFSET,
0311       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_bypass),
0312       NULL },
0313 
0314     { ngx_string("uwsgi_no_cache"),
0315       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0316       ngx_http_set_predicate_slot,
0317       NGX_HTTP_LOC_CONF_OFFSET,
0318       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.no_cache),
0319       NULL },
0320 
0321     { ngx_string("uwsgi_cache_valid"),
0322       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0323       ngx_http_file_cache_valid_set_slot,
0324       NGX_HTTP_LOC_CONF_OFFSET,
0325       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_valid),
0326       NULL },
0327 
0328     { ngx_string("uwsgi_cache_min_uses"),
0329       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0330       ngx_conf_set_num_slot,
0331       NGX_HTTP_LOC_CONF_OFFSET,
0332       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_min_uses),
0333       NULL },
0334 
0335     { ngx_string("uwsgi_cache_max_range_offset"),
0336       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0337       ngx_conf_set_off_slot,
0338       NGX_HTTP_LOC_CONF_OFFSET,
0339       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_max_range_offset),
0340       NULL },
0341 
0342     { ngx_string("uwsgi_cache_use_stale"),
0343       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0344       ngx_conf_set_bitmask_slot,
0345       NGX_HTTP_LOC_CONF_OFFSET,
0346       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_use_stale),
0347       &ngx_http_uwsgi_next_upstream_masks },
0348 
0349     { ngx_string("uwsgi_cache_methods"),
0350       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0351       ngx_conf_set_bitmask_slot,
0352       NGX_HTTP_LOC_CONF_OFFSET,
0353       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_methods),
0354       &ngx_http_upstream_cache_method_mask },
0355 
0356     { ngx_string("uwsgi_cache_lock"),
0357       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0358       ngx_conf_set_flag_slot,
0359       NGX_HTTP_LOC_CONF_OFFSET,
0360       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_lock),
0361       NULL },
0362 
0363     { ngx_string("uwsgi_cache_lock_timeout"),
0364       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0365       ngx_conf_set_msec_slot,
0366       NGX_HTTP_LOC_CONF_OFFSET,
0367       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_lock_timeout),
0368       NULL },
0369 
0370     { ngx_string("uwsgi_cache_lock_age"),
0371       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0372       ngx_conf_set_msec_slot,
0373       NGX_HTTP_LOC_CONF_OFFSET,
0374       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_lock_age),
0375       NULL },
0376 
0377     { ngx_string("uwsgi_cache_revalidate"),
0378       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0379       ngx_conf_set_flag_slot,
0380       NGX_HTTP_LOC_CONF_OFFSET,
0381       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_revalidate),
0382       NULL },
0383 
0384     { ngx_string("uwsgi_cache_background_update"),
0385       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0386       ngx_conf_set_flag_slot,
0387       NGX_HTTP_LOC_CONF_OFFSET,
0388       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_background_update),
0389       NULL },
0390 
0391 #endif
0392 
0393     { ngx_string("uwsgi_temp_path"),
0394       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
0395       ngx_conf_set_path_slot,
0396       NGX_HTTP_LOC_CONF_OFFSET,
0397       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.temp_path),
0398       NULL },
0399 
0400     { ngx_string("uwsgi_max_temp_file_size"),
0401       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0402       ngx_conf_set_size_slot,
0403       NGX_HTTP_LOC_CONF_OFFSET,
0404       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.max_temp_file_size_conf),
0405       NULL },
0406 
0407     { ngx_string("uwsgi_temp_file_write_size"),
0408       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0409       ngx_conf_set_size_slot,
0410       NGX_HTTP_LOC_CONF_OFFSET,
0411       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.temp_file_write_size_conf),
0412       NULL },
0413 
0414     { ngx_string("uwsgi_next_upstream"),
0415       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0416       ngx_conf_set_bitmask_slot,
0417       NGX_HTTP_LOC_CONF_OFFSET,
0418       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.next_upstream),
0419       &ngx_http_uwsgi_next_upstream_masks },
0420 
0421     { ngx_string("uwsgi_next_upstream_tries"),
0422       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0423       ngx_conf_set_num_slot,
0424       NGX_HTTP_LOC_CONF_OFFSET,
0425       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.next_upstream_tries),
0426       NULL },
0427 
0428     { ngx_string("uwsgi_next_upstream_timeout"),
0429       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0430       ngx_conf_set_msec_slot,
0431       NGX_HTTP_LOC_CONF_OFFSET,
0432       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.next_upstream_timeout),
0433       NULL },
0434 
0435     { ngx_string("uwsgi_param"),
0436       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE23,
0437       ngx_http_upstream_param_set_slot,
0438       NGX_HTTP_LOC_CONF_OFFSET,
0439       offsetof(ngx_http_uwsgi_loc_conf_t, params_source),
0440       NULL },
0441 
0442     { ngx_string("uwsgi_string"),
0443       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0444       ngx_conf_set_str_slot,
0445       NGX_HTTP_LOC_CONF_OFFSET,
0446       offsetof(ngx_http_uwsgi_loc_conf_t, uwsgi_string),
0447       NULL },
0448 
0449     { ngx_string("uwsgi_pass_header"),
0450       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0451       ngx_conf_set_str_array_slot,
0452       NGX_HTTP_LOC_CONF_OFFSET,
0453       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.pass_headers),
0454       NULL },
0455 
0456     { ngx_string("uwsgi_hide_header"),
0457       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0458       ngx_conf_set_str_array_slot,
0459       NGX_HTTP_LOC_CONF_OFFSET,
0460       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.hide_headers),
0461       NULL },
0462 
0463     { ngx_string("uwsgi_ignore_headers"),
0464       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0465       ngx_conf_set_bitmask_slot,
0466       NGX_HTTP_LOC_CONF_OFFSET,
0467       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ignore_headers),
0468       &ngx_http_upstream_ignore_headers_masks },
0469 
0470 #if (NGX_HTTP_SSL)
0471 
0472     { ngx_string("uwsgi_ssl_session_reuse"),
0473       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0474       ngx_conf_set_flag_slot,
0475       NGX_HTTP_LOC_CONF_OFFSET,
0476       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ssl_session_reuse),
0477       NULL },
0478 
0479     { ngx_string("uwsgi_ssl_protocols"),
0480       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0481       ngx_conf_set_bitmask_slot,
0482       NGX_HTTP_LOC_CONF_OFFSET,
0483       offsetof(ngx_http_uwsgi_loc_conf_t, ssl_protocols),
0484       &ngx_http_uwsgi_ssl_protocols },
0485 
0486     { ngx_string("uwsgi_ssl_ciphers"),
0487       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0488       ngx_conf_set_str_slot,
0489       NGX_HTTP_LOC_CONF_OFFSET,
0490       offsetof(ngx_http_uwsgi_loc_conf_t, ssl_ciphers),
0491       NULL },
0492 
0493     { ngx_string("uwsgi_ssl_name"),
0494       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0495       ngx_http_set_complex_value_slot,
0496       NGX_HTTP_LOC_CONF_OFFSET,
0497       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ssl_name),
0498       NULL },
0499 
0500     { ngx_string("uwsgi_ssl_server_name"),
0501       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0502       ngx_conf_set_flag_slot,
0503       NGX_HTTP_LOC_CONF_OFFSET,
0504       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ssl_server_name),
0505       NULL },
0506 
0507     { ngx_string("uwsgi_ssl_verify"),
0508       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0509       ngx_conf_set_flag_slot,
0510       NGX_HTTP_LOC_CONF_OFFSET,
0511       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ssl_verify),
0512       NULL },
0513 
0514     { ngx_string("uwsgi_ssl_verify_depth"),
0515       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0516       ngx_conf_set_num_slot,
0517       NGX_HTTP_LOC_CONF_OFFSET,
0518       offsetof(ngx_http_uwsgi_loc_conf_t, ssl_verify_depth),
0519       NULL },
0520 
0521     { ngx_string("uwsgi_ssl_trusted_certificate"),
0522       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0523       ngx_conf_set_str_slot,
0524       NGX_HTTP_LOC_CONF_OFFSET,
0525       offsetof(ngx_http_uwsgi_loc_conf_t, ssl_trusted_certificate),
0526       NULL },
0527 
0528     { ngx_string("uwsgi_ssl_crl"),
0529       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0530       ngx_conf_set_str_slot,
0531       NGX_HTTP_LOC_CONF_OFFSET,
0532       offsetof(ngx_http_uwsgi_loc_conf_t, ssl_crl),
0533       NULL },
0534 
0535     { ngx_string("uwsgi_ssl_certificate"),
0536       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0537       ngx_conf_set_str_slot,
0538       NGX_HTTP_LOC_CONF_OFFSET,
0539       offsetof(ngx_http_uwsgi_loc_conf_t, ssl_certificate),
0540       NULL },
0541 
0542     { ngx_string("uwsgi_ssl_certificate_key"),
0543       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0544       ngx_conf_set_str_slot,
0545       NGX_HTTP_LOC_CONF_OFFSET,
0546       offsetof(ngx_http_uwsgi_loc_conf_t, ssl_certificate_key),
0547       NULL },
0548 
0549     { ngx_string("uwsgi_ssl_password_file"),
0550       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0551       ngx_http_uwsgi_ssl_password_file,
0552       NGX_HTTP_LOC_CONF_OFFSET,
0553       0,
0554       NULL },
0555 
0556 #endif
0557 
0558       ngx_null_command
0559 };
0560 
0561 
0562 static ngx_http_module_t ngx_http_uwsgi_module_ctx = {
0563     NULL,                                  /* preconfiguration */
0564     NULL,                                  /* postconfiguration */
0565 
0566     ngx_http_uwsgi_create_main_conf,       /* create main configuration */
0567     NULL,                                  /* init main configuration */
0568 
0569     NULL,                                  /* create server configuration */
0570     NULL,                                  /* merge server configuration */
0571 
0572     ngx_http_uwsgi_create_loc_conf,        /* create location configuration */
0573     ngx_http_uwsgi_merge_loc_conf          /* merge location configuration */
0574 };
0575 
0576 
0577 ngx_module_t ngx_http_uwsgi_module = {
0578     NGX_MODULE_V1,
0579     &ngx_http_uwsgi_module_ctx,            /* module context */
0580     ngx_http_uwsgi_commands,               /* module directives */
0581     NGX_HTTP_MODULE,                       /* module type */
0582     NULL,                                  /* init master */
0583     NULL,                                  /* init module */
0584     NULL,                                  /* init process */
0585     NULL,                                  /* init thread */
0586     NULL,                                  /* exit thread */
0587     NULL,                                  /* exit process */
0588     NULL,                                  /* exit master */
0589     NGX_MODULE_V1_PADDING
0590 };
0591 
0592 
0593 static ngx_str_t ngx_http_uwsgi_hide_headers[] = {
0594     ngx_string("X-Accel-Expires"),
0595     ngx_string("X-Accel-Redirect"),
0596     ngx_string("X-Accel-Limit-Rate"),
0597     ngx_string("X-Accel-Buffering"),
0598     ngx_string("X-Accel-Charset"),
0599     ngx_null_string
0600 };
0601 
0602 
0603 #if (NGX_HTTP_CACHE)
0604 
0605 static ngx_keyval_t  ngx_http_uwsgi_cache_headers[] = {
0606     { ngx_string("HTTP_IF_MODIFIED_SINCE"),
0607       ngx_string("$upstream_cache_last_modified") },
0608     { ngx_string("HTTP_IF_UNMODIFIED_SINCE"), ngx_string("") },
0609     { ngx_string("HTTP_IF_NONE_MATCH"), ngx_string("$upstream_cache_etag") },
0610     { ngx_string("HTTP_IF_MATCH"), ngx_string("") },
0611     { ngx_string("HTTP_RANGE"), ngx_string("") },
0612     { ngx_string("HTTP_IF_RANGE"), ngx_string("") },
0613     { ngx_null_string, ngx_null_string }
0614 };
0615 
0616 #endif
0617 
0618 
0619 static ngx_path_init_t ngx_http_uwsgi_temp_path = {
0620     ngx_string(NGX_HTTP_UWSGI_TEMP_PATH), { 1, 2, 0 }
0621 };
0622 
0623 
0624 static ngx_int_t
0625 ngx_http_uwsgi_handler(ngx_http_request_t *r)
0626 {
0627     ngx_int_t                    rc;
0628     ngx_http_status_t           *status;
0629     ngx_http_upstream_t         *u;
0630     ngx_http_uwsgi_loc_conf_t   *uwcf;
0631 #if (NGX_HTTP_CACHE)
0632     ngx_http_uwsgi_main_conf_t  *uwmcf;
0633 #endif
0634 
0635     if (ngx_http_upstream_create(r) != NGX_OK) {
0636         return NGX_HTTP_INTERNAL_SERVER_ERROR;
0637     }
0638 
0639     status = ngx_pcalloc(r->pool, sizeof(ngx_http_status_t));
0640     if (status == NULL) {
0641         return NGX_HTTP_INTERNAL_SERVER_ERROR;
0642     }
0643 
0644     ngx_http_set_ctx(r, status, ngx_http_uwsgi_module);
0645 
0646     uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module);
0647 
0648     u = r->upstream;
0649 
0650     if (uwcf->uwsgi_lengths == NULL) {
0651 
0652 #if (NGX_HTTP_SSL)
0653         u->ssl = (uwcf->upstream.ssl != NULL);
0654 
0655         if (u->ssl) {
0656             ngx_str_set(&u->schema, "suwsgi://");
0657 
0658         } else {
0659             ngx_str_set(&u->schema, "uwsgi://");
0660         }
0661 #else
0662         ngx_str_set(&u->schema, "uwsgi://");
0663 #endif
0664 
0665     } else {
0666         if (ngx_http_uwsgi_eval(r, uwcf) != NGX_OK) {
0667             return NGX_HTTP_INTERNAL_SERVER_ERROR;
0668         }
0669     }
0670 
0671     u->output.tag = (ngx_buf_tag_t) &ngx_http_uwsgi_module;
0672 
0673     u->conf = &uwcf->upstream;
0674 
0675 #if (NGX_HTTP_CACHE)
0676     uwmcf = ngx_http_get_module_main_conf(r, ngx_http_uwsgi_module);
0677 
0678     u->caches = &uwmcf->caches;
0679     u->create_key = ngx_http_uwsgi_create_key;
0680 #endif
0681 
0682     u->create_request = ngx_http_uwsgi_create_request;
0683     u->reinit_request = ngx_http_uwsgi_reinit_request;
0684     u->process_header = ngx_http_uwsgi_process_status_line;
0685     u->abort_request = ngx_http_uwsgi_abort_request;
0686     u->finalize_request = ngx_http_uwsgi_finalize_request;
0687     r->state = 0;
0688 
0689     u->buffering = uwcf->upstream.buffering;
0690 
0691     u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
0692     if (u->pipe == NULL) {
0693         return NGX_HTTP_INTERNAL_SERVER_ERROR;
0694     }
0695 
0696     u->pipe->input_filter = ngx_event_pipe_copy_input_filter;
0697     u->pipe->input_ctx = r;
0698 
0699     if (!uwcf->upstream.request_buffering
0700         && uwcf->upstream.pass_request_body
0701         && !r->headers_in.chunked)
0702     {
0703         r->request_body_no_buffering = 1;
0704     }
0705 
0706     rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
0707 
0708     if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
0709         return rc;
0710     }
0711 
0712     return NGX_DONE;
0713 }
0714 
0715 
0716 static ngx_int_t
0717 ngx_http_uwsgi_eval(ngx_http_request_t *r, ngx_http_uwsgi_loc_conf_t * uwcf)
0718 {
0719     size_t                add;
0720     ngx_url_t             url;
0721     ngx_http_upstream_t  *u;
0722 
0723     ngx_memzero(&url, sizeof(ngx_url_t));
0724 
0725     if (ngx_http_script_run(r, &url.url, uwcf->uwsgi_lengths->elts, 0,
0726                             uwcf->uwsgi_values->elts)
0727         == NULL)
0728     {
0729         return NGX_ERROR;
0730     }
0731 
0732     if (url.url.len > 8
0733         && ngx_strncasecmp(url.url.data, (u_char *) "uwsgi://", 8) == 0)
0734     {
0735         add = 8;
0736 
0737     } else if (url.url.len > 9
0738                && ngx_strncasecmp(url.url.data, (u_char *) "suwsgi://", 9) == 0)
0739     {
0740 
0741 #if (NGX_HTTP_SSL)
0742         add = 9;
0743         r->upstream->ssl = 1;
0744 #else
0745         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
0746                       "suwsgi protocol requires SSL support");
0747         return NGX_ERROR;
0748 #endif
0749 
0750     } else {
0751         add = 0;
0752     }
0753 
0754     u = r->upstream;
0755 
0756     if (add) {
0757         u->schema.len = add;
0758         u->schema.data = url.url.data;
0759 
0760         url.url.data += add;
0761         url.url.len -= add;
0762 
0763     } else {
0764         ngx_str_set(&u->schema, "uwsgi://");
0765     }
0766 
0767     url.no_resolve = 1;
0768 
0769     if (ngx_parse_url(r->pool, &url) != NGX_OK) {
0770         if (url.err) {
0771             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
0772                           "%s in upstream \"%V\"", url.err, &url.url);
0773         }
0774 
0775         return NGX_ERROR;
0776     }
0777 
0778     u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
0779     if (u->resolved == NULL) {
0780         return NGX_ERROR;
0781     }
0782 
0783     if (url.addrs) {
0784         u->resolved->sockaddr = url.addrs[0].sockaddr;
0785         u->resolved->socklen = url.addrs[0].socklen;
0786         u->resolved->name = url.addrs[0].name;
0787         u->resolved->naddrs = 1;
0788     }
0789 
0790     u->resolved->host = url.host;
0791     u->resolved->port = url.port;
0792     u->resolved->no_port = url.no_port;
0793 
0794     return NGX_OK;
0795 }
0796 
0797 
0798 #if (NGX_HTTP_CACHE)
0799 
0800 static ngx_int_t
0801 ngx_http_uwsgi_create_key(ngx_http_request_t *r)
0802 {
0803     ngx_str_t                  *key;
0804     ngx_http_uwsgi_loc_conf_t  *uwcf;
0805 
0806     key = ngx_array_push(&r->cache->keys);
0807     if (key == NULL) {
0808         return NGX_ERROR;
0809     }
0810 
0811     uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module);
0812 
0813     if (ngx_http_complex_value(r, &uwcf->cache_key, key) != NGX_OK) {
0814         return NGX_ERROR;
0815     }
0816 
0817     return NGX_OK;
0818 }
0819 
0820 #endif
0821 
0822 
0823 static ngx_int_t
0824 ngx_http_uwsgi_create_request(ngx_http_request_t *r)
0825 {
0826     u_char                        ch, *lowcase_key;
0827     size_t                        key_len, val_len, len, allocated;
0828     ngx_uint_t                    i, n, hash, skip_empty, header_params;
0829     ngx_buf_t                    *b;
0830     ngx_chain_t                  *cl, *body;
0831     ngx_list_part_t              *part;
0832     ngx_table_elt_t              *header, **ignored;
0833     ngx_http_uwsgi_params_t      *params;
0834     ngx_http_script_code_pt       code;
0835     ngx_http_script_engine_t      e, le;
0836     ngx_http_uwsgi_loc_conf_t    *uwcf;
0837     ngx_http_script_len_code_pt   lcode;
0838 
0839     len = 0;
0840     header_params = 0;
0841     ignored = NULL;
0842 
0843     uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module);
0844 
0845 #if (NGX_HTTP_CACHE)
0846     params = r->upstream->cacheable ? &uwcf->params_cache : &uwcf->params;
0847 #else
0848     params = &uwcf->params;
0849 #endif
0850 
0851     if (params->lengths) {
0852         ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
0853 
0854         ngx_http_script_flush_no_cacheable_variables(r, params->flushes);
0855         le.flushed = 1;
0856 
0857         le.ip = params->lengths->elts;
0858         le.request = r;
0859 
0860         while (*(uintptr_t *) le.ip) {
0861 
0862             lcode = *(ngx_http_script_len_code_pt *) le.ip;
0863             key_len = lcode(&le);
0864 
0865             lcode = *(ngx_http_script_len_code_pt *) le.ip;
0866             skip_empty = lcode(&le);
0867 
0868             for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
0869                 lcode = *(ngx_http_script_len_code_pt *) le.ip;
0870             }
0871             le.ip += sizeof(uintptr_t);
0872 
0873             if (skip_empty && val_len == 0) {
0874                 continue;
0875             }
0876 
0877             len += 2 + key_len + 2 + val_len;
0878         }
0879     }
0880 
0881     if (uwcf->upstream.pass_request_headers) {
0882 
0883         allocated = 0;
0884         lowcase_key = NULL;
0885 
0886         if (params->number) {
0887             n = 0;
0888             part = &r->headers_in.headers.part;
0889 
0890             while (part) {
0891                 n += part->nelts;
0892                 part = part->next;
0893             }
0894 
0895             ignored = ngx_palloc(r->pool, n * sizeof(void *));
0896             if (ignored == NULL) {
0897                 return NGX_ERROR;
0898             }
0899         }
0900 
0901         part = &r->headers_in.headers.part;
0902         header = part->elts;
0903 
0904         for (i = 0; /* void */ ; i++) {
0905 
0906             if (i >= part->nelts) {
0907                 if (part->next == NULL) {
0908                     break;
0909                 }
0910 
0911                 part = part->next;
0912                 header = part->elts;
0913                 i = 0;
0914             }
0915 
0916             if (params->number) {
0917                 if (allocated < header[i].key.len) {
0918                     allocated = header[i].key.len + 16;
0919                     lowcase_key = ngx_pnalloc(r->pool, allocated);
0920                     if (lowcase_key == NULL) {
0921                         return NGX_ERROR;
0922                     }
0923                 }
0924 
0925                 hash = 0;
0926 
0927                 for (n = 0; n < header[i].key.len; n++) {
0928                     ch = header[i].key.data[n];
0929 
0930                     if (ch >= 'A' && ch <= 'Z') {
0931                         ch |= 0x20;
0932 
0933                     } else if (ch == '-') {
0934                         ch = '_';
0935                     }
0936 
0937                     hash = ngx_hash(hash, ch);
0938                     lowcase_key[n] = ch;
0939                 }
0940 
0941                 if (ngx_hash_find(&params->hash, hash, lowcase_key, n)) {
0942                     ignored[header_params++] = &header[i];
0943                     continue;
0944                 }
0945             }
0946 
0947             len += 2 + sizeof("HTTP_") - 1 + header[i].key.len
0948                  + 2 + header[i].value.len;
0949         }
0950     }
0951 
0952     len += uwcf->uwsgi_string.len;
0953 
0954 #if 0
0955     /* allow custom uwsgi packet */
0956     if (len > 0 && len < 2) {
0957         ngx_log_error (NGX_LOG_ALERT, r->connection->log, 0,
0958                        "uwsgi request is too little: %uz", len);
0959         return NGX_ERROR;
0960     }
0961 #endif
0962 
0963     b = ngx_create_temp_buf(r->pool, len + 4);
0964     if (b == NULL) {
0965         return NGX_ERROR;
0966     }
0967 
0968     cl = ngx_alloc_chain_link(r->pool);
0969     if (cl == NULL) {
0970         return NGX_ERROR;
0971     }
0972 
0973     cl->buf = b;
0974 
0975     *b->last++ = (u_char) uwcf->modifier1;
0976     *b->last++ = (u_char) (len & 0xff);
0977     *b->last++ = (u_char) ((len >> 8) & 0xff);
0978     *b->last++ = (u_char) uwcf->modifier2;
0979 
0980     if (params->lengths) {
0981         ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
0982 
0983         e.ip = params->values->elts;
0984         e.pos = b->last;
0985         e.request = r;
0986         e.flushed = 1;
0987 
0988         le.ip = params->lengths->elts;
0989 
0990         while (*(uintptr_t *) le.ip) {
0991 
0992             lcode = *(ngx_http_script_len_code_pt *) le.ip;
0993             key_len = (u_char) lcode(&le);
0994 
0995             lcode = *(ngx_http_script_len_code_pt *) le.ip;
0996             skip_empty = lcode(&le);
0997 
0998             for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
0999                 lcode = *(ngx_http_script_len_code_pt *) le.ip;
1000             }
1001             le.ip += sizeof(uintptr_t);
1002 
1003             if (skip_empty && val_len == 0) {
1004                 e.skip = 1;
1005 
1006                 while (*(uintptr_t *) e.ip) {
1007                     code = *(ngx_http_script_code_pt *) e.ip;
1008                     code((ngx_http_script_engine_t *) &e);
1009                 }
1010                 e.ip += sizeof(uintptr_t);
1011 
1012                 e.skip = 0;
1013 
1014                 continue;
1015             }
1016 
1017             *e.pos++ = (u_char) (key_len & 0xff);
1018             *e.pos++ = (u_char) ((key_len >> 8) & 0xff);
1019 
1020             code = *(ngx_http_script_code_pt *) e.ip;
1021             code((ngx_http_script_engine_t *) &e);
1022 
1023             *e.pos++ = (u_char) (val_len & 0xff);
1024             *e.pos++ = (u_char) ((val_len >> 8) & 0xff);
1025 
1026             while (*(uintptr_t *) e.ip) {
1027                 code = *(ngx_http_script_code_pt *) e.ip;
1028                 code((ngx_http_script_engine_t *) &e);
1029             }
1030 
1031             e.ip += sizeof(uintptr_t);
1032 
1033             ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1034                            "uwsgi param: \"%*s: %*s\"",
1035                            key_len, e.pos - (key_len + 2 + val_len),
1036                            val_len, e.pos - val_len);
1037         }
1038 
1039         b->last = e.pos;
1040     }
1041 
1042     if (uwcf->upstream.pass_request_headers) {
1043 
1044         part = &r->headers_in.headers.part;
1045         header = part->elts;
1046 
1047         for (i = 0; /* void */ ; i++) {
1048 
1049             if (i >= part->nelts) {
1050                 if (part->next == NULL) {
1051                     break;
1052                 }
1053 
1054                 part = part->next;
1055                 header = part->elts;
1056                 i = 0;
1057             }
1058 
1059             for (n = 0; n < header_params; n++) {
1060                 if (&header[i] == ignored[n]) {
1061                     goto next;
1062                 }
1063             }
1064 
1065             key_len = sizeof("HTTP_") - 1 + header[i].key.len;
1066             *b->last++ = (u_char) (key_len & 0xff);
1067             *b->last++ = (u_char) ((key_len >> 8) & 0xff);
1068 
1069             b->last = ngx_cpymem(b->last, "HTTP_", sizeof("HTTP_") - 1);
1070             for (n = 0; n < header[i].key.len; n++) {
1071                 ch = header[i].key.data[n];
1072 
1073                 if (ch >= 'a' && ch <= 'z') {
1074                     ch &= ~0x20;
1075 
1076                 } else if (ch == '-') {
1077                     ch = '_';
1078                 }
1079 
1080                 *b->last++ = ch;
1081             }
1082 
1083             val_len = header[i].value.len;
1084             *b->last++ = (u_char) (val_len & 0xff);
1085             *b->last++ = (u_char) ((val_len >> 8) & 0xff);
1086             b->last = ngx_copy(b->last, header[i].value.data, val_len);
1087 
1088             ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1089                            "uwsgi param: \"%*s: %*s\"",
1090                            key_len, b->last - (key_len + 2 + val_len),
1091                            val_len, b->last - val_len);
1092         next:
1093 
1094             continue;
1095         }
1096     }
1097 
1098     b->last = ngx_copy(b->last, uwcf->uwsgi_string.data,
1099                        uwcf->uwsgi_string.len);
1100 
1101     if (r->request_body_no_buffering) {
1102         r->upstream->request_bufs = cl;
1103 
1104     } else if (uwcf->upstream.pass_request_body) {
1105         body = r->upstream->request_bufs;
1106         r->upstream->request_bufs = cl;
1107 
1108         while (body) {
1109             b = ngx_alloc_buf(r->pool);
1110             if (b == NULL) {
1111                 return NGX_ERROR;
1112             }
1113 
1114             ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
1115 
1116             cl->next = ngx_alloc_chain_link(r->pool);
1117             if (cl->next == NULL) {
1118                 return NGX_ERROR;
1119             }
1120 
1121             cl = cl->next;
1122             cl->buf = b;
1123 
1124             body = body->next;
1125         }
1126 
1127     } else {
1128         r->upstream->request_bufs = cl;
1129     }
1130 
1131     cl->next = NULL;
1132 
1133     return NGX_OK;
1134 }
1135 
1136 
1137 static ngx_int_t
1138 ngx_http_uwsgi_reinit_request(ngx_http_request_t *r)
1139 {
1140     ngx_http_status_t  *status;
1141 
1142     status = ngx_http_get_module_ctx(r, ngx_http_uwsgi_module);
1143 
1144     if (status == NULL) {
1145         return NGX_OK;
1146     }
1147 
1148     status->code = 0;
1149     status->count = 0;
1150     status->start = NULL;
1151     status->end = NULL;
1152 
1153     r->upstream->process_header = ngx_http_uwsgi_process_status_line;
1154     r->state = 0;
1155 
1156     return NGX_OK;
1157 }
1158 
1159 
1160 static ngx_int_t
1161 ngx_http_uwsgi_process_status_line(ngx_http_request_t *r)
1162 {
1163     size_t                 len;
1164     ngx_int_t              rc;
1165     ngx_http_status_t     *status;
1166     ngx_http_upstream_t   *u;
1167 
1168     status = ngx_http_get_module_ctx(r, ngx_http_uwsgi_module);
1169 
1170     if (status == NULL) {
1171         return NGX_ERROR;
1172     }
1173 
1174     u = r->upstream;
1175 
1176     rc = ngx_http_parse_status_line(r, &u->buffer, status);
1177 
1178     if (rc == NGX_AGAIN) {
1179         return rc;
1180     }
1181 
1182     if (rc == NGX_ERROR) {
1183         u->process_header = ngx_http_uwsgi_process_header;
1184         return ngx_http_uwsgi_process_header(r);
1185     }
1186 
1187     if (u->state && u->state->status == 0) {
1188         u->state->status = status->code;
1189     }
1190 
1191     u->headers_in.status_n = status->code;
1192 
1193     len = status->end - status->start;
1194     u->headers_in.status_line.len = len;
1195 
1196     u->headers_in.status_line.data = ngx_pnalloc(r->pool, len);
1197     if (u->headers_in.status_line.data == NULL) {
1198         return NGX_ERROR;
1199     }
1200 
1201     ngx_memcpy(u->headers_in.status_line.data, status->start, len);
1202 
1203     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1204                    "http uwsgi status %ui \"%V\"",
1205                    u->headers_in.status_n, &u->headers_in.status_line);
1206 
1207     u->process_header = ngx_http_uwsgi_process_header;
1208 
1209     return ngx_http_uwsgi_process_header(r);
1210 }
1211 
1212 
1213 static ngx_int_t
1214 ngx_http_uwsgi_process_header(ngx_http_request_t *r)
1215 {
1216     ngx_str_t                      *status_line;
1217     ngx_int_t                       rc, status;
1218     ngx_table_elt_t                *h;
1219     ngx_http_upstream_t            *u;
1220     ngx_http_upstream_header_t     *hh;
1221     ngx_http_upstream_main_conf_t  *umcf;
1222 
1223     umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
1224 
1225     for ( ;; ) {
1226 
1227         rc = ngx_http_parse_header_line(r, &r->upstream->buffer, 1);
1228 
1229         if (rc == NGX_OK) {
1230 
1231             /* a header line has been parsed successfully */
1232 
1233             h = ngx_list_push(&r->upstream->headers_in.headers);
1234             if (h == NULL) {
1235                 return NGX_ERROR;
1236             }
1237 
1238             h->hash = r->header_hash;
1239 
1240             h->key.len = r->header_name_end - r->header_name_start;
1241             h->value.len = r->header_end - r->header_start;
1242 
1243             h->key.data = ngx_pnalloc(r->pool,
1244                                       h->key.len + 1 + h->value.len + 1
1245                                       + h->key.len);
1246             if (h->key.data == NULL) {
1247                 h->hash = 0;
1248                 return NGX_ERROR;
1249             }
1250 
1251             h->value.data = h->key.data + h->key.len + 1;
1252             h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;
1253 
1254             ngx_memcpy(h->key.data, r->header_name_start, h->key.len);
1255             h->key.data[h->key.len] = '\0';
1256             ngx_memcpy(h->value.data, r->header_start, h->value.len);
1257             h->value.data[h->value.len] = '\0';
1258 
1259             if (h->key.len == r->lowcase_index) {
1260                 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
1261 
1262             } else {
1263                 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
1264             }
1265 
1266             hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
1267                                h->lowcase_key, h->key.len);
1268 
1269             if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
1270                 return NGX_ERROR;
1271             }
1272 
1273             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1274                            "http uwsgi header: \"%V: %V\"", &h->key, &h->value);
1275 
1276             continue;
1277         }
1278 
1279         if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
1280 
1281             /* a whole header has been parsed successfully */
1282 
1283             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1284                            "http uwsgi header done");
1285 
1286             u = r->upstream;
1287 
1288             if (u->headers_in.status_n) {
1289                 goto done;
1290             }
1291 
1292             if (u->headers_in.status) {
1293                 status_line = &u->headers_in.status->value;
1294 
1295                 status = ngx_atoi(status_line->data, 3);
1296                 if (status == NGX_ERROR) {
1297                     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1298                                   "upstream sent invalid status \"%V\"",
1299                                   status_line);
1300                     return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1301                 }
1302 
1303                 u->headers_in.status_n = status;
1304                 u->headers_in.status_line = *status_line;
1305 
1306             } else if (u->headers_in.location) {
1307                 u->headers_in.status_n = 302;
1308                 ngx_str_set(&u->headers_in.status_line,
1309                             "302 Moved Temporarily");
1310 
1311             } else {
1312                 u->headers_in.status_n = 200;
1313                 ngx_str_set(&u->headers_in.status_line, "200 OK");
1314             }
1315 
1316             if (u->state && u->state->status == 0) {
1317                 u->state->status = u->headers_in.status_n;
1318             }
1319 
1320         done:
1321 
1322             if (u->headers_in.status_n == NGX_HTTP_SWITCHING_PROTOCOLS
1323                 && r->headers_in.upgrade)
1324             {
1325                 u->upgrade = 1;
1326             }
1327 
1328             return NGX_OK;
1329         }
1330 
1331         if (rc == NGX_AGAIN) {
1332             return NGX_AGAIN;
1333         }
1334 
1335         /* there was error while a header line parsing */
1336 
1337         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1338                       "upstream sent invalid header");
1339 
1340         return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1341     }
1342 }
1343 
1344 
1345 static void
1346 ngx_http_uwsgi_abort_request(ngx_http_request_t *r)
1347 {
1348     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1349                    "abort http uwsgi request");
1350 
1351     return;
1352 }
1353 
1354 
1355 static void
1356 ngx_http_uwsgi_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
1357 {
1358     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1359                    "finalize http uwsgi request");
1360 
1361     return;
1362 }
1363 
1364 
1365 static void *
1366 ngx_http_uwsgi_create_main_conf(ngx_conf_t *cf)
1367 {
1368     ngx_http_uwsgi_main_conf_t  *conf;
1369 
1370     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_uwsgi_main_conf_t));
1371     if (conf == NULL) {
1372         return NULL;
1373     }
1374 
1375 #if (NGX_HTTP_CACHE)
1376     if (ngx_array_init(&conf->caches, cf->pool, 4,
1377                        sizeof(ngx_http_file_cache_t *))
1378         != NGX_OK)
1379     {
1380         return NULL;
1381     }
1382 #endif
1383 
1384     return conf;
1385 }
1386 
1387 
1388 static void *
1389 ngx_http_uwsgi_create_loc_conf(ngx_conf_t *cf)
1390 {
1391     ngx_http_uwsgi_loc_conf_t  *conf;
1392 
1393     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_uwsgi_loc_conf_t));
1394     if (conf == NULL) {
1395         return NULL;
1396     }
1397 
1398     conf->modifier1 = NGX_CONF_UNSET_UINT;
1399     conf->modifier2 = NGX_CONF_UNSET_UINT;
1400 
1401     conf->upstream.store = NGX_CONF_UNSET;
1402     conf->upstream.store_access = NGX_CONF_UNSET_UINT;
1403     conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
1404     conf->upstream.buffering = NGX_CONF_UNSET;
1405     conf->upstream.request_buffering = NGX_CONF_UNSET;
1406     conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
1407     conf->upstream.force_ranges = NGX_CONF_UNSET;
1408 
1409     conf->upstream.local = NGX_CONF_UNSET_PTR;
1410 
1411     conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
1412     conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
1413     conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
1414     conf->upstream.next_upstream_timeout = NGX_CONF_UNSET_MSEC;
1415 
1416     conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
1417     conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
1418     conf->upstream.limit_rate = NGX_CONF_UNSET_SIZE;
1419 
1420     conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
1421     conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
1422     conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
1423 
1424     conf->upstream.pass_request_headers = NGX_CONF_UNSET;
1425     conf->upstream.pass_request_body = NGX_CONF_UNSET;
1426 
1427 #if (NGX_HTTP_CACHE)
1428     conf->upstream.cache = NGX_CONF_UNSET;
1429     conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
1430     conf->upstream.cache_max_range_offset = NGX_CONF_UNSET;
1431     conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
1432     conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
1433     conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
1434     conf->upstream.cache_lock = NGX_CONF_UNSET;
1435     conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
1436     conf->upstream.cache_lock_age = NGX_CONF_UNSET_MSEC;
1437     conf->upstream.cache_revalidate = NGX_CONF_UNSET;
1438     conf->upstream.cache_background_update = NGX_CONF_UNSET;
1439 #endif
1440 
1441     conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
1442     conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;
1443 
1444     conf->upstream.intercept_errors = NGX_CONF_UNSET;
1445 
1446 #if (NGX_HTTP_SSL)
1447     conf->upstream.ssl_session_reuse = NGX_CONF_UNSET;
1448     conf->upstream.ssl_server_name = NGX_CONF_UNSET;
1449     conf->upstream.ssl_verify = NGX_CONF_UNSET;
1450     conf->ssl_verify_depth = NGX_CONF_UNSET_UINT;
1451     conf->ssl_passwords = NGX_CONF_UNSET_PTR;
1452 #endif
1453 
1454     /* "uwsgi_cyclic_temp_file" is disabled */
1455     conf->upstream.cyclic_temp_file = 0;
1456 
1457     conf->upstream.change_buffering = 1;
1458 
1459     ngx_str_set(&conf->upstream.module, "uwsgi");
1460 
1461     return conf;
1462 }
1463 
1464 
1465 static char *
1466 ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
1467 {
1468     ngx_http_uwsgi_loc_conf_t *prev = parent;
1469     ngx_http_uwsgi_loc_conf_t *conf = child;
1470 
1471     size_t                        size;
1472     ngx_int_t                     rc;
1473     ngx_hash_init_t               hash;
1474     ngx_http_core_loc_conf_t     *clcf;
1475 
1476 #if (NGX_HTTP_CACHE)
1477 
1478     if (conf->upstream.store > 0) {
1479         conf->upstream.cache = 0;
1480     }
1481 
1482     if (conf->upstream.cache > 0) {
1483         conf->upstream.store = 0;
1484     }
1485 
1486 #endif
1487 
1488     if (conf->upstream.store == NGX_CONF_UNSET) {
1489         ngx_conf_merge_value(conf->upstream.store, prev->upstream.store, 0);
1490 
1491         conf->upstream.store_lengths = prev->upstream.store_lengths;
1492         conf->upstream.store_values = prev->upstream.store_values;
1493     }
1494 
1495     ngx_conf_merge_uint_value(conf->upstream.store_access,
1496                               prev->upstream.store_access, 0600);
1497 
1498     ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,
1499                               prev->upstream.next_upstream_tries, 0);
1500 
1501     ngx_conf_merge_value(conf->upstream.buffering,
1502                               prev->upstream.buffering, 1);
1503 
1504     ngx_conf_merge_value(conf->upstream.request_buffering,
1505                               prev->upstream.request_buffering, 1);
1506 
1507     ngx_conf_merge_value(conf->upstream.ignore_client_abort,
1508                               prev->upstream.ignore_client_abort, 0);
1509 
1510     ngx_conf_merge_value(conf->upstream.force_ranges,
1511                               prev->upstream.force_ranges, 0);
1512 
1513     ngx_conf_merge_ptr_value(conf->upstream.local,
1514                               prev->upstream.local, NULL);
1515 
1516     ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
1517                               prev->upstream.connect_timeout, 60000);
1518 
1519     ngx_conf_merge_msec_value(conf->upstream.send_timeout,
1520                               prev->upstream.send_timeout, 60000);
1521 
1522     ngx_conf_merge_msec_value(conf->upstream.read_timeout,
1523                               prev->upstream.read_timeout, 60000);
1524 
1525     ngx_conf_merge_msec_value(conf->upstream.next_upstream_timeout,
1526                               prev->upstream.next_upstream_timeout, 0);
1527 
1528     ngx_conf_merge_size_value(conf->upstream.send_lowat,
1529                               prev->upstream.send_lowat, 0);
1530 
1531     ngx_conf_merge_size_value(conf->upstream.buffer_size,
1532                               prev->upstream.buffer_size,
1533                               (size_t) ngx_pagesize);
1534 
1535     ngx_conf_merge_size_value(conf->upstream.limit_rate,
1536                               prev->upstream.limit_rate, 0);
1537 
1538 
1539     ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
1540                               8, ngx_pagesize);
1541 
1542     if (conf->upstream.bufs.num < 2) {
1543         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1544                            "there must be at least 2 \"uwsgi_buffers\"");
1545         return NGX_CONF_ERROR;
1546     }
1547 
1548 
1549     size = conf->upstream.buffer_size;
1550     if (size < conf->upstream.bufs.size) {
1551         size = conf->upstream.bufs.size;
1552     }
1553 
1554 
1555     ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
1556                               prev->upstream.busy_buffers_size_conf,
1557                               NGX_CONF_UNSET_SIZE);
1558 
1559     if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
1560         conf->upstream.busy_buffers_size = 2 * size;
1561     } else {
1562         conf->upstream.busy_buffers_size =
1563             conf->upstream.busy_buffers_size_conf;
1564     }
1565 
1566     if (conf->upstream.busy_buffers_size < size) {
1567         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1568             "\"uwsgi_busy_buffers_size\" must be equal to or greater "
1569             "than the maximum of the value of \"uwsgi_buffer_size\" and "
1570             "one of the \"uwsgi_buffers\"");
1571 
1572         return NGX_CONF_ERROR;
1573     }
1574 
1575     if (conf->upstream.busy_buffers_size
1576         > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
1577     {
1578         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1579             "\"uwsgi_busy_buffers_size\" must be less than "
1580             "the size of all \"uwsgi_buffers\" minus one buffer");
1581 
1582         return NGX_CONF_ERROR;
1583     }
1584 
1585 
1586     ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
1587                               prev->upstream.temp_file_write_size_conf,
1588                               NGX_CONF_UNSET_SIZE);
1589 
1590     if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
1591         conf->upstream.temp_file_write_size = 2 * size;
1592     } else {
1593         conf->upstream.temp_file_write_size =
1594             conf->upstream.temp_file_write_size_conf;
1595     }
1596 
1597     if (conf->upstream.temp_file_write_size < size) {
1598         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1599             "\"uwsgi_temp_file_write_size\" must be equal to or greater than "
1600             "the maximum of the value of \"uwsgi_buffer_size\" and "
1601             "one of the \"uwsgi_buffers\"");
1602 
1603         return NGX_CONF_ERROR;
1604     }
1605 
1606 
1607     ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
1608                               prev->upstream.max_temp_file_size_conf,
1609                               NGX_CONF_UNSET_SIZE);
1610 
1611     if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
1612         conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
1613     } else {
1614         conf->upstream.max_temp_file_size =
1615             conf->upstream.max_temp_file_size_conf;
1616     }
1617 
1618     if (conf->upstream.max_temp_file_size != 0
1619         && conf->upstream.max_temp_file_size < size)
1620     {
1621         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1622             "\"uwsgi_max_temp_file_size\" must be equal to zero to disable "
1623             "temporary files usage or must be equal to or greater than "
1624             "the maximum of the value of \"uwsgi_buffer_size\" and "
1625             "one of the \"uwsgi_buffers\"");
1626 
1627         return NGX_CONF_ERROR;
1628     }
1629 
1630 
1631     ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
1632                                  prev->upstream.ignore_headers,
1633                                  NGX_CONF_BITMASK_SET);
1634 
1635 
1636     ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
1637                                  prev->upstream.next_upstream,
1638                                  (NGX_CONF_BITMASK_SET
1639                                   |NGX_HTTP_UPSTREAM_FT_ERROR
1640                                   |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
1641 
1642     if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
1643         conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
1644                                        |NGX_HTTP_UPSTREAM_FT_OFF;
1645     }
1646 
1647     if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
1648                                   prev->upstream.temp_path,
1649                                   &ngx_http_uwsgi_temp_path)
1650         != NGX_OK)
1651     {
1652         return NGX_CONF_ERROR;
1653     }
1654 
1655 #if (NGX_HTTP_CACHE)
1656 
1657     if (conf->upstream.cache == NGX_CONF_UNSET) {
1658         ngx_conf_merge_value(conf->upstream.cache,
1659                               prev->upstream.cache, 0);
1660 
1661         conf->upstream.cache_zone = prev->upstream.cache_zone;
1662         conf->upstream.cache_value = prev->upstream.cache_value;
1663     }
1664 
1665     if (conf->upstream.cache_zone && conf->upstream.cache_zone->data == NULL) {
1666         ngx_shm_zone_t  *shm_zone;
1667 
1668         shm_zone = conf->upstream.cache_zone;
1669 
1670         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1671                            "\"uwsgi_cache\" zone \"%V\" is unknown",
1672                            &shm_zone->shm.name);
1673 
1674         return NGX_CONF_ERROR;
1675     }
1676 
1677     ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
1678                               prev->upstream.cache_min_uses, 1);
1679 
1680     ngx_conf_merge_off_value(conf->upstream.cache_max_range_offset,
1681                               prev->upstream.cache_max_range_offset,
1682                               NGX_MAX_OFF_T_VALUE);
1683 
1684     ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
1685                               prev->upstream.cache_use_stale,
1686                               (NGX_CONF_BITMASK_SET
1687                                |NGX_HTTP_UPSTREAM_FT_OFF));
1688 
1689     if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
1690         conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
1691                                          |NGX_HTTP_UPSTREAM_FT_OFF;
1692     }
1693 
1694     if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
1695         conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
1696     }
1697 
1698     if (conf->upstream.cache_methods == 0) {
1699         conf->upstream.cache_methods = prev->upstream.cache_methods;
1700     }
1701 
1702     conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;
1703 
1704     ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
1705                              prev->upstream.cache_bypass, NULL);
1706 
1707     ngx_conf_merge_ptr_value(conf->upstream.no_cache,
1708                              prev->upstream.no_cache, NULL);
1709 
1710     ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
1711                              prev->upstream.cache_valid, NULL);
1712 
1713     if (conf->cache_key.value.data == NULL) {
1714         conf->cache_key = prev->cache_key;
1715     }
1716 
1717     if (conf->upstream.cache && conf->cache_key.value.data == NULL) {
1718         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1719                            "no \"uwsgi_cache_key\" for \"uwsgi_cache\"");
1720     }
1721 
1722     ngx_conf_merge_value(conf->upstream.cache_lock,
1723                               prev->upstream.cache_lock, 0);
1724 
1725     ngx_conf_merge_msec_value(conf->upstream.cache_lock_timeout,
1726                               prev->upstream.cache_lock_timeout, 5000);
1727 
1728     ngx_conf_merge_msec_value(conf->upstream.cache_lock_age,
1729                               prev->upstream.cache_lock_age, 5000);
1730 
1731     ngx_conf_merge_value(conf->upstream.cache_revalidate,
1732                               prev->upstream.cache_revalidate, 0);
1733 
1734     ngx_conf_merge_value(conf->upstream.cache_background_update,
1735                               prev->upstream.cache_background_update, 0);
1736 
1737 #endif
1738 
1739     ngx_conf_merge_value(conf->upstream.pass_request_headers,
1740                          prev->upstream.pass_request_headers, 1);
1741     ngx_conf_merge_value(conf->upstream.pass_request_body,
1742                          prev->upstream.pass_request_body, 1);
1743 
1744     ngx_conf_merge_value(conf->upstream.intercept_errors,
1745                          prev->upstream.intercept_errors, 0);
1746 
1747 #if (NGX_HTTP_SSL)
1748 
1749     ngx_conf_merge_value(conf->upstream.ssl_session_reuse,
1750                               prev->upstream.ssl_session_reuse, 1);
1751 
1752     ngx_conf_merge_bitmask_value(conf->ssl_protocols, prev->ssl_protocols,
1753                                  (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1
1754                                   |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
1755 
1756     ngx_conf_merge_str_value(conf->ssl_ciphers, prev->ssl_ciphers,
1757                              "DEFAULT");
1758 
1759     if (conf->upstream.ssl_name == NULL) {
1760         conf->upstream.ssl_name = prev->upstream.ssl_name;
1761     }
1762 
1763     ngx_conf_merge_value(conf->upstream.ssl_server_name,
1764                               prev->upstream.ssl_server_name, 0);
1765     ngx_conf_merge_value(conf->upstream.ssl_verify,
1766                               prev->upstream.ssl_verify, 0);
1767     ngx_conf_merge_uint_value(conf->ssl_verify_depth,
1768                               prev->ssl_verify_depth, 1);
1769     ngx_conf_merge_str_value(conf->ssl_trusted_certificate,
1770                               prev->ssl_trusted_certificate, "");
1771     ngx_conf_merge_str_value(conf->ssl_crl, prev->ssl_crl, "");
1772 
1773     ngx_conf_merge_str_value(conf->ssl_certificate,
1774                               prev->ssl_certificate, "");
1775     ngx_conf_merge_str_value(conf->ssl_certificate_key,
1776                               prev->ssl_certificate_key, "");
1777     ngx_conf_merge_ptr_value(conf->ssl_passwords, prev->ssl_passwords, NULL);
1778 
1779     if (conf->ssl && ngx_http_uwsgi_set_ssl(cf, conf) != NGX_OK) {
1780         return NGX_CONF_ERROR;
1781     }
1782 
1783 #endif
1784 
1785     ngx_conf_merge_str_value(conf->uwsgi_string, prev->uwsgi_string, "");
1786 
1787     hash.max_size = 512;
1788     hash.bucket_size = ngx_align(64, ngx_cacheline_size);
1789     hash.name = "uwsgi_hide_headers_hash";
1790 
1791     if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
1792             &prev->upstream, ngx_http_uwsgi_hide_headers, &hash)
1793         != NGX_OK)
1794     {
1795         return NGX_CONF_ERROR;
1796     }
1797 
1798     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
1799 
1800     if (clcf->noname
1801         && conf->upstream.upstream == NULL && conf->uwsgi_lengths == NULL)
1802     {
1803         conf->upstream.upstream = prev->upstream.upstream;
1804 
1805         conf->uwsgi_lengths = prev->uwsgi_lengths;
1806         conf->uwsgi_values = prev->uwsgi_values;
1807 
1808 #if (NGX_HTTP_SSL)
1809         conf->upstream.ssl = prev->upstream.ssl;
1810 #endif
1811     }
1812 
1813     if (clcf->lmt_excpt && clcf->handler == NULL
1814         && (conf->upstream.upstream || conf->uwsgi_lengths))
1815     {
1816         clcf->handler = ngx_http_uwsgi_handler;
1817     }
1818 
1819     ngx_conf_merge_uint_value(conf->modifier1, prev->modifier1, 0);
1820     ngx_conf_merge_uint_value(conf->modifier2, prev->modifier2, 0);
1821 
1822     if (conf->params_source == NULL) {
1823         conf->params = prev->params;
1824 #if (NGX_HTTP_CACHE)
1825         conf->params_cache = prev->params_cache;
1826 #endif
1827         conf->params_source = prev->params_source;
1828     }
1829 
1830     rc = ngx_http_uwsgi_init_params(cf, conf, &conf->params, NULL);
1831     if (rc != NGX_OK) {
1832         return NGX_CONF_ERROR;
1833     }
1834 
1835 #if (NGX_HTTP_CACHE)
1836 
1837     if (conf->upstream.cache) {
1838         rc = ngx_http_uwsgi_init_params(cf, conf, &conf->params_cache,
1839                                         ngx_http_uwsgi_cache_headers);
1840         if (rc != NGX_OK) {
1841             return NGX_CONF_ERROR;
1842         }
1843     }
1844 
1845 #endif
1846 
1847     /*
1848      * special handling to preserve conf->params in the "http" section
1849      * to inherit it to all servers
1850      */
1851 
1852     if (prev->params.hash.buckets == NULL
1853         && conf->params_source == prev->params_source)
1854     {
1855         prev->params = conf->params;
1856 #if (NGX_HTTP_CACHE)
1857         prev->params_cache = conf->params_cache;
1858 #endif
1859     }
1860 
1861     return NGX_CONF_OK;
1862 }
1863 
1864 
1865 static ngx_int_t
1866 ngx_http_uwsgi_init_params(ngx_conf_t *cf, ngx_http_uwsgi_loc_conf_t *conf,
1867     ngx_http_uwsgi_params_t *params, ngx_keyval_t *default_params)
1868 {
1869     u_char                       *p;
1870     size_t                        size;
1871     uintptr_t                    *code;
1872     ngx_uint_t                    i, nsrc;
1873     ngx_array_t                   headers_names, params_merged;
1874     ngx_keyval_t                 *h;
1875     ngx_hash_key_t               *hk;
1876     ngx_hash_init_t               hash;
1877     ngx_http_upstream_param_t    *src, *s;
1878     ngx_http_script_compile_t     sc;
1879     ngx_http_script_copy_code_t  *copy;
1880 
1881     if (params->hash.buckets) {
1882         return NGX_OK;
1883     }
1884 
1885     if (conf->params_source == NULL && default_params == NULL) {
1886         params->hash.buckets = (void *) 1;
1887         return NGX_OK;
1888     }
1889 
1890     params->lengths = ngx_array_create(cf->pool, 64, 1);
1891     if (params->lengths == NULL) {
1892         return NGX_ERROR;
1893     }
1894 
1895     params->values = ngx_array_create(cf->pool, 512, 1);
1896     if (params->values == NULL) {
1897         return NGX_ERROR;
1898     }
1899 
1900     if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
1901         != NGX_OK)
1902     {
1903         return NGX_ERROR;
1904     }
1905 
1906     if (conf->params_source) {
1907         src = conf->params_source->elts;
1908         nsrc = conf->params_source->nelts;
1909 
1910     } else {
1911         src = NULL;
1912         nsrc = 0;
1913     }
1914 
1915     if (default_params) {
1916         if (ngx_array_init(&params_merged, cf->temp_pool, 4,
1917                            sizeof(ngx_http_upstream_param_t))
1918             != NGX_OK)
1919         {
1920             return NGX_ERROR;
1921         }
1922 
1923         for (i = 0; i < nsrc; i++) {
1924 
1925             s = ngx_array_push(&params_merged);
1926             if (s == NULL) {
1927                 return NGX_ERROR;
1928             }
1929 
1930             *s = src[i];
1931         }
1932 
1933         h = default_params;
1934 
1935         while (h->key.len) {
1936 
1937             src = params_merged.elts;
1938             nsrc = params_merged.nelts;
1939 
1940             for (i = 0; i < nsrc; i++) {
1941                 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
1942                     goto next;
1943                 }
1944             }
1945 
1946             s = ngx_array_push(&params_merged);
1947             if (s == NULL) {
1948                 return NGX_ERROR;
1949             }
1950 
1951             s->key = h->key;
1952             s->value = h->value;
1953             s->skip_empty = 1;
1954 
1955         next:
1956 
1957             h++;
1958         }
1959 
1960         src = params_merged.elts;
1961         nsrc = params_merged.nelts;
1962     }
1963 
1964     for (i = 0; i < nsrc; i++) {
1965 
1966         if (src[i].key.len > sizeof("HTTP_") - 1
1967             && ngx_strncmp(src[i].key.data, "HTTP_", sizeof("HTTP_") - 1) == 0)
1968         {
1969             hk = ngx_array_push(&headers_names);
1970             if (hk == NULL) {
1971                 return NGX_ERROR;
1972             }
1973 
1974             hk->key.len = src[i].key.len - 5;
1975             hk->key.data = src[i].key.data + 5;
1976             hk->key_hash = ngx_hash_key_lc(hk->key.data, hk->key.len);
1977             hk->value = (void *) 1;
1978 
1979             if (src[i].value.len == 0) {
1980                 continue;
1981             }
1982         }
1983 
1984         copy = ngx_array_push_n(params->lengths,
1985                                 sizeof(ngx_http_script_copy_code_t));
1986         if (copy == NULL) {
1987             return NGX_ERROR;
1988         }
1989 
1990         copy->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code;
1991         copy->len = src[i].key.len;
1992 
1993         copy = ngx_array_push_n(params->lengths,
1994                                 sizeof(ngx_http_script_copy_code_t));
1995         if (copy == NULL) {
1996             return NGX_ERROR;
1997         }
1998 
1999         copy->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code;
2000         copy->len = src[i].skip_empty;
2001 
2002 
2003         size = (sizeof(ngx_http_script_copy_code_t)
2004                 + src[i].key.len + sizeof(uintptr_t) - 1)
2005                & ~(sizeof(uintptr_t) - 1);
2006 
2007         copy = ngx_array_push_n(params->values, size);
2008         if (copy == NULL) {
2009             return NGX_ERROR;
2010         }
2011 
2012         copy->code = ngx_http_script_copy_code;
2013         copy->len = src[i].key.len;
2014 
2015         p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
2016         ngx_memcpy(p, src[i].key.data, src[i].key.len);
2017 
2018 
2019         ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2020 
2021         sc.cf = cf;
2022         sc.source = &src[i].value;
2023         sc.flushes = &params->flushes;
2024         sc.lengths = &params->lengths;
2025         sc.values = &params->values;
2026 
2027         if (ngx_http_script_compile(&sc) != NGX_OK) {
2028             return NGX_ERROR;
2029         }
2030 
2031         code = ngx_array_push_n(params->lengths, sizeof(uintptr_t));
2032         if (code == NULL) {
2033             return NGX_ERROR;
2034         }
2035 
2036         *code = (uintptr_t) NULL;
2037 
2038 
2039         code = ngx_array_push_n(params->values, sizeof(uintptr_t));
2040         if (code == NULL) {
2041             return NGX_ERROR;
2042         }
2043 
2044         *code = (uintptr_t) NULL;
2045     }
2046 
2047     code = ngx_array_push_n(params->lengths, sizeof(uintptr_t));
2048     if (code == NULL) {
2049         return NGX_ERROR;
2050     }
2051 
2052     *code = (uintptr_t) NULL;
2053 
2054     params->number = headers_names.nelts;
2055 
2056     hash.hash = &params->hash;
2057     hash.key = ngx_hash_key_lc;
2058     hash.max_size = 512;
2059     hash.bucket_size = 64;
2060     hash.name = "uwsgi_params_hash";
2061     hash.pool = cf->pool;
2062     hash.temp_pool = NULL;
2063 
2064     return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts);
2065 }
2066 
2067 
2068 static char *
2069 ngx_http_uwsgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2070 {
2071     ngx_http_uwsgi_loc_conf_t *uwcf = conf;
2072 
2073     size_t                      add;
2074     ngx_url_t                   u;
2075     ngx_str_t                  *value, *url;
2076     ngx_uint_t                  n;
2077     ngx_http_core_loc_conf_t   *clcf;
2078     ngx_http_script_compile_t   sc;
2079 
2080     if (uwcf->upstream.upstream || uwcf->uwsgi_lengths) {
2081         return "is duplicate";
2082     }
2083 
2084     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
2085     clcf->handler = ngx_http_uwsgi_handler;
2086 
2087     value = cf->args->elts;
2088 
2089     url = &value[1];
2090 
2091     n = ngx_http_script_variables_count(url);
2092 
2093     if (n) {
2094 
2095         ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2096 
2097         sc.cf = cf;
2098         sc.source = url;
2099         sc.lengths = &uwcf->uwsgi_lengths;
2100         sc.values = &uwcf->uwsgi_values;
2101         sc.variables = n;
2102         sc.complete_lengths = 1;
2103         sc.complete_values = 1;
2104 
2105         if (ngx_http_script_compile(&sc) != NGX_OK) {
2106             return NGX_CONF_ERROR;
2107         }
2108 
2109 #if (NGX_HTTP_SSL)
2110         uwcf->ssl = 1;
2111 #endif
2112 
2113         return NGX_CONF_OK;
2114     }
2115 
2116     if (ngx_strncasecmp(url->data, (u_char *) "uwsgi://", 8) == 0) {
2117         add = 8;
2118 
2119     } else if (ngx_strncasecmp(url->data, (u_char *) "suwsgi://", 9) == 0) {
2120 
2121 #if (NGX_HTTP_SSL)
2122         add = 9;
2123         uwcf->ssl = 1;
2124 #else
2125         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2126                            "suwsgi protocol requires SSL support");
2127         return NGX_CONF_ERROR;
2128 #endif
2129 
2130     } else {
2131         add = 0;
2132     }
2133 
2134     ngx_memzero(&u, sizeof(ngx_url_t));
2135 
2136     u.url.len = url->len - add;
2137     u.url.data = url->data + add;
2138     u.no_resolve = 1;
2139 
2140     uwcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
2141     if (uwcf->upstream.upstream == NULL) {
2142         return NGX_CONF_ERROR;
2143     }
2144 
2145     if (clcf->name.data[clcf->name.len - 1] == '/') {
2146         clcf->auto_redirect = 1;
2147     }
2148 
2149     return NGX_CONF_OK;
2150 }
2151 
2152 
2153 static char *
2154 ngx_http_uwsgi_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2155 {
2156     ngx_http_uwsgi_loc_conf_t *uwcf = conf;
2157 
2158     ngx_str_t                  *value;
2159     ngx_http_script_compile_t   sc;
2160 
2161     if (uwcf->upstream.store != NGX_CONF_UNSET) {
2162         return "is duplicate";
2163     }
2164 
2165     value = cf->args->elts;
2166 
2167     if (ngx_strcmp(value[1].data, "off") == 0) {
2168         uwcf->upstream.store = 0;
2169         return NGX_CONF_OK;
2170     }
2171 
2172 #if (NGX_HTTP_CACHE)
2173 
2174     if (uwcf->upstream.cache > 0) {
2175         return "is incompatible with \"uwsgi_cache\"";
2176     }
2177 
2178 #endif
2179 
2180     uwcf->upstream.store = 1;
2181 
2182     if (ngx_strcmp(value[1].data, "on") == 0) {
2183         return NGX_CONF_OK;
2184     }
2185 
2186     /* include the terminating '\0' into script */
2187     value[1].len++;
2188 
2189     ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2190 
2191     sc.cf = cf;
2192     sc.source = &value[1];
2193     sc.lengths = &uwcf->upstream.store_lengths;
2194     sc.values = &uwcf->upstream.store_values;
2195     sc.variables = ngx_http_script_variables_count(&value[1]);
2196     sc.complete_lengths = 1;
2197     sc.complete_values = 1;
2198 
2199     if (ngx_http_script_compile(&sc) != NGX_OK) {
2200         return NGX_CONF_ERROR;
2201     }
2202 
2203     return NGX_CONF_OK;
2204 }
2205 
2206 
2207 #if (NGX_HTTP_CACHE)
2208 
2209 static char *
2210 ngx_http_uwsgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2211 {
2212     ngx_http_uwsgi_loc_conf_t *uwcf = conf;
2213 
2214     ngx_str_t                         *value;
2215     ngx_http_complex_value_t           cv;
2216     ngx_http_compile_complex_value_t   ccv;
2217 
2218     value = cf->args->elts;
2219 
2220     if (uwcf->upstream.cache != NGX_CONF_UNSET) {
2221         return "is duplicate";
2222     }
2223 
2224     if (ngx_strcmp(value[1].data, "off") == 0) {
2225         uwcf->upstream.cache = 0;
2226         return NGX_CONF_OK;
2227     }
2228 
2229     if (uwcf->upstream.store > 0) {
2230         return "is incompatible with \"uwsgi_store\"";
2231     }
2232 
2233     uwcf->upstream.cache = 1;
2234 
2235     ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
2236 
2237     ccv.cf = cf;
2238     ccv.value = &value[1];
2239     ccv.complex_value = &cv;
2240 
2241     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
2242         return NGX_CONF_ERROR;
2243     }
2244 
2245     if (cv.lengths != NULL) {
2246 
2247         uwcf->upstream.cache_value = ngx_palloc(cf->pool,
2248                                              sizeof(ngx_http_complex_value_t));
2249         if (uwcf->upstream.cache_value == NULL) {
2250             return NGX_CONF_ERROR;
2251         }
2252 
2253         *uwcf->upstream.cache_value = cv;
2254 
2255         return NGX_CONF_OK;
2256     }
2257 
2258     uwcf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0,
2259                                                       &ngx_http_uwsgi_module);
2260     if (uwcf->upstream.cache_zone == NULL) {
2261         return NGX_CONF_ERROR;
2262     }
2263 
2264     return NGX_CONF_OK;
2265 }
2266 
2267 
2268 static char *
2269 ngx_http_uwsgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2270 {
2271     ngx_http_uwsgi_loc_conf_t *uwcf = conf;
2272 
2273     ngx_str_t                         *value;
2274     ngx_http_compile_complex_value_t   ccv;
2275 
2276     value = cf->args->elts;
2277 
2278     if (uwcf->cache_key.value.data) {
2279         return "is duplicate";
2280     }
2281 
2282     ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
2283 
2284     ccv.cf = cf;
2285     ccv.value = &value[1];
2286     ccv.complex_value = &uwcf->cache_key;
2287 
2288     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
2289         return NGX_CONF_ERROR;
2290     }
2291 
2292     return NGX_CONF_OK;
2293 }
2294 
2295 #endif
2296 
2297 
2298 #if (NGX_HTTP_SSL)
2299 
2300 static char *
2301 ngx_http_uwsgi_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2302 {
2303     ngx_http_uwsgi_loc_conf_t *uwcf = conf;
2304 
2305     ngx_str_t  *value;
2306 
2307     if (uwcf->ssl_passwords != NGX_CONF_UNSET_PTR) {
2308         return "is duplicate";
2309     }
2310 
2311     value = cf->args->elts;
2312 
2313     uwcf->ssl_passwords = ngx_ssl_read_password_file(cf, &value[1]);
2314 
2315     if (uwcf->ssl_passwords == NULL) {
2316         return NGX_CONF_ERROR;
2317     }
2318 
2319     return NGX_CONF_OK;
2320 }
2321 
2322 
2323 static ngx_int_t
2324 ngx_http_uwsgi_set_ssl(ngx_conf_t *cf, ngx_http_uwsgi_loc_conf_t *uwcf)
2325 {
2326     ngx_pool_cleanup_t  *cln;
2327 
2328     uwcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
2329     if (uwcf->upstream.ssl == NULL) {
2330         return NGX_ERROR;
2331     }
2332 
2333     uwcf->upstream.ssl->log = cf->log;
2334 
2335     if (ngx_ssl_create(uwcf->upstream.ssl, uwcf->ssl_protocols, NULL)
2336         != NGX_OK)
2337     {
2338         return NGX_ERROR;
2339     }
2340 
2341     cln = ngx_pool_cleanup_add(cf->pool, 0);
2342     if (cln == NULL) {
2343         return NGX_ERROR;
2344     }
2345 
2346     cln->handler = ngx_ssl_cleanup_ctx;
2347     cln->data = uwcf->upstream.ssl;
2348 
2349     if (uwcf->ssl_certificate.len) {
2350 
2351         if (uwcf->ssl_certificate_key.len == 0) {
2352             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
2353                           "no \"uwsgi_ssl_certificate_key\" is defined "
2354                           "for certificate \"%V\"", &uwcf->ssl_certificate);
2355             return NGX_ERROR;
2356         }
2357 
2358         if (ngx_ssl_certificate(cf, uwcf->upstream.ssl, &uwcf->ssl_certificate,
2359                                 &uwcf->ssl_certificate_key, uwcf->ssl_passwords)
2360             != NGX_OK)
2361         {
2362             return NGX_ERROR;
2363         }
2364     }
2365 
2366     if (ngx_ssl_ciphers(cf, uwcf->upstream.ssl, &uwcf->ssl_ciphers, 0)
2367         != NGX_OK)
2368     {
2369         return NGX_ERROR;
2370     }
2371 
2372     if (uwcf->upstream.ssl_verify) {
2373         if (uwcf->ssl_trusted_certificate.len == 0) {
2374             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
2375                       "no uwsgi_ssl_trusted_certificate for uwsgi_ssl_verify");
2376             return NGX_ERROR;
2377         }
2378 
2379         if (ngx_ssl_trusted_certificate(cf, uwcf->upstream.ssl,
2380                                         &uwcf->ssl_trusted_certificate,
2381                                         uwcf->ssl_verify_depth)
2382             != NGX_OK)
2383         {
2384             return NGX_ERROR;
2385         }
2386 
2387         if (ngx_ssl_crl(cf, uwcf->upstream.ssl, &uwcf->ssl_crl) != NGX_OK) {
2388             return NGX_ERROR;
2389         }
2390     }
2391 
2392     return NGX_OK;
2393 }
2394 
2395 #endif