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     ngx_array_t           *lengths;
0015     ngx_array_t           *values;
0016     ngx_str_t              name;
0017 
0018     unsigned               code:10;
0019     unsigned               test_dir:1;
0020 } ngx_http_try_file_t;
0021 
0022 
0023 typedef struct {
0024     ngx_http_try_file_t   *try_files;
0025 } ngx_http_try_files_loc_conf_t;
0026 
0027 
0028 static ngx_int_t ngx_http_try_files_handler(ngx_http_request_t *r);
0029 static char *ngx_http_try_files(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
0030 static void *ngx_http_try_files_create_loc_conf(ngx_conf_t *cf);
0031 static ngx_int_t ngx_http_try_files_init(ngx_conf_t *cf);
0032 
0033 
0034 static ngx_command_t  ngx_http_try_files_commands[] = {
0035 
0036     { ngx_string("try_files"),
0037       NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
0038       ngx_http_try_files,
0039       NGX_HTTP_LOC_CONF_OFFSET,
0040       0,
0041       NULL },
0042 
0043       ngx_null_command
0044 };
0045 
0046 
0047 static ngx_http_module_t  ngx_http_try_files_module_ctx = {
0048     NULL,                                  /* preconfiguration */
0049     ngx_http_try_files_init,               /* postconfiguration */
0050 
0051     NULL,                                  /* create main configuration */
0052     NULL,                                  /* init main configuration */
0053 
0054     NULL,                                  /* create server configuration */
0055     NULL,                                  /* merge server configuration */
0056 
0057     ngx_http_try_files_create_loc_conf,    /* create location configuration */
0058     NULL                                   /* merge location configuration */
0059 };
0060 
0061 
0062 ngx_module_t  ngx_http_try_files_module = {
0063     NGX_MODULE_V1,
0064     &ngx_http_try_files_module_ctx,        /* module context */
0065     ngx_http_try_files_commands,           /* module directives */
0066     NGX_HTTP_MODULE,                       /* module type */
0067     NULL,                                  /* init master */
0068     NULL,                                  /* init module */
0069     NULL,                                  /* init process */
0070     NULL,                                  /* init thread */
0071     NULL,                                  /* exit thread */
0072     NULL,                                  /* exit process */
0073     NULL,                                  /* exit master */
0074     NGX_MODULE_V1_PADDING
0075 };
0076 
0077 
0078 static ngx_int_t
0079 ngx_http_try_files_handler(ngx_http_request_t *r)
0080 {
0081     size_t                          len, root, alias, reserve, allocated;
0082     u_char                         *p, *name;
0083     ngx_str_t                       path, args;
0084     ngx_uint_t                      test_dir;
0085     ngx_http_try_file_t            *tf;
0086     ngx_open_file_info_t            of;
0087     ngx_http_script_code_pt         code;
0088     ngx_http_script_engine_t        e;
0089     ngx_http_core_loc_conf_t       *clcf;
0090     ngx_http_script_len_code_pt     lcode;
0091     ngx_http_try_files_loc_conf_t  *tlcf;
0092 
0093     tlcf = ngx_http_get_module_loc_conf(r, ngx_http_try_files_module);
0094 
0095     if (tlcf->try_files == NULL) {
0096         return NGX_DECLINED;
0097     }
0098 
0099     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0100                    "try files handler");
0101 
0102     allocated = 0;
0103     root = 0;
0104     name = NULL;
0105     /* suppress MSVC warning */
0106     path.data = NULL;
0107 
0108     tf = tlcf->try_files;
0109 
0110     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
0111 
0112     alias = clcf->alias;
0113 
0114     for ( ;; ) {
0115 
0116         if (tf->lengths) {
0117             ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
0118 
0119             e.ip = tf->lengths->elts;
0120             e.request = r;
0121 
0122             /* 1 is for terminating '\0' as in static names */
0123             len = 1;
0124 
0125             while (*(uintptr_t *) e.ip) {
0126                 lcode = *(ngx_http_script_len_code_pt *) e.ip;
0127                 len += lcode(&e);
0128             }
0129 
0130         } else {
0131             len = tf->name.len;
0132         }
0133 
0134         if (!alias) {
0135             reserve = len > r->uri.len ? len - r->uri.len : 0;
0136 
0137         } else if (alias == NGX_MAX_SIZE_T_VALUE) {
0138             reserve = len;
0139 
0140         } else {
0141             reserve = len > r->uri.len - alias ? len - (r->uri.len - alias) : 0;
0142         }
0143 
0144         if (reserve > allocated || !allocated) {
0145 
0146             /* 16 bytes are preallocation */
0147             allocated = reserve + 16;
0148 
0149             if (ngx_http_map_uri_to_path(r, &path, &root, allocated) == NULL) {
0150                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
0151             }
0152 
0153             name = path.data + root;
0154         }
0155 
0156         if (tf->values == NULL) {
0157 
0158             /* tf->name.len includes the terminating '\0' */
0159 
0160             ngx_memcpy(name, tf->name.data, tf->name.len);
0161 
0162             path.len = (name + tf->name.len - 1) - path.data;
0163 
0164         } else {
0165             e.ip = tf->values->elts;
0166             e.pos = name;
0167             e.flushed = 1;
0168 
0169             while (*(uintptr_t *) e.ip) {
0170                 code = *(ngx_http_script_code_pt *) e.ip;
0171                 code((ngx_http_script_engine_t *) &e);
0172             }
0173 
0174             path.len = e.pos - path.data;
0175 
0176             *e.pos = '\0';
0177 
0178             if (alias && alias != NGX_MAX_SIZE_T_VALUE
0179                 && ngx_strncmp(name, r->uri.data, alias) == 0)
0180             {
0181                 ngx_memmove(name, name + alias, len - alias);
0182                 path.len -= alias;
0183             }
0184         }
0185 
0186         test_dir = tf->test_dir;
0187 
0188         tf++;
0189 
0190         ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0191                        "trying to use %s: \"%s\" \"%s\"",
0192                        test_dir ? "dir" : "file", name, path.data);
0193 
0194         if (tf->lengths == NULL && tf->name.len == 0) {
0195 
0196             if (tf->code) {
0197                 return tf->code;
0198             }
0199 
0200             path.len -= root;
0201             path.data += root;
0202 
0203             if (path.data[0] == '@') {
0204                 (void) ngx_http_named_location(r, &path);
0205 
0206             } else {
0207                 ngx_http_split_args(r, &path, &args);
0208 
0209                 (void) ngx_http_internal_redirect(r, &path, &args);
0210             }
0211 
0212             ngx_http_finalize_request(r, NGX_DONE);
0213             return NGX_DONE;
0214         }
0215 
0216         ngx_memzero(&of, sizeof(ngx_open_file_info_t));
0217 
0218         of.read_ahead = clcf->read_ahead;
0219         of.directio = clcf->directio;
0220         of.valid = clcf->open_file_cache_valid;
0221         of.min_uses = clcf->open_file_cache_min_uses;
0222         of.test_only = 1;
0223         of.errors = clcf->open_file_cache_errors;
0224         of.events = clcf->open_file_cache_events;
0225 
0226         if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
0227             return NGX_HTTP_INTERNAL_SERVER_ERROR;
0228         }
0229 
0230         if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
0231             != NGX_OK)
0232         {
0233             if (of.err == 0) {
0234                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
0235             }
0236 
0237             if (of.err != NGX_ENOENT
0238                 && of.err != NGX_ENOTDIR
0239                 && of.err != NGX_ENAMETOOLONG)
0240             {
0241                 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
0242                               "%s \"%s\" failed", of.failed, path.data);
0243             }
0244 
0245             continue;
0246         }
0247 
0248         if (of.is_dir != test_dir) {
0249             continue;
0250         }
0251 
0252         path.len -= root;
0253         path.data += root;
0254 
0255         if (!alias) {
0256             r->uri = path;
0257 
0258         } else if (alias == NGX_MAX_SIZE_T_VALUE) {
0259             if (!test_dir) {
0260                 r->uri = path;
0261                 r->add_uri_to_alias = 1;
0262             }
0263 
0264         } else {
0265             name = r->uri.data;
0266 
0267             r->uri.len = alias + path.len;
0268             r->uri.data = ngx_pnalloc(r->pool, r->uri.len);
0269             if (r->uri.data == NULL) {
0270                 r->uri.len = 0;
0271                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
0272             }
0273 
0274             p = ngx_copy(r->uri.data, name, alias);
0275             ngx_memcpy(p, path.data, path.len);
0276         }
0277 
0278         ngx_http_set_exten(r);
0279 
0280         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0281                        "try file uri: \"%V\"", &r->uri);
0282 
0283         return NGX_DECLINED;
0284     }
0285 
0286     /* not reached */
0287 }
0288 
0289 
0290 static char *
0291 ngx_http_try_files(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0292 {
0293     ngx_http_try_files_loc_conf_t *tlcf = conf;
0294 
0295     ngx_str_t                  *value;
0296     ngx_int_t                   code;
0297     ngx_uint_t                  i, n;
0298     ngx_http_try_file_t        *tf;
0299     ngx_http_script_compile_t   sc;
0300 
0301     if (tlcf->try_files) {
0302         return "is duplicate";
0303     }
0304 
0305     tf = ngx_pcalloc(cf->pool, cf->args->nelts * sizeof(ngx_http_try_file_t));
0306     if (tf == NULL) {
0307         return NGX_CONF_ERROR;
0308     }
0309 
0310     tlcf->try_files = tf;
0311 
0312     value = cf->args->elts;
0313 
0314     for (i = 0; i < cf->args->nelts - 1; i++) {
0315 
0316         tf[i].name = value[i + 1];
0317 
0318         if (tf[i].name.len > 0
0319             && tf[i].name.data[tf[i].name.len - 1] == '/'
0320             && i + 2 < cf->args->nelts)
0321         {
0322             tf[i].test_dir = 1;
0323             tf[i].name.len--;
0324             tf[i].name.data[tf[i].name.len] = '\0';
0325         }
0326 
0327         n = ngx_http_script_variables_count(&tf[i].name);
0328 
0329         if (n) {
0330             ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
0331 
0332             sc.cf = cf;
0333             sc.source = &tf[i].name;
0334             sc.lengths = &tf[i].lengths;
0335             sc.values = &tf[i].values;
0336             sc.variables = n;
0337             sc.complete_lengths = 1;
0338             sc.complete_values = 1;
0339 
0340             if (ngx_http_script_compile(&sc) != NGX_OK) {
0341                 return NGX_CONF_ERROR;
0342             }
0343 
0344         } else {
0345             /* add trailing '\0' to length */
0346             tf[i].name.len++;
0347         }
0348     }
0349 
0350     if (tf[i - 1].name.data[0] == '=') {
0351 
0352         code = ngx_atoi(tf[i - 1].name.data + 1, tf[i - 1].name.len - 2);
0353 
0354         if (code == NGX_ERROR || code > 999) {
0355             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0356                                "invalid code \"%*s\"",
0357                                tf[i - 1].name.len - 1, tf[i - 1].name.data);
0358             return NGX_CONF_ERROR;
0359         }
0360 
0361         tf[i].code = code;
0362     }
0363 
0364     return NGX_CONF_OK;
0365 }
0366 
0367 
0368 static void *
0369 ngx_http_try_files_create_loc_conf(ngx_conf_t *cf)
0370 {
0371     ngx_http_try_files_loc_conf_t  *tlcf;
0372 
0373     tlcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_try_files_loc_conf_t));
0374     if (tlcf == NULL) {
0375         return NULL;
0376     }
0377 
0378     /*
0379      * set by ngx_pcalloc():
0380      *
0381      *     tlcf->try_files = NULL;
0382      */
0383 
0384     return tlcf;
0385 }
0386 
0387 
0388 static ngx_int_t
0389 ngx_http_try_files_init(ngx_conf_t *cf)
0390 {
0391     ngx_http_handler_pt        *h;
0392     ngx_http_core_main_conf_t  *cmcf;
0393 
0394     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
0395 
0396     h = ngx_array_push(&cmcf->phases[NGX_HTTP_PRECONTENT_PHASE].handlers);
0397     if (h == NULL) {
0398         return NGX_ERROR;
0399     }
0400 
0401     *h = ngx_http_try_files_handler;
0402 
0403     return NGX_OK;
0404 }