Klaus Demo nginx / 2e2b81d
ngx_http_random_index_module Igor Sysoev 13 years ago
4 changed file(s) with 335 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
8080 # ngx_http_dav_module
8181 # ngx_http_autoindex_module
8282 # ngx_http_index_module
83 # ngx_http_random_index_module
8384 #
8485 # ngx_http_access_module
8586 # ngx_http_realip_module
176177
177178 HTTP_MODULES="$HTTP_MODULES $HTTP_INDEX_MODULE"
178179
180 if [ $HTTP_RANDOM_INDEX = YES ]; then
181 have=NGX_HTTP_RANDOM_INDEX . auto/have
182 HTTP_MODULES="$HTTP_MODULES $HTTP_RANDOM_INDEX_MODULE"
183 HTTP_SRCS="$HTTP_SRCS $HTTP_RANDOM_INDEX_SRCS"
184 fi
185
179186 if [ $HTTP_AUTH_BASIC = YES ]; then
180187 have=NGX_HTTP_AUTH_BASIC . auto/have
181188 have=NGX_CRYPT . auto/have
6363 HTTP_AUTH_BASIC=YES
6464 HTTP_USERID=YES
6565 HTTP_AUTOINDEX=YES
66 HTTP_RANDOM_INDEX=NO
6667 HTTP_STATUS=NO
6768 HTTP_GEO=YES
6869 HTTP_MAP=YES
170171 --with-http_dav_module) HTTP_DAV=YES ;;
171172 --with-http_flv_module) HTTP_FLV=YES ;;
172173 --with-http_gzip_static_module) HTTP_GZIP_STATIC=YES ;;
174 --with-http_random_index_module) HTTP_RANDOM_INDEX=YES ;;
173175
174176 --without-http_charset_module) HTTP_CHARSET=NO ;;
175177 --without-http_gzip_module) HTTP_GZIP=NO ;;
286288 --with-http_dav_module enable ngx_http_dav_module
287289 --with-http_flv_module enable ngx_http_flv_module
288290 --with-http_gzip_static_module enable ngx_http_gzip_static_module
291 --with-http_random_index_module enable ngx_http_random_index_module
289292 --with-http_stub_status_module enable ngx_http_stub_status_module
290293
291294 --without-http_charset_module disable ngx_http_charset_module
365365 HTTP_AUTOINDEX_SRCS=src/http/modules/ngx_http_autoindex_module.c
366366
367367
368 HTTP_RANDOM_INDEX_MODULE=ngx_http_random_index_module
369 HTTP_RANDOM_INDEX_SRCS=src/http/modules/ngx_http_random_index_module.c
370
371
368372 HTTP_STATUS_MODULE=ngx_http_status_module
369373 HTTP_STATUS_SRCS=src/http/modules/ngx_http_status_module.c
370374
0
1 /*
2 * Copyright (C) Igor Sysoev
3 */
4
5
6 #include <ngx_config.h>
7 #include <ngx_core.h>
8 #include <ngx_http.h>
9
10
11 typedef struct {
12 ngx_flag_t enable;
13 } ngx_http_random_index_loc_conf_t;
14
15
16 #define NGX_HTTP_RANDOM_INDEX_PREALLOCATE 50
17
18
19 static ngx_int_t ngx_http_random_index_error(ngx_http_request_t *r,
20 ngx_dir_t *dir, ngx_str_t *name);
21 static ngx_int_t ngx_http_random_index_init(ngx_conf_t *cf);
22 static void *ngx_http_random_index_create_loc_conf(ngx_conf_t *cf);
23 static char *ngx_http_random_index_merge_loc_conf(ngx_conf_t *cf,
24 void *parent, void *child);
25
26
27 static ngx_command_t ngx_http_random_index_commands[] = {
28
29 { ngx_string("random_index"),
30 NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
31 ngx_conf_set_flag_slot,
32 NGX_HTTP_LOC_CONF_OFFSET,
33 offsetof(ngx_http_random_index_loc_conf_t, enable),
34 NULL },
35
36 ngx_null_command
37 };
38
39
40 static ngx_http_module_t ngx_http_random_index_module_ctx = {
41 NULL, /* preconfiguration */
42 ngx_http_random_index_init, /* postconfiguration */
43
44 NULL, /* create main configuration */
45 NULL, /* init main configuration */
46
47 NULL, /* create server configuration */
48 NULL, /* merge server configuration */
49
50 ngx_http_random_index_create_loc_conf, /* create location configration */
51 ngx_http_random_index_merge_loc_conf /* merge location configration */
52 };
53
54
55 ngx_module_t ngx_http_random_index_module = {
56 NGX_MODULE_V1,
57 &ngx_http_random_index_module_ctx, /* module context */
58 ngx_http_random_index_commands, /* module directives */
59 NGX_HTTP_MODULE, /* module type */
60 NULL, /* init master */
61 NULL, /* init module */
62 NULL, /* init process */
63 NULL, /* init thread */
64 NULL, /* exit thread */
65 NULL, /* exit process */
66 NULL, /* exit master */
67 NGX_MODULE_V1_PADDING
68 };
69
70
71 static ngx_int_t
72 ngx_http_random_index_handler(ngx_http_request_t *r)
73 {
74 u_char *last, *filename;
75 size_t len, allocated, root;
76 ngx_err_t err;
77 ngx_int_t rc;
78 ngx_str_t path, uri, *name;
79 ngx_dir_t dir;
80 ngx_uint_t n, level;
81 ngx_array_t names;
82 ngx_http_random_index_loc_conf_t *rlcf;
83
84 if (r->uri.data[r->uri.len - 1] != '/') {
85 return NGX_DECLINED;
86 }
87
88 /* TODO: Win32 */
89 if (r->zero_in_uri) {
90 return NGX_DECLINED;
91 }
92
93 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
94 return NGX_DECLINED;
95 }
96
97 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_random_index_module);
98
99 if (!rlcf->enable) {
100 return NGX_DECLINED;
101 }
102
103 #if (NGX_HAVE_D_TYPE)
104 len = NGX_DIR_MASK_LEN;
105 #else
106 len = NGX_HTTP_RANDOM_INDEX_PREALLOCATE;
107 #endif
108
109 last = ngx_http_map_uri_to_path(r, &path, &root, len);
110 if (last == NULL) {
111 return NGX_HTTP_INTERNAL_SERVER_ERROR;
112 }
113
114 allocated = path.len;
115
116 path.len = last - path.data - 1;
117 path.data[path.len] = '\0';
118
119 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
120 "http random index: \"%s\"", path.data);
121
122 if (ngx_open_dir(&path, &dir) == NGX_ERROR) {
123 err = ngx_errno;
124
125 if (err == NGX_ENOENT
126 || err == NGX_ENOTDIR
127 || err == NGX_ENAMETOOLONG)
128 {
129 level = NGX_LOG_ERR;
130 rc = NGX_HTTP_NOT_FOUND;
131
132 } else if (err == NGX_EACCES) {
133 level = NGX_LOG_ERR;
134 rc = NGX_HTTP_FORBIDDEN;
135
136 } else {
137 level = NGX_LOG_CRIT;
138 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
139 }
140
141 ngx_log_error(level, r->connection->log, err,
142 ngx_open_dir_n " \"%s\" failed", path.data);
143
144 return rc;
145 }
146
147 if (ngx_array_init(&names, r->pool, 32, sizeof(ngx_str_t)) != NGX_OK) {
148 return ngx_http_random_index_error(r, &dir, &path);
149 }
150
151 filename = path.data;
152 filename[path.len] = '/';
153
154 for ( ;; ) {
155 ngx_set_errno(0);
156
157 if (ngx_read_dir(&dir) == NGX_ERROR) {
158 err = ngx_errno;
159
160 if (err != NGX_ENOMOREFILES) {
161 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
162 ngx_read_dir_n " \"%V\" failed", &path);
163 return ngx_http_random_index_error(r, &dir, &path);
164 }
165
166 break;
167 }
168
169 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
170 "http random index file: \"%s\"", ngx_de_name(&dir));
171
172 if (ngx_de_name(&dir)[0] == '.') {
173 continue;
174 }
175
176 len = ngx_de_namelen(&dir);
177
178 if (!dir.valid_type) {
179
180 /* 1 byte for '/' and 1 byte for terminating '\0' */
181
182 if (path.len + 1 + len + 1 > allocated) {
183 allocated = path.len + 1 + len + 1
184 + NGX_HTTP_RANDOM_INDEX_PREALLOCATE;
185
186 filename = ngx_pnalloc(r->pool, allocated);
187 if (filename == NULL) {
188 return ngx_http_random_index_error(r, &dir, &path);
189 }
190
191 last = ngx_cpystrn(filename, path.data, path.len + 1);
192 *last++ = '/';
193 }
194
195 ngx_cpystrn(last, ngx_de_name(&dir), len + 1);
196
197 if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) {
198 err = ngx_errno;
199
200 if (err != NGX_ENOENT) {
201 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
202 ngx_de_info_n " \"%s\" failed", filename);
203 return ngx_http_random_index_error(r, &dir, &path);
204 }
205
206 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) {
207 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
208 ngx_de_link_info_n " \"%s\" failed",
209 filename);
210 return ngx_http_random_index_error(r, &dir, &path);
211 }
212 }
213 }
214
215 if (!ngx_de_is_file(&dir)) {
216 continue;
217 }
218
219 name = ngx_array_push(&names);
220 if (name == NULL) {
221 return ngx_http_random_index_error(r, &dir, &path);
222 }
223
224 name->len = len;
225
226 name->data = ngx_pnalloc(r->pool, len);
227 if (name->data == NULL) {
228 return ngx_http_random_index_error(r, &dir, &path);
229 }
230
231 ngx_memcpy(name->data, ngx_de_name(&dir), len);
232 }
233
234 if (ngx_close_dir(&dir) == NGX_ERROR) {
235 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
236 ngx_close_dir_n " \"%s\" failed", &path);
237 }
238
239 n = names.nelts;
240
241 if (n == 0) {
242 return NGX_DECLINED;
243 }
244
245 name = names.elts;
246
247 n = (ngx_uint_t) (((uint64_t) ngx_random() * n) / 0x80000000);
248
249 uri.len = r->uri.len + name[n].len;
250
251 uri.data = ngx_pnalloc(r->pool, uri.len);
252 if (uri.data == NULL) {
253 return NGX_HTTP_INTERNAL_SERVER_ERROR;
254 }
255
256 last = ngx_copy(uri.data, r->uri.data, r->uri.len);
257 ngx_memcpy(last, name[n].data, name[n].len);
258
259 return ngx_http_internal_redirect(r, &uri, &r->args);
260 }
261
262
263 static ngx_int_t
264 ngx_http_random_index_error(ngx_http_request_t *r, ngx_dir_t *dir,
265 ngx_str_t *name)
266 {
267 if (ngx_close_dir(dir) == NGX_ERROR) {
268 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
269 ngx_close_dir_n " \"%V\" failed", name);
270 }
271
272 return NGX_HTTP_INTERNAL_SERVER_ERROR;
273 }
274
275
276 static void *
277 ngx_http_random_index_create_loc_conf(ngx_conf_t *cf)
278 {
279 ngx_http_random_index_loc_conf_t *conf;
280
281 conf = ngx_palloc(cf->pool, sizeof(ngx_http_random_index_loc_conf_t));
282 if (conf == NULL) {
283 return NGX_CONF_ERROR;
284 }
285
286 conf->enable = NGX_CONF_UNSET;
287
288 return conf;
289 }
290
291
292 static char *
293 ngx_http_random_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
294 {
295 ngx_http_random_index_loc_conf_t *prev = parent;
296 ngx_http_random_index_loc_conf_t *conf = child;
297
298 ngx_conf_merge_value(conf->enable, prev->enable, 0);
299
300 return NGX_CONF_OK;
301 }
302
303
304 static ngx_int_t
305 ngx_http_random_index_init(ngx_conf_t *cf)
306 {
307 ngx_http_handler_pt *h;
308 ngx_http_core_main_conf_t *cmcf;
309
310 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
311
312 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
313 if (h == NULL) {
314 return NGX_ERROR;
315 }
316
317 *h = ngx_http_random_index_handler;
318
319 return NGX_OK;
320 }