src/http/modules/ngx_http_random_index_module.c - nginx-1.7.10

Global variables defined

Data types defined

Functions defined

Macros 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_flag_t  enable;
  10. } ngx_http_random_index_loc_conf_t;


  11. #define NGX_HTTP_RANDOM_INDEX_PREALLOCATE  50


  12. static ngx_int_t ngx_http_random_index_error(ngx_http_request_t *r,
  13.     ngx_dir_t *dir, ngx_str_t *name);
  14. static ngx_int_t ngx_http_random_index_init(ngx_conf_t *cf);
  15. static void *ngx_http_random_index_create_loc_conf(ngx_conf_t *cf);
  16. static char *ngx_http_random_index_merge_loc_conf(ngx_conf_t *cf,
  17.     void *parent, void *child);


  18. static ngx_command_t  ngx_http_random_index_commands[] = {

  19.     { ngx_string("random_index"),
  20.       NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  21.       ngx_conf_set_flag_slot,
  22.       NGX_HTTP_LOC_CONF_OFFSET,
  23.       offsetof(ngx_http_random_index_loc_conf_t, enable),
  24.       NULL },

  25.       ngx_null_command
  26. };


  27. static ngx_http_module_t  ngx_http_random_index_module_ctx = {
  28.     NULL,                                  /* preconfiguration */
  29.     ngx_http_random_index_init,            /* postconfiguration */

  30.     NULL,                                  /* create main configuration */
  31.     NULL,                                  /* init main configuration */

  32.     NULL,                                  /* create server configuration */
  33.     NULL,                                  /* merge server configuration */

  34.     ngx_http_random_index_create_loc_conf, /* create location configuration */
  35.     ngx_http_random_index_merge_loc_conf   /* merge location configuration */
  36. };


  37. ngx_module_t  ngx_http_random_index_module = {
  38.     NGX_MODULE_V1,
  39.     &ngx_http_random_index_module_ctx,     /* module context */
  40.     ngx_http_random_index_commands,        /* module directives */
  41.     NGX_HTTP_MODULE,                       /* module type */
  42.     NULL,                                  /* init master */
  43.     NULL,                                  /* init module */
  44.     NULL,                                  /* init process */
  45.     NULL,                                  /* init thread */
  46.     NULL,                                  /* exit thread */
  47.     NULL,                                  /* exit process */
  48.     NULL,                                  /* exit master */
  49.     NGX_MODULE_V1_PADDING
  50. };


  51. static ngx_int_t
  52. ngx_http_random_index_handler(ngx_http_request_t *r)
  53. {
  54.     u_char                            *last, *filename;
  55.     size_t                             len, allocated, root;
  56.     ngx_err_t                          err;
  57.     ngx_int_t                          rc;
  58.     ngx_str_t                          path, uri, *name;
  59.     ngx_dir_t                          dir;
  60.     ngx_uint_t                         n, level;
  61.     ngx_array_t                        names;
  62.     ngx_http_random_index_loc_conf_t  *rlcf;

  63.     if (r->uri.data[r->uri.len - 1] != '/') {
  64.         return NGX_DECLINED;
  65.     }

  66.     if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
  67.         return NGX_DECLINED;
  68.     }

  69.     rlcf = ngx_http_get_module_loc_conf(r, ngx_http_random_index_module);

  70.     if (!rlcf->enable) {
  71.         return NGX_DECLINED;
  72.     }

  73. #if (NGX_HAVE_D_TYPE)
  74.     len = NGX_DIR_MASK_LEN;
  75. #else
  76.     len = NGX_HTTP_RANDOM_INDEX_PREALLOCATE;
  77. #endif

  78.     last = ngx_http_map_uri_to_path(r, &path, &root, len);
  79.     if (last == NULL) {
  80.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  81.     }

  82.     allocated = path.len;

  83.     path.len = last - path.data - 1;
  84.     path.data[path.len] = '\0';

  85.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  86.                    "http random index: \"%s\"", path.data);

  87.     if (ngx_open_dir(&path, &dir) == NGX_ERROR) {
  88.         err = ngx_errno;

  89.         if (err == NGX_ENOENT
  90.             || err == NGX_ENOTDIR
  91.             || err == NGX_ENAMETOOLONG)
  92.         {
  93.             level = NGX_LOG_ERR;
  94.             rc = NGX_HTTP_NOT_FOUND;

  95.         } else if (err == NGX_EACCES) {
  96.             level = NGX_LOG_ERR;
  97.             rc = NGX_HTTP_FORBIDDEN;

  98.         } else {
  99.             level = NGX_LOG_CRIT;
  100.             rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
  101.         }

  102.         ngx_log_error(level, r->connection->log, err,
  103.                       ngx_open_dir_n " \"%s\" failed", path.data);

  104.         return rc;
  105.     }

  106.     if (ngx_array_init(&names, r->pool, 32, sizeof(ngx_str_t)) != NGX_OK) {
  107.         return ngx_http_random_index_error(r, &dir, &path);
  108.     }

  109.     filename = path.data;
  110.     filename[path.len] = '/';

  111.     for ( ;; ) {
  112.         ngx_set_errno(0);

  113.         if (ngx_read_dir(&dir) == NGX_ERROR) {
  114.             err = ngx_errno;

  115.             if (err != NGX_ENOMOREFILES) {
  116.                 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
  117.                               ngx_read_dir_n " \"%V\" failed", &path);
  118.                 return ngx_http_random_index_error(r, &dir, &path);
  119.             }

  120.             break;
  121.         }

  122.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  123.                        "http random index file: \"%s\"", ngx_de_name(&dir));

  124.         if (ngx_de_name(&dir)[0] == '.') {
  125.             continue;
  126.         }

  127.         len = ngx_de_namelen(&dir);

  128.         if (dir.type == 0 || ngx_de_is_link(&dir)) {

  129.             /* 1 byte for '/' and 1 byte for terminating '\0' */

  130.             if (path.len + 1 + len + 1 > allocated) {
  131.                 allocated = path.len + 1 + len + 1
  132.                                      + NGX_HTTP_RANDOM_INDEX_PREALLOCATE;

  133.                 filename = ngx_pnalloc(r->pool, allocated);
  134.                 if (filename == NULL) {
  135.                     return ngx_http_random_index_error(r, &dir, &path);
  136.                 }

  137.                 last = ngx_cpystrn(filename, path.data, path.len + 1);
  138.                 *last++ = '/';
  139.             }

  140.             ngx_cpystrn(last, ngx_de_name(&dir), len + 1);

  141.             if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) {
  142.                 err = ngx_errno;

  143.                 if (err != NGX_ENOENT) {
  144.                     ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
  145.                                   ngx_de_info_n " \"%s\" failed", filename);
  146.                     return ngx_http_random_index_error(r, &dir, &path);
  147.                 }

  148.                 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) {
  149.                     ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
  150.                                   ngx_de_link_info_n " \"%s\" failed",
  151.                                   filename);
  152.                     return ngx_http_random_index_error(r, &dir, &path);
  153.                 }
  154.             }
  155.         }

  156.         if (!ngx_de_is_file(&dir)) {
  157.             continue;
  158.         }

  159.         name = ngx_array_push(&names);
  160.         if (name == NULL) {
  161.             return ngx_http_random_index_error(r, &dir, &path);
  162.         }

  163.         name->len = len;

  164.         name->data = ngx_pnalloc(r->pool, len);
  165.         if (name->data == NULL) {
  166.             return ngx_http_random_index_error(r, &dir, &path);
  167.         }

  168.         ngx_memcpy(name->data, ngx_de_name(&dir), len);
  169.     }

  170.     if (ngx_close_dir(&dir) == NGX_ERROR) {
  171.         ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
  172.                       ngx_close_dir_n " \"%s\" failed", &path);
  173.     }

  174.     n = names.nelts;

  175.     if (n == 0) {
  176.         return NGX_DECLINED;
  177.     }

  178.     name = names.elts;

  179.     n = (ngx_uint_t) (((uint64_t) ngx_random() * n) / 0x80000000);

  180.     uri.len = r->uri.len + name[n].len;

  181.     uri.data = ngx_pnalloc(r->pool, uri.len);
  182.     if (uri.data == NULL) {
  183.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  184.     }

  185.     last = ngx_copy(uri.data, r->uri.data, r->uri.len);
  186.     ngx_memcpy(last, name[n].data, name[n].len);

  187.     return ngx_http_internal_redirect(r, &uri, &r->args);
  188. }


  189. static ngx_int_t
  190. ngx_http_random_index_error(ngx_http_request_t *r, ngx_dir_t *dir,
  191.     ngx_str_t *name)
  192. {
  193.     if (ngx_close_dir(dir) == NGX_ERROR) {
  194.         ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
  195.                       ngx_close_dir_n " \"%V\" failed", name);
  196.     }

  197.     return NGX_HTTP_INTERNAL_SERVER_ERROR;
  198. }


  199. static void *
  200. ngx_http_random_index_create_loc_conf(ngx_conf_t *cf)
  201. {
  202.     ngx_http_random_index_loc_conf_t  *conf;

  203.     conf = ngx_palloc(cf->pool, sizeof(ngx_http_random_index_loc_conf_t));
  204.     if (conf == NULL) {
  205.         return NULL;
  206.     }

  207.     conf->enable = NGX_CONF_UNSET;

  208.     return conf;
  209. }


  210. static char *
  211. ngx_http_random_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
  212. {
  213.     ngx_http_random_index_loc_conf_t *prev = parent;
  214.     ngx_http_random_index_loc_conf_t *conf = child;

  215.     ngx_conf_merge_value(conf->enable, prev->enable, 0);

  216.     return NGX_CONF_OK;
  217. }


  218. static ngx_int_t
  219. ngx_http_random_index_init(ngx_conf_t *cf)
  220. {
  221.     ngx_http_handler_pt        *h;
  222.     ngx_http_core_main_conf_t  *cmcf;

  223.     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

  224.     h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
  225.     if (h == NULL) {
  226.         return NGX_ERROR;
  227.     }

  228.     *h = ngx_http_random_index_handler;

  229.     return NGX_OK;
  230. }