3
* Copyright (C) Igor Sysoev
4
* Copyright (C) Nginx, Inc.
8
#include <ngx_config.h>
15
} ngx_http_random_index_loc_conf_t;
18
#define NGX_HTTP_RANDOM_INDEX_PREALLOCATE 50
21
static ngx_int_t ngx_http_random_index_error(ngx_http_request_t *r,
22
ngx_dir_t *dir, ngx_str_t *name);
23
static ngx_int_t ngx_http_random_index_init(ngx_conf_t *cf);
24
static void *ngx_http_random_index_create_loc_conf(ngx_conf_t *cf);
25
static char *ngx_http_random_index_merge_loc_conf(ngx_conf_t *cf,
26
void *parent, void *child);
29
static ngx_command_t ngx_http_random_index_commands[] = {
31
{ ngx_string("random_index"),
32
NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
33
ngx_conf_set_flag_slot,
34
NGX_HTTP_LOC_CONF_OFFSET,
35
offsetof(ngx_http_random_index_loc_conf_t, enable),
42
static ngx_http_module_t ngx_http_random_index_module_ctx = {
43
NULL, /* preconfiguration */
44
ngx_http_random_index_init, /* postconfiguration */
46
NULL, /* create main configuration */
47
NULL, /* init main configuration */
49
NULL, /* create server configuration */
50
NULL, /* merge server configuration */
52
ngx_http_random_index_create_loc_conf, /* create location configuration */
53
ngx_http_random_index_merge_loc_conf /* merge location configuration */
57
ngx_module_t ngx_http_random_index_module = {
59
&ngx_http_random_index_module_ctx, /* module context */
60
ngx_http_random_index_commands, /* module directives */
61
NGX_HTTP_MODULE, /* module type */
62
NULL, /* init master */
63
NULL, /* init module */
64
NULL, /* init process */
65
NULL, /* init thread */
66
NULL, /* exit thread */
67
NULL, /* exit process */
68
NULL, /* exit master */
74
ngx_http_random_index_handler(ngx_http_request_t *r)
76
u_char *last, *filename;
77
size_t len, allocated, root;
80
ngx_str_t path, uri, *name;
84
ngx_http_random_index_loc_conf_t *rlcf;
86
if (r->uri.data[r->uri.len - 1] != '/') {
90
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
94
rlcf = ngx_http_get_module_loc_conf(r, ngx_http_random_index_module);
100
#if (NGX_HAVE_D_TYPE)
101
len = NGX_DIR_MASK_LEN;
103
len = NGX_HTTP_RANDOM_INDEX_PREALLOCATE;
106
last = ngx_http_map_uri_to_path(r, &path, &root, len);
108
return NGX_HTTP_INTERNAL_SERVER_ERROR;
111
allocated = path.len;
113
path.len = last - path.data - 1;
114
path.data[path.len] = '\0';
116
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
117
"http random index: \"%s\"", path.data);
119
if (ngx_open_dir(&path, &dir) == NGX_ERROR) {
122
if (err == NGX_ENOENT
123
|| err == NGX_ENOTDIR
124
|| err == NGX_ENAMETOOLONG)
127
rc = NGX_HTTP_NOT_FOUND;
129
} else if (err == NGX_EACCES) {
131
rc = NGX_HTTP_FORBIDDEN;
134
level = NGX_LOG_CRIT;
135
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
138
ngx_log_error(level, r->connection->log, err,
139
ngx_open_dir_n " \"%s\" failed", path.data);
144
if (ngx_array_init(&names, r->pool, 32, sizeof(ngx_str_t)) != NGX_OK) {
145
return ngx_http_random_index_error(r, &dir, &path);
148
filename = path.data;
149
filename[path.len] = '/';
154
if (ngx_read_dir(&dir) == NGX_ERROR) {
157
if (err != NGX_ENOMOREFILES) {
158
ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
159
ngx_read_dir_n " \"%V\" failed", &path);
160
return ngx_http_random_index_error(r, &dir, &path);
166
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
167
"http random index file: \"%s\"", ngx_de_name(&dir));
169
if (ngx_de_name(&dir)[0] == '.') {
173
len = ngx_de_namelen(&dir);
175
if (dir.type == 0 || ngx_de_is_link(&dir)) {
177
/* 1 byte for '/' and 1 byte for terminating '\0' */
179
if (path.len + 1 + len + 1 > allocated) {
180
allocated = path.len + 1 + len + 1
181
+ NGX_HTTP_RANDOM_INDEX_PREALLOCATE;
183
filename = ngx_pnalloc(r->pool, allocated);
184
if (filename == NULL) {
185
return ngx_http_random_index_error(r, &dir, &path);
188
last = ngx_cpystrn(filename, path.data, path.len + 1);
192
ngx_cpystrn(last, ngx_de_name(&dir), len + 1);
194
if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) {
197
if (err != NGX_ENOENT) {
198
ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
199
ngx_de_info_n " \"%s\" failed", filename);
200
return ngx_http_random_index_error(r, &dir, &path);
203
if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) {
204
ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
205
ngx_de_link_info_n " \"%s\" failed",
207
return ngx_http_random_index_error(r, &dir, &path);
212
if (!ngx_de_is_file(&dir)) {
216
name = ngx_array_push(&names);
218
return ngx_http_random_index_error(r, &dir, &path);
223
name->data = ngx_pnalloc(r->pool, len);
224
if (name->data == NULL) {
225
return ngx_http_random_index_error(r, &dir, &path);
228
ngx_memcpy(name->data, ngx_de_name(&dir), len);
231
if (ngx_close_dir(&dir) == NGX_ERROR) {
232
ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
233
ngx_close_dir_n " \"%s\" failed", &path);
244
n = (ngx_uint_t) (((uint64_t) ngx_random() * n) / 0x80000000);
246
uri.len = r->uri.len + name[n].len;
248
uri.data = ngx_pnalloc(r->pool, uri.len);
249
if (uri.data == NULL) {
250
return NGX_HTTP_INTERNAL_SERVER_ERROR;
253
last = ngx_copy(uri.data, r->uri.data, r->uri.len);
254
ngx_memcpy(last, name[n].data, name[n].len);
256
return ngx_http_internal_redirect(r, &uri, &r->args);
261
ngx_http_random_index_error(ngx_http_request_t *r, ngx_dir_t *dir,
264
if (ngx_close_dir(dir) == NGX_ERROR) {
265
ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
266
ngx_close_dir_n " \"%V\" failed", name);
269
return NGX_HTTP_INTERNAL_SERVER_ERROR;
274
ngx_http_random_index_create_loc_conf(ngx_conf_t *cf)
276
ngx_http_random_index_loc_conf_t *conf;
278
conf = ngx_palloc(cf->pool, sizeof(ngx_http_random_index_loc_conf_t));
283
conf->enable = NGX_CONF_UNSET;
290
ngx_http_random_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
292
ngx_http_random_index_loc_conf_t *prev = parent;
293
ngx_http_random_index_loc_conf_t *conf = child;
295
ngx_conf_merge_value(conf->enable, prev->enable, 0);
302
ngx_http_random_index_init(ngx_conf_t *cf)
304
ngx_http_handler_pt *h;
305
ngx_http_core_main_conf_t *cmcf;
307
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
309
h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
314
*h = ngx_http_random_index_handler;