2235
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 typedef struct {
|
|
13 ngx_flag_t enable;
|
|
14 } ngx_http_random_index_loc_conf_t;
|
|
15
|
|
16
|
|
17 #define NGX_HTTP_RANDOM_INDEX_PREALLOCATE 50
|
|
18
|
|
19
|
|
20 static ngx_int_t ngx_http_random_index_error(ngx_http_request_t *r,
|
|
21 ngx_dir_t *dir, ngx_str_t *name);
|
|
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);
|
2258
|
25 static ngx_int_t ngx_http_random_index_add_variable(ngx_conf_t *cf);
|
|
26 static ngx_int_t ngx_http_random_index_init(ngx_conf_t *cf);
|
2235
|
27
|
|
28
|
|
29 static ngx_command_t ngx_http_random_index_commands[] = {
|
|
30
|
|
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),
|
|
36 NULL },
|
|
37
|
|
38 ngx_null_command
|
|
39 };
|
|
40
|
|
41
|
|
42 static ngx_http_module_t ngx_http_random_index_module_ctx = {
|
2258
|
43 ngx_http_random_index_add_variable, /* preconfiguration */
|
2235
|
44 ngx_http_random_index_init, /* postconfiguration */
|
|
45
|
|
46 NULL, /* create main configuration */
|
|
47 NULL, /* init main configuration */
|
|
48
|
|
49 NULL, /* create server configuration */
|
|
50 NULL, /* merge server configuration */
|
|
51
|
|
52 ngx_http_random_index_create_loc_conf, /* create location configration */
|
|
53 ngx_http_random_index_merge_loc_conf /* merge location configration */
|
|
54 };
|
|
55
|
|
56
|
|
57 ngx_module_t ngx_http_random_index_module = {
|
|
58 NGX_MODULE_V1,
|
|
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 */
|
|
69 NGX_MODULE_V1_PADDING
|
|
70 };
|
|
71
|
|
72
|
2258
|
73 static ngx_str_t ngx_http_random_index = ngx_string("random_index");
|
|
74 static ngx_int_t ngx_random_index_variable_index;
|
|
75
|
|
76
|
2235
|
77 static ngx_int_t
|
|
78 ngx_http_random_index_handler(ngx_http_request_t *r)
|
|
79 {
|
|
80 u_char *last, *filename;
|
|
81 size_t len, allocated, root;
|
|
82 ngx_err_t err;
|
|
83 ngx_int_t rc;
|
|
84 ngx_str_t path, uri, *name;
|
|
85 ngx_dir_t dir;
|
|
86 ngx_uint_t n, level;
|
|
87 ngx_array_t names;
|
2258
|
88 ngx_http_variable_value_t *v;
|
2235
|
89 ngx_http_random_index_loc_conf_t *rlcf;
|
|
90
|
|
91 if (r->uri.data[r->uri.len - 1] != '/') {
|
|
92 return NGX_DECLINED;
|
|
93 }
|
|
94
|
|
95 /* TODO: Win32 */
|
|
96 if (r->zero_in_uri) {
|
|
97 return NGX_DECLINED;
|
|
98 }
|
|
99
|
|
100 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
|
|
101 return NGX_DECLINED;
|
|
102 }
|
|
103
|
|
104 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_random_index_module);
|
|
105
|
|
106 if (!rlcf->enable) {
|
|
107 return NGX_DECLINED;
|
|
108 }
|
|
109
|
|
110 #if (NGX_HAVE_D_TYPE)
|
|
111 len = NGX_DIR_MASK_LEN;
|
|
112 #else
|
|
113 len = NGX_HTTP_RANDOM_INDEX_PREALLOCATE;
|
|
114 #endif
|
|
115
|
|
116 last = ngx_http_map_uri_to_path(r, &path, &root, len);
|
|
117 if (last == NULL) {
|
|
118 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
119 }
|
|
120
|
|
121 allocated = path.len;
|
|
122
|
|
123 path.len = last - path.data - 1;
|
|
124 path.data[path.len] = '\0';
|
|
125
|
|
126 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
127 "http random index: \"%s\"", path.data);
|
|
128
|
|
129 if (ngx_open_dir(&path, &dir) == NGX_ERROR) {
|
|
130 err = ngx_errno;
|
|
131
|
|
132 if (err == NGX_ENOENT
|
|
133 || err == NGX_ENOTDIR
|
|
134 || err == NGX_ENAMETOOLONG)
|
|
135 {
|
|
136 level = NGX_LOG_ERR;
|
|
137 rc = NGX_HTTP_NOT_FOUND;
|
|
138
|
|
139 } else if (err == NGX_EACCES) {
|
|
140 level = NGX_LOG_ERR;
|
|
141 rc = NGX_HTTP_FORBIDDEN;
|
|
142
|
|
143 } else {
|
|
144 level = NGX_LOG_CRIT;
|
|
145 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
146 }
|
|
147
|
|
148 ngx_log_error(level, r->connection->log, err,
|
|
149 ngx_open_dir_n " \"%s\" failed", path.data);
|
|
150
|
|
151 return rc;
|
|
152 }
|
|
153
|
|
154 if (ngx_array_init(&names, r->pool, 32, sizeof(ngx_str_t)) != NGX_OK) {
|
|
155 return ngx_http_random_index_error(r, &dir, &path);
|
|
156 }
|
|
157
|
|
158 filename = path.data;
|
|
159 filename[path.len] = '/';
|
|
160
|
|
161 for ( ;; ) {
|
|
162 ngx_set_errno(0);
|
|
163
|
|
164 if (ngx_read_dir(&dir) == NGX_ERROR) {
|
|
165 err = ngx_errno;
|
|
166
|
|
167 if (err != NGX_ENOMOREFILES) {
|
|
168 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
|
|
169 ngx_read_dir_n " \"%V\" failed", &path);
|
|
170 return ngx_http_random_index_error(r, &dir, &path);
|
|
171 }
|
|
172
|
|
173 break;
|
|
174 }
|
|
175
|
|
176 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
177 "http random index file: \"%s\"", ngx_de_name(&dir));
|
|
178
|
|
179 if (ngx_de_name(&dir)[0] == '.') {
|
|
180 continue;
|
|
181 }
|
|
182
|
|
183 len = ngx_de_namelen(&dir);
|
|
184
|
|
185 if (!dir.valid_type) {
|
|
186
|
|
187 /* 1 byte for '/' and 1 byte for terminating '\0' */
|
|
188
|
|
189 if (path.len + 1 + len + 1 > allocated) {
|
|
190 allocated = path.len + 1 + len + 1
|
|
191 + NGX_HTTP_RANDOM_INDEX_PREALLOCATE;
|
|
192
|
|
193 filename = ngx_pnalloc(r->pool, allocated);
|
|
194 if (filename == NULL) {
|
|
195 return ngx_http_random_index_error(r, &dir, &path);
|
|
196 }
|
|
197
|
|
198 last = ngx_cpystrn(filename, path.data, path.len + 1);
|
|
199 *last++ = '/';
|
|
200 }
|
|
201
|
|
202 ngx_cpystrn(last, ngx_de_name(&dir), len + 1);
|
|
203
|
|
204 if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) {
|
|
205 err = ngx_errno;
|
|
206
|
|
207 if (err != NGX_ENOENT) {
|
|
208 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
|
|
209 ngx_de_info_n " \"%s\" failed", filename);
|
|
210 return ngx_http_random_index_error(r, &dir, &path);
|
|
211 }
|
|
212
|
|
213 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) {
|
|
214 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
215 ngx_de_link_info_n " \"%s\" failed",
|
|
216 filename);
|
|
217 return ngx_http_random_index_error(r, &dir, &path);
|
|
218 }
|
|
219 }
|
|
220 }
|
|
221
|
|
222 if (!ngx_de_is_file(&dir)) {
|
|
223 continue;
|
|
224 }
|
|
225
|
|
226 name = ngx_array_push(&names);
|
|
227 if (name == NULL) {
|
|
228 return ngx_http_random_index_error(r, &dir, &path);
|
|
229 }
|
|
230
|
|
231 name->len = len;
|
|
232
|
|
233 name->data = ngx_pnalloc(r->pool, len);
|
|
234 if (name->data == NULL) {
|
|
235 return ngx_http_random_index_error(r, &dir, &path);
|
|
236 }
|
|
237
|
|
238 ngx_memcpy(name->data, ngx_de_name(&dir), len);
|
|
239 }
|
|
240
|
|
241 if (ngx_close_dir(&dir) == NGX_ERROR) {
|
|
242 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
|
|
243 ngx_close_dir_n " \"%s\" failed", &path);
|
|
244 }
|
|
245
|
|
246 n = names.nelts;
|
|
247
|
|
248 if (n == 0) {
|
|
249 return NGX_DECLINED;
|
|
250 }
|
|
251
|
|
252 name = names.elts;
|
|
253
|
|
254 n = (ngx_uint_t) (((uint64_t) ngx_random() * n) / 0x80000000);
|
|
255
|
|
256 uri.len = r->uri.len + name[n].len;
|
|
257
|
|
258 uri.data = ngx_pnalloc(r->pool, uri.len);
|
|
259 if (uri.data == NULL) {
|
|
260 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
261 }
|
|
262
|
|
263 last = ngx_copy(uri.data, r->uri.data, r->uri.len);
|
|
264 ngx_memcpy(last, name[n].data, name[n].len);
|
|
265
|
2258
|
266 v = &r->variables[ngx_random_index_variable_index];
|
|
267
|
|
268 v->len = name[n].len;
|
|
269 v->valid = 1;
|
|
270 v->no_cacheable = 0;
|
|
271 v->not_found = 0;
|
|
272 v->data = name[n].data;
|
|
273
|
2235
|
274 return ngx_http_internal_redirect(r, &uri, &r->args);
|
|
275 }
|
|
276
|
|
277
|
|
278 static ngx_int_t
|
|
279 ngx_http_random_index_error(ngx_http_request_t *r, ngx_dir_t *dir,
|
|
280 ngx_str_t *name)
|
|
281 {
|
|
282 if (ngx_close_dir(dir) == NGX_ERROR) {
|
|
283 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
|
|
284 ngx_close_dir_n " \"%V\" failed", name);
|
|
285 }
|
|
286
|
|
287 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
288 }
|
|
289
|
|
290
|
2258
|
291 static ngx_int_t
|
|
292 ngx_http_random_index_variable(ngx_http_request_t *r,
|
|
293 ngx_http_variable_value_t *v, uintptr_t data)
|
|
294 {
|
|
295 /*
|
|
296 * the "random_index" directive stores index file name directly inside
|
|
297 * r->variables[] because request context is not preserved while
|
|
298 * an internal redirection
|
|
299 */
|
|
300
|
|
301 v->not_found = 1;
|
|
302
|
|
303 return NGX_OK;
|
|
304 }
|
|
305
|
|
306
|
2235
|
307 static void *
|
|
308 ngx_http_random_index_create_loc_conf(ngx_conf_t *cf)
|
|
309 {
|
|
310 ngx_http_random_index_loc_conf_t *conf;
|
|
311
|
|
312 conf = ngx_palloc(cf->pool, sizeof(ngx_http_random_index_loc_conf_t));
|
|
313 if (conf == NULL) {
|
|
314 return NGX_CONF_ERROR;
|
|
315 }
|
|
316
|
|
317 conf->enable = NGX_CONF_UNSET;
|
|
318
|
|
319 return conf;
|
|
320 }
|
|
321
|
|
322
|
|
323 static char *
|
|
324 ngx_http_random_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
325 {
|
|
326 ngx_http_random_index_loc_conf_t *prev = parent;
|
|
327 ngx_http_random_index_loc_conf_t *conf = child;
|
|
328
|
|
329 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
330
|
|
331 return NGX_CONF_OK;
|
|
332 }
|
|
333
|
|
334
|
|
335 static ngx_int_t
|
2258
|
336 ngx_http_random_index_add_variable(ngx_conf_t *cf)
|
|
337 {
|
|
338 ngx_int_t index;
|
|
339 ngx_http_variable_t *var;
|
|
340
|
|
341 var = ngx_http_add_variable(cf, &ngx_http_random_index,
|
|
342 NGX_HTTP_VAR_NOHASH);
|
|
343 if (var == NULL) {
|
|
344 return NGX_ERROR;
|
|
345 }
|
|
346
|
|
347 index = ngx_http_get_variable_index(cf, &ngx_http_random_index);
|
|
348 if (index == NGX_ERROR) {
|
|
349 return NGX_ERROR;
|
|
350 }
|
|
351
|
|
352 ngx_random_index_variable_index = index;
|
|
353
|
|
354 var->get_handler = ngx_http_random_index_variable;
|
|
355
|
|
356 return NGX_OK;
|
|
357 }
|
|
358
|
|
359
|
|
360 static ngx_int_t
|
2235
|
361 ngx_http_random_index_init(ngx_conf_t *cf)
|
|
362 {
|
|
363 ngx_http_handler_pt *h;
|
|
364 ngx_http_core_main_conf_t *cmcf;
|
|
365
|
|
366 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
367
|
|
368 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
369 if (h == NULL) {
|
|
370 return NGX_ERROR;
|
|
371 }
|
|
372
|
|
373 *h = ngx_http_random_index_handler;
|
|
374
|
|
375 return NGX_OK;
|
|
376 }
|