src/http/modules/ngx_http_try_files_module.c - nginx-1.31.4 nginx/ @ 8d9666701

Global variables defined

Data types defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>


  8. typedef struct {
  9.     ngx_array_t           *lengths;
  10.     ngx_array_t           *values;
  11.     ngx_str_t              name;

  12.     unsigned               code:10;
  13.     unsigned               test_dir:1;
  14. } ngx_http_try_file_t;


  15. typedef struct {
  16.     ngx_http_try_file_t   *try_files;
  17. } ngx_http_try_files_loc_conf_t;


  18. static ngx_int_t ngx_http_try_files_handler(ngx_http_request_t *r);
  19. static char *ngx_http_try_files(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  20. static void *ngx_http_try_files_create_loc_conf(ngx_conf_t *cf);
  21. static ngx_int_t ngx_http_try_files_init(ngx_conf_t *cf);


  22. static ngx_command_t  ngx_http_try_files_commands[] = {

  23.     { ngx_string("try_files"),
  24.       NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
  25.       ngx_http_try_files,
  26.       NGX_HTTP_LOC_CONF_OFFSET,
  27.       0,
  28.       NULL },

  29.       ngx_null_command
  30. };


  31. static ngx_http_module_t  ngx_http_try_files_module_ctx = {
  32.     NULL,                                  /* preconfiguration */
  33.     ngx_http_try_files_init,               /* postconfiguration */

  34.     NULL,                                  /* create main configuration */
  35.     NULL,                                  /* init main configuration */

  36.     NULL,                                  /* create server configuration */
  37.     NULL,                                  /* merge server configuration */

  38.     ngx_http_try_files_create_loc_conf,    /* create location configuration */
  39.     NULL                                   /* merge location configuration */
  40. };


  41. ngx_module_t  ngx_http_try_files_module = {
  42.     NGX_MODULE_V1,
  43.     &ngx_http_try_files_module_ctx,        /* module context */
  44.     ngx_http_try_files_commands,           /* module directives */
  45.     NGX_HTTP_MODULE,                       /* module type */
  46.     NULL,                                  /* init master */
  47.     NULL,                                  /* init module */
  48.     NULL,                                  /* init process */
  49.     NULL,                                  /* init thread */
  50.     NULL,                                  /* exit thread */
  51.     NULL,                                  /* exit process */
  52.     NULL,                                  /* exit master */
  53.     NGX_MODULE_V1_PADDING
  54. };


  55. static ngx_int_t
  56. ngx_http_try_files_handler(ngx_http_request_t *r)
  57. {
  58.     size_t                          len, root, alias, reserve, allocated, n;
  59.     u_char                         *p, *name;
  60.     ngx_str_t                       path, args;
  61.     ngx_uint_t                      test_dir;
  62.     ngx_http_try_file_t            *tf;
  63.     ngx_open_file_info_t            of;
  64.     ngx_http_script_code_pt         code;
  65.     ngx_http_script_engine_t        e;
  66.     ngx_http_core_loc_conf_t       *clcf;
  67.     ngx_http_script_len_code_pt     lcode;
  68.     ngx_http_try_files_loc_conf_t  *tlcf;

  69.     tlcf = ngx_http_get_module_loc_conf(r, ngx_http_try_files_module);

  70.     if (tlcf->try_files == NULL) {
  71.         return NGX_DECLINED;
  72.     }

  73.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  74.                    "try files handler");

  75.     allocated = 0;
  76.     root = 0;
  77.     name = NULL;
  78.     /* suppress MSVC warning */
  79.     path.data = NULL;

  80.     tf = tlcf->try_files;

  81.     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

  82.     alias = clcf->alias;

  83.     for ( ;; ) {

  84.         if (tf->lengths) {
  85.             ngx_memzero(&e, sizeof(ngx_http_script_engine_t));

  86.             e.ip = tf->lengths->elts;
  87.             e.request = r;

  88.             /* 1 is for terminating '\0' as in static names */
  89.             len = 1;

  90.             while (*(uintptr_t *) e.ip) {
  91.                 lcode = *(ngx_http_script_len_code_pt *) e.ip;
  92.                 len += lcode(&e);
  93.             }

  94.         } else {
  95.             len = tf->name.len;
  96.         }

  97.         if (!alias) {
  98.             reserve = len > r->uri.len ? len - r->uri.len : 0;

  99.         } else if (alias == NGX_MAX_SIZE_T_VALUE) {
  100.             reserve = len;

  101.         } else {
  102.             reserve = len > r->uri.len - alias ? len - (r->uri.len - alias) : 0;
  103.         }

  104.         if (reserve > allocated || !allocated) {

  105.             /* 16 bytes are preallocation */
  106.             allocated = reserve + 16;

  107.             if (ngx_http_map_uri_to_path(r, &path, &root, allocated) == NULL) {
  108.                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
  109.             }

  110.             name = path.data + root;
  111.         }

  112.         if (tf->values == NULL) {

  113.             /* tf->name.len includes the terminating '\0' */

  114.             ngx_memcpy(name, tf->name.data, tf->name.len);

  115.             path.len = (name + tf->name.len - 1) - path.data;

  116.         } else {
  117.             n = allocated;

  118.             if (alias != NGX_MAX_SIZE_T_VALUE) {
  119.                 n += (r->uri.len - alias);
  120.             }

  121.             e.ip = tf->values->elts;
  122.             e.pos = name;
  123.             e.end = name + n;
  124.             e.flushed = 1;

  125.             while (*(uintptr_t *) e.ip) {
  126.                 code = *(ngx_http_script_code_pt *) e.ip;
  127.                 code((ngx_http_script_engine_t *) &e);
  128.             }

  129.             if (e.status) {
  130.                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
  131.             }

  132.             if (ngx_http_script_check_length(&e, 1) != NGX_OK) {
  133.                 return NGX_ERROR;
  134.             }

  135.             path.len = e.pos - path.data;

  136.             *e.pos = '\0';

  137.             if (alias && alias != NGX_MAX_SIZE_T_VALUE
  138.                 && ngx_strncmp(name, r->uri.data, alias) == 0)
  139.             {
  140.                 ngx_memmove(name, name + alias, len - alias);
  141.                 path.len -= alias;
  142.             }
  143.         }

  144.         test_dir = tf->test_dir;

  145.         tf++;

  146.         ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  147.                        "trying to use %s: \"%s\" \"%s\"",
  148.                        test_dir ? "dir" : "file", name, path.data);

  149.         if (tf->lengths == NULL && tf->name.len == 0) {

  150.             if (tf->code) {
  151.                 return tf->code;
  152.             }

  153.             path.len -= root;
  154.             path.data += root;

  155.             if (path.data[0] == '@') {
  156.                 (void) ngx_http_named_location(r, &path);

  157.             } else {
  158.                 ngx_http_split_args(r, &path, &args);

  159.                 (void) ngx_http_internal_redirect(r, &path, &args);
  160.             }

  161.             ngx_http_finalize_request(r, NGX_DONE);
  162.             return NGX_DONE;
  163.         }

  164.         ngx_memzero(&of, sizeof(ngx_open_file_info_t));

  165.         of.read_ahead = clcf->read_ahead;
  166.         of.directio = clcf->directio;
  167.         of.valid = clcf->open_file_cache_valid;
  168.         of.min_uses = clcf->open_file_cache_min_uses;
  169.         of.test_only = 1;
  170.         of.errors = clcf->open_file_cache_errors;
  171.         of.events = clcf->open_file_cache_events;

  172.         if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
  173.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  174.         }

  175.         if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
  176.             != NGX_OK)
  177.         {
  178.             if (of.err == 0) {
  179.                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
  180.             }

  181.             if (of.err != NGX_ENOENT
  182.                 && of.err != NGX_ENOTDIR
  183.                 && of.err != NGX_ENAMETOOLONG)
  184.             {
  185.                 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
  186.                               "%s \"%s\" failed", of.failed, path.data);
  187.             }

  188.             continue;
  189.         }

  190.         if (of.is_dir != test_dir) {
  191.             continue;
  192.         }

  193.         path.len -= root;
  194.         path.data += root;

  195.         if (!alias) {
  196.             r->uri = path;

  197.         } else if (alias == NGX_MAX_SIZE_T_VALUE) {
  198.             if (!test_dir) {
  199.                 r->uri = path;
  200.                 r->add_uri_to_alias = 1;
  201.             }

  202.         } else {
  203.             name = r->uri.data;

  204.             r->uri.len = alias + path.len;
  205.             r->uri.data = ngx_pnalloc(r->pool, r->uri.len);
  206.             if (r->uri.data == NULL) {
  207.                 r->uri.len = 0;
  208.                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
  209.             }

  210.             p = ngx_copy(r->uri.data, name, alias);
  211.             ngx_memcpy(p, path.data, path.len);
  212.         }

  213.         ngx_http_set_exten(r);

  214.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  215.                        "try file uri: \"%V\"", &r->uri);

  216.         return NGX_DECLINED;
  217.     }

  218.     /* not reached */
  219. }


  220. static char *
  221. ngx_http_try_files(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  222. {
  223.     ngx_http_try_files_loc_conf_t *tlcf = conf;

  224.     ngx_str_t                  *value;
  225.     ngx_int_t                   code;
  226.     ngx_uint_t                  i, n;
  227.     ngx_http_try_file_t        *tf;
  228.     ngx_http_script_compile_t   sc;

  229.     if (tlcf->try_files) {
  230.         return "is duplicate";
  231.     }

  232.     tf = ngx_pcalloc(cf->pool, cf->args->nelts * sizeof(ngx_http_try_file_t));
  233.     if (tf == NULL) {
  234.         return NGX_CONF_ERROR;
  235.     }

  236.     tlcf->try_files = tf;

  237.     value = cf->args->elts;

  238.     for (i = 0; i < cf->args->nelts - 1; i++) {

  239.         tf[i].name = value[i + 1];

  240.         if (tf[i].name.len > 0
  241.             && tf[i].name.data[tf[i].name.len - 1] == '/'
  242.             && i + 2 < cf->args->nelts)
  243.         {
  244.             tf[i].test_dir = 1;
  245.             tf[i].name.len--;
  246.             tf[i].name.data[tf[i].name.len] = '\0';
  247.         }

  248.         n = ngx_http_script_variables_count(&tf[i].name);

  249.         if (n) {
  250.             ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));

  251.             sc.cf = cf;
  252.             sc.source = &tf[i].name;
  253.             sc.lengths = &tf[i].lengths;
  254.             sc.values = &tf[i].values;
  255.             sc.variables = n;
  256.             sc.complete_lengths = 1;
  257.             sc.complete_values = 1;

  258.             if (ngx_http_script_compile(&sc) != NGX_OK) {
  259.                 return NGX_CONF_ERROR;
  260.             }

  261.         } else {
  262.             /* add trailing '\0' to length */
  263.             tf[i].name.len++;
  264.         }
  265.     }

  266.     if (tf[i - 1].name.data[0] == '=') {

  267.         code = ngx_atoi(tf[i - 1].name.data + 1, tf[i - 1].name.len - 2);

  268.         if (code == NGX_ERROR || code > 999) {
  269.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  270.                                "invalid code \"%*s\"",
  271.                                tf[i - 1].name.len - 1, tf[i - 1].name.data);
  272.             return NGX_CONF_ERROR;
  273.         }

  274.         tf[i].code = code;
  275.     }

  276.     return NGX_CONF_OK;
  277. }


  278. static void *
  279. ngx_http_try_files_create_loc_conf(ngx_conf_t *cf)
  280. {
  281.     ngx_http_try_files_loc_conf_t  *tlcf;

  282.     tlcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_try_files_loc_conf_t));
  283.     if (tlcf == NULL) {
  284.         return NULL;
  285.     }

  286.     /*
  287.      * set by ngx_pcalloc():
  288.      *
  289.      *     tlcf->try_files = NULL;
  290.      */

  291.     return tlcf;
  292. }


  293. static ngx_int_t
  294. ngx_http_try_files_init(ngx_conf_t *cf)
  295. {
  296.     ngx_http_handler_pt        *h;
  297.     ngx_http_core_main_conf_t  *cmcf;

  298.     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

  299.     h = ngx_array_push(&cmcf->phases[NGX_HTTP_PRECONTENT_PHASE].handlers);
  300.     if (h == NULL) {
  301.         return NGX_ERROR;
  302.     }

  303.     *h = ngx_http_try_files_handler;

  304.     return NGX_OK;
  305. }