upstream nginx-0.7.35
[nginx.git] / nginx / src / http / modules / ngx_http_random_index_module.c
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 ngx_int_t ngx_http_random_index_init(ngx_conf_t *cf);
23 static void *ngx_http_random_index_create_loc_conf(ngx_conf_t *cf);
24 static char *ngx_http_random_index_merge_loc_conf(ngx_conf_t *cf,
25     void *parent, void *child);
26
27
28 static ngx_command_t  ngx_http_random_index_commands[] = {
29
30     { ngx_string("random_index"),
31       NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
32       ngx_conf_set_flag_slot,
33       NGX_HTTP_LOC_CONF_OFFSET,
34       offsetof(ngx_http_random_index_loc_conf_t, enable),
35       NULL },
36
37       ngx_null_command
38 };
39
40
41 static ngx_http_module_t  ngx_http_random_index_module_ctx = {
42     NULL,                                  /* preconfiguration */
43     ngx_http_random_index_init,            /* postconfiguration */
44
45     NULL,                                  /* create main configuration */
46     NULL,                                  /* init main configuration */
47
48     NULL,                                  /* create server configuration */
49     NULL,                                  /* merge server configuration */
50
51     ngx_http_random_index_create_loc_conf, /* create location configration */
52     ngx_http_random_index_merge_loc_conf   /* merge location configration */
53 };
54
55
56 ngx_module_t  ngx_http_random_index_module = {
57     NGX_MODULE_V1,
58     &ngx_http_random_index_module_ctx,     /* module context */
59     ngx_http_random_index_commands,        /* module directives */
60     NGX_HTTP_MODULE,                       /* module type */
61     NULL,                                  /* init master */
62     NULL,                                  /* init module */
63     NULL,                                  /* init process */
64     NULL,                                  /* init thread */
65     NULL,                                  /* exit thread */
66     NULL,                                  /* exit process */
67     NULL,                                  /* exit master */
68     NGX_MODULE_V1_PADDING
69 };
70
71
72 static ngx_int_t
73 ngx_http_random_index_handler(ngx_http_request_t *r)
74 {
75     u_char                            *last, *filename;
76     size_t                             len, allocated, root;
77     ngx_err_t                          err;
78     ngx_int_t                          rc;
79     ngx_str_t                          path, uri, *name;
80     ngx_dir_t                          dir;
81     ngx_uint_t                         n, level;
82     ngx_array_t                        names;
83     ngx_http_random_index_loc_conf_t  *rlcf;
84
85     if (r->uri.data[r->uri.len - 1] != '/') {
86         return NGX_DECLINED;
87     }
88
89     /* TODO: Win32 */
90     if (r->zero_in_uri) {
91         return NGX_DECLINED;
92     }
93
94     if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
95         return NGX_DECLINED;
96     }
97
98     rlcf = ngx_http_get_module_loc_conf(r, ngx_http_random_index_module);
99
100     if (!rlcf->enable) {
101         return NGX_DECLINED;
102     }
103
104 #if (NGX_HAVE_D_TYPE)
105     len = NGX_DIR_MASK_LEN;
106 #else
107     len = NGX_HTTP_RANDOM_INDEX_PREALLOCATE;
108 #endif
109
110     last = ngx_http_map_uri_to_path(r, &path, &root, len);
111     if (last == NULL) {
112         return NGX_HTTP_INTERNAL_SERVER_ERROR;
113     }
114
115     allocated = path.len;
116
117     path.len = last - path.data - 1;
118     path.data[path.len] = '\0';
119
120     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
121                    "http random index: \"%s\"", path.data);
122
123     if (ngx_open_dir(&path, &dir) == NGX_ERROR) {
124         err = ngx_errno;
125
126         if (err == NGX_ENOENT
127             || err == NGX_ENOTDIR
128             || err == NGX_ENAMETOOLONG)
129         {
130             level = NGX_LOG_ERR;
131             rc = NGX_HTTP_NOT_FOUND;
132
133         } else if (err == NGX_EACCES) {
134             level = NGX_LOG_ERR;
135             rc = NGX_HTTP_FORBIDDEN;
136
137         } else {
138             level = NGX_LOG_CRIT;
139             rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
140         }
141
142         ngx_log_error(level, r->connection->log, err,
143                       ngx_open_dir_n " \"%s\" failed", path.data);
144
145         return rc;
146     }
147
148     if (ngx_array_init(&names, r->pool, 32, sizeof(ngx_str_t)) != NGX_OK) {
149         return ngx_http_random_index_error(r, &dir, &path);
150     }
151
152     filename = path.data;
153     filename[path.len] = '/';
154
155     for ( ;; ) {
156         ngx_set_errno(0);
157
158         if (ngx_read_dir(&dir) == NGX_ERROR) {
159             err = ngx_errno;
160
161             if (err != NGX_ENOMOREFILES) {
162                 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
163                               ngx_read_dir_n " \"%V\" failed", &path);
164                 return ngx_http_random_index_error(r, &dir, &path);
165             }
166
167             break;
168         }
169
170         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
171                        "http random index file: \"%s\"", ngx_de_name(&dir));
172
173         if (ngx_de_name(&dir)[0] == '.') {
174             continue;
175         }
176
177         len = ngx_de_namelen(&dir);
178
179         if (!dir.valid_type) {
180
181             /* 1 byte for '/' and 1 byte for terminating '\0' */
182
183             if (path.len + 1 + len + 1 > allocated) {
184                 allocated = path.len + 1 + len + 1
185                                      + NGX_HTTP_RANDOM_INDEX_PREALLOCATE;
186
187                 filename = ngx_pnalloc(r->pool, allocated);
188                 if (filename == NULL) {
189                     return ngx_http_random_index_error(r, &dir, &path);
190                 }
191
192                 last = ngx_cpystrn(filename, path.data, path.len + 1);
193                 *last++ = '/';
194             }
195
196             ngx_cpystrn(last, ngx_de_name(&dir), len + 1);
197
198             if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) {
199                 err = ngx_errno;
200
201                 if (err != NGX_ENOENT) {
202                     ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
203                                   ngx_de_info_n " \"%s\" failed", filename);
204                     return ngx_http_random_index_error(r, &dir, &path);
205                 }
206
207                 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) {
208                     ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
209                                   ngx_de_link_info_n " \"%s\" failed",
210                                   filename);
211                     return ngx_http_random_index_error(r, &dir, &path);
212                 }
213             }
214         }
215
216         if (!ngx_de_is_file(&dir)) {
217             continue;
218         }
219
220         name = ngx_array_push(&names);
221         if (name == NULL) {
222             return ngx_http_random_index_error(r, &dir, &path);
223         }
224
225         name->len = len;
226
227         name->data = ngx_pnalloc(r->pool, len);
228         if (name->data == NULL) {
229             return ngx_http_random_index_error(r, &dir, &path);
230         }
231
232         ngx_memcpy(name->data, ngx_de_name(&dir), len);
233     }
234
235     if (ngx_close_dir(&dir) == NGX_ERROR) {
236         ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
237                       ngx_close_dir_n " \"%s\" failed", &path);
238     }
239
240     n = names.nelts;
241
242     if (n == 0) {
243         return NGX_DECLINED;
244     }
245
246     name = names.elts;
247
248     n = (ngx_uint_t) (((uint64_t) ngx_random() * n) / 0x80000000);
249
250     uri.len = r->uri.len + name[n].len;
251
252     uri.data = ngx_pnalloc(r->pool, uri.len);
253     if (uri.data == NULL) {
254         return NGX_HTTP_INTERNAL_SERVER_ERROR;
255     }
256
257     last = ngx_copy(uri.data, r->uri.data, r->uri.len);
258     ngx_memcpy(last, name[n].data, name[n].len);
259
260     return ngx_http_internal_redirect(r, &uri, &r->args);
261 }
262
263
264 static ngx_int_t
265 ngx_http_random_index_error(ngx_http_request_t *r, ngx_dir_t *dir,
266     ngx_str_t *name)
267 {
268     if (ngx_close_dir(dir) == NGX_ERROR) {
269         ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
270                       ngx_close_dir_n " \"%V\" failed", name);
271     }
272
273     return NGX_HTTP_INTERNAL_SERVER_ERROR;
274 }
275
276
277 static void *
278 ngx_http_random_index_create_loc_conf(ngx_conf_t *cf)
279 {
280     ngx_http_random_index_loc_conf_t  *conf;
281
282     conf = ngx_palloc(cf->pool, sizeof(ngx_http_random_index_loc_conf_t));
283     if (conf == NULL) {
284         return NGX_CONF_ERROR;
285     }
286
287     conf->enable = NGX_CONF_UNSET;
288
289     return conf;
290 }
291
292
293 static char *
294 ngx_http_random_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
295 {
296     ngx_http_random_index_loc_conf_t *prev = parent;
297     ngx_http_random_index_loc_conf_t *conf = child;
298
299     ngx_conf_merge_value(conf->enable, prev->enable, 0);
300
301     return NGX_CONF_OK;
302 }
303
304
305 static ngx_int_t
306 ngx_http_random_index_init(ngx_conf_t *cf)
307 {
308     ngx_http_handler_pt        *h;
309     ngx_http_core_main_conf_t  *cmcf;
310
311     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
312
313     h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
314     if (h == NULL) {
315         return NGX_ERROR;
316     }
317
318     *h = ngx_http_random_index_handler;
319
320     return NGX_OK;
321 }