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) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_http.h>
0011 
0012 
0013 typedef struct {
0014     u_char    *name;
0015     uint32_t   method;
0016 } ngx_http_method_name_t;
0017 
0018 
0019 #define NGX_HTTP_REQUEST_BODY_FILE_OFF    0
0020 #define NGX_HTTP_REQUEST_BODY_FILE_ON     1
0021 #define NGX_HTTP_REQUEST_BODY_FILE_CLEAN  2
0022 
0023 
0024 static ngx_int_t ngx_http_core_find_location(ngx_http_request_t *r);
0025 static ngx_int_t ngx_http_core_find_static_location(ngx_http_request_t *r,
0026     ngx_http_location_tree_node_t *node);
0027 
0028 static ngx_int_t ngx_http_core_preconfiguration(ngx_conf_t *cf);
0029 static ngx_int_t ngx_http_core_postconfiguration(ngx_conf_t *cf);
0030 static void *ngx_http_core_create_main_conf(ngx_conf_t *cf);
0031 static char *ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf);
0032 static void *ngx_http_core_create_srv_conf(ngx_conf_t *cf);
0033 static char *ngx_http_core_merge_srv_conf(ngx_conf_t *cf,
0034     void *parent, void *child);
0035 static void *ngx_http_core_create_loc_conf(ngx_conf_t *cf);
0036 static char *ngx_http_core_merge_loc_conf(ngx_conf_t *cf,
0037     void *parent, void *child);
0038 
0039 static char *ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd,
0040     void *dummy);
0041 static char *ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd,
0042     void *dummy);
0043 static ngx_int_t ngx_http_core_regex_location(ngx_conf_t *cf,
0044     ngx_http_core_loc_conf_t *clcf, ngx_str_t *regex, ngx_uint_t caseless);
0045 
0046 static char *ngx_http_core_types(ngx_conf_t *cf, ngx_command_t *cmd,
0047     void *conf);
0048 static char *ngx_http_core_type(ngx_conf_t *cf, ngx_command_t *dummy,
0049     void *conf);
0050 
0051 static char *ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd,
0052     void *conf);
0053 static char *ngx_http_core_server_name(ngx_conf_t *cf, ngx_command_t *cmd,
0054     void *conf);
0055 static char *ngx_http_core_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
0056 static char *ngx_http_core_limit_except(ngx_conf_t *cf, ngx_command_t *cmd,
0057     void *conf);
0058 static char *ngx_http_core_set_aio(ngx_conf_t *cf, ngx_command_t *cmd,
0059     void *conf);
0060 static char *ngx_http_core_directio(ngx_conf_t *cf, ngx_command_t *cmd,
0061     void *conf);
0062 static char *ngx_http_core_error_page(ngx_conf_t *cf, ngx_command_t *cmd,
0063     void *conf);
0064 static char *ngx_http_core_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
0065     void *conf);
0066 static char *ngx_http_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd,
0067     void *conf);
0068 static char *ngx_http_core_keepalive(ngx_conf_t *cf, ngx_command_t *cmd,
0069     void *conf);
0070 static char *ngx_http_core_internal(ngx_conf_t *cf, ngx_command_t *cmd,
0071     void *conf);
0072 static char *ngx_http_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd,
0073     void *conf);
0074 #if (NGX_HTTP_GZIP)
0075 static ngx_int_t ngx_http_gzip_accept_encoding(ngx_str_t *ae);
0076 static ngx_uint_t ngx_http_gzip_quantity(u_char *p, u_char *last);
0077 static char *ngx_http_gzip_disable(ngx_conf_t *cf, ngx_command_t *cmd,
0078     void *conf);
0079 #endif
0080 static ngx_int_t ngx_http_get_forwarded_addr_internal(ngx_http_request_t *r,
0081     ngx_addr_t *addr, u_char *xff, size_t xfflen, ngx_array_t *proxies,
0082     int recursive);
0083 #if (NGX_HAVE_OPENAT)
0084 static char *ngx_http_disable_symlinks(ngx_conf_t *cf, ngx_command_t *cmd,
0085     void *conf);
0086 #endif
0087 
0088 static char *ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data);
0089 static char *ngx_http_core_pool_size(ngx_conf_t *cf, void *post, void *data);
0090 
0091 static ngx_conf_post_t  ngx_http_core_lowat_post =
0092     { ngx_http_core_lowat_check };
0093 
0094 static ngx_conf_post_handler_pt  ngx_http_core_pool_size_p =
0095     ngx_http_core_pool_size;
0096 
0097 
0098 static ngx_conf_enum_t  ngx_http_core_request_body_in_file[] = {
0099     { ngx_string("off"), NGX_HTTP_REQUEST_BODY_FILE_OFF },
0100     { ngx_string("on"), NGX_HTTP_REQUEST_BODY_FILE_ON },
0101     { ngx_string("clean"), NGX_HTTP_REQUEST_BODY_FILE_CLEAN },
0102     { ngx_null_string, 0 }
0103 };
0104 
0105 
0106 static ngx_conf_enum_t  ngx_http_core_satisfy[] = {
0107     { ngx_string("all"), NGX_HTTP_SATISFY_ALL },
0108     { ngx_string("any"), NGX_HTTP_SATISFY_ANY },
0109     { ngx_null_string, 0 }
0110 };
0111 
0112 
0113 static ngx_conf_enum_t  ngx_http_core_lingering_close[] = {
0114     { ngx_string("off"), NGX_HTTP_LINGERING_OFF },
0115     { ngx_string("on"), NGX_HTTP_LINGERING_ON },
0116     { ngx_string("always"), NGX_HTTP_LINGERING_ALWAYS },
0117     { ngx_null_string, 0 }
0118 };
0119 
0120 
0121 static ngx_conf_enum_t  ngx_http_core_server_tokens[] = {
0122     { ngx_string("off"), NGX_HTTP_SERVER_TOKENS_OFF },
0123     { ngx_string("on"), NGX_HTTP_SERVER_TOKENS_ON },
0124     { ngx_string("build"), NGX_HTTP_SERVER_TOKENS_BUILD },
0125     { ngx_null_string, 0 }
0126 };
0127 
0128 
0129 static ngx_conf_enum_t  ngx_http_core_if_modified_since[] = {
0130     { ngx_string("off"), NGX_HTTP_IMS_OFF },
0131     { ngx_string("exact"), NGX_HTTP_IMS_EXACT },
0132     { ngx_string("before"), NGX_HTTP_IMS_BEFORE },
0133     { ngx_null_string, 0 }
0134 };
0135 
0136 
0137 static ngx_conf_bitmask_t  ngx_http_core_keepalive_disable[] = {
0138     { ngx_string("none"), NGX_HTTP_KEEPALIVE_DISABLE_NONE },
0139     { ngx_string("msie6"), NGX_HTTP_KEEPALIVE_DISABLE_MSIE6 },
0140     { ngx_string("safari"), NGX_HTTP_KEEPALIVE_DISABLE_SAFARI },
0141     { ngx_null_string, 0 }
0142 };
0143 
0144 
0145 static ngx_path_init_t  ngx_http_client_temp_path = {
0146     ngx_string(NGX_HTTP_CLIENT_TEMP_PATH), { 0, 0, 0 }
0147 };
0148 
0149 
0150 #if (NGX_HTTP_GZIP)
0151 
0152 static ngx_conf_enum_t  ngx_http_gzip_http_version[] = {
0153     { ngx_string("1.0"), NGX_HTTP_VERSION_10 },
0154     { ngx_string("1.1"), NGX_HTTP_VERSION_11 },
0155     { ngx_null_string, 0 }
0156 };
0157 
0158 
0159 static ngx_conf_bitmask_t  ngx_http_gzip_proxied_mask[] = {
0160     { ngx_string("off"), NGX_HTTP_GZIP_PROXIED_OFF },
0161     { ngx_string("expired"), NGX_HTTP_GZIP_PROXIED_EXPIRED },
0162     { ngx_string("no-cache"), NGX_HTTP_GZIP_PROXIED_NO_CACHE },
0163     { ngx_string("no-store"), NGX_HTTP_GZIP_PROXIED_NO_STORE },
0164     { ngx_string("private"), NGX_HTTP_GZIP_PROXIED_PRIVATE },
0165     { ngx_string("no_last_modified"), NGX_HTTP_GZIP_PROXIED_NO_LM },
0166     { ngx_string("no_etag"), NGX_HTTP_GZIP_PROXIED_NO_ETAG },
0167     { ngx_string("auth"), NGX_HTTP_GZIP_PROXIED_AUTH },
0168     { ngx_string("any"), NGX_HTTP_GZIP_PROXIED_ANY },
0169     { ngx_null_string, 0 }
0170 };
0171 
0172 
0173 static ngx_str_t  ngx_http_gzip_no_cache = ngx_string("no-cache");
0174 static ngx_str_t  ngx_http_gzip_no_store = ngx_string("no-store");
0175 static ngx_str_t  ngx_http_gzip_private = ngx_string("private");
0176 
0177 #endif
0178 
0179 
0180 static ngx_command_t  ngx_http_core_commands[] = {
0181 
0182     { ngx_string("variables_hash_max_size"),
0183       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
0184       ngx_conf_set_num_slot,
0185       NGX_HTTP_MAIN_CONF_OFFSET,
0186       offsetof(ngx_http_core_main_conf_t, variables_hash_max_size),
0187       NULL },
0188 
0189     { ngx_string("variables_hash_bucket_size"),
0190       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
0191       ngx_conf_set_num_slot,
0192       NGX_HTTP_MAIN_CONF_OFFSET,
0193       offsetof(ngx_http_core_main_conf_t, variables_hash_bucket_size),
0194       NULL },
0195 
0196     { ngx_string("server_names_hash_max_size"),
0197       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
0198       ngx_conf_set_num_slot,
0199       NGX_HTTP_MAIN_CONF_OFFSET,
0200       offsetof(ngx_http_core_main_conf_t, server_names_hash_max_size),
0201       NULL },
0202 
0203     { ngx_string("server_names_hash_bucket_size"),
0204       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
0205       ngx_conf_set_num_slot,
0206       NGX_HTTP_MAIN_CONF_OFFSET,
0207       offsetof(ngx_http_core_main_conf_t, server_names_hash_bucket_size),
0208       NULL },
0209 
0210     { ngx_string("server"),
0211       NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
0212       ngx_http_core_server,
0213       0,
0214       0,
0215       NULL },
0216 
0217     { ngx_string("connection_pool_size"),
0218       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
0219       ngx_conf_set_size_slot,
0220       NGX_HTTP_SRV_CONF_OFFSET,
0221       offsetof(ngx_http_core_srv_conf_t, connection_pool_size),
0222       &ngx_http_core_pool_size_p },
0223 
0224     { ngx_string("request_pool_size"),
0225       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
0226       ngx_conf_set_size_slot,
0227       NGX_HTTP_SRV_CONF_OFFSET,
0228       offsetof(ngx_http_core_srv_conf_t, request_pool_size),
0229       &ngx_http_core_pool_size_p },
0230 
0231     { ngx_string("client_header_timeout"),
0232       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
0233       ngx_conf_set_msec_slot,
0234       NGX_HTTP_SRV_CONF_OFFSET,
0235       offsetof(ngx_http_core_srv_conf_t, client_header_timeout),
0236       NULL },
0237 
0238     { ngx_string("client_header_buffer_size"),
0239       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
0240       ngx_conf_set_size_slot,
0241       NGX_HTTP_SRV_CONF_OFFSET,
0242       offsetof(ngx_http_core_srv_conf_t, client_header_buffer_size),
0243       NULL },
0244 
0245     { ngx_string("large_client_header_buffers"),
0246       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE2,
0247       ngx_conf_set_bufs_slot,
0248       NGX_HTTP_SRV_CONF_OFFSET,
0249       offsetof(ngx_http_core_srv_conf_t, large_client_header_buffers),
0250       NULL },
0251 
0252     { ngx_string("ignore_invalid_headers"),
0253       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
0254       ngx_conf_set_flag_slot,
0255       NGX_HTTP_SRV_CONF_OFFSET,
0256       offsetof(ngx_http_core_srv_conf_t, ignore_invalid_headers),
0257       NULL },
0258 
0259     { ngx_string("merge_slashes"),
0260       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
0261       ngx_conf_set_flag_slot,
0262       NGX_HTTP_SRV_CONF_OFFSET,
0263       offsetof(ngx_http_core_srv_conf_t, merge_slashes),
0264       NULL },
0265 
0266     { ngx_string("underscores_in_headers"),
0267       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
0268       ngx_conf_set_flag_slot,
0269       NGX_HTTP_SRV_CONF_OFFSET,
0270       offsetof(ngx_http_core_srv_conf_t, underscores_in_headers),
0271       NULL },
0272 
0273     { ngx_string("location"),
0274       NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12,
0275       ngx_http_core_location,
0276       NGX_HTTP_SRV_CONF_OFFSET,
0277       0,
0278       NULL },
0279 
0280     { ngx_string("listen"),
0281       NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
0282       ngx_http_core_listen,
0283       NGX_HTTP_SRV_CONF_OFFSET,
0284       0,
0285       NULL },
0286 
0287     { ngx_string("server_name"),
0288       NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
0289       ngx_http_core_server_name,
0290       NGX_HTTP_SRV_CONF_OFFSET,
0291       0,
0292       NULL },
0293 
0294     { ngx_string("types_hash_max_size"),
0295       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0296       ngx_conf_set_num_slot,
0297       NGX_HTTP_LOC_CONF_OFFSET,
0298       offsetof(ngx_http_core_loc_conf_t, types_hash_max_size),
0299       NULL },
0300 
0301     { ngx_string("types_hash_bucket_size"),
0302       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0303       ngx_conf_set_num_slot,
0304       NGX_HTTP_LOC_CONF_OFFSET,
0305       offsetof(ngx_http_core_loc_conf_t, types_hash_bucket_size),
0306       NULL },
0307 
0308     { ngx_string("types"),
0309       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
0310                                           |NGX_CONF_BLOCK|NGX_CONF_NOARGS,
0311       ngx_http_core_types,
0312       NGX_HTTP_LOC_CONF_OFFSET,
0313       0,
0314       NULL },
0315 
0316     { ngx_string("default_type"),
0317       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0318       ngx_conf_set_str_slot,
0319       NGX_HTTP_LOC_CONF_OFFSET,
0320       offsetof(ngx_http_core_loc_conf_t, default_type),
0321       NULL },
0322 
0323     { ngx_string("root"),
0324       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
0325                         |NGX_CONF_TAKE1,
0326       ngx_http_core_root,
0327       NGX_HTTP_LOC_CONF_OFFSET,
0328       0,
0329       NULL },
0330 
0331     { ngx_string("alias"),
0332       NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0333       ngx_http_core_root,
0334       NGX_HTTP_LOC_CONF_OFFSET,
0335       0,
0336       NULL },
0337 
0338     { ngx_string("limit_except"),
0339       NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_1MORE,
0340       ngx_http_core_limit_except,
0341       NGX_HTTP_LOC_CONF_OFFSET,
0342       0,
0343       NULL },
0344 
0345     { ngx_string("client_max_body_size"),
0346       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0347       ngx_conf_set_off_slot,
0348       NGX_HTTP_LOC_CONF_OFFSET,
0349       offsetof(ngx_http_core_loc_conf_t, client_max_body_size),
0350       NULL },
0351 
0352     { ngx_string("client_body_buffer_size"),
0353       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0354       ngx_conf_set_size_slot,
0355       NGX_HTTP_LOC_CONF_OFFSET,
0356       offsetof(ngx_http_core_loc_conf_t, client_body_buffer_size),
0357       NULL },
0358 
0359     { ngx_string("client_body_timeout"),
0360       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0361       ngx_conf_set_msec_slot,
0362       NGX_HTTP_LOC_CONF_OFFSET,
0363       offsetof(ngx_http_core_loc_conf_t, client_body_timeout),
0364       NULL },
0365 
0366     { ngx_string("client_body_temp_path"),
0367       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
0368       ngx_conf_set_path_slot,
0369       NGX_HTTP_LOC_CONF_OFFSET,
0370       offsetof(ngx_http_core_loc_conf_t, client_body_temp_path),
0371       NULL },
0372 
0373     { ngx_string("client_body_in_file_only"),
0374       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0375       ngx_conf_set_enum_slot,
0376       NGX_HTTP_LOC_CONF_OFFSET,
0377       offsetof(ngx_http_core_loc_conf_t, client_body_in_file_only),
0378       &ngx_http_core_request_body_in_file },
0379 
0380     { ngx_string("client_body_in_single_buffer"),
0381       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0382       ngx_conf_set_flag_slot,
0383       NGX_HTTP_LOC_CONF_OFFSET,
0384       offsetof(ngx_http_core_loc_conf_t, client_body_in_single_buffer),
0385       NULL },
0386 
0387     { ngx_string("sendfile"),
0388       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
0389                         |NGX_CONF_FLAG,
0390       ngx_conf_set_flag_slot,
0391       NGX_HTTP_LOC_CONF_OFFSET,
0392       offsetof(ngx_http_core_loc_conf_t, sendfile),
0393       NULL },
0394 
0395     { ngx_string("sendfile_max_chunk"),
0396       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0397       ngx_conf_set_size_slot,
0398       NGX_HTTP_LOC_CONF_OFFSET,
0399       offsetof(ngx_http_core_loc_conf_t, sendfile_max_chunk),
0400       NULL },
0401 
0402     { ngx_string("subrequest_output_buffer_size"),
0403       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0404       ngx_conf_set_size_slot,
0405       NGX_HTTP_LOC_CONF_OFFSET,
0406       offsetof(ngx_http_core_loc_conf_t, subrequest_output_buffer_size),
0407       NULL },
0408 
0409     { ngx_string("aio"),
0410       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0411       ngx_http_core_set_aio,
0412       NGX_HTTP_LOC_CONF_OFFSET,
0413       0,
0414       NULL },
0415 
0416     { ngx_string("aio_write"),
0417       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0418       ngx_conf_set_flag_slot,
0419       NGX_HTTP_LOC_CONF_OFFSET,
0420       offsetof(ngx_http_core_loc_conf_t, aio_write),
0421       NULL },
0422 
0423     { ngx_string("read_ahead"),
0424       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0425       ngx_conf_set_size_slot,
0426       NGX_HTTP_LOC_CONF_OFFSET,
0427       offsetof(ngx_http_core_loc_conf_t, read_ahead),
0428       NULL },
0429 
0430     { ngx_string("directio"),
0431       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0432       ngx_http_core_directio,
0433       NGX_HTTP_LOC_CONF_OFFSET,
0434       0,
0435       NULL },
0436 
0437     { ngx_string("directio_alignment"),
0438       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0439       ngx_conf_set_off_slot,
0440       NGX_HTTP_LOC_CONF_OFFSET,
0441       offsetof(ngx_http_core_loc_conf_t, directio_alignment),
0442       NULL },
0443 
0444     { ngx_string("tcp_nopush"),
0445       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0446       ngx_conf_set_flag_slot,
0447       NGX_HTTP_LOC_CONF_OFFSET,
0448       offsetof(ngx_http_core_loc_conf_t, tcp_nopush),
0449       NULL },
0450 
0451     { ngx_string("tcp_nodelay"),
0452       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0453       ngx_conf_set_flag_slot,
0454       NGX_HTTP_LOC_CONF_OFFSET,
0455       offsetof(ngx_http_core_loc_conf_t, tcp_nodelay),
0456       NULL },
0457 
0458     { ngx_string("send_timeout"),
0459       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0460       ngx_conf_set_msec_slot,
0461       NGX_HTTP_LOC_CONF_OFFSET,
0462       offsetof(ngx_http_core_loc_conf_t, send_timeout),
0463       NULL },
0464 
0465     { ngx_string("send_lowat"),
0466       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0467       ngx_conf_set_size_slot,
0468       NGX_HTTP_LOC_CONF_OFFSET,
0469       offsetof(ngx_http_core_loc_conf_t, send_lowat),
0470       &ngx_http_core_lowat_post },
0471 
0472     { ngx_string("postpone_output"),
0473       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0474       ngx_conf_set_size_slot,
0475       NGX_HTTP_LOC_CONF_OFFSET,
0476       offsetof(ngx_http_core_loc_conf_t, postpone_output),
0477       NULL },
0478 
0479     { ngx_string("limit_rate"),
0480       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
0481                         |NGX_CONF_TAKE1,
0482       ngx_conf_set_size_slot,
0483       NGX_HTTP_LOC_CONF_OFFSET,
0484       offsetof(ngx_http_core_loc_conf_t, limit_rate),
0485       NULL },
0486 
0487     { ngx_string("limit_rate_after"),
0488       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
0489                         |NGX_CONF_TAKE1,
0490       ngx_conf_set_size_slot,
0491       NGX_HTTP_LOC_CONF_OFFSET,
0492       offsetof(ngx_http_core_loc_conf_t, limit_rate_after),
0493       NULL },
0494 
0495     { ngx_string("keepalive_timeout"),
0496       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
0497       ngx_http_core_keepalive,
0498       NGX_HTTP_LOC_CONF_OFFSET,
0499       0,
0500       NULL },
0501 
0502     { ngx_string("keepalive_requests"),
0503       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0504       ngx_conf_set_num_slot,
0505       NGX_HTTP_LOC_CONF_OFFSET,
0506       offsetof(ngx_http_core_loc_conf_t, keepalive_requests),
0507       NULL },
0508 
0509     { ngx_string("keepalive_disable"),
0510       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
0511       ngx_conf_set_bitmask_slot,
0512       NGX_HTTP_LOC_CONF_OFFSET,
0513       offsetof(ngx_http_core_loc_conf_t, keepalive_disable),
0514       &ngx_http_core_keepalive_disable },
0515 
0516     { ngx_string("satisfy"),
0517       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0518       ngx_conf_set_enum_slot,
0519       NGX_HTTP_LOC_CONF_OFFSET,
0520       offsetof(ngx_http_core_loc_conf_t, satisfy),
0521       &ngx_http_core_satisfy },
0522 
0523     { ngx_string("internal"),
0524       NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
0525       ngx_http_core_internal,
0526       NGX_HTTP_LOC_CONF_OFFSET,
0527       0,
0528       NULL },
0529 
0530     { ngx_string("lingering_close"),
0531       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0532       ngx_conf_set_enum_slot,
0533       NGX_HTTP_LOC_CONF_OFFSET,
0534       offsetof(ngx_http_core_loc_conf_t, lingering_close),
0535       &ngx_http_core_lingering_close },
0536 
0537     { ngx_string("lingering_time"),
0538       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0539       ngx_conf_set_msec_slot,
0540       NGX_HTTP_LOC_CONF_OFFSET,
0541       offsetof(ngx_http_core_loc_conf_t, lingering_time),
0542       NULL },
0543 
0544     { ngx_string("lingering_timeout"),
0545       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0546       ngx_conf_set_msec_slot,
0547       NGX_HTTP_LOC_CONF_OFFSET,
0548       offsetof(ngx_http_core_loc_conf_t, lingering_timeout),
0549       NULL },
0550 
0551     { ngx_string("reset_timedout_connection"),
0552       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0553       ngx_conf_set_flag_slot,
0554       NGX_HTTP_LOC_CONF_OFFSET,
0555       offsetof(ngx_http_core_loc_conf_t, reset_timedout_connection),
0556       NULL },
0557 
0558     { ngx_string("absolute_redirect"),
0559       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0560       ngx_conf_set_flag_slot,
0561       NGX_HTTP_LOC_CONF_OFFSET,
0562       offsetof(ngx_http_core_loc_conf_t, absolute_redirect),
0563       NULL },
0564 
0565     { ngx_string("server_name_in_redirect"),
0566       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0567       ngx_conf_set_flag_slot,
0568       NGX_HTTP_LOC_CONF_OFFSET,
0569       offsetof(ngx_http_core_loc_conf_t, server_name_in_redirect),
0570       NULL },
0571 
0572     { ngx_string("port_in_redirect"),
0573       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0574       ngx_conf_set_flag_slot,
0575       NGX_HTTP_LOC_CONF_OFFSET,
0576       offsetof(ngx_http_core_loc_conf_t, port_in_redirect),
0577       NULL },
0578 
0579     { ngx_string("msie_padding"),
0580       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0581       ngx_conf_set_flag_slot,
0582       NGX_HTTP_LOC_CONF_OFFSET,
0583       offsetof(ngx_http_core_loc_conf_t, msie_padding),
0584       NULL },
0585 
0586     { ngx_string("msie_refresh"),
0587       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0588       ngx_conf_set_flag_slot,
0589       NGX_HTTP_LOC_CONF_OFFSET,
0590       offsetof(ngx_http_core_loc_conf_t, msie_refresh),
0591       NULL },
0592 
0593     { ngx_string("log_not_found"),
0594       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0595       ngx_conf_set_flag_slot,
0596       NGX_HTTP_LOC_CONF_OFFSET,
0597       offsetof(ngx_http_core_loc_conf_t, log_not_found),
0598       NULL },
0599 
0600     { ngx_string("log_subrequest"),
0601       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0602       ngx_conf_set_flag_slot,
0603       NGX_HTTP_LOC_CONF_OFFSET,
0604       offsetof(ngx_http_core_loc_conf_t, log_subrequest),
0605       NULL },
0606 
0607     { ngx_string("recursive_error_pages"),
0608       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0609       ngx_conf_set_flag_slot,
0610       NGX_HTTP_LOC_CONF_OFFSET,
0611       offsetof(ngx_http_core_loc_conf_t, recursive_error_pages),
0612       NULL },
0613 
0614     { ngx_string("server_tokens"),
0615       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0616       ngx_conf_set_enum_slot,
0617       NGX_HTTP_LOC_CONF_OFFSET,
0618       offsetof(ngx_http_core_loc_conf_t, server_tokens),
0619       &ngx_http_core_server_tokens },
0620 
0621     { ngx_string("if_modified_since"),
0622       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0623       ngx_conf_set_enum_slot,
0624       NGX_HTTP_LOC_CONF_OFFSET,
0625       offsetof(ngx_http_core_loc_conf_t, if_modified_since),
0626       &ngx_http_core_if_modified_since },
0627 
0628     { ngx_string("max_ranges"),
0629       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0630       ngx_conf_set_num_slot,
0631       NGX_HTTP_LOC_CONF_OFFSET,
0632       offsetof(ngx_http_core_loc_conf_t, max_ranges),
0633       NULL },
0634 
0635     { ngx_string("chunked_transfer_encoding"),
0636       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0637       ngx_conf_set_flag_slot,
0638       NGX_HTTP_LOC_CONF_OFFSET,
0639       offsetof(ngx_http_core_loc_conf_t, chunked_transfer_encoding),
0640       NULL },
0641 
0642     { ngx_string("etag"),
0643       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0644       ngx_conf_set_flag_slot,
0645       NGX_HTTP_LOC_CONF_OFFSET,
0646       offsetof(ngx_http_core_loc_conf_t, etag),
0647       NULL },
0648 
0649     { ngx_string("error_page"),
0650       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
0651                         |NGX_CONF_2MORE,
0652       ngx_http_core_error_page,
0653       NGX_HTTP_LOC_CONF_OFFSET,
0654       0,
0655       NULL },
0656 
0657     { ngx_string("post_action"),
0658       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
0659                         |NGX_CONF_TAKE1,
0660       ngx_conf_set_str_slot,
0661       NGX_HTTP_LOC_CONF_OFFSET,
0662       offsetof(ngx_http_core_loc_conf_t, post_action),
0663       NULL },
0664 
0665     { ngx_string("error_log"),
0666       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0667       ngx_http_core_error_log,
0668       NGX_HTTP_LOC_CONF_OFFSET,
0669       0,
0670       NULL },
0671 
0672     { ngx_string("open_file_cache"),
0673       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
0674       ngx_http_core_open_file_cache,
0675       NGX_HTTP_LOC_CONF_OFFSET,
0676       offsetof(ngx_http_core_loc_conf_t, open_file_cache),
0677       NULL },
0678 
0679     { ngx_string("open_file_cache_valid"),
0680       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0681       ngx_conf_set_sec_slot,
0682       NGX_HTTP_LOC_CONF_OFFSET,
0683       offsetof(ngx_http_core_loc_conf_t, open_file_cache_valid),
0684       NULL },
0685 
0686     { ngx_string("open_file_cache_min_uses"),
0687       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0688       ngx_conf_set_num_slot,
0689       NGX_HTTP_LOC_CONF_OFFSET,
0690       offsetof(ngx_http_core_loc_conf_t, open_file_cache_min_uses),
0691       NULL },
0692 
0693     { ngx_string("open_file_cache_errors"),
0694       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0695       ngx_conf_set_flag_slot,
0696       NGX_HTTP_LOC_CONF_OFFSET,
0697       offsetof(ngx_http_core_loc_conf_t, open_file_cache_errors),
0698       NULL },
0699 
0700     { ngx_string("open_file_cache_events"),
0701       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0702       ngx_conf_set_flag_slot,
0703       NGX_HTTP_LOC_CONF_OFFSET,
0704       offsetof(ngx_http_core_loc_conf_t, open_file_cache_events),
0705       NULL },
0706 
0707     { ngx_string("resolver"),
0708       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0709       ngx_http_core_resolver,
0710       NGX_HTTP_LOC_CONF_OFFSET,
0711       0,
0712       NULL },
0713 
0714     { ngx_string("resolver_timeout"),
0715       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0716       ngx_conf_set_msec_slot,
0717       NGX_HTTP_LOC_CONF_OFFSET,
0718       offsetof(ngx_http_core_loc_conf_t, resolver_timeout),
0719       NULL },
0720 
0721 #if (NGX_HTTP_GZIP)
0722 
0723     { ngx_string("gzip_vary"),
0724       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0725       ngx_conf_set_flag_slot,
0726       NGX_HTTP_LOC_CONF_OFFSET,
0727       offsetof(ngx_http_core_loc_conf_t, gzip_vary),
0728       NULL },
0729 
0730     { ngx_string("gzip_http_version"),
0731       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0732       ngx_conf_set_enum_slot,
0733       NGX_HTTP_LOC_CONF_OFFSET,
0734       offsetof(ngx_http_core_loc_conf_t, gzip_http_version),
0735       &ngx_http_gzip_http_version },
0736 
0737     { ngx_string("gzip_proxied"),
0738       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0739       ngx_conf_set_bitmask_slot,
0740       NGX_HTTP_LOC_CONF_OFFSET,
0741       offsetof(ngx_http_core_loc_conf_t, gzip_proxied),
0742       &ngx_http_gzip_proxied_mask },
0743 
0744     { ngx_string("gzip_disable"),
0745       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
0746       ngx_http_gzip_disable,
0747       NGX_HTTP_LOC_CONF_OFFSET,
0748       0,
0749       NULL },
0750 
0751 #endif
0752 
0753 #if (NGX_HAVE_OPENAT)
0754 
0755     { ngx_string("disable_symlinks"),
0756       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
0757       ngx_http_disable_symlinks,
0758       NGX_HTTP_LOC_CONF_OFFSET,
0759       0,
0760       NULL },
0761 
0762 #endif
0763 
0764       ngx_null_command
0765 };
0766 
0767 
0768 static ngx_http_module_t  ngx_http_core_module_ctx = {
0769     ngx_http_core_preconfiguration,        /* preconfiguration */
0770     ngx_http_core_postconfiguration,       /* postconfiguration */
0771 
0772     ngx_http_core_create_main_conf,        /* create main configuration */
0773     ngx_http_core_init_main_conf,          /* init main configuration */
0774 
0775     ngx_http_core_create_srv_conf,         /* create server configuration */
0776     ngx_http_core_merge_srv_conf,          /* merge server configuration */
0777 
0778     ngx_http_core_create_loc_conf,         /* create location configuration */
0779     ngx_http_core_merge_loc_conf           /* merge location configuration */
0780 };
0781 
0782 
0783 ngx_module_t  ngx_http_core_module = {
0784     NGX_MODULE_V1,
0785     &ngx_http_core_module_ctx,             /* module context */
0786     ngx_http_core_commands,                /* module directives */
0787     NGX_HTTP_MODULE,                       /* module type */
0788     NULL,                                  /* init master */
0789     NULL,                                  /* init module */
0790     NULL,                                  /* init process */
0791     NULL,                                  /* init thread */
0792     NULL,                                  /* exit thread */
0793     NULL,                                  /* exit process */
0794     NULL,                                  /* exit master */
0795     NGX_MODULE_V1_PADDING
0796 };
0797 
0798 
0799 ngx_str_t  ngx_http_core_get_method = { 3, (u_char *) "GET" };
0800 
0801 
0802 void
0803 ngx_http_handler(ngx_http_request_t *r)
0804 {
0805     ngx_http_core_main_conf_t  *cmcf;
0806 
0807     r->connection->log->action = NULL;
0808 
0809     if (!r->internal) {
0810         switch (r->headers_in.connection_type) {
0811         case 0:
0812             r->keepalive = (r->http_version > NGX_HTTP_VERSION_10);
0813             break;
0814 
0815         case NGX_HTTP_CONNECTION_CLOSE:
0816             r->keepalive = 0;
0817             break;
0818 
0819         case NGX_HTTP_CONNECTION_KEEP_ALIVE:
0820             r->keepalive = 1;
0821             break;
0822         }
0823 
0824         r->lingering_close = (r->headers_in.content_length_n > 0
0825                               || r->headers_in.chunked);
0826         r->phase_handler = 0;
0827 
0828     } else {
0829         cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
0830         r->phase_handler = cmcf->phase_engine.server_rewrite_index;
0831     }
0832 
0833     r->valid_location = 1;
0834 #if (NGX_HTTP_GZIP)
0835     r->gzip_tested = 0;
0836     r->gzip_ok = 0;
0837     r->gzip_vary = 0;
0838 #endif
0839 
0840     r->write_event_handler = ngx_http_core_run_phases;
0841     ngx_http_core_run_phases(r);
0842 }
0843 
0844 
0845 void
0846 ngx_http_core_run_phases(ngx_http_request_t *r)
0847 {
0848     ngx_int_t                   rc;
0849     ngx_http_phase_handler_t   *ph;
0850     ngx_http_core_main_conf_t  *cmcf;
0851 
0852     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
0853 
0854     ph = cmcf->phase_engine.handlers;
0855 
0856     while (ph[r->phase_handler].checker) {
0857 
0858         rc = ph[r->phase_handler].checker(r, &ph[r->phase_handler]);
0859 
0860         if (rc == NGX_OK) {
0861             return;
0862         }
0863     }
0864 }
0865 
0866 
0867 ngx_int_t
0868 ngx_http_core_generic_phase(ngx_http_request_t *r, ngx_http_phase_handler_t *ph)
0869 {
0870     ngx_int_t  rc;
0871 
0872     /*
0873      * generic phase checker,
0874      * used by the post read and pre-access phases
0875      */
0876 
0877     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0878                    "generic phase: %ui", r->phase_handler);
0879 
0880     rc = ph->handler(r);
0881 
0882     if (rc == NGX_OK) {
0883         r->phase_handler = ph->next;
0884         return NGX_AGAIN;
0885     }
0886 
0887     if (rc == NGX_DECLINED) {
0888         r->phase_handler++;
0889         return NGX_AGAIN;
0890     }
0891 
0892     if (rc == NGX_AGAIN || rc == NGX_DONE) {
0893         return NGX_OK;
0894     }
0895 
0896     /* rc == NGX_ERROR || rc == NGX_HTTP_...  */
0897 
0898     ngx_http_finalize_request(r, rc);
0899 
0900     return NGX_OK;
0901 }
0902 
0903 
0904 ngx_int_t
0905 ngx_http_core_rewrite_phase(ngx_http_request_t *r, ngx_http_phase_handler_t *ph)
0906 {
0907     ngx_int_t  rc;
0908 
0909     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0910                    "rewrite phase: %ui", r->phase_handler);
0911 
0912     rc = ph->handler(r);
0913 
0914     if (rc == NGX_DECLINED) {
0915         r->phase_handler++;
0916         return NGX_AGAIN;
0917     }
0918 
0919     if (rc == NGX_DONE) {
0920         return NGX_OK;
0921     }
0922 
0923     /* NGX_OK, NGX_AGAIN, NGX_ERROR, NGX_HTTP_...  */
0924 
0925     ngx_http_finalize_request(r, rc);
0926 
0927     return NGX_OK;
0928 }
0929 
0930 
0931 ngx_int_t
0932 ngx_http_core_find_config_phase(ngx_http_request_t *r,
0933     ngx_http_phase_handler_t *ph)
0934 {
0935     u_char                    *p;
0936     size_t                     len;
0937     ngx_int_t                  rc;
0938     ngx_http_core_loc_conf_t  *clcf;
0939 
0940     r->content_handler = NULL;
0941     r->uri_changed = 0;
0942 
0943     rc = ngx_http_core_find_location(r);
0944 
0945     if (rc == NGX_ERROR) {
0946         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
0947         return NGX_OK;
0948     }
0949 
0950     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
0951 
0952     if (!r->internal && clcf->internal) {
0953         ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
0954         return NGX_OK;
0955     }
0956 
0957     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0958                    "using configuration \"%s%V\"",
0959                    (clcf->noname ? "*" : (clcf->exact_match ? "=" : "")),
0960                    &clcf->name);
0961 
0962     ngx_http_update_location_config(r);
0963 
0964     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0965                    "http cl:%O max:%O",
0966                    r->headers_in.content_length_n, clcf->client_max_body_size);
0967 
0968     if (r->headers_in.content_length_n != -1
0969         && !r->discard_body
0970         && clcf->client_max_body_size
0971         && clcf->client_max_body_size < r->headers_in.content_length_n)
0972     {
0973         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
0974                       "client intended to send too large body: %O bytes",
0975                       r->headers_in.content_length_n);
0976 
0977         r->expect_tested = 1;
0978         (void) ngx_http_discard_request_body(r);
0979         ngx_http_finalize_request(r, NGX_HTTP_REQUEST_ENTITY_TOO_LARGE);
0980         return NGX_OK;
0981     }
0982 
0983     if (rc == NGX_DONE) {
0984         ngx_http_clear_location(r);
0985 
0986         r->headers_out.location = ngx_list_push(&r->headers_out.headers);
0987         if (r->headers_out.location == NULL) {
0988             ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
0989             return NGX_OK;
0990         }
0991 
0992         r->headers_out.location->hash = 1;
0993         ngx_str_set(&r->headers_out.location->key, "Location");
0994 
0995         if (r->args.len == 0) {
0996             r->headers_out.location->value = clcf->name;
0997 
0998         } else {
0999             len = clcf->name.len + 1 + r->args.len;
1000             p = ngx_pnalloc(r->pool, len);
1001 
1002             if (p == NULL) {
1003                 ngx_http_clear_location(r);
1004                 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1005                 return NGX_OK;
1006             }
1007 
1008             r->headers_out.location->value.len = len;
1009             r->headers_out.location->value.data = p;
1010 
1011             p = ngx_cpymem(p, clcf->name.data, clcf->name.len);
1012             *p++ = '?';
1013             ngx_memcpy(p, r->args.data, r->args.len);
1014         }
1015 
1016         ngx_http_finalize_request(r, NGX_HTTP_MOVED_PERMANENTLY);
1017         return NGX_OK;
1018     }
1019 
1020     r->phase_handler++;
1021     return NGX_AGAIN;
1022 }
1023 
1024 
1025 ngx_int_t
1026 ngx_http_core_post_rewrite_phase(ngx_http_request_t *r,
1027     ngx_http_phase_handler_t *ph)
1028 {
1029     ngx_http_core_srv_conf_t  *cscf;
1030 
1031     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1032                    "post rewrite phase: %ui", r->phase_handler);
1033 
1034     if (!r->uri_changed) {
1035         r->phase_handler++;
1036         return NGX_AGAIN;
1037     }
1038 
1039     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1040                    "uri changes: %d", r->uri_changes);
1041 
1042     /*
1043      * gcc before 3.3 compiles the broken code for
1044      *     if (r->uri_changes-- == 0)
1045      * if the r->uri_changes is defined as
1046      *     unsigned  uri_changes:4
1047      */
1048 
1049     r->uri_changes--;
1050 
1051     if (r->uri_changes == 0) {
1052         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1053                       "rewrite or internal redirection cycle "
1054                       "while processing \"%V\"", &r->uri);
1055 
1056         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1057         return NGX_OK;
1058     }
1059 
1060     r->phase_handler = ph->next;
1061 
1062     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1063     r->loc_conf = cscf->ctx->loc_conf;
1064 
1065     return NGX_AGAIN;
1066 }
1067 
1068 
1069 ngx_int_t
1070 ngx_http_core_access_phase(ngx_http_request_t *r, ngx_http_phase_handler_t *ph)
1071 {
1072     ngx_int_t                  rc;
1073     ngx_http_core_loc_conf_t  *clcf;
1074 
1075     if (r != r->main) {
1076         r->phase_handler = ph->next;
1077         return NGX_AGAIN;
1078     }
1079 
1080     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1081                    "access phase: %ui", r->phase_handler);
1082 
1083     rc = ph->handler(r);
1084 
1085     if (rc == NGX_DECLINED) {
1086         r->phase_handler++;
1087         return NGX_AGAIN;
1088     }
1089 
1090     if (rc == NGX_AGAIN || rc == NGX_DONE) {
1091         return NGX_OK;
1092     }
1093 
1094     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1095 
1096     if (clcf->satisfy == NGX_HTTP_SATISFY_ALL) {
1097 
1098         if (rc == NGX_OK) {
1099             r->phase_handler++;
1100             return NGX_AGAIN;
1101         }
1102 
1103     } else {
1104         if (rc == NGX_OK) {
1105             r->access_code = 0;
1106 
1107             if (r->headers_out.www_authenticate) {
1108                 r->headers_out.www_authenticate->hash = 0;
1109             }
1110 
1111             r->phase_handler = ph->next;
1112             return NGX_AGAIN;
1113         }
1114 
1115         if (rc == NGX_HTTP_FORBIDDEN || rc == NGX_HTTP_UNAUTHORIZED) {
1116             if (r->access_code != NGX_HTTP_UNAUTHORIZED) {
1117                 r->access_code = rc;
1118             }
1119 
1120             r->phase_handler++;
1121             return NGX_AGAIN;
1122         }
1123     }
1124 
1125     /* rc == NGX_ERROR || rc == NGX_HTTP_...  */
1126 
1127     ngx_http_finalize_request(r, rc);
1128     return NGX_OK;
1129 }
1130 
1131 
1132 ngx_int_t
1133 ngx_http_core_post_access_phase(ngx_http_request_t *r,
1134     ngx_http_phase_handler_t *ph)
1135 {
1136     ngx_int_t  access_code;
1137 
1138     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1139                    "post access phase: %ui", r->phase_handler);
1140 
1141     access_code = r->access_code;
1142 
1143     if (access_code) {
1144         if (access_code == NGX_HTTP_FORBIDDEN) {
1145             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1146                           "access forbidden by rule");
1147         }
1148 
1149         r->access_code = 0;
1150         ngx_http_finalize_request(r, access_code);
1151         return NGX_OK;
1152     }
1153 
1154     r->phase_handler++;
1155     return NGX_AGAIN;
1156 }
1157 
1158 
1159 ngx_int_t
1160 ngx_http_core_content_phase(ngx_http_request_t *r,
1161     ngx_http_phase_handler_t *ph)
1162 {
1163     size_t     root;
1164     ngx_int_t  rc;
1165     ngx_str_t  path;
1166 
1167     if (r->content_handler) {
1168         r->write_event_handler = ngx_http_request_empty_handler;
1169         ngx_http_finalize_request(r, r->content_handler(r));
1170         return NGX_OK;
1171     }
1172 
1173     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1174                    "content phase: %ui", r->phase_handler);
1175 
1176     rc = ph->handler(r);
1177 
1178     if (rc != NGX_DECLINED) {
1179         ngx_http_finalize_request(r, rc);
1180         return NGX_OK;
1181     }
1182 
1183     /* rc == NGX_DECLINED */
1184 
1185     ph++;
1186 
1187     if (ph->checker) {
1188         r->phase_handler++;
1189         return NGX_AGAIN;
1190     }
1191 
1192     /* no content handler was found */
1193 
1194     if (r->uri.data[r->uri.len - 1] == '/') {
1195 
1196         if (ngx_http_map_uri_to_path(r, &path, &root, 0) != NULL) {
1197             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1198                           "directory index of \"%s\" is forbidden", path.data);
1199         }
1200 
1201         ngx_http_finalize_request(r, NGX_HTTP_FORBIDDEN);
1202         return NGX_OK;
1203     }
1204 
1205     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no handler found");
1206 
1207     ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
1208     return NGX_OK;
1209 }
1210 
1211 
1212 void
1213 ngx_http_update_location_config(ngx_http_request_t *r)
1214 {
1215     ngx_http_core_loc_conf_t  *clcf;
1216 
1217     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1218 
1219     if (r->method & clcf->limit_except) {
1220         r->loc_conf = clcf->limit_except_loc_conf;
1221         clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1222     }
1223 
1224     if (r == r->main) {
1225         ngx_set_connection_log(r->connection, clcf->error_log);
1226     }
1227 
1228     if ((ngx_io.flags & NGX_IO_SENDFILE) && clcf->sendfile) {
1229         r->connection->sendfile = 1;
1230 
1231     } else {
1232         r->connection->sendfile = 0;
1233     }
1234 
1235     if (clcf->client_body_in_file_only) {
1236         r->request_body_in_file_only = 1;
1237         r->request_body_in_persistent_file = 1;
1238         r->request_body_in_clean_file =
1239             clcf->client_body_in_file_only == NGX_HTTP_REQUEST_BODY_FILE_CLEAN;
1240         r->request_body_file_log_level = NGX_LOG_NOTICE;
1241 
1242     } else {
1243         r->request_body_file_log_level = NGX_LOG_WARN;
1244     }
1245 
1246     r->request_body_in_single_buf = clcf->client_body_in_single_buffer;
1247 
1248     if (r->keepalive) {
1249         if (clcf->keepalive_timeout == 0) {
1250             r->keepalive = 0;
1251 
1252         } else if (r->connection->requests >= clcf->keepalive_requests) {
1253             r->keepalive = 0;
1254 
1255         } else if (r->headers_in.msie6
1256                    && r->method == NGX_HTTP_POST
1257                    && (clcf->keepalive_disable
1258                        & NGX_HTTP_KEEPALIVE_DISABLE_MSIE6))
1259         {
1260             /*
1261              * MSIE may wait for some time if an response for
1262              * a POST request was sent over a keepalive connection
1263              */
1264             r->keepalive = 0;
1265 
1266         } else if (r->headers_in.safari
1267                    && (clcf->keepalive_disable
1268                        & NGX_HTTP_KEEPALIVE_DISABLE_SAFARI))
1269         {
1270             /*
1271              * Safari may send a POST request to a closed keepalive
1272              * connection and may stall for some time, see
1273              *     https://bugs.webkit.org/show_bug.cgi?id=5760
1274              */
1275             r->keepalive = 0;
1276         }
1277     }
1278 
1279     if (!clcf->tcp_nopush) {
1280         /* disable TCP_NOPUSH/TCP_CORK use */
1281         r->connection->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;
1282     }
1283 
1284     if (r->limit_rate == 0) {
1285         r->limit_rate = clcf->limit_rate;
1286     }
1287 
1288     if (clcf->handler) {
1289         r->content_handler = clcf->handler;
1290     }
1291 }
1292 
1293 
1294 /*
1295  * NGX_OK       - exact or regex match
1296  * NGX_DONE     - auto redirect
1297  * NGX_AGAIN    - inclusive match
1298  * NGX_ERROR    - regex error
1299  * NGX_DECLINED - no match
1300  */
1301 
1302 static ngx_int_t
1303 ngx_http_core_find_location(ngx_http_request_t *r)
1304 {
1305     ngx_int_t                  rc;
1306     ngx_http_core_loc_conf_t  *pclcf;
1307 #if (NGX_PCRE)
1308     ngx_int_t                  n;
1309     ngx_uint_t                 noregex;
1310     ngx_http_core_loc_conf_t  *clcf, **clcfp;
1311 
1312     noregex = 0;
1313 #endif
1314 
1315     pclcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1316 
1317     rc = ngx_http_core_find_static_location(r, pclcf->static_locations);
1318 
1319     if (rc == NGX_AGAIN) {
1320 
1321 #if (NGX_PCRE)
1322         clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1323 
1324         noregex = clcf->noregex;
1325 #endif
1326 
1327         /* look up nested locations */
1328 
1329         rc = ngx_http_core_find_location(r);
1330     }
1331 
1332     if (rc == NGX_OK || rc == NGX_DONE) {
1333         return rc;
1334     }
1335 
1336     /* rc == NGX_DECLINED or rc == NGX_AGAIN in nested location */
1337 
1338 #if (NGX_PCRE)
1339 
1340     if (noregex == 0 && pclcf->regex_locations) {
1341 
1342         for (clcfp = pclcf->regex_locations; *clcfp; clcfp++) {
1343 
1344             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1345                            "test location: ~ \"%V\"", &(*clcfp)->name);
1346 
1347             n = ngx_http_regex_exec(r, (*clcfp)->regex, &r->uri);
1348 
1349             if (n == NGX_OK) {
1350                 r->loc_conf = (*clcfp)->loc_conf;
1351 
1352                 /* look up nested locations */
1353 
1354                 rc = ngx_http_core_find_location(r);
1355 
1356                 return (rc == NGX_ERROR) ? rc : NGX_OK;
1357             }
1358 
1359             if (n == NGX_DECLINED) {
1360                 continue;
1361             }
1362 
1363             return NGX_ERROR;
1364         }
1365     }
1366 #endif
1367 
1368     return rc;
1369 }
1370 
1371 
1372 /*
1373  * NGX_OK       - exact match
1374  * NGX_DONE     - auto redirect
1375  * NGX_AGAIN    - inclusive match
1376  * NGX_DECLINED - no match
1377  */
1378 
1379 static ngx_int_t
1380 ngx_http_core_find_static_location(ngx_http_request_t *r,
1381     ngx_http_location_tree_node_t *node)
1382 {
1383     u_char     *uri;
1384     size_t      len, n;
1385     ngx_int_t   rc, rv;
1386 
1387     len = r->uri.len;
1388     uri = r->uri.data;
1389 
1390     rv = NGX_DECLINED;
1391 
1392     for ( ;; ) {
1393 
1394         if (node == NULL) {
1395             return rv;
1396         }
1397 
1398         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1399                        "test location: \"%*s\"",
1400                        (size_t) node->len, node->name);
1401 
1402         n = (len <= (size_t) node->len) ? len : node->len;
1403 
1404         rc = ngx_filename_cmp(uri, node->name, n);
1405 
1406         if (rc != 0) {
1407             node = (rc < 0) ? node->left : node->right;
1408 
1409             continue;
1410         }
1411 
1412         if (len > (size_t) node->len) {
1413 
1414             if (node->inclusive) {
1415 
1416                 r->loc_conf = node->inclusive->loc_conf;
1417                 rv = NGX_AGAIN;
1418 
1419                 node = node->tree;
1420                 uri += n;
1421                 len -= n;
1422 
1423                 continue;
1424             }
1425 
1426             /* exact only */
1427 
1428             node = node->right;
1429 
1430             continue;
1431         }
1432 
1433         if (len == (size_t) node->len) {
1434 
1435             if (node->exact) {
1436                 r->loc_conf = node->exact->loc_conf;
1437                 return NGX_OK;
1438 
1439             } else {
1440                 r->loc_conf = node->inclusive->loc_conf;
1441                 return NGX_AGAIN;
1442             }
1443         }
1444 
1445         /* len < node->len */
1446 
1447         if (len + 1 == (size_t) node->len && node->auto_redirect) {
1448 
1449             r->loc_conf = (node->exact) ? node->exact->loc_conf:
1450                                           node->inclusive->loc_conf;
1451             rv = NGX_DONE;
1452         }
1453 
1454         node = node->left;
1455     }
1456 }
1457 
1458 
1459 void *
1460 ngx_http_test_content_type(ngx_http_request_t *r, ngx_hash_t *types_hash)
1461 {
1462     u_char      c, *lowcase;
1463     size_t      len;
1464     ngx_uint_t  i, hash;
1465 
1466     if (types_hash->size == 0) {
1467         return (void *) 4;
1468     }
1469 
1470     if (r->headers_out.content_type.len == 0) {
1471         return NULL;
1472     }
1473 
1474     len = r->headers_out.content_type_len;
1475 
1476     if (r->headers_out.content_type_lowcase == NULL) {
1477 
1478         lowcase = ngx_pnalloc(r->pool, len);
1479         if (lowcase == NULL) {
1480             return NULL;
1481         }
1482 
1483         r->headers_out.content_type_lowcase = lowcase;
1484 
1485         hash = 0;
1486 
1487         for (i = 0; i < len; i++) {
1488             c = ngx_tolower(r->headers_out.content_type.data[i]);
1489             hash = ngx_hash(hash, c);
1490             lowcase[i] = c;
1491         }
1492 
1493         r->headers_out.content_type_hash = hash;
1494     }
1495 
1496     return ngx_hash_find(types_hash, r->headers_out.content_type_hash,
1497                          r->headers_out.content_type_lowcase, len);
1498 }
1499 
1500 
1501 ngx_int_t
1502 ngx_http_set_content_type(ngx_http_request_t *r)
1503 {
1504     u_char                     c, *exten;
1505     ngx_str_t                 *type;
1506     ngx_uint_t                 i, hash;
1507     ngx_http_core_loc_conf_t  *clcf;
1508 
1509     if (r->headers_out.content_type.len) {
1510         return NGX_OK;
1511     }
1512 
1513     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1514 
1515     if (r->exten.len) {
1516 
1517         hash = 0;
1518 
1519         for (i = 0; i < r->exten.len; i++) {
1520             c = r->exten.data[i];
1521 
1522             if (c >= 'A' && c <= 'Z') {
1523 
1524                 exten = ngx_pnalloc(r->pool, r->exten.len);
1525                 if (exten == NULL) {
1526                     return NGX_ERROR;
1527                 }
1528 
1529                 hash = ngx_hash_strlow(exten, r->exten.data, r->exten.len);
1530 
1531                 r->exten.data = exten;
1532 
1533                 break;
1534             }
1535 
1536             hash = ngx_hash(hash, c);
1537         }
1538 
1539         type = ngx_hash_find(&clcf->types_hash, hash,
1540                              r->exten.data, r->exten.len);
1541 
1542         if (type) {
1543             r->headers_out.content_type_len = type->len;
1544             r->headers_out.content_type = *type;
1545 
1546             return NGX_OK;
1547         }
1548     }
1549 
1550     r->headers_out.content_type_len = clcf->default_type.len;
1551     r->headers_out.content_type = clcf->default_type;
1552 
1553     return NGX_OK;
1554 }
1555 
1556 
1557 void
1558 ngx_http_set_exten(ngx_http_request_t *r)
1559 {
1560     ngx_int_t  i;
1561 
1562     ngx_str_null(&r->exten);
1563 
1564     for (i = r->uri.len - 1; i > 1; i--) {
1565         if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') {
1566 
1567             r->exten.len = r->uri.len - i - 1;
1568             r->exten.data = &r->uri.data[i + 1];
1569 
1570             return;
1571 
1572         } else if (r->uri.data[i] == '/') {
1573             return;
1574         }
1575     }
1576 
1577     return;
1578 }
1579 
1580 
1581 ngx_int_t
1582 ngx_http_set_etag(ngx_http_request_t *r)
1583 {
1584     ngx_table_elt_t           *etag;
1585     ngx_http_core_loc_conf_t  *clcf;
1586 
1587     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1588 
1589     if (!clcf->etag) {
1590         return NGX_OK;
1591     }
1592 
1593     etag = ngx_list_push(&r->headers_out.headers);
1594     if (etag == NULL) {
1595         return NGX_ERROR;
1596     }
1597 
1598     etag->hash = 1;
1599     ngx_str_set(&etag->key, "ETag");
1600 
1601     etag->value.data = ngx_pnalloc(r->pool, NGX_OFF_T_LEN + NGX_TIME_T_LEN + 3);
1602     if (etag->value.data == NULL) {
1603         etag->hash = 0;
1604         return NGX_ERROR;
1605     }
1606 
1607     etag->value.len = ngx_sprintf(etag->value.data, "\"%xT-%xO\"",
1608                                   r->headers_out.last_modified_time,
1609                                   r->headers_out.content_length_n)
1610                       - etag->value.data;
1611 
1612     r->headers_out.etag = etag;
1613 
1614     return NGX_OK;
1615 }
1616 
1617 
1618 void
1619 ngx_http_weak_etag(ngx_http_request_t *r)
1620 {
1621     size_t            len;
1622     u_char           *p;
1623     ngx_table_elt_t  *etag;
1624 
1625     etag = r->headers_out.etag;
1626 
1627     if (etag == NULL) {
1628         return;
1629     }
1630 
1631     if (etag->value.len > 2
1632         && etag->value.data[0] == 'W'
1633         && etag->value.data[1] == '/')
1634     {
1635         return;
1636     }
1637 
1638     if (etag->value.len < 1 || etag->value.data[0] != '"') {
1639         r->headers_out.etag->hash = 0;
1640         r->headers_out.etag = NULL;
1641         return;
1642     }
1643 
1644     p = ngx_pnalloc(r->pool, etag->value.len + 2);
1645     if (p == NULL) {
1646         r->headers_out.etag->hash = 0;
1647         r->headers_out.etag = NULL;
1648         return;
1649     }
1650 
1651     len = ngx_sprintf(p, "W/%V", &etag->value) - p;
1652 
1653     etag->value.data = p;
1654     etag->value.len = len;
1655 }
1656 
1657 
1658 ngx_int_t
1659 ngx_http_send_response(ngx_http_request_t *r, ngx_uint_t status,
1660     ngx_str_t *ct, ngx_http_complex_value_t *cv)
1661 {
1662     ngx_int_t     rc;
1663     ngx_str_t     val;
1664     ngx_buf_t    *b;
1665     ngx_chain_t   out;
1666 
1667     if (ngx_http_discard_request_body(r) != NGX_OK) {
1668         return NGX_HTTP_INTERNAL_SERVER_ERROR;
1669     }
1670 
1671     r->headers_out.status = status;
1672 
1673     if (ngx_http_complex_value(r, cv, &val) != NGX_OK) {
1674         return NGX_HTTP_INTERNAL_SERVER_ERROR;
1675     }
1676 
1677     if (status == NGX_HTTP_MOVED_PERMANENTLY
1678         || status == NGX_HTTP_MOVED_TEMPORARILY
1679         || status == NGX_HTTP_SEE_OTHER
1680         || status == NGX_HTTP_TEMPORARY_REDIRECT
1681         || status == NGX_HTTP_PERMANENT_REDIRECT)
1682     {
1683         ngx_http_clear_location(r);
1684 
1685         r->headers_out.location = ngx_list_push(&r->headers_out.headers);
1686         if (r->headers_out.location == NULL) {
1687             return NGX_HTTP_INTERNAL_SERVER_ERROR;
1688         }
1689 
1690         r->headers_out.location->hash = 1;
1691         ngx_str_set(&r->headers_out.location->key, "Location");
1692         r->headers_out.location->value = val;
1693 
1694         return status;
1695     }
1696 
1697     r->headers_out.content_length_n = val.len;
1698 
1699     if (ct) {
1700         r->headers_out.content_type_len = ct->len;
1701         r->headers_out.content_type = *ct;
1702 
1703     } else {
1704         if (ngx_http_set_content_type(r) != NGX_OK) {
1705             return NGX_HTTP_INTERNAL_SERVER_ERROR;
1706         }
1707     }
1708 
1709     if (r->method == NGX_HTTP_HEAD || (r != r->main && val.len == 0)) {
1710         return ngx_http_send_header(r);
1711     }
1712 
1713     b = ngx_calloc_buf(r->pool);
1714     if (b == NULL) {
1715         return NGX_HTTP_INTERNAL_SERVER_ERROR;
1716     }
1717 
1718     b->pos = val.data;
1719     b->last = val.data + val.len;
1720     b->memory = val.len ? 1 : 0;
1721     b->last_buf = (r == r->main) ? 1 : 0;
1722     b->last_in_chain = 1;
1723 
1724     out.buf = b;
1725     out.next = NULL;
1726 
1727     rc = ngx_http_send_header(r);
1728 
1729     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
1730         return rc;
1731     }
1732 
1733     return ngx_http_output_filter(r, &out);
1734 }
1735 
1736 
1737 ngx_int_t
1738 ngx_http_send_header(ngx_http_request_t *r)
1739 {
1740     if (r->post_action) {
1741         return NGX_OK;
1742     }
1743 
1744     if (r->header_sent) {
1745         ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1746                       "header already sent");
1747         return NGX_ERROR;
1748     }
1749 
1750     if (r->err_status) {
1751         r->headers_out.status = r->err_status;
1752         r->headers_out.status_line.len = 0;
1753     }
1754 
1755     return ngx_http_top_header_filter(r);
1756 }
1757 
1758 
1759 ngx_int_t
1760 ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *in)
1761 {
1762     ngx_int_t          rc;
1763     ngx_connection_t  *c;
1764 
1765     c = r->connection;
1766 
1767     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1768                    "http output filter \"%V?%V\"", &r->uri, &r->args);
1769 
1770     rc = ngx_http_top_body_filter(r, in);
1771 
1772     if (rc == NGX_ERROR) {
1773         /* NGX_ERROR may be returned by any filter */
1774         c->error = 1;
1775     }
1776 
1777     return rc;
1778 }
1779 
1780 
1781 u_char *
1782 ngx_http_map_uri_to_path(ngx_http_request_t *r, ngx_str_t *path,
1783     size_t *root_length, size_t reserved)
1784 {
1785     u_char                    *last;
1786     size_t                     alias;
1787     ngx_http_core_loc_conf_t  *clcf;
1788 
1789     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1790 
1791     alias = clcf->alias;
1792 
1793     if (alias && !r->valid_location) {
1794         ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1795                       "\"alias\" cannot be used in location \"%V\" "
1796                       "where URI was rewritten", &clcf->name);
1797         return NULL;
1798     }
1799 
1800     if (clcf->root_lengths == NULL) {
1801 
1802         *root_length = clcf->root.len;
1803 
1804         path->len = clcf->root.len + reserved + r->uri.len - alias + 1;
1805 
1806         path->data = ngx_pnalloc(r->pool, path->len);
1807         if (path->data == NULL) {
1808             return NULL;
1809         }
1810 
1811         last = ngx_copy(path->data, clcf->root.data, clcf->root.len);
1812 
1813     } else {
1814 
1815         if (alias == NGX_MAX_SIZE_T_VALUE) {
1816             reserved += r->add_uri_to_alias ? r->uri.len + 1 : 1;
1817 
1818         } else {
1819             reserved += r->uri.len - alias + 1;
1820         }
1821 
1822         if (ngx_http_script_run(r, path, clcf->root_lengths->elts, reserved,
1823                                 clcf->root_values->elts)
1824             == NULL)
1825         {
1826             return NULL;
1827         }
1828 
1829         if (ngx_get_full_name(r->pool, (ngx_str_t *) &ngx_cycle->prefix, path)
1830             != NGX_OK)
1831         {
1832             return NULL;
1833         }
1834 
1835         *root_length = path->len - reserved;
1836         last = path->data + *root_length;
1837 
1838         if (alias == NGX_MAX_SIZE_T_VALUE) {
1839             if (!r->add_uri_to_alias) {
1840                 *last = '\0';
1841                 return last;
1842             }
1843 
1844             alias = 0;
1845         }
1846     }
1847 
1848     last = ngx_cpystrn(last, r->uri.data + alias, r->uri.len - alias + 1);
1849 
1850     return last;
1851 }
1852 
1853 
1854 ngx_int_t
1855 ngx_http_auth_basic_user(ngx_http_request_t *r)
1856 {
1857     ngx_str_t   auth, encoded;
1858     ngx_uint_t  len;
1859 
1860     if (r->headers_in.user.len == 0 && r->headers_in.user.data != NULL) {
1861         return NGX_DECLINED;
1862     }
1863 
1864     if (r->headers_in.authorization == NULL) {
1865         r->headers_in.user.data = (u_char *) "";
1866         return NGX_DECLINED;
1867     }
1868 
1869     encoded = r->headers_in.authorization->value;
1870 
1871     if (encoded.len < sizeof("Basic ") - 1
1872         || ngx_strncasecmp(encoded.data, (u_char *) "Basic ",
1873                            sizeof("Basic ") - 1)
1874            != 0)
1875     {
1876         r->headers_in.user.data = (u_char *) "";
1877         return NGX_DECLINED;
1878     }
1879 
1880     encoded.len -= sizeof("Basic ") - 1;
1881     encoded.data += sizeof("Basic ") - 1;
1882 
1883     while (encoded.len && encoded.data[0] == ' ') {
1884         encoded.len--;
1885         encoded.data++;
1886     }
1887 
1888     if (encoded.len == 0) {
1889         r->headers_in.user.data = (u_char *) "";
1890         return NGX_DECLINED;
1891     }
1892 
1893     auth.len = ngx_base64_decoded_length(encoded.len);
1894     auth.data = ngx_pnalloc(r->pool, auth.len + 1);
1895     if (auth.data == NULL) {
1896         return NGX_ERROR;
1897     }
1898 
1899     if (ngx_decode_base64(&auth, &encoded) != NGX_OK) {
1900         r->headers_in.user.data = (u_char *) "";
1901         return NGX_DECLINED;
1902     }
1903 
1904     auth.data[auth.len] = '\0';
1905 
1906     for (len = 0; len < auth.len; len++) {
1907         if (auth.data[len] == ':') {
1908             break;
1909         }
1910     }
1911 
1912     if (len == 0 || len == auth.len) {
1913         r->headers_in.user.data = (u_char *) "";
1914         return NGX_DECLINED;
1915     }
1916 
1917     r->headers_in.user.len = len;
1918     r->headers_in.user.data = auth.data;
1919     r->headers_in.passwd.len = auth.len - len - 1;
1920     r->headers_in.passwd.data = &auth.data[len + 1];
1921 
1922     return NGX_OK;
1923 }
1924 
1925 
1926 #if (NGX_HTTP_GZIP)
1927 
1928 ngx_int_t
1929 ngx_http_gzip_ok(ngx_http_request_t *r)
1930 {
1931     time_t                     date, expires;
1932     ngx_uint_t                 p;
1933     ngx_array_t               *cc;
1934     ngx_table_elt_t           *e, *d, *ae;
1935     ngx_http_core_loc_conf_t  *clcf;
1936 
1937     r->gzip_tested = 1;
1938 
1939     if (r != r->main) {
1940         return NGX_DECLINED;
1941     }
1942 
1943     ae = r->headers_in.accept_encoding;
1944     if (ae == NULL) {
1945         return NGX_DECLINED;
1946     }
1947 
1948     if (ae->value.len < sizeof("gzip") - 1) {
1949         return NGX_DECLINED;
1950     }
1951 
1952     /*
1953      * test first for the most common case "gzip,...":
1954      *   MSIE:    "gzip, deflate"
1955      *   Firefox: "gzip,deflate"
1956      *   Chrome:  "gzip,deflate,sdch"
1957      *   Safari:  "gzip, deflate"
1958      *   Opera:   "gzip, deflate"
1959      */
1960 
1961     if (ngx_memcmp(ae->value.data, "gzip,", 5) != 0
1962         && ngx_http_gzip_accept_encoding(&ae->value) != NGX_OK)
1963     {
1964         return NGX_DECLINED;
1965     }
1966 
1967     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1968 
1969     if (r->headers_in.msie6 && clcf->gzip_disable_msie6) {
1970         return NGX_DECLINED;
1971     }
1972 
1973     if (r->http_version < clcf->gzip_http_version) {
1974         return NGX_DECLINED;
1975     }
1976 
1977     if (r->headers_in.via == NULL) {
1978         goto ok;
1979     }
1980 
1981     p = clcf->gzip_proxied;
1982 
1983     if (p & NGX_HTTP_GZIP_PROXIED_OFF) {
1984         return NGX_DECLINED;
1985     }
1986 
1987     if (p & NGX_HTTP_GZIP_PROXIED_ANY) {
1988         goto ok;
1989     }
1990 
1991     if (r->headers_in.authorization && (p & NGX_HTTP_GZIP_PROXIED_AUTH)) {
1992         goto ok;
1993     }
1994 
1995     e = r->headers_out.expires;
1996 
1997     if (e) {
1998 
1999         if (!(p & NGX_HTTP_GZIP_PROXIED_EXPIRED)) {
2000             return NGX_DECLINED;
2001         }
2002 
2003         expires = ngx_parse_http_time(e->value.data, e->value.len);
2004         if (expires == NGX_ERROR) {
2005             return NGX_DECLINED;
2006         }
2007 
2008         d = r->headers_out.date;
2009 
2010         if (d) {
2011             date = ngx_parse_http_time(d->value.data, d->value.len);
2012             if (date == NGX_ERROR) {
2013                 return NGX_DECLINED;
2014             }
2015 
2016         } else {
2017             date = ngx_time();
2018         }
2019 
2020         if (expires < date) {
2021             goto ok;
2022         }
2023 
2024         return NGX_DECLINED;
2025     }
2026 
2027     cc = &r->headers_out.cache_control;
2028 
2029     if (cc->elts) {
2030 
2031         if ((p & NGX_HTTP_GZIP_PROXIED_NO_CACHE)
2032             && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_no_cache,
2033                                                  NULL)
2034                >= 0)
2035         {
2036             goto ok;
2037         }
2038 
2039         if ((p & NGX_HTTP_GZIP_PROXIED_NO_STORE)
2040             && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_no_store,
2041                                                  NULL)
2042                >= 0)
2043         {
2044             goto ok;
2045         }
2046 
2047         if ((p & NGX_HTTP_GZIP_PROXIED_PRIVATE)
2048             && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_private,
2049                                                  NULL)
2050                >= 0)
2051         {
2052             goto ok;
2053         }
2054 
2055         return NGX_DECLINED;
2056     }
2057 
2058     if ((p & NGX_HTTP_GZIP_PROXIED_NO_LM) && r->headers_out.last_modified) {
2059         return NGX_DECLINED;
2060     }
2061 
2062     if ((p & NGX_HTTP_GZIP_PROXIED_NO_ETAG) && r->headers_out.etag) {
2063         return NGX_DECLINED;
2064     }
2065 
2066 ok:
2067 
2068 #if (NGX_PCRE)
2069 
2070     if (clcf->gzip_disable && r->headers_in.user_agent) {
2071 
2072         if (ngx_regex_exec_array(clcf->gzip_disable,
2073                                  &r->headers_in.user_agent->value,
2074                                  r->connection->log)
2075             != NGX_DECLINED)
2076         {
2077             return NGX_DECLINED;
2078         }
2079     }
2080 
2081 #endif
2082 
2083     r->gzip_ok = 1;
2084 
2085     return NGX_OK;
2086 }
2087 
2088 
2089 /*
2090  * gzip is enabled for the following quantities:
2091  *     "gzip; q=0.001" ... "gzip; q=1.000"
2092  * gzip is disabled for the following quantities:
2093  *     "gzip; q=0" ... "gzip; q=0.000", and for any invalid cases
2094  */
2095 
2096 static ngx_int_t
2097 ngx_http_gzip_accept_encoding(ngx_str_t *ae)
2098 {
2099     u_char  *p, *start, *last;
2100 
2101     start = ae->data;
2102     last = start + ae->len;
2103 
2104     for ( ;; ) {
2105         p = ngx_strcasestrn(start, "gzip", 4 - 1);
2106         if (p == NULL) {
2107             return NGX_DECLINED;
2108         }
2109 
2110         if (p == start || (*(p - 1) == ',' || *(p - 1) == ' ')) {
2111             break;
2112         }
2113 
2114         start = p + 4;
2115     }
2116 
2117     p += 4;
2118 
2119     while (p < last) {
2120         switch (*p++) {
2121         case ',':
2122             return NGX_OK;
2123         case ';':
2124             goto quantity;
2125         case ' ':
2126             continue;
2127         default:
2128             return NGX_DECLINED;
2129         }
2130     }
2131 
2132     return NGX_OK;
2133 
2134 quantity:
2135 
2136     while (p < last) {
2137         switch (*p++) {
2138         case 'q':
2139         case 'Q':
2140             goto equal;
2141         case ' ':
2142             continue;
2143         default:
2144             return NGX_DECLINED;
2145         }
2146     }
2147 
2148     return NGX_OK;
2149 
2150 equal:
2151 
2152     if (p + 2 > last || *p++ != '=') {
2153         return NGX_DECLINED;
2154     }
2155 
2156     if (ngx_http_gzip_quantity(p, last) == 0) {
2157         return NGX_DECLINED;
2158     }
2159 
2160     return NGX_OK;
2161 }
2162 
2163 
2164 static ngx_uint_t
2165 ngx_http_gzip_quantity(u_char *p, u_char *last)
2166 {
2167     u_char      c;
2168     ngx_uint_t  n, q;
2169 
2170     c = *p++;
2171 
2172     if (c != '0' && c != '1') {
2173         return 0;
2174     }
2175 
2176     q = (c - '0') * 100;
2177 
2178     if (p == last) {
2179         return q;
2180     }
2181 
2182     c = *p++;
2183 
2184     if (c == ',' || c == ' ') {
2185         return q;
2186     }
2187 
2188     if (c != '.') {
2189         return 0;
2190     }
2191 
2192     n = 0;
2193 
2194     while (p < last) {
2195         c = *p++;
2196 
2197         if (c == ',' || c == ' ') {
2198             break;
2199         }
2200 
2201         if (c >= '0' && c <= '9') {
2202             q += c - '0';
2203             n++;
2204             continue;
2205         }
2206 
2207         return 0;
2208     }
2209 
2210     if (q > 100 || n > 3) {
2211         return 0;
2212     }
2213 
2214     return q;
2215 }
2216 
2217 #endif
2218 
2219 
2220 ngx_int_t
2221 ngx_http_subrequest(ngx_http_request_t *r,
2222     ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **psr,
2223     ngx_http_post_subrequest_t *ps, ngx_uint_t flags)
2224 {
2225     ngx_time_t                    *tp;
2226     ngx_connection_t              *c;
2227     ngx_http_request_t            *sr;
2228     ngx_http_core_srv_conf_t      *cscf;
2229     ngx_http_postponed_request_t  *pr, *p;
2230 
2231     if (r->subrequests == 0) {
2232         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2233                       "subrequests cycle while processing \"%V\"", uri);
2234         return NGX_ERROR;
2235     }
2236 
2237     /*
2238      * 1000 is reserved for other purposes.
2239      */
2240     if (r->main->count >= 65535 - 1000) {
2241         ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
2242                       "request reference counter overflow "
2243                       "while processing \"%V\"", uri);
2244         return NGX_ERROR;
2245     }
2246 
2247     if (r->subrequest_in_memory) {
2248         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2249                       "nested in-memory subrequest \"%V\"", uri);
2250         return NGX_ERROR;
2251     }
2252 
2253     sr = ngx_pcalloc(r->pool, sizeof(ngx_http_request_t));
2254     if (sr == NULL) {
2255         return NGX_ERROR;
2256     }
2257 
2258     sr->signature = NGX_HTTP_MODULE;
2259 
2260     c = r->connection;
2261     sr->connection = c;
2262 
2263     sr->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
2264     if (sr->ctx == NULL) {
2265         return NGX_ERROR;
2266     }
2267 
2268     if (ngx_list_init(&sr->headers_out.headers, r->pool, 20,
2269                       sizeof(ngx_table_elt_t))
2270         != NGX_OK)
2271     {
2272         return NGX_ERROR;
2273     }
2274 
2275     if (ngx_list_init(&sr->headers_out.trailers, r->pool, 4,
2276                       sizeof(ngx_table_elt_t))
2277         != NGX_OK)
2278     {
2279         return NGX_ERROR;
2280     }
2281 
2282     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2283     sr->main_conf = cscf->ctx->main_conf;
2284     sr->srv_conf = cscf->ctx->srv_conf;
2285     sr->loc_conf = cscf->ctx->loc_conf;
2286 
2287     sr->pool = r->pool;
2288 
2289     sr->headers_in = r->headers_in;
2290 
2291     ngx_http_clear_content_length(sr);
2292     ngx_http_clear_accept_ranges(sr);
2293     ngx_http_clear_last_modified(sr);
2294 
2295     sr->request_body = r->request_body;
2296 
2297 #if (NGX_HTTP_V2)
2298     sr->stream = r->stream;
2299 #endif
2300 
2301     sr->method = NGX_HTTP_GET;
2302     sr->http_version = r->http_version;
2303 
2304     sr->request_line = r->request_line;
2305     sr->uri = *uri;
2306 
2307     if (args) {
2308         sr->args = *args;
2309     }
2310 
2311     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
2312                    "http subrequest \"%V?%V\"", uri, &sr->args);
2313 
2314     sr->subrequest_in_memory = (flags & NGX_HTTP_SUBREQUEST_IN_MEMORY) != 0;
2315     sr->waited = (flags & NGX_HTTP_SUBREQUEST_WAITED) != 0;
2316     sr->background = (flags & NGX_HTTP_SUBREQUEST_BACKGROUND) != 0;
2317 
2318     sr->unparsed_uri = r->unparsed_uri;
2319     sr->method_name = ngx_http_core_get_method;
2320     sr->http_protocol = r->http_protocol;
2321 
2322     ngx_http_set_exten(sr);
2323 
2324     sr->main = r->main;
2325     sr->parent = r;
2326     sr->post_subrequest = ps;
2327     sr->read_event_handler = ngx_http_request_empty_handler;
2328     sr->write_event_handler = ngx_http_handler;
2329 
2330     sr->variables = r->variables;
2331 
2332     sr->log_handler = r->log_handler;
2333 
2334     if (sr->subrequest_in_memory) {
2335         sr->filter_need_in_memory = 1;
2336     }
2337 
2338     if (!sr->background) {
2339         if (c->data == r && r->postponed == NULL) {
2340             c->data = sr;
2341         }
2342 
2343         pr = ngx_palloc(r->pool, sizeof(ngx_http_postponed_request_t));
2344         if (pr == NULL) {
2345             return NGX_ERROR;
2346         }
2347 
2348         pr->request = sr;
2349         pr->out = NULL;
2350         pr->next = NULL;
2351 
2352         if (r->postponed) {
2353             for (p = r->postponed; p->next; p = p->next) { /* void */ }
2354             p->next = pr;
2355 
2356         } else {
2357             r->postponed = pr;
2358         }
2359     }
2360 
2361     sr->internal = 1;
2362 
2363     sr->discard_body = r->discard_body;
2364     sr->expect_tested = 1;
2365     sr->main_filter_need_in_memory = r->main_filter_need_in_memory;
2366 
2367     sr->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
2368     sr->subrequests = r->subrequests - 1;
2369 
2370     tp = ngx_timeofday();
2371     sr->start_sec = tp->sec;
2372     sr->start_msec = tp->msec;
2373 
2374     r->main->count++;
2375 
2376     *psr = sr;
2377 
2378     if (flags & NGX_HTTP_SUBREQUEST_CLONE) {
2379         sr->method = r->method;
2380         sr->method_name = r->method_name;
2381         sr->loc_conf = r->loc_conf;
2382         sr->valid_location = r->valid_location;
2383         sr->valid_unparsed_uri = r->valid_unparsed_uri;
2384         sr->content_handler = r->content_handler;
2385         sr->phase_handler = r->phase_handler;
2386         sr->write_event_handler = ngx_http_core_run_phases;
2387 
2388         ngx_http_update_location_config(sr);
2389     }
2390 
2391     return ngx_http_post_request(sr, NULL);
2392 }
2393 
2394 
2395 ngx_int_t
2396 ngx_http_internal_redirect(ngx_http_request_t *r,
2397     ngx_str_t *uri, ngx_str_t *args)
2398 {
2399     ngx_http_core_srv_conf_t  *cscf;
2400 
2401     r->uri_changes--;
2402 
2403     if (r->uri_changes == 0) {
2404         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2405                       "rewrite or internal redirection cycle "
2406                       "while internally redirecting to \"%V\"", uri);
2407 
2408         r->main->count++;
2409         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
2410         return NGX_DONE;
2411     }
2412 
2413     r->uri = *uri;
2414 
2415     if (args) {
2416         r->args = *args;
2417 
2418     } else {
2419         ngx_str_null(&r->args);
2420     }
2421 
2422     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2423                    "internal redirect: \"%V?%V\"", uri, &r->args);
2424 
2425     ngx_http_set_exten(r);
2426 
2427     /* clear the modules contexts */
2428     ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);
2429 
2430     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2431     r->loc_conf = cscf->ctx->loc_conf;
2432 
2433     ngx_http_update_location_config(r);
2434 
2435 #if (NGX_HTTP_CACHE)
2436     r->cache = NULL;
2437 #endif
2438 
2439     r->internal = 1;
2440     r->valid_unparsed_uri = 0;
2441     r->add_uri_to_alias = 0;
2442     r->main->count++;
2443 
2444     ngx_http_handler(r);
2445 
2446     return NGX_DONE;
2447 }
2448 
2449 
2450 ngx_int_t
2451 ngx_http_named_location(ngx_http_request_t *r, ngx_str_t *name)
2452 {
2453     ngx_http_core_srv_conf_t    *cscf;
2454     ngx_http_core_loc_conf_t   **clcfp;
2455     ngx_http_core_main_conf_t   *cmcf;
2456 
2457     r->main->count++;
2458     r->uri_changes--;
2459 
2460     if (r->uri_changes == 0) {
2461         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2462                       "rewrite or internal redirection cycle "
2463                       "while redirect to named location \"%V\"", name);
2464 
2465         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
2466         return NGX_DONE;
2467     }
2468 
2469     if (r->uri.len == 0) {
2470         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2471                       "empty URI in redirect to named location \"%V\"", name);
2472 
2473         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
2474         return NGX_DONE;
2475     }
2476 
2477     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2478 
2479     if (cscf->named_locations) {
2480 
2481         for (clcfp = cscf->named_locations; *clcfp; clcfp++) {
2482 
2483             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2484                            "test location: \"%V\"", &(*clcfp)->name);
2485 
2486             if (name->len != (*clcfp)->name.len
2487                 || ngx_strncmp(name->data, (*clcfp)->name.data, name->len) != 0)
2488             {
2489                 continue;
2490             }
2491 
2492             ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2493                            "using location: %V \"%V?%V\"",
2494                            name, &r->uri, &r->args);
2495 
2496             r->internal = 1;
2497             r->content_handler = NULL;
2498             r->uri_changed = 0;
2499             r->loc_conf = (*clcfp)->loc_conf;
2500 
2501             /* clear the modules contexts */
2502             ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);
2503 
2504             ngx_http_update_location_config(r);
2505 
2506             cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
2507 
2508             r->phase_handler = cmcf->phase_engine.location_rewrite_index;
2509 
2510             r->write_event_handler = ngx_http_core_run_phases;
2511             ngx_http_core_run_phases(r);
2512 
2513             return NGX_DONE;
2514         }
2515     }
2516 
2517     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2518                   "could not find named location \"%V\"", name);
2519 
2520     ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
2521 
2522     return NGX_DONE;
2523 }
2524 
2525 
2526 ngx_http_cleanup_t *
2527 ngx_http_cleanup_add(ngx_http_request_t *r, size_t size)
2528 {
2529     ngx_http_cleanup_t  *cln;
2530 
2531     r = r->main;
2532 
2533     cln = ngx_palloc(r->pool, sizeof(ngx_http_cleanup_t));
2534     if (cln == NULL) {
2535         return NULL;
2536     }
2537 
2538     if (size) {
2539         cln->data = ngx_palloc(r->pool, size);
2540         if (cln->data == NULL) {
2541             return NULL;
2542         }
2543 
2544     } else {
2545         cln->data = NULL;
2546     }
2547 
2548     cln->handler = NULL;
2549     cln->next = r->cleanup;
2550 
2551     r->cleanup = cln;
2552 
2553     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2554                    "http cleanup add: %p", cln);
2555 
2556     return cln;
2557 }
2558 
2559 
2560 ngx_int_t
2561 ngx_http_set_disable_symlinks(ngx_http_request_t *r,
2562     ngx_http_core_loc_conf_t *clcf, ngx_str_t *path, ngx_open_file_info_t *of)
2563 {
2564 #if (NGX_HAVE_OPENAT)
2565     u_char     *p;
2566     ngx_str_t   from;
2567 
2568     of->disable_symlinks = clcf->disable_symlinks;
2569 
2570     if (clcf->disable_symlinks_from == NULL) {
2571         return NGX_OK;
2572     }
2573 
2574     if (ngx_http_complex_value(r, clcf->disable_symlinks_from, &from)
2575         != NGX_OK)
2576     {
2577         return NGX_ERROR;
2578     }
2579 
2580     if (from.len == 0
2581         || from.len > path->len
2582         || ngx_memcmp(path->data, from.data, from.len) != 0)
2583     {
2584         return NGX_OK;
2585     }
2586 
2587     if (from.len == path->len) {
2588         of->disable_symlinks = NGX_DISABLE_SYMLINKS_OFF;
2589         return NGX_OK;
2590     }
2591 
2592     p = path->data + from.len;
2593 
2594     if (*p == '/') {
2595         of->disable_symlinks_from = from.len;
2596         return NGX_OK;
2597     }
2598 
2599     p--;
2600 
2601     if (*p == '/') {
2602         of->disable_symlinks_from = from.len - 1;
2603     }
2604 #endif
2605 
2606     return NGX_OK;
2607 }
2608 
2609 
2610 ngx_int_t
2611 ngx_http_get_forwarded_addr(ngx_http_request_t *r, ngx_addr_t *addr,
2612     ngx_array_t *headers, ngx_str_t *value, ngx_array_t *proxies,
2613     int recursive)
2614 {
2615     ngx_int_t          rc;
2616     ngx_uint_t         i, found;
2617     ngx_table_elt_t  **h;
2618 
2619     if (headers == NULL) {
2620         return ngx_http_get_forwarded_addr_internal(r, addr, value->data,
2621                                                     value->len, proxies,
2622                                                     recursive);
2623     }
2624 
2625     i = headers->nelts;
2626     h = headers->elts;
2627 
2628     rc = NGX_DECLINED;
2629 
2630     found = 0;
2631 
2632     while (i-- > 0) {
2633         rc = ngx_http_get_forwarded_addr_internal(r, addr, h[i]->value.data,
2634                                                   h[i]->value.len, proxies,
2635                                                   recursive);
2636 
2637         if (!recursive) {
2638             break;
2639         }
2640 
2641         if (rc == NGX_DECLINED && found) {
2642             rc = NGX_DONE;
2643             break;
2644         }
2645 
2646         if (rc != NGX_OK) {
2647             break;
2648         }
2649 
2650         found = 1;
2651     }
2652 
2653     return rc;
2654 }
2655 
2656 
2657 static ngx_int_t
2658 ngx_http_get_forwarded_addr_internal(ngx_http_request_t *r, ngx_addr_t *addr,
2659     u_char *xff, size_t xfflen, ngx_array_t *proxies, int recursive)
2660 {
2661     u_char      *p;
2662     ngx_int_t    rc;
2663     ngx_addr_t   paddr;
2664 
2665     if (ngx_cidr_match(addr->sockaddr, proxies) != NGX_OK) {
2666         return NGX_DECLINED;
2667     }
2668 
2669     for (p = xff + xfflen - 1; p > xff; p--, xfflen--) {
2670         if (*p != ' ' && *p != ',') {
2671             break;
2672         }
2673     }
2674 
2675     for ( /* void */ ; p > xff; p--) {
2676         if (*p == ' ' || *p == ',') {
2677             p++;
2678             break;
2679         }
2680     }
2681 
2682     if (ngx_parse_addr_port(r->pool, &paddr, p, xfflen - (p - xff)) != NGX_OK) {
2683         return NGX_DECLINED;
2684     }
2685 
2686     *addr = paddr;
2687 
2688     if (recursive && p > xff) {
2689         rc = ngx_http_get_forwarded_addr_internal(r, addr, xff, p - 1 - xff,
2690                                                   proxies, 1);
2691 
2692         if (rc == NGX_DECLINED) {
2693             return NGX_DONE;
2694         }
2695 
2696         /* rc == NGX_OK || rc == NGX_DONE  */
2697         return rc;
2698     }
2699 
2700     return NGX_OK;
2701 }
2702 
2703 
2704 static char *
2705 ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
2706 {
2707     char                        *rv;
2708     void                        *mconf;
2709     ngx_uint_t                   i;
2710     ngx_conf_t                   pcf;
2711     ngx_http_module_t           *module;
2712     struct sockaddr_in          *sin;
2713     ngx_http_conf_ctx_t         *ctx, *http_ctx;
2714     ngx_http_listen_opt_t        lsopt;
2715     ngx_http_core_srv_conf_t    *cscf, **cscfp;
2716     ngx_http_core_main_conf_t   *cmcf;
2717 
2718     ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
2719     if (ctx == NULL) {
2720         return NGX_CONF_ERROR;
2721     }
2722 
2723     http_ctx = cf->ctx;
2724     ctx->main_conf = http_ctx->main_conf;
2725 
2726     /* the server{}'s srv_conf */
2727 
2728     ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
2729     if (ctx->srv_conf == NULL) {
2730         return NGX_CONF_ERROR;
2731     }
2732 
2733     /* the server{}'s loc_conf */
2734 
2735     ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
2736     if (ctx->loc_conf == NULL) {
2737         return NGX_CONF_ERROR;
2738     }
2739 
2740     for (i = 0; cf->cycle->modules[i]; i++) {
2741         if (cf->cycle->modules[i]->type != NGX_HTTP_MODULE) {
2742             continue;
2743         }
2744 
2745         module = cf->cycle->modules[i]->ctx;
2746 
2747         if (module->create_srv_conf) {
2748             mconf = module->create_srv_conf(cf);
2749             if (mconf == NULL) {
2750                 return NGX_CONF_ERROR;
2751             }
2752 
2753             ctx->srv_conf[cf->cycle->modules[i]->ctx_index] = mconf;
2754         }
2755 
2756         if (module->create_loc_conf) {
2757             mconf = module->create_loc_conf(cf);
2758             if (mconf == NULL) {
2759                 return NGX_CONF_ERROR;
2760             }
2761 
2762             ctx->loc_conf[cf->cycle->modules[i]->ctx_index] = mconf;
2763         }
2764     }
2765 
2766 
2767     /* the server configuration context */
2768 
2769     cscf = ctx->srv_conf[ngx_http_core_module.ctx_index];
2770     cscf->ctx = ctx;
2771 
2772 
2773     cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
2774 
2775     cscfp = ngx_array_push(&cmcf->servers);
2776     if (cscfp == NULL) {
2777         return NGX_CONF_ERROR;
2778     }
2779 
2780     *cscfp = cscf;
2781 
2782 
2783     /* parse inside server{} */
2784 
2785     pcf = *cf;
2786     cf->ctx = ctx;
2787     cf->cmd_type = NGX_HTTP_SRV_CONF;
2788 
2789     rv = ngx_conf_parse(cf, NULL);
2790 
2791     *cf = pcf;
2792 
2793     if (rv == NGX_CONF_OK && !cscf->listen) {
2794         ngx_memzero(&lsopt, sizeof(ngx_http_listen_opt_t));
2795 
2796         sin = &lsopt.sockaddr.sockaddr_in;
2797 
2798         sin->sin_family = AF_INET;
2799 #if (NGX_WIN32)
2800         sin->sin_port = htons(80);
2801 #else
2802         sin->sin_port = htons((getuid() == 0) ? 80 : 8000);
2803 #endif
2804         sin->sin_addr.s_addr = INADDR_ANY;
2805 
2806         lsopt.socklen = sizeof(struct sockaddr_in);
2807 
2808         lsopt.backlog = NGX_LISTEN_BACKLOG;
2809         lsopt.rcvbuf = -1;
2810         lsopt.sndbuf = -1;
2811 #if (NGX_HAVE_SETFIB)
2812         lsopt.setfib = -1;
2813 #endif
2814 #if (NGX_HAVE_TCP_FASTOPEN)
2815         lsopt.fastopen = -1;
2816 #endif
2817         lsopt.wildcard = 1;
2818 
2819         (void) ngx_sock_ntop(&lsopt.sockaddr.sockaddr, lsopt.socklen,
2820                              lsopt.addr, NGX_SOCKADDR_STRLEN, 1);
2821 
2822         if (ngx_http_add_listen(cf, cscf, &lsopt) != NGX_OK) {
2823             return NGX_CONF_ERROR;
2824         }
2825     }
2826 
2827     return rv;
2828 }
2829 
2830 
2831 static char *
2832 ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
2833 {
2834     char                      *rv;
2835     u_char                    *mod;
2836     size_t                     len;
2837     ngx_str_t                 *value, *name;
2838     ngx_uint_t                 i;
2839     ngx_conf_t                 save;
2840     ngx_http_module_t         *module;
2841     ngx_http_conf_ctx_t       *ctx, *pctx;
2842     ngx_http_core_loc_conf_t  *clcf, *pclcf;
2843 
2844     ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
2845     if (ctx == NULL) {
2846         return NGX_CONF_ERROR;
2847     }
2848 
2849     pctx = cf->ctx;
2850     ctx->main_conf = pctx->main_conf;
2851     ctx->srv_conf = pctx->srv_conf;
2852 
2853     ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
2854     if (ctx->loc_conf == NULL) {
2855         return NGX_CONF_ERROR;
2856     }
2857 
2858     for (i = 0; cf->cycle->modules[i]; i++) {
2859         if (cf->cycle->modules[i]->type != NGX_HTTP_MODULE) {
2860             continue;
2861         }
2862 
2863         module = cf->cycle->modules[i]->ctx;
2864 
2865         if (module->create_loc_conf) {
2866             ctx->loc_conf[cf->cycle->modules[i]->ctx_index] =
2867                                                    module->create_loc_conf(cf);
2868             if (ctx->loc_conf[cf->cycle->modules[i]->ctx_index] == NULL) {
2869                 return NGX_CONF_ERROR;
2870             }
2871         }
2872     }
2873 
2874     clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
2875     clcf->loc_conf = ctx->loc_conf;
2876 
2877     value = cf->args->elts;
2878 
2879     if (cf->args->nelts == 3) {
2880 
2881         len = value[1].len;
2882         mod = value[1].data;
2883         name = &value[2];
2884 
2885         if (len == 1 && mod[0] == '=') {
2886 
2887             clcf->name = *name;
2888             clcf->exact_match = 1;
2889 
2890         } else if (len == 2 && mod[0] == '^' && mod[1] == '~') {
2891 
2892             clcf->name = *name;
2893             clcf->noregex = 1;
2894 
2895         } else if (len == 1 && mod[0] == '~') {
2896 
2897             if (ngx_http_core_regex_location(cf, clcf, name, 0) != NGX_OK) {
2898                 return NGX_CONF_ERROR;
2899             }
2900 
2901         } else if (len == 2 && mod[0] == '~' && mod[1] == '*') {
2902 
2903             if (ngx_http_core_regex_location(cf, clcf, name, 1) != NGX_OK) {
2904                 return NGX_CONF_ERROR;
2905             }
2906 
2907         } else {
2908             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2909                                "invalid location modifier \"%V\"", &value[1]);
2910             return NGX_CONF_ERROR;
2911         }
2912 
2913     } else {
2914 
2915         name = &value[1];
2916 
2917         if (name->data[0] == '=') {
2918 
2919             clcf->name.len = name->len - 1;
2920             clcf->name.data = name->data + 1;
2921             clcf->exact_match = 1;
2922 
2923         } else if (name->data[0] == '^' && name->data[1] == '~') {
2924 
2925             clcf->name.len = name->len - 2;
2926             clcf->name.data = name->data + 2;
2927             clcf->noregex = 1;
2928 
2929         } else if (name->data[0] == '~') {
2930 
2931             name->len--;
2932             name->data++;
2933 
2934             if (name->data[0] == '*') {
2935 
2936                 name->len--;
2937                 name->data++;
2938 
2939                 if (ngx_http_core_regex_location(cf, clcf, name, 1) != NGX_OK) {
2940                     return NGX_CONF_ERROR;
2941                 }
2942 
2943             } else {
2944                 if (ngx_http_core_regex_location(cf, clcf, name, 0) != NGX_OK) {
2945                     return NGX_CONF_ERROR;
2946                 }
2947             }
2948 
2949         } else {
2950 
2951             clcf->name = *name;
2952 
2953             if (name->data[0] == '@') {
2954                 clcf->named = 1;
2955             }
2956         }
2957     }
2958 
2959     pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
2960 
2961     if (cf->cmd_type == NGX_HTTP_LOC_CONF) {
2962 
2963         /* nested location */
2964 
2965 #if 0
2966         clcf->prev_location = pclcf;
2967 #endif
2968 
2969         if (pclcf->exact_match) {
2970             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2971                                "location \"%V\" cannot be inside "
2972                                "the exact location \"%V\"",
2973                                &clcf->name, &pclcf->name);
2974             return NGX_CONF_ERROR;
2975         }
2976 
2977         if (pclcf->named) {
2978             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2979                                "location \"%V\" cannot be inside "
2980                                "the named location \"%V\"",
2981                                &clcf->name, &pclcf->name);
2982             return NGX_CONF_ERROR;
2983         }
2984 
2985         if (clcf->named) {
2986             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2987                                "named location \"%V\" can be "
2988                                "on the server level only",
2989                                &clcf->name);
2990             return NGX_CONF_ERROR;
2991         }
2992 
2993         len = pclcf->name.len;
2994 
2995 #if (NGX_PCRE)
2996         if (clcf->regex == NULL
2997             && ngx_filename_cmp(clcf->name.data, pclcf->name.data, len) != 0)
2998 #else
2999         if (ngx_filename_cmp(clcf->name.data, pclcf->name.data, len) != 0)
3000 #endif
3001         {
3002             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3003                                "location \"%V\" is outside location \"%V\"",
3004                                &clcf->name, &pclcf->name);
3005             return NGX_CONF_ERROR;
3006         }
3007     }
3008 
3009     if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) {
3010         return NGX_CONF_ERROR;
3011     }
3012 
3013     save = *cf;
3014     cf->ctx = ctx;
3015     cf->cmd_type = NGX_HTTP_LOC_CONF;
3016 
3017     rv = ngx_conf_parse(cf, NULL);
3018 
3019     *cf = save;
3020 
3021     return rv;
3022 }
3023 
3024 
3025 static ngx_int_t
3026 ngx_http_core_regex_location(ngx_conf_t *cf, ngx_http_core_loc_conf_t *clcf,
3027     ngx_str_t *regex, ngx_uint_t caseless)
3028 {
3029 #if (NGX_PCRE)
3030     ngx_regex_compile_t  rc;
3031     u_char               errstr[NGX_MAX_CONF_ERRSTR];
3032 
3033     ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
3034 
3035     rc.pattern = *regex;
3036     rc.err.len = NGX_MAX_CONF_ERRSTR;
3037     rc.err.data = errstr;
3038 
3039 #if (NGX_HAVE_CASELESS_FILESYSTEM)
3040     rc.options = NGX_REGEX_CASELESS;
3041 #else
3042     rc.options = caseless ? NGX_REGEX_CASELESS : 0;
3043 #endif
3044 
3045     clcf->regex = ngx_http_regex_compile(cf, &rc);
3046     if (clcf->regex == NULL) {
3047         return NGX_ERROR;
3048     }
3049 
3050     clcf->name = *regex;
3051 
3052     return NGX_OK;
3053 
3054 #else
3055 
3056     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3057                        "using regex \"%V\" requires PCRE library",
3058                        regex);
3059     return NGX_ERROR;
3060 
3061 #endif
3062 }
3063 
3064 
3065 static char *
3066 ngx_http_core_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3067 {
3068     ngx_http_core_loc_conf_t *clcf = conf;
3069 
3070     char        *rv;
3071     ngx_conf_t   save;
3072 
3073     if (clcf->types == NULL) {
3074         clcf->types = ngx_array_create(cf->pool, 64, sizeof(ngx_hash_key_t));
3075         if (clcf->types == NULL) {
3076             return NGX_CONF_ERROR;
3077         }
3078     }
3079 
3080     save = *cf;
3081     cf->handler = ngx_http_core_type;
3082     cf->handler_conf = conf;
3083 
3084     rv = ngx_conf_parse(cf, NULL);
3085 
3086     *cf = save;
3087 
3088     return rv;
3089 }
3090 
3091 
3092 static char *
3093 ngx_http_core_type(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
3094 {
3095     ngx_http_core_loc_conf_t *clcf = conf;
3096 
3097     ngx_str_t       *value, *content_type, *old;
3098     ngx_uint_t       i, n, hash;
3099     ngx_hash_key_t  *type;
3100 
3101     value = cf->args->elts;
3102 
3103     if (ngx_strcmp(value[0].data, "include") == 0) {
3104         if (cf->args->nelts != 2) {
3105             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3106                                "invalid number of arguments"
3107                                " in \"include\" directive");
3108             return NGX_CONF_ERROR;
3109         }
3110 
3111         return ngx_conf_include(cf, dummy, conf);
3112     }
3113 
3114     content_type = ngx_palloc(cf->pool, sizeof(ngx_str_t));
3115     if (content_type == NULL) {
3116         return NGX_CONF_ERROR;
3117     }
3118 
3119     *content_type = value[0];
3120 
3121     for (i = 1; i < cf->args->nelts; i++) {
3122 
3123         hash = ngx_hash_strlow(value[i].data, value[i].data, value[i].len);
3124 
3125         type = clcf->types->elts;
3126         for (n = 0; n < clcf->types->nelts; n++) {
3127             if (ngx_strcmp(value[i].data, type[n].key.data) == 0) {
3128                 old = type[n].value;
3129                 type[n].value = content_type;
3130 
3131                 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
3132                                    "duplicate extension \"%V\", "
3133                                    "content type: \"%V\", "
3134                                    "previous content type: \"%V\"",
3135                                    &value[i], content_type, old);
3136                 goto next;
3137             }
3138         }
3139 
3140 
3141         type = ngx_array_push(clcf->types);
3142         if (type == NULL) {
3143             return NGX_CONF_ERROR;
3144         }
3145 
3146         type->key = value[i];
3147         type->key_hash = hash;
3148         type->value = content_type;
3149 
3150     next:
3151         continue;
3152     }
3153 
3154     return NGX_CONF_OK;
3155 }
3156 
3157 
3158 static ngx_int_t
3159 ngx_http_core_preconfiguration(ngx_conf_t *cf)
3160 {
3161     return ngx_http_variables_add_core_vars(cf);
3162 }
3163 
3164 
3165 static ngx_int_t
3166 ngx_http_core_postconfiguration(ngx_conf_t *cf)
3167 {
3168     ngx_http_top_request_body_filter = ngx_http_request_body_save_filter;
3169 
3170     return NGX_OK;
3171 }
3172 
3173 
3174 static void *
3175 ngx_http_core_create_main_conf(ngx_conf_t *cf)
3176 {
3177     ngx_http_core_main_conf_t  *cmcf;
3178 
3179     cmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_main_conf_t));
3180     if (cmcf == NULL) {
3181         return NULL;
3182     }
3183 
3184     if (ngx_array_init(&cmcf->servers, cf->pool, 4,
3185                        sizeof(ngx_http_core_srv_conf_t *))
3186         != NGX_OK)
3187     {
3188         return NULL;
3189     }
3190 
3191     cmcf->server_names_hash_max_size = NGX_CONF_UNSET_UINT;
3192     cmcf->server_names_hash_bucket_size = NGX_CONF_UNSET_UINT;
3193 
3194     cmcf->variables_hash_max_size = NGX_CONF_UNSET_UINT;
3195     cmcf->variables_hash_bucket_size = NGX_CONF_UNSET_UINT;
3196 
3197     return cmcf;
3198 }
3199 
3200 
3201 static char *
3202 ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf)
3203 {
3204     ngx_http_core_main_conf_t *cmcf = conf;
3205 
3206     ngx_conf_init_uint_value(cmcf->server_names_hash_max_size, 512);
3207     ngx_conf_init_uint_value(cmcf->server_names_hash_bucket_size,
3208                              ngx_cacheline_size);
3209 
3210     cmcf->server_names_hash_bucket_size =
3211             ngx_align(cmcf->server_names_hash_bucket_size, ngx_cacheline_size);
3212 
3213 
3214     ngx_conf_init_uint_value(cmcf->variables_hash_max_size, 1024);
3215     ngx_conf_init_uint_value(cmcf->variables_hash_bucket_size, 64);
3216 
3217     cmcf->variables_hash_bucket_size =
3218                ngx_align(cmcf->variables_hash_bucket_size, ngx_cacheline_size);
3219 
3220     if (cmcf->ncaptures) {
3221         cmcf->ncaptures = (cmcf->ncaptures + 1) * 3;
3222     }
3223 
3224     return NGX_CONF_OK;
3225 }
3226 
3227 
3228 static void *
3229 ngx_http_core_create_srv_conf(ngx_conf_t *cf)
3230 {
3231     ngx_http_core_srv_conf_t  *cscf;
3232 
3233     cscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_srv_conf_t));
3234     if (cscf == NULL) {
3235         return NULL;
3236     }
3237 
3238     /*
3239      * set by ngx_pcalloc():
3240      *
3241      *     conf->client_large_buffers.num = 0;
3242      */
3243 
3244     if (ngx_array_init(&cscf->server_names, cf->temp_pool, 4,
3245                        sizeof(ngx_http_server_name_t))
3246         != NGX_OK)
3247     {
3248         return NULL;
3249     }
3250 
3251     cscf->connection_pool_size = NGX_CONF_UNSET_SIZE;
3252     cscf->request_pool_size = NGX_CONF_UNSET_SIZE;
3253     cscf->client_header_timeout = NGX_CONF_UNSET_MSEC;
3254     cscf->client_header_buffer_size = NGX_CONF_UNSET_SIZE;
3255     cscf->ignore_invalid_headers = NGX_CONF_UNSET;
3256     cscf->merge_slashes = NGX_CONF_UNSET;
3257     cscf->underscores_in_headers = NGX_CONF_UNSET;
3258 
3259     return cscf;
3260 }
3261 
3262 
3263 static char *
3264 ngx_http_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
3265 {
3266     ngx_http_core_srv_conf_t *prev = parent;
3267     ngx_http_core_srv_conf_t *conf = child;
3268 
3269     ngx_str_t                name;
3270     ngx_http_server_name_t  *sn;
3271 
3272     /* TODO: it does not merge, it inits only */
3273 
3274     ngx_conf_merge_size_value(conf->connection_pool_size,
3275                               prev->connection_pool_size, 64 * sizeof(void *));
3276     ngx_conf_merge_size_value(conf->request_pool_size,
3277                               prev->request_pool_size, 4096);
3278     ngx_conf_merge_msec_value(conf->client_header_timeout,
3279                               prev->client_header_timeout, 60000);
3280     ngx_conf_merge_size_value(conf->client_header_buffer_size,
3281                               prev->client_header_buffer_size, 1024);
3282     ngx_conf_merge_bufs_value(conf->large_client_header_buffers,
3283                               prev->large_client_header_buffers,
3284                               4, 8192);
3285 
3286     if (conf->large_client_header_buffers.size < conf->connection_pool_size) {
3287         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3288                            "the \"large_client_header_buffers\" size must be "
3289                            "equal to or greater than \"connection_pool_size\"");
3290         return NGX_CONF_ERROR;
3291     }
3292 
3293     ngx_conf_merge_value(conf->ignore_invalid_headers,
3294                               prev->ignore_invalid_headers, 1);
3295 
3296     ngx_conf_merge_value(conf->merge_slashes, prev->merge_slashes, 1);
3297 
3298     ngx_conf_merge_value(conf->underscores_in_headers,
3299                               prev->underscores_in_headers, 0);
3300 
3301     if (conf->server_names.nelts == 0) {
3302         /* the array has 4 empty preallocated elements, so push cannot fail */
3303         sn = ngx_array_push(&conf->server_names);
3304 #if (NGX_PCRE)
3305         sn->regex = NULL;
3306 #endif
3307         sn->server = conf;
3308         ngx_str_set(&sn->name, "");
3309     }
3310 
3311     sn = conf->server_names.elts;
3312     name = sn[0].name;
3313 
3314 #if (NGX_PCRE)
3315     if (sn->regex) {
3316         name.len++;
3317         name.data--;
3318     } else
3319 #endif
3320 
3321     if (name.data[0] == '.') {
3322         name.len--;
3323         name.data++;
3324     }
3325 
3326     conf->server_name.len = name.len;
3327     conf->server_name.data = ngx_pstrdup(cf->pool, &name);
3328     if (conf->server_name.data == NULL) {
3329         return NGX_CONF_ERROR;
3330     }
3331 
3332     return NGX_CONF_OK;
3333 }
3334 
3335 
3336 static void *
3337 ngx_http_core_create_loc_conf(ngx_conf_t *cf)
3338 {
3339     ngx_http_core_loc_conf_t  *clcf;
3340 
3341     clcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_loc_conf_t));
3342     if (clcf == NULL) {
3343         return NULL;
3344     }
3345 
3346     /*
3347      * set by ngx_pcalloc():
3348      *
3349      *     clcf->root = { 0, NULL };
3350      *     clcf->limit_except = 0;
3351      *     clcf->post_action = { 0, NULL };
3352      *     clcf->types = NULL;
3353      *     clcf->default_type = { 0, NULL };
3354      *     clcf->error_log = NULL;
3355      *     clcf->error_pages = NULL;
3356      *     clcf->client_body_path = NULL;
3357      *     clcf->regex = NULL;
3358      *     clcf->exact_match = 0;
3359      *     clcf->auto_redirect = 0;
3360      *     clcf->alias = 0;
3361      *     clcf->gzip_proxied = 0;
3362      *     clcf->keepalive_disable = 0;
3363      */
3364 
3365     clcf->client_max_body_size = NGX_CONF_UNSET;
3366     clcf->client_body_buffer_size = NGX_CONF_UNSET_SIZE;
3367     clcf->client_body_timeout = NGX_CONF_UNSET_MSEC;
3368     clcf->satisfy = NGX_CONF_UNSET_UINT;
3369     clcf->if_modified_since = NGX_CONF_UNSET_UINT;
3370     clcf->max_ranges = NGX_CONF_UNSET_UINT;
3371     clcf->client_body_in_file_only = NGX_CONF_UNSET_UINT;
3372     clcf->client_body_in_single_buffer = NGX_CONF_UNSET;
3373     clcf->internal = NGX_CONF_UNSET;
3374     clcf->sendfile = NGX_CONF_UNSET;
3375     clcf->sendfile_max_chunk = NGX_CONF_UNSET_SIZE;
3376     clcf->subrequest_output_buffer_size = NGX_CONF_UNSET_SIZE;
3377     clcf->aio = NGX_CONF_UNSET;
3378     clcf->aio_write = NGX_CONF_UNSET;
3379 #if (NGX_THREADS)
3380     clcf->thread_pool = NGX_CONF_UNSET_PTR;
3381     clcf->thread_pool_value = NGX_CONF_UNSET_PTR;
3382 #endif
3383     clcf->read_ahead = NGX_CONF_UNSET_SIZE;
3384     clcf->directio = NGX_CONF_UNSET;
3385     clcf->directio_alignment = NGX_CONF_UNSET;
3386     clcf->tcp_nopush = NGX_CONF_UNSET;
3387     clcf->tcp_nodelay = NGX_CONF_UNSET;
3388     clcf->send_timeout = NGX_CONF_UNSET_MSEC;
3389     clcf->send_lowat = NGX_CONF_UNSET_SIZE;
3390     clcf->postpone_output = NGX_CONF_UNSET_SIZE;
3391     clcf->limit_rate = NGX_CONF_UNSET_SIZE;
3392     clcf->limit_rate_after = NGX_CONF_UNSET_SIZE;
3393     clcf->keepalive_timeout = NGX_CONF_UNSET_MSEC;
3394     clcf->keepalive_header = NGX_CONF_UNSET;
3395     clcf->keepalive_requests = NGX_CONF_UNSET_UINT;
3396     clcf->lingering_close = NGX_CONF_UNSET_UINT;
3397     clcf->lingering_time = NGX_CONF_UNSET_MSEC;
3398     clcf->lingering_timeout = NGX_CONF_UNSET_MSEC;
3399     clcf->resolver_timeout = NGX_CONF_UNSET_MSEC;
3400     clcf->reset_timedout_connection = NGX_CONF_UNSET;
3401     clcf->absolute_redirect = NGX_CONF_UNSET;
3402     clcf->server_name_in_redirect = NGX_CONF_UNSET;
3403     clcf->port_in_redirect = NGX_CONF_UNSET;
3404     clcf->msie_padding = NGX_CONF_UNSET;
3405     clcf->msie_refresh = NGX_CONF_UNSET;
3406     clcf->log_not_found = NGX_CONF_UNSET;
3407     clcf->log_subrequest = NGX_CONF_UNSET;
3408     clcf->recursive_error_pages = NGX_CONF_UNSET;
3409     clcf->chunked_transfer_encoding = NGX_CONF_UNSET;
3410     clcf->etag = NGX_CONF_UNSET;
3411     clcf->server_tokens = NGX_CONF_UNSET_UINT;
3412     clcf->types_hash_max_size = NGX_CONF_UNSET_UINT;
3413     clcf->types_hash_bucket_size = NGX_CONF_UNSET_UINT;
3414 
3415     clcf->open_file_cache = NGX_CONF_UNSET_PTR;
3416     clcf->open_file_cache_valid = NGX_CONF_UNSET;
3417     clcf->open_file_cache_min_uses = NGX_CONF_UNSET_UINT;
3418     clcf->open_file_cache_errors = NGX_CONF_UNSET;
3419     clcf->open_file_cache_events = NGX_CONF_UNSET;
3420 
3421 #if (NGX_HTTP_GZIP)
3422     clcf->gzip_vary = NGX_CONF_UNSET;
3423     clcf->gzip_http_version = NGX_CONF_UNSET_UINT;
3424 #if (NGX_PCRE)
3425     clcf->gzip_disable = NGX_CONF_UNSET_PTR;
3426 #endif
3427     clcf->gzip_disable_msie6 = 3;
3428 #if (NGX_HTTP_DEGRADATION)
3429     clcf->gzip_disable_degradation = 3;
3430 #endif
3431 #endif
3432 
3433 #if (NGX_HAVE_OPENAT)
3434     clcf->disable_symlinks = NGX_CONF_UNSET_UINT;
3435     clcf->disable_symlinks_from = NGX_CONF_UNSET_PTR;
3436 #endif
3437 
3438     return clcf;
3439 }
3440 
3441 
3442 static ngx_str_t  ngx_http_core_text_html_type = ngx_string("text/html");
3443 static ngx_str_t  ngx_http_core_image_gif_type = ngx_string("image/gif");
3444 static ngx_str_t  ngx_http_core_image_jpeg_type = ngx_string("image/jpeg");
3445 
3446 static ngx_hash_key_t  ngx_http_core_default_types[] = {
3447     { ngx_string("html"), 0, &ngx_http_core_text_html_type },
3448     { ngx_string("gif"), 0, &ngx_http_core_image_gif_type },
3449     { ngx_string("jpg"), 0, &ngx_http_core_image_jpeg_type },
3450     { ngx_null_string, 0, NULL }
3451 };
3452 
3453 
3454 static char *
3455 ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
3456 {
3457     ngx_http_core_loc_conf_t *prev = parent;
3458     ngx_http_core_loc_conf_t *conf = child;
3459 
3460     ngx_uint_t        i;
3461     ngx_hash_key_t   *type;
3462     ngx_hash_init_t   types_hash;
3463 
3464     if (conf->root.data == NULL) {
3465 
3466         conf->alias = prev->alias;
3467         conf->root = prev->root;
3468         conf->root_lengths = prev->root_lengths;
3469         conf->root_values = prev->root_values;
3470 
3471         if (prev->root.data == NULL) {
3472             ngx_str_set(&conf->root, "html");
3473 
3474             if (ngx_conf_full_name(cf->cycle, &conf->root, 0) != NGX_OK) {
3475                 return NGX_CONF_ERROR;
3476             }
3477         }
3478     }
3479 
3480     if (conf->post_action.data == NULL) {
3481         conf->post_action = prev->post_action;
3482     }
3483 
3484     ngx_conf_merge_uint_value(conf->types_hash_max_size,
3485                               prev->types_hash_max_size, 1024);
3486 
3487     ngx_conf_merge_uint_value(conf->types_hash_bucket_size,
3488                               prev->types_hash_bucket_size, 64);
3489 
3490     conf->types_hash_bucket_size = ngx_align(conf->types_hash_bucket_size,
3491                                              ngx_cacheline_size);
3492 
3493     /*
3494      * the special handling of the "types" directive in the "http" section
3495      * to inherit the http's conf->types_hash to all servers
3496      */
3497 
3498     if (prev->types && prev->types_hash.buckets == NULL) {
3499 
3500         types_hash.hash = &prev->types_hash;
3501         types_hash.key = ngx_hash_key_lc;
3502         types_hash.max_size = conf->types_hash_max_size;
3503         types_hash.bucket_size = conf->types_hash_bucket_size;
3504         types_hash.name = "types_hash";
3505         types_hash.pool = cf->pool;
3506         types_hash.temp_pool = NULL;
3507 
3508         if (ngx_hash_init(&types_hash, prev->types->elts, prev->types->nelts)
3509             != NGX_OK)
3510         {
3511             return NGX_CONF_ERROR;
3512         }
3513     }
3514 
3515     if (conf->types == NULL) {
3516         conf->types = prev->types;
3517         conf->types_hash = prev->types_hash;
3518     }
3519 
3520     if (conf->types == NULL) {
3521         conf->types = ngx_array_create(cf->pool, 3, sizeof(ngx_hash_key_t));
3522         if (conf->types == NULL) {
3523             return NGX_CONF_ERROR;
3524         }
3525 
3526         for (i = 0; ngx_http_core_default_types[i].key.len; i++) {
3527             type = ngx_array_push(conf->types);
3528             if (type == NULL) {
3529                 return NGX_CONF_ERROR;
3530             }
3531 
3532             type->key = ngx_http_core_default_types[i].key;
3533             type->key_hash =
3534                        ngx_hash_key_lc(ngx_http_core_default_types[i].key.data,
3535                                        ngx_http_core_default_types[i].key.len);
3536             type->value = ngx_http_core_default_types[i].value;
3537         }
3538     }
3539 
3540     if (conf->types_hash.buckets == NULL) {
3541 
3542         types_hash.hash = &conf->types_hash;
3543         types_hash.key = ngx_hash_key_lc;
3544         types_hash.max_size = conf->types_hash_max_size;
3545         types_hash.bucket_size = conf->types_hash_bucket_size;
3546         types_hash.name = "types_hash";
3547         types_hash.pool = cf->pool;
3548         types_hash.temp_pool = NULL;
3549 
3550         if (ngx_hash_init(&types_hash, conf->types->elts, conf->types->nelts)
3551             != NGX_OK)
3552         {
3553             return NGX_CONF_ERROR;
3554         }
3555     }
3556 
3557     if (conf->error_log == NULL) {
3558         if (prev->error_log) {
3559             conf->error_log = prev->error_log;
3560         } else {
3561             conf->error_log = &cf->cycle->new_log;
3562         }
3563     }
3564 
3565     if (conf->error_pages == NULL && prev->error_pages) {
3566         conf->error_pages = prev->error_pages;
3567     }
3568 
3569     ngx_conf_merge_str_value(conf->default_type,
3570                               prev->default_type, "text/plain");
3571 
3572     ngx_conf_merge_off_value(conf->client_max_body_size,
3573                               prev->client_max_body_size, 1 * 1024 * 1024);
3574     ngx_conf_merge_size_value(conf->client_body_buffer_size,
3575                               prev->client_body_buffer_size,
3576                               (size_t) 2 * ngx_pagesize);
3577     ngx_conf_merge_msec_value(conf->client_body_timeout,
3578                               prev->client_body_timeout, 60000);
3579 
3580     ngx_conf_merge_bitmask_value(conf->keepalive_disable,
3581                               prev->keepalive_disable,
3582                               (NGX_CONF_BITMASK_SET
3583                                |NGX_HTTP_KEEPALIVE_DISABLE_MSIE6));
3584     ngx_conf_merge_uint_value(conf->satisfy, prev->satisfy,
3585                               NGX_HTTP_SATISFY_ALL);
3586     ngx_conf_merge_uint_value(conf->if_modified_since, prev->if_modified_since,
3587                               NGX_HTTP_IMS_EXACT);
3588     ngx_conf_merge_uint_value(conf->max_ranges, prev->max_ranges,
3589                               NGX_MAX_INT32_VALUE);
3590     ngx_conf_merge_uint_value(conf->client_body_in_file_only,
3591                               prev->client_body_in_file_only,
3592                               NGX_HTTP_REQUEST_BODY_FILE_OFF);
3593     ngx_conf_merge_value(conf->client_body_in_single_buffer,
3594                               prev->client_body_in_single_buffer, 0);
3595     ngx_conf_merge_value(conf->internal, prev->internal, 0);
3596     ngx_conf_merge_value(conf->sendfile, prev->sendfile, 0);
3597     ngx_conf_merge_size_value(conf->sendfile_max_chunk,
3598                               prev->sendfile_max_chunk, 0);
3599     ngx_conf_merge_size_value(conf->subrequest_output_buffer_size,
3600                               prev->subrequest_output_buffer_size,
3601                               (size_t) ngx_pagesize);
3602     ngx_conf_merge_value(conf->aio, prev->aio, NGX_HTTP_AIO_OFF);
3603     ngx_conf_merge_value(conf->aio_write, prev->aio_write, 0);
3604 #if (NGX_THREADS)
3605     ngx_conf_merge_ptr_value(conf->thread_pool, prev->thread_pool, NULL);
3606     ngx_conf_merge_ptr_value(conf->thread_pool_value, prev->thread_pool_value,
3607                              NULL);
3608 #endif
3609     ngx_conf_merge_size_value(conf->read_ahead, prev->read_ahead, 0);
3610     ngx_conf_merge_off_value(conf->directio, prev->directio,
3611                               NGX_OPEN_FILE_DIRECTIO_OFF);
3612     ngx_conf_merge_off_value(conf->directio_alignment, prev->directio_alignment,
3613                               512);
3614     ngx_conf_merge_value(conf->tcp_nopush, prev->tcp_nopush, 0);
3615     ngx_conf_merge_value(conf->tcp_nodelay, prev->tcp_nodelay, 1);
3616 
3617     ngx_conf_merge_msec_value(conf->send_timeout, prev->send_timeout, 60000);
3618     ngx_conf_merge_size_value(conf->send_lowat, prev->send_lowat, 0);
3619     ngx_conf_merge_size_value(conf->postpone_output, prev->postpone_output,
3620                               1460);
3621     ngx_conf_merge_size_value(conf->limit_rate, prev->limit_rate, 0);
3622     ngx_conf_merge_size_value(conf->limit_rate_after, prev->limit_rate_after,
3623                               0);
3624     ngx_conf_merge_msec_value(conf->keepalive_timeout,
3625                               prev->keepalive_timeout, 75000);
3626     ngx_conf_merge_sec_value(conf->keepalive_header,
3627                               prev->keepalive_header, 0);
3628     ngx_conf_merge_uint_value(conf->keepalive_requests,
3629                               prev->keepalive_requests, 100);
3630     ngx_conf_merge_uint_value(conf->lingering_close,
3631                               prev->lingering_close, NGX_HTTP_LINGERING_ON);
3632     ngx_conf_merge_msec_value(conf->lingering_time,
3633                               prev->lingering_time, 30000);
3634     ngx_conf_merge_msec_value(conf->lingering_timeout,
3635                               prev->lingering_timeout, 5000);
3636     ngx_conf_merge_msec_value(conf->resolver_timeout,
3637                               prev->resolver_timeout, 30000);
3638 
3639     if (conf->resolver == NULL) {
3640 
3641         if (prev->resolver == NULL) {
3642 
3643             /*
3644              * create dummy resolver in http {} context
3645              * to inherit it in all servers
3646              */
3647 
3648             prev->resolver = ngx_resolver_create(cf, NULL, 0);
3649             if (prev->resolver == NULL) {
3650                 return NGX_CONF_ERROR;
3651             }
3652         }
3653 
3654         conf->resolver = prev->resolver;
3655     }
3656 
3657     if (ngx_conf_merge_path_value(cf, &conf->client_body_temp_path,
3658                               prev->client_body_temp_path,
3659                               &ngx_http_client_temp_path)
3660         != NGX_OK)
3661     {
3662         return NGX_CONF_ERROR;
3663     }
3664 
3665     ngx_conf_merge_value(conf->reset_timedout_connection,
3666                               prev->reset_timedout_connection, 0);
3667     ngx_conf_merge_value(conf->absolute_redirect,
3668                               prev->absolute_redirect, 1);
3669     ngx_conf_merge_value(conf->server_name_in_redirect,
3670                               prev->server_name_in_redirect, 0);
3671     ngx_conf_merge_value(conf->port_in_redirect, prev->port_in_redirect, 1);
3672     ngx_conf_merge_value(conf->msie_padding, prev->msie_padding, 1);
3673     ngx_conf_merge_value(conf->msie_refresh, prev->msie_refresh, 0);
3674     ngx_conf_merge_value(conf->log_not_found, prev->log_not_found, 1);
3675     ngx_conf_merge_value(conf->log_subrequest, prev->log_subrequest, 0);
3676     ngx_conf_merge_value(conf->recursive_error_pages,
3677                               prev->recursive_error_pages, 0);
3678     ngx_conf_merge_value(conf->chunked_transfer_encoding,
3679                               prev->chunked_transfer_encoding, 1);
3680     ngx_conf_merge_value(conf->etag, prev->etag, 1);
3681 
3682     ngx_conf_merge_uint_value(conf->server_tokens, prev->server_tokens,
3683                               NGX_HTTP_SERVER_TOKENS_ON);
3684 
3685     ngx_conf_merge_ptr_value(conf->open_file_cache,
3686                               prev->open_file_cache, NULL);
3687 
3688     ngx_conf_merge_sec_value(conf->open_file_cache_valid,
3689                               prev->open_file_cache_valid, 60);
3690 
3691     ngx_conf_merge_uint_value(conf->open_file_cache_min_uses,
3692                               prev->open_file_cache_min_uses, 1);
3693 
3694     ngx_conf_merge_sec_value(conf->open_file_cache_errors,
3695                               prev->open_file_cache_errors, 0);
3696 
3697     ngx_conf_merge_sec_value(conf->open_file_cache_events,
3698                               prev->open_file_cache_events, 0);
3699 #if (NGX_HTTP_GZIP)
3700 
3701     ngx_conf_merge_value(conf->gzip_vary, prev->gzip_vary, 0);
3702     ngx_conf_merge_uint_value(conf->gzip_http_version, prev->gzip_http_version,
3703                               NGX_HTTP_VERSION_11);
3704     ngx_conf_merge_bitmask_value(conf->gzip_proxied, prev->gzip_proxied,
3705                               (NGX_CONF_BITMASK_SET|NGX_HTTP_GZIP_PROXIED_OFF));
3706 
3707 #if (NGX_PCRE)
3708     ngx_conf_merge_ptr_value(conf->gzip_disable, prev->gzip_disable, NULL);
3709 #endif
3710 
3711     if (conf->gzip_disable_msie6 == 3) {
3712         conf->gzip_disable_msie6 =
3713             (prev->gzip_disable_msie6 == 3) ? 0 : prev->gzip_disable_msie6;
3714     }
3715 
3716 #if (NGX_HTTP_DEGRADATION)
3717 
3718     if (conf->gzip_disable_degradation == 3) {
3719         conf->gzip_disable_degradation =
3720             (prev->gzip_disable_degradation == 3) ?
3721                  0 : prev->gzip_disable_degradation;
3722     }
3723 
3724 #endif
3725 #endif
3726 
3727 #if (NGX_HAVE_OPENAT)
3728     ngx_conf_merge_uint_value(conf->disable_symlinks, prev->disable_symlinks,
3729                               NGX_DISABLE_SYMLINKS_OFF);
3730     ngx_conf_merge_ptr_value(conf->disable_symlinks_from,
3731                              prev->disable_symlinks_from, NULL);
3732 #endif
3733 
3734     return NGX_CONF_OK;
3735 }
3736 
3737 
3738 static char *
3739 ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3740 {
3741     ngx_http_core_srv_conf_t *cscf = conf;
3742 
3743     ngx_str_t              *value, size;
3744     ngx_url_t               u;
3745     ngx_uint_t              n;
3746     ngx_http_listen_opt_t   lsopt;
3747 
3748     cscf->listen = 1;
3749 
3750     value = cf->args->elts;
3751 
3752     ngx_memzero(&u, sizeof(ngx_url_t));
3753 
3754     u.url = value[1];
3755     u.listen = 1;
3756     u.default_port = 80;
3757 
3758     if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
3759         if (u.err) {
3760             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3761                                "%s in \"%V\" of the \"listen\" directive",
3762                                u.err, &u.url);
3763         }
3764 
3765         return NGX_CONF_ERROR;
3766     }
3767 
3768     ngx_memzero(&lsopt, sizeof(ngx_http_listen_opt_t));
3769 
3770     ngx_memcpy(&lsopt.sockaddr.sockaddr, &u.sockaddr, u.socklen);
3771 
3772     lsopt.socklen = u.socklen;
3773     lsopt.backlog = NGX_LISTEN_BACKLOG;
3774     lsopt.rcvbuf = -1;
3775     lsopt.sndbuf = -1;
3776 #if (NGX_HAVE_SETFIB)
3777     lsopt.setfib = -1;
3778 #endif
3779 #if (NGX_HAVE_TCP_FASTOPEN)
3780     lsopt.fastopen = -1;
3781 #endif
3782     lsopt.wildcard = u.wildcard;
3783 #if (NGX_HAVE_INET6)
3784     lsopt.ipv6only = 1;
3785 #endif
3786 
3787     (void) ngx_sock_ntop(&lsopt.sockaddr.sockaddr, lsopt.socklen, lsopt.addr,
3788                          NGX_SOCKADDR_STRLEN, 1);
3789 
3790     for (n = 2; n < cf->args->nelts; n++) {
3791 
3792         if (ngx_strcmp(value[n].data, "default_server") == 0
3793             || ngx_strcmp(value[n].data, "default") == 0)
3794         {
3795             lsopt.default_server = 1;
3796             continue;
3797         }
3798 
3799         if (ngx_strcmp(value[n].data, "bind") == 0) {
3800             lsopt.set = 1;
3801             lsopt.bind = 1;
3802             continue;
3803         }
3804 
3805 #if (NGX_HAVE_SETFIB)
3806         if (ngx_strncmp(value[n].data, "setfib=", 7) == 0) {
3807             lsopt.setfib = ngx_atoi(value[n].data + 7, value[n].len - 7);
3808             lsopt.set = 1;
3809             lsopt.bind = 1;
3810 
3811             if (lsopt.setfib == NGX_ERROR) {
3812                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3813                                    "invalid setfib \"%V\"", &value[n]);
3814                 return NGX_CONF_ERROR;
3815             }
3816 
3817             continue;
3818         }
3819 #endif
3820 
3821 #if (NGX_HAVE_TCP_FASTOPEN)
3822         if (ngx_strncmp(value[n].data, "fastopen=", 9) == 0) {
3823             lsopt.fastopen = ngx_atoi(value[n].data + 9, value[n].len - 9);
3824             lsopt.set = 1;
3825             lsopt.bind = 1;
3826 
3827             if (lsopt.fastopen == NGX_ERROR) {
3828                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3829                                    "invalid fastopen \"%V\"", &value[n]);
3830                 return NGX_CONF_ERROR;
3831             }
3832 
3833             continue;
3834         }
3835 #endif
3836 
3837         if (ngx_strncmp(value[n].data, "backlog=", 8) == 0) {
3838             lsopt.backlog = ngx_atoi(value[n].data + 8, value[n].len - 8);
3839             lsopt.set = 1;
3840             lsopt.bind = 1;
3841 
3842             if (lsopt.backlog == NGX_ERROR || lsopt.backlog == 0) {
3843                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3844                                    "invalid backlog \"%V\"", &value[n]);
3845                 return NGX_CONF_ERROR;
3846             }
3847 
3848             continue;
3849         }
3850 
3851         if (ngx_strncmp(value[n].data, "rcvbuf=", 7) == 0) {
3852             size.len = value[n].len - 7;
3853             size.data = value[n].data + 7;
3854 
3855             lsopt.rcvbuf = ngx_parse_size(&size);
3856             lsopt.set = 1;
3857             lsopt.bind = 1;
3858 
3859             if (lsopt.rcvbuf == NGX_ERROR) {
3860                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3861                                    "invalid rcvbuf \"%V\"", &value[n]);
3862                 return NGX_CONF_ERROR;
3863             }
3864 
3865             continue;
3866         }
3867 
3868         if (ngx_strncmp(value[n].data, "sndbuf=", 7) == 0) {
3869             size.len = value[n].len - 7;
3870             size.data = value[n].data + 7;
3871 
3872             lsopt.sndbuf = ngx_parse_size(&size);
3873             lsopt.set = 1;
3874             lsopt.bind = 1;
3875 
3876             if (lsopt.sndbuf == NGX_ERROR) {
3877                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3878                                    "invalid sndbuf \"%V\"", &value[n]);
3879                 return NGX_CONF_ERROR;
3880             }
3881 
3882             continue;
3883         }
3884 
3885         if (ngx_strncmp(value[n].data, "accept_filter=", 14) == 0) {
3886 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
3887             lsopt.accept_filter = (char *) &value[n].data[14];
3888             lsopt.set = 1;
3889             lsopt.bind = 1;
3890 #else
3891             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3892                                "accept filters \"%V\" are not supported "
3893                                "on this platform, ignored",
3894                                &value[n]);
3895 #endif
3896             continue;
3897         }
3898 
3899         if (ngx_strcmp(value[n].data, "deferred") == 0) {
3900 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
3901             lsopt.deferred_accept = 1;
3902             lsopt.set = 1;
3903             lsopt.bind = 1;
3904 #else
3905             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3906                                "the deferred accept is not supported "
3907                                "on this platform, ignored");
3908 #endif
3909             continue;
3910         }
3911 
3912         if (ngx_strncmp(value[n].data, "ipv6only=o", 10) == 0) {
3913 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
3914             struct sockaddr  *sa;
3915 
3916             sa = &lsopt.sockaddr.sockaddr;
3917 
3918             if (sa->sa_family == AF_INET6) {
3919 
3920                 if (ngx_strcmp(&value[n].data[10], "n") == 0) {
3921                     lsopt.ipv6only = 1;
3922 
3923                 } else if (ngx_strcmp(&value[n].data[10], "ff") == 0) {
3924                     lsopt.ipv6only = 0;
3925 
3926                 } else {
3927                     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3928                                        "invalid ipv6only flags \"%s\"",
3929                                        &value[n].data[9]);
3930                     return NGX_CONF_ERROR;
3931                 }
3932 
3933                 lsopt.set = 1;
3934                 lsopt.bind = 1;
3935 
3936             } else {
3937                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3938                                    "ipv6only is not supported "
3939                                    "on addr \"%s\", ignored", lsopt.addr);
3940             }
3941 
3942             continue;
3943 #else
3944             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3945                                "ipv6only is not supported "
3946                                "on this platform");
3947             return NGX_CONF_ERROR;
3948 #endif
3949         }
3950 
3951         if (ngx_strcmp(value[n].data, "reuseport") == 0) {
3952 #if (NGX_HAVE_REUSEPORT)
3953             lsopt.reuseport = 1;
3954             lsopt.set = 1;
3955             lsopt.bind = 1;
3956 #else
3957             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3958                                "reuseport is not supported "
3959                                "on this platform, ignored");
3960 #endif
3961             continue;
3962         }
3963 
3964         if (ngx_strcmp(value[n].data, "ssl") == 0) {
3965 #if (NGX_HTTP_SSL)
3966             lsopt.ssl = 1;
3967             continue;
3968 #else
3969             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3970                                "the \"ssl\" parameter requires "
3971                                "ngx_http_ssl_module");
3972             return NGX_CONF_ERROR;
3973 #endif
3974         }
3975 
3976         if (ngx_strcmp(value[n].data, "http2") == 0) {
3977 #if (NGX_HTTP_V2)
3978             lsopt.http2 = 1;
3979             continue;
3980 #else
3981             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3982                                "the \"http2\" parameter requires "
3983                                "ngx_http_v2_module");
3984             return NGX_CONF_ERROR;
3985 #endif
3986         }
3987 
3988         if (ngx_strcmp(value[n].data, "spdy") == 0) {
3989             ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
3990                                "invalid parameter \"spdy\": "
3991                                "ngx_http_spdy_module was superseded "
3992                                "by ngx_http_v2_module");
3993             continue;
3994         }
3995 
3996         if (ngx_strncmp(value[n].data, "so_keepalive=", 13) == 0) {
3997 
3998             if (ngx_strcmp(&value[n].data[13], "on") == 0) {
3999                 lsopt.so_keepalive = 1;
4000 
4001             } else if (ngx_strcmp(&value[n].data[13], "off") == 0) {
4002                 lsopt.so_keepalive = 2;
4003 
4004             } else {
4005 
4006 #if (NGX_HAVE_KEEPALIVE_TUNABLE)
4007                 u_char     *p, *end;
4008                 ngx_str_t   s;
4009 
4010                 end = value[n].data + value[n].len;
4011                 s.data = value[n].data + 13;
4012 
4013                 p = ngx_strlchr(s.data, end, ':');
4014                 if (p == NULL) {
4015                     p = end;
4016                 }
4017 
4018                 if (p > s.data) {
4019                     s.len = p - s.data;
4020 
4021                     lsopt.tcp_keepidle = ngx_parse_time(&s, 1);
4022                     if (lsopt.tcp_keepidle == (time_t) NGX_ERROR) {
4023                         goto invalid_so_keepalive;
4024                     }
4025                 }
4026 
4027                 s.data = (p < end) ? (p + 1) : end;
4028 
4029                 p = ngx_strlchr(s.data, end, ':');
4030                 if (p == NULL) {
4031                     p = end;
4032                 }
4033 
4034                 if (p > s.data) {
4035                     s.len = p - s.data;
4036 
4037                     lsopt.tcp_keepintvl = ngx_parse_time(&s, 1);
4038                     if (lsopt.tcp_keepintvl == (time_t) NGX_ERROR) {
4039                         goto invalid_so_keepalive;
4040                     }
4041                 }
4042 
4043                 s.data = (p < end) ? (p + 1) : end;
4044 
4045                 if (s.data < end) {
4046                     s.len = end - s.data;
4047 
4048                     lsopt.tcp_keepcnt = ngx_atoi(s.data, s.len);
4049                     if (lsopt.tcp_keepcnt == NGX_ERROR) {
4050                         goto invalid_so_keepalive;
4051                     }
4052                 }
4053 
4054                 if (lsopt.tcp_keepidle == 0 && lsopt.tcp_keepintvl == 0
4055                     && lsopt.tcp_keepcnt == 0)
4056                 {
4057                     goto invalid_so_keepalive;
4058                 }
4059 
4060                 lsopt.so_keepalive = 1;
4061 
4062 #else
4063 
4064                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4065                                    "the \"so_keepalive\" parameter accepts "
4066                                    "only \"on\" or \"off\" on this platform");
4067                 return NGX_CONF_ERROR;
4068 
4069 #endif
4070             }
4071 
4072             lsopt.set = 1;
4073             lsopt.bind = 1;
4074 
4075             continue;
4076 
4077 #if (NGX_HAVE_KEEPALIVE_TUNABLE)
4078         invalid_so_keepalive:
4079 
4080             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4081                                "invalid so_keepalive value: \"%s\"",
4082                                &value[n].data[13]);
4083             return NGX_CONF_ERROR;
4084 #endif
4085         }
4086 
4087         if (ngx_strcmp(value[n].data, "proxy_protocol") == 0) {
4088             lsopt.proxy_protocol = 1;
4089             continue;
4090         }
4091 
4092         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4093                            "invalid parameter \"%V\"", &value[n]);
4094         return NGX_CONF_ERROR;
4095     }
4096 
4097     if (ngx_http_add_listen(cf, cscf, &lsopt) == NGX_OK) {
4098         return NGX_CONF_OK;
4099     }
4100 
4101     return NGX_CONF_ERROR;
4102 }
4103 
4104 
4105 static char *
4106 ngx_http_core_server_name(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4107 {
4108     ngx_http_core_srv_conf_t *cscf = conf;
4109 
4110     u_char                   ch;
4111     ngx_str_t               *value;
4112     ngx_uint_t               i;
4113     ngx_http_server_name_t  *sn;
4114 
4115     value = cf->args->elts;
4116 
4117     for (i = 1; i < cf->args->nelts; i++) {
4118 
4119         ch = value[i].data[0];
4120 
4121         if ((ch == '*' && (value[i].len < 3 || value[i].data[1] != '.'))
4122             || (ch == '.' && value[i].len < 2))
4123         {
4124             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4125                                "server name \"%V\" is invalid", &value[i]);
4126             return NGX_CONF_ERROR;
4127         }
4128 
4129         if (ngx_strchr(value[i].data, '/')) {
4130             ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
4131                                "server name \"%V\" has suspicious symbols",
4132                                &value[i]);
4133         }
4134 
4135         sn = ngx_array_push(&cscf->server_names);
4136         if (sn == NULL) {
4137             return NGX_CONF_ERROR;
4138         }
4139 
4140 #if (NGX_PCRE)
4141         sn->regex = NULL;
4142 #endif
4143         sn->server = cscf;
4144 
4145         if (ngx_strcasecmp(value[i].data, (u_char *) "$hostname") == 0) {
4146             sn->name = cf->cycle->hostname;
4147 
4148         } else {
4149             sn->name = value[i];
4150         }
4151 
4152         if (value[i].data[0] != '~') {
4153             ngx_strlow(sn->name.data, sn->name.data, sn->name.len);
4154             continue;
4155         }
4156 
4157 #if (NGX_PCRE)
4158         {
4159         u_char               *p;
4160         ngx_regex_compile_t   rc;
4161         u_char                errstr[NGX_MAX_CONF_ERRSTR];
4162 
4163         if (value[i].len == 1) {
4164             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4165                                "empty regex in server name \"%V\"", &value[i]);
4166             return NGX_CONF_ERROR;
4167         }
4168 
4169         value[i].len--;
4170         value[i].data++;
4171 
4172         ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
4173 
4174         rc.pattern = value[i];
4175         rc.err.len = NGX_MAX_CONF_ERRSTR;
4176         rc.err.data = errstr;
4177 
4178         for (p = value[i].data; p < value[i].data + value[i].len; p++) {
4179             if (*p >= 'A' && *p <= 'Z') {
4180                 rc.options = NGX_REGEX_CASELESS;
4181                 break;
4182             }
4183         }
4184 
4185         sn->regex = ngx_http_regex_compile(cf, &rc);
4186         if (sn->regex == NULL) {
4187             return NGX_CONF_ERROR;
4188         }
4189 
4190         sn->name = value[i];
4191         cscf->captures = (rc.captures > 0);
4192         }
4193 #else
4194         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4195                            "using regex \"%V\" "
4196                            "requires PCRE library", &value[i]);
4197 
4198         return NGX_CONF_ERROR;
4199 #endif
4200     }
4201 
4202     return NGX_CONF_OK;
4203 }
4204 
4205 
4206 static char *
4207 ngx_http_core_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4208 {
4209     ngx_http_core_loc_conf_t *clcf = conf;
4210 
4211     ngx_str_t                  *value;
4212     ngx_int_t                   alias;
4213     ngx_uint_t                  n;
4214     ngx_http_script_compile_t   sc;
4215 
4216     alias = (cmd->name.len == sizeof("alias") - 1) ? 1 : 0;
4217 
4218     if (clcf->root.data) {
4219 
4220         if ((clcf->alias != 0) == alias) {
4221             return "is duplicate";
4222         }
4223 
4224         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4225                            "\"%V\" directive is duplicate, "
4226                            "\"%s\" directive was specified earlier",
4227                            &cmd->name, clcf->alias ? "alias" : "root");
4228 
4229         return NGX_CONF_ERROR;
4230     }
4231 
4232     if (clcf->named && alias) {
4233         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4234                            "the \"alias\" directive cannot be used "
4235                            "inside the named location");
4236 
4237         return NGX_CONF_ERROR;
4238     }
4239 
4240     value = cf->args->elts;
4241 
4242     if (ngx_strstr(value[1].data, "$document_root")
4243         || ngx_strstr(value[1].data, "${document_root}"))
4244     {
4245         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4246                            "the $document_root variable cannot be used "
4247                            "in the \"%V\" directive",
4248                            &cmd->name);
4249 
4250         return NGX_CONF_ERROR;
4251     }
4252 
4253     if (ngx_strstr(value[1].data, "$realpath_root")
4254         || ngx_strstr(value[1].data, "${realpath_root}"))
4255     {
4256         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4257                            "the $realpath_root variable cannot be used "
4258                            "in the \"%V\" directive",
4259                            &cmd->name);
4260 
4261         return NGX_CONF_ERROR;
4262     }
4263 
4264     clcf->alias = alias ? clcf->name.len : 0;
4265     clcf->root = value[1];
4266 
4267     if (!alias && clcf->root.len > 0
4268         && clcf->root.data[clcf->root.len - 1] == '/')
4269     {
4270         clcf->root.len--;
4271     }
4272 
4273     if (clcf->root.data[0] != '$') {
4274         if (ngx_conf_full_name(cf->cycle, &clcf->root, 0) != NGX_OK) {
4275             return NGX_CONF_ERROR;
4276         }
4277     }
4278 
4279     n = ngx_http_script_variables_count(&clcf->root);
4280 
4281     ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
4282     sc.variables = n;
4283 
4284 #if (NGX_PCRE)
4285     if (alias && clcf->regex) {
4286         clcf->alias = NGX_MAX_SIZE_T_VALUE;
4287         n = 1;
4288     }
4289 #endif
4290 
4291     if (n) {
4292         sc.cf = cf;
4293         sc.source = &clcf->root;
4294         sc.lengths = &clcf->root_lengths;
4295         sc.values = &clcf->root_values;
4296         sc.complete_lengths = 1;
4297         sc.complete_values = 1;
4298 
4299         if (ngx_http_script_compile(&sc) != NGX_OK) {
4300             return NGX_CONF_ERROR;
4301         }
4302     }
4303 
4304     return NGX_CONF_OK;
4305 }
4306 
4307 
4308 static ngx_http_method_name_t  ngx_methods_names[] = {
4309     { (u_char *) "GET",       (uint32_t) ~NGX_HTTP_GET },
4310     { (u_char *) "HEAD",      (uint32_t) ~NGX_HTTP_HEAD },
4311     { (u_char *) "POST",      (uint32_t) ~NGX_HTTP_POST },
4312     { (u_char *) "PUT",       (uint32_t) ~NGX_HTTP_PUT },
4313     { (u_char *) "DELETE",    (uint32_t) ~NGX_HTTP_DELETE },
4314     { (u_char *) "MKCOL",     (uint32_t) ~NGX_HTTP_MKCOL },
4315     { (u_char *) "COPY",      (uint32_t) ~NGX_HTTP_COPY },
4316     { (u_char *) "MOVE",      (uint32_t) ~NGX_HTTP_MOVE },
4317     { (u_char *) "OPTIONS",   (uint32_t) ~NGX_HTTP_OPTIONS },
4318     { (u_char *) "PROPFIND",  (uint32_t) ~NGX_HTTP_PROPFIND },
4319     { (u_char *) "PROPPATCH", (uint32_t) ~NGX_HTTP_PROPPATCH },
4320     { (u_char *) "LOCK",      (uint32_t) ~NGX_HTTP_LOCK },
4321     { (u_char *) "UNLOCK",    (uint32_t) ~NGX_HTTP_UNLOCK },
4322     { (u_char *) "PATCH",     (uint32_t) ~NGX_HTTP_PATCH },
4323     { NULL, 0 }
4324 };
4325 
4326 
4327 static char *
4328 ngx_http_core_limit_except(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4329 {
4330     ngx_http_core_loc_conf_t *pclcf = conf;
4331 
4332     char                      *rv;
4333     void                      *mconf;
4334     ngx_str_t                 *value;
4335     ngx_uint_t                 i;
4336     ngx_conf_t                 save;
4337     ngx_http_module_t         *module;
4338     ngx_http_conf_ctx_t       *ctx, *pctx;
4339     ngx_http_method_name_t    *name;
4340     ngx_http_core_loc_conf_t  *clcf;
4341 
4342     if (pclcf->limit_except) {
4343         return "is duplicate";
4344     }
4345 
4346     pclcf->limit_except = 0xffffffff;
4347 
4348     value = cf->args->elts;
4349 
4350     for (i = 1; i < cf->args->nelts; i++) {
4351         for (name = ngx_methods_names; name->name; name++) {
4352 
4353             if (ngx_strcasecmp(value[i].data, name->name) == 0) {
4354                 pclcf->limit_except &= name->method;
4355                 goto next;
4356             }
4357         }
4358 
4359         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4360                            "invalid method \"%V\"", &value[i]);
4361         return NGX_CONF_ERROR;
4362 
4363     next:
4364         continue;
4365     }
4366 
4367     if (!(pclcf->limit_except & NGX_HTTP_GET)) {
4368         pclcf->limit_except &= (uint32_t) ~NGX_HTTP_HEAD;
4369     }
4370 
4371     ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
4372     if (ctx == NULL) {
4373         return NGX_CONF_ERROR;
4374     }
4375 
4376     pctx = cf->ctx;
4377     ctx->main_conf = pctx->main_conf;
4378     ctx->srv_conf = pctx->srv_conf;
4379 
4380     ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
4381     if (ctx->loc_conf == NULL) {
4382         return NGX_CONF_ERROR;
4383     }
4384 
4385     for (i = 0; cf->cycle->modules[i]; i++) {
4386         if (cf->cycle->modules[i]->type != NGX_HTTP_MODULE) {
4387             continue;
4388         }
4389 
4390         module = cf->cycle->modules[i]->ctx;
4391 
4392         if (module->create_loc_conf) {
4393 
4394             mconf = module->create_loc_conf(cf);
4395             if (mconf == NULL) {
4396                 return NGX_CONF_ERROR;
4397             }
4398 
4399             ctx->loc_conf[cf->cycle->modules[i]->ctx_index] = mconf;
4400         }
4401     }
4402 
4403 
4404     clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
4405     pclcf->limit_except_loc_conf = ctx->loc_conf;
4406     clcf->loc_conf = ctx->loc_conf;
4407     clcf->name = pclcf->name;
4408     clcf->noname = 1;
4409     clcf->lmt_excpt = 1;
4410 
4411     if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) {
4412         return NGX_CONF_ERROR;
4413     }
4414 
4415     save = *cf;
4416     cf->ctx = ctx;
4417     cf->cmd_type = NGX_HTTP_LMT_CONF;
4418 
4419     rv = ngx_conf_parse(cf, NULL);
4420 
4421     *cf = save;
4422 
4423     return rv;
4424 }
4425 
4426 
4427 static char *
4428 ngx_http_core_set_aio(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4429 {
4430     ngx_http_core_loc_conf_t *clcf = conf;
4431 
4432     ngx_str_t  *value;
4433 
4434     if (clcf->aio != NGX_CONF_UNSET) {
4435         return "is duplicate";
4436     }
4437 
4438 #if (NGX_THREADS)
4439     clcf->thread_pool = NULL;
4440     clcf->thread_pool_value = NULL;
4441 #endif
4442 
4443     value = cf->args->elts;
4444 
4445     if (ngx_strcmp(value[1].data, "off") == 0) {
4446         clcf->aio = NGX_HTTP_AIO_OFF;
4447         return NGX_CONF_OK;
4448     }
4449 
4450     if (ngx_strcmp(value[1].data, "on") == 0) {
4451 #if (NGX_HAVE_FILE_AIO)
4452         clcf->aio = NGX_HTTP_AIO_ON;
4453         return NGX_CONF_OK;
4454 #else
4455         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4456                            "\"aio on\" "
4457                            "is unsupported on this platform");
4458         return NGX_CONF_ERROR;
4459 #endif
4460     }
4461 
4462 #if (NGX_HAVE_AIO_SENDFILE)
4463 
4464     if (ngx_strcmp(value[1].data, "sendfile") == 0) {
4465         clcf->aio = NGX_HTTP_AIO_ON;
4466 
4467         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
4468                            "the \"sendfile\" parameter of "
4469                            "the \"aio\" directive is deprecated");
4470         return NGX_CONF_OK;
4471     }
4472 
4473 #endif
4474 
4475     if (ngx_strncmp(value[1].data, "threads", 7) == 0
4476         && (value[1].len == 7 || value[1].data[7] == '='))
4477     {
4478 #if (NGX_THREADS)
4479         ngx_str_t                          name;
4480         ngx_thread_pool_t                 *tp;
4481         ngx_http_complex_value_t           cv;
4482         ngx_http_compile_complex_value_t   ccv;
4483 
4484         clcf->aio = NGX_HTTP_AIO_THREADS;
4485 
4486         if (value[1].len >= 8) {
4487             name.len = value[1].len - 8;
4488             name.data = value[1].data + 8;
4489 
4490             ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
4491 
4492             ccv.cf = cf;
4493             ccv.value = &name;
4494             ccv.complex_value = &cv;
4495 
4496             if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
4497                 return NGX_CONF_ERROR;
4498             }
4499 
4500             if (cv.lengths != NULL) {
4501                 clcf->thread_pool_value = ngx_palloc(cf->pool,
4502                                     sizeof(ngx_http_complex_value_t));
4503                 if (clcf->thread_pool_value == NULL) {
4504                     return NGX_CONF_ERROR;
4505                 }
4506 
4507                 *clcf->thread_pool_value = cv;
4508 
4509                 return NGX_CONF_OK;
4510             }
4511 
4512             tp = ngx_thread_pool_add(cf, &name);
4513 
4514         } else {
4515             tp = ngx_thread_pool_add(cf, NULL);
4516         }
4517 
4518         if (tp == NULL) {
4519             return NGX_CONF_ERROR;
4520         }
4521 
4522         clcf->thread_pool = tp;
4523 
4524         return NGX_CONF_OK;
4525 #else
4526         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4527                            "\"aio threads\" "
4528                            "is unsupported on this platform");
4529         return NGX_CONF_ERROR;
4530 #endif
4531     }
4532 
4533     return "invalid value";
4534 }
4535 
4536 
4537 static char *
4538 ngx_http_core_directio(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4539 {
4540     ngx_http_core_loc_conf_t *clcf = conf;
4541 
4542     ngx_str_t  *value;
4543 
4544     if (clcf->directio != NGX_CONF_UNSET) {
4545         return "is duplicate";
4546     }
4547 
4548     value = cf->args->elts;
4549 
4550     if (ngx_strcmp(value[1].data, "off") == 0) {
4551         clcf->directio = NGX_OPEN_FILE_DIRECTIO_OFF;
4552         return NGX_CONF_OK;
4553     }
4554 
4555     clcf->directio = ngx_parse_offset(&value[1]);
4556     if (clcf->directio == (off_t) NGX_ERROR) {
4557         return "invalid value";
4558     }
4559 
4560     return NGX_CONF_OK;
4561 }
4562 
4563 
4564 static char *
4565 ngx_http_core_error_page(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4566 {
4567     ngx_http_core_loc_conf_t *clcf = conf;
4568 
4569     u_char                            *p;
4570     ngx_int_t                          overwrite;
4571     ngx_str_t                         *value, uri, args;
4572     ngx_uint_t                         i, n;
4573     ngx_http_err_page_t               *err;
4574     ngx_http_complex_value_t           cv;
4575     ngx_http_compile_complex_value_t   ccv;
4576 
4577     if (clcf->error_pages == NULL) {
4578         clcf->error_pages = ngx_array_create(cf->pool, 4,
4579                                              sizeof(ngx_http_err_page_t));
4580         if (clcf->error_pages == NULL) {
4581             return NGX_CONF_ERROR;
4582         }
4583     }
4584 
4585     value = cf->args->elts;
4586 
4587     i = cf->args->nelts - 2;
4588 
4589     if (value[i].data[0] == '=') {
4590         if (i == 1) {
4591             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4592                                "invalid value \"%V\"", &value[i]);
4593             return NGX_CONF_ERROR;
4594         }
4595 
4596         if (value[i].len > 1) {
4597             overwrite = ngx_atoi(&value[i].data[1], value[i].len - 1);
4598 
4599             if (overwrite == NGX_ERROR) {
4600                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4601                                    "invalid value \"%V\"", &value[i]);
4602                 return NGX_CONF_ERROR;
4603             }
4604 
4605         } else {
4606             overwrite = 0;
4607         }
4608 
4609         n = 2;
4610 
4611     } else {
4612         overwrite = -1;
4613         n = 1;
4614     }
4615 
4616     uri = value[cf->args->nelts - 1];
4617 
4618     ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
4619 
4620     ccv.cf = cf;
4621     ccv.value = &uri;
4622     ccv.complex_value = &cv;
4623 
4624     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
4625         return NGX_CONF_ERROR;
4626     }
4627 
4628     ngx_str_null(&args);
4629 
4630     if (cv.lengths == NULL && uri.len && uri.data[0] == '/') {
4631         p = (u_char *) ngx_strchr(uri.data, '?');
4632 
4633         if (p) {
4634             cv.value.len = p - uri.data;
4635             cv.value.data = uri.data;
4636             p++;
4637             args.len = (uri.data + uri.len) - p;
4638             args.data = p;
4639         }
4640     }
4641 
4642     for (i = 1; i < cf->args->nelts - n; i++) {
4643         err = ngx_array_push(clcf->error_pages);
4644         if (err == NULL) {
4645             return NGX_CONF_ERROR;
4646         }
4647 
4648         err->status = ngx_atoi(value[i].data, value[i].len);
4649 
4650         if (err->status == NGX_ERROR || err->status == 499) {
4651             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4652                                "invalid value \"%V\"", &value[i]);
4653             return NGX_CONF_ERROR;
4654         }
4655 
4656         if (err->status < 300 || err->status > 599) {
4657             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4658                                "value \"%V\" must be between 300 and 599",
4659                                &value[i]);
4660             return NGX_CONF_ERROR;
4661         }
4662 
4663         err->overwrite = overwrite;
4664 
4665         if (overwrite == -1) {
4666             switch (err->status) {
4667                 case NGX_HTTP_TO_HTTPS:
4668                 case NGX_HTTPS_CERT_ERROR:
4669                 case NGX_HTTPS_NO_CERT:
4670                     err->overwrite = NGX_HTTP_BAD_REQUEST;
4671             }
4672         }
4673 
4674         err->value = cv;
4675         err->args = args;
4676     }
4677 
4678     return NGX_CONF_OK;
4679 }
4680 
4681 
4682 static char *
4683 ngx_http_core_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4684 {
4685     ngx_http_core_loc_conf_t *clcf = conf;
4686 
4687     time_t       inactive;
4688     ngx_str_t   *value, s;
4689     ngx_int_t    max;
4690     ngx_uint_t   i;
4691 
4692     if (clcf->open_file_cache != NGX_CONF_UNSET_PTR) {
4693         return "is duplicate";
4694     }
4695 
4696     value = cf->args->elts;
4697 
4698     max = 0;
4699     inactive = 60;
4700 
4701     for (i = 1; i < cf->args->nelts; i++) {
4702 
4703         if (ngx_strncmp(value[i].data, "max=", 4) == 0) {
4704 
4705             max = ngx_atoi(value[i].data + 4, value[i].len - 4);
4706             if (max <= 0) {
4707                 goto failed;
4708             }
4709 
4710             continue;
4711         }
4712 
4713         if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
4714 
4715             s.len = value[i].len - 9;
4716             s.data = value[i].data + 9;
4717 
4718             inactive = ngx_parse_time(&s, 1);
4719             if (inactive == (time_t) NGX_ERROR) {
4720                 goto failed;
4721             }
4722 
4723             continue;
4724         }
4725 
4726         if (ngx_strcmp(value[i].data, "off") == 0) {
4727 
4728             clcf->open_file_cache = NULL;
4729 
4730             continue;
4731         }
4732 
4733     failed:
4734 
4735         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4736                            "invalid \"open_file_cache\" parameter \"%V\"",
4737                            &value[i]);
4738         return NGX_CONF_ERROR;
4739     }
4740 
4741     if (clcf->open_file_cache == NULL) {
4742         return NGX_CONF_OK;
4743     }
4744 
4745     if (max == 0) {
4746         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4747                         "\"open_file_cache\" must have the \"max\" parameter");
4748         return NGX_CONF_ERROR;
4749     }
4750 
4751     clcf->open_file_cache = ngx_open_file_cache_init(cf->pool, max, inactive);
4752     if (clcf->open_file_cache) {
4753         return NGX_CONF_OK;
4754     }
4755 
4756     return NGX_CONF_ERROR;
4757 }
4758 
4759 
4760 static char *
4761 ngx_http_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4762 {
4763     ngx_http_core_loc_conf_t *clcf = conf;
4764 
4765     return ngx_log_set_log(cf, &clcf->error_log);
4766 }
4767 
4768 
4769 static char *
4770 ngx_http_core_keepalive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4771 {
4772     ngx_http_core_loc_conf_t *clcf = conf;
4773 
4774     ngx_str_t  *value;
4775 
4776     if (clcf->keepalive_timeout != NGX_CONF_UNSET_MSEC) {
4777         return "is duplicate";
4778     }
4779 
4780     value = cf->args->elts;
4781 
4782     clcf->keepalive_timeout = ngx_parse_time(&value[1], 0);
4783 
4784     if (clcf->keepalive_timeout == (ngx_msec_t) NGX_ERROR) {
4785         return "invalid value";
4786     }
4787 
4788     if (cf->args->nelts == 2) {
4789         return NGX_CONF_OK;
4790     }
4791 
4792     clcf->keepalive_header = ngx_parse_time(&value[2], 1);
4793 
4794     if (clcf->keepalive_header == (time_t) NGX_ERROR) {
4795         return "invalid value";
4796     }
4797 
4798     return NGX_CONF_OK;
4799 }
4800 
4801 
4802 static char *
4803 ngx_http_core_internal(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4804 {
4805     ngx_http_core_loc_conf_t *clcf = conf;
4806 
4807     if (clcf->internal != NGX_CONF_UNSET) {
4808         return "is duplicate";
4809     }
4810 
4811     clcf->internal = 1;
4812 
4813     return NGX_CONF_OK;
4814 }
4815 
4816 
4817 static char *
4818 ngx_http_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4819 {
4820     ngx_http_core_loc_conf_t  *clcf = conf;
4821 
4822     ngx_str_t  *value;
4823 
4824     if (clcf->resolver) {
4825         return "is duplicate";
4826     }
4827 
4828     value = cf->args->elts;
4829 
4830     clcf->resolver = ngx_resolver_create(cf, &value[1], cf->args->nelts - 1);
4831     if (clcf->resolver == NULL) {
4832         return NGX_CONF_ERROR;
4833     }
4834 
4835     return NGX_CONF_OK;
4836 }
4837 
4838 
4839 #if (NGX_HTTP_GZIP)
4840 
4841 static char *
4842 ngx_http_gzip_disable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4843 {
4844     ngx_http_core_loc_conf_t  *clcf = conf;
4845 
4846 #if (NGX_PCRE)
4847 
4848     ngx_str_t            *value;
4849     ngx_uint_t            i;
4850     ngx_regex_elt_t      *re;
4851     ngx_regex_compile_t   rc;
4852     u_char                errstr[NGX_MAX_CONF_ERRSTR];
4853 
4854     if (clcf->gzip_disable == NGX_CONF_UNSET_PTR) {
4855         clcf->gzip_disable = ngx_array_create(cf->pool, 2,
4856                                               sizeof(ngx_regex_elt_t));
4857         if (clcf->gzip_disable == NULL) {
4858             return NGX_CONF_ERROR;
4859         }
4860     }
4861 
4862     value = cf->args->elts;
4863 
4864     ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
4865 
4866     rc.pool = cf->pool;
4867     rc.err.len = NGX_MAX_CONF_ERRSTR;
4868     rc.err.data = errstr;
4869 
4870     for (i = 1; i < cf->args->nelts; i++) {
4871 
4872         if (ngx_strcmp(value[i].data, "msie6") == 0) {
4873             clcf->gzip_disable_msie6 = 1;
4874             continue;
4875         }
4876 
4877 #if (NGX_HTTP_DEGRADATION)
4878 
4879         if (ngx_strcmp(value[i].data, "degradation") == 0) {
4880             clcf->gzip_disable_degradation = 1;
4881             continue;
4882         }
4883 
4884 #endif
4885 
4886         re = ngx_array_push(clcf->gzip_disable);
4887         if (re == NULL) {
4888             return NGX_CONF_ERROR;
4889         }
4890 
4891         rc.pattern = value[i];
4892         rc.options = NGX_REGEX_CASELESS;
4893 
4894         if (ngx_regex_compile(&rc) != NGX_OK) {
4895             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V", &rc.err);
4896             return NGX_CONF_ERROR;
4897         }
4898 
4899         re->regex = rc.regex;
4900         re->name = value[i].data;
4901     }
4902 
4903     return NGX_CONF_OK;
4904 
4905 #else
4906     ngx_str_t   *value;
4907     ngx_uint_t   i;
4908 
4909     value = cf->args->elts;
4910 
4911     for (i = 1; i < cf->args->nelts; i++) {
4912         if (ngx_strcmp(value[i].data, "msie6") == 0) {
4913             clcf->gzip_disable_msie6 = 1;
4914             continue;
4915         }
4916 
4917 #if (NGX_HTTP_DEGRADATION)
4918 
4919         if (ngx_strcmp(value[i].data, "degradation") == 0) {
4920             clcf->gzip_disable_degradation = 1;
4921             continue;
4922         }
4923 
4924 #endif
4925 
4926         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4927                            "without PCRE library \"gzip_disable\" supports "
4928                            "builtin \"msie6\" and \"degradation\" mask only");
4929 
4930         return NGX_CONF_ERROR;
4931     }
4932 
4933     return NGX_CONF_OK;
4934 
4935 #endif
4936 }
4937 
4938 #endif
4939 
4940 
4941 #if (NGX_HAVE_OPENAT)
4942 
4943 static char *
4944 ngx_http_disable_symlinks(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4945 {
4946     ngx_http_core_loc_conf_t *clcf = conf;
4947 
4948     ngx_str_t                         *value;
4949     ngx_uint_t                         i;
4950     ngx_http_compile_complex_value_t   ccv;
4951 
4952     if (clcf->disable_symlinks != NGX_CONF_UNSET_UINT) {
4953         return "is duplicate";
4954     }
4955 
4956     value = cf->args->elts;
4957 
4958     for (i = 1; i < cf->args->nelts; i++) {
4959 
4960         if (ngx_strcmp(value[i].data, "off") == 0) {
4961             clcf->disable_symlinks = NGX_DISABLE_SYMLINKS_OFF;
4962             continue;
4963         }
4964 
4965         if (ngx_strcmp(value[i].data, "if_not_owner") == 0) {
4966             clcf->disable_symlinks = NGX_DISABLE_SYMLINKS_NOTOWNER;
4967             continue;
4968         }
4969 
4970         if (ngx_strcmp(value[i].data, "on") == 0) {
4971             clcf->disable_symlinks = NGX_DISABLE_SYMLINKS_ON;
4972             continue;
4973         }
4974 
4975         if (ngx_strncmp(value[i].data, "from=", 5) == 0) {
4976             value[i].len -= 5;
4977             value[i].data += 5;
4978 
4979             ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
4980 
4981             ccv.cf = cf;
4982             ccv.value = &value[i];
4983             ccv.complex_value = ngx_palloc(cf->pool,
4984                                            sizeof(ngx_http_complex_value_t));
4985             if (ccv.complex_value == NULL) {
4986                 return NGX_CONF_ERROR;
4987             }
4988 
4989             if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
4990                 return NGX_CONF_ERROR;
4991             }
4992 
4993             clcf->disable_symlinks_from = ccv.complex_value;
4994 
4995             continue;
4996         }
4997 
4998         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4999                            "invalid parameter \"%V\"", &value[i]);
5000         return NGX_CONF_ERROR;
5001     }
5002 
5003     if (clcf->disable_symlinks == NGX_CONF_UNSET_UINT) {
5004         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5005                            "\"%V\" must have \"off\", \"on\" "
5006                            "or \"if_not_owner\" parameter",
5007                            &cmd->name);
5008         return NGX_CONF_ERROR;
5009     }
5010 
5011     if (cf->args->nelts == 2) {
5012         clcf->disable_symlinks_from = NULL;
5013         return NGX_CONF_OK;
5014     }
5015 
5016     if (clcf->disable_symlinks_from == NGX_CONF_UNSET_PTR) {
5017         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5018                            "duplicate parameters \"%V %V\"",
5019                            &value[1], &value[2]);
5020         return NGX_CONF_ERROR;
5021     }
5022 
5023     if (clcf->disable_symlinks == NGX_DISABLE_SYMLINKS_OFF) {
5024         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5025                            "\"from=\" cannot be used with \"off\" parameter");
5026         return NGX_CONF_ERROR;
5027     }
5028 
5029     return NGX_CONF_OK;
5030 }
5031 
5032 #endif
5033 
5034 
5035 static char *
5036 ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data)
5037 {
5038 #if (NGX_FREEBSD)
5039     ssize_t *np = data;
5040 
5041     if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
5042         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5043                            "\"send_lowat\" must be less than %d "
5044                            "(sysctl net.inet.tcp.sendspace)",
5045                            ngx_freebsd_net_inet_tcp_sendspace);
5046 
5047         return NGX_CONF_ERROR;
5048     }
5049 
5050 #elif !(NGX_HAVE_SO_SNDLOWAT)
5051     ssize_t *np = data;
5052 
5053     ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
5054                        "\"send_lowat\" is not supported, ignored");
5055 
5056     *np = 0;
5057 
5058 #endif
5059 
5060     return NGX_CONF_OK;
5061 }
5062 
5063 
5064 static char *
5065 ngx_http_core_pool_size(ngx_conf_t *cf, void *post, void *data)
5066 {
5067     size_t *sp = data;
5068 
5069     if (*sp < NGX_MIN_POOL_SIZE) {
5070         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5071                            "the pool size must be no less than %uz",
5072                            NGX_MIN_POOL_SIZE);
5073         return NGX_CONF_ERROR;
5074     }
5075 
5076     if (*sp % NGX_POOL_ALIGNMENT) {
5077         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5078                            "the pool size must be a multiple of %uz",
5079                            NGX_POOL_ALIGNMENT);
5080         return NGX_CONF_ERROR;
5081     }
5082 
5083     return NGX_CONF_OK;
5084 }