Update and clean Tomato RAF files
[tomato.git] / release / src / router / nginx / src / http / modules / ngx_http_random_index_module.c
blobb0f0e080912f8797a7f8610fed0c7298eb698e14
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 #include <ngx_http.h>
13 typedef struct {
14 ngx_flag_t enable;
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),
36 NULL },
38 ngx_null_command
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 = {
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
73 static ngx_int_t
74 ngx_http_random_index_handler(ngx_http_request_t *r)
76 u_char *last, *filename;
77 size_t len, allocated, root;
78 ngx_err_t err;
79 ngx_int_t rc;
80 ngx_str_t path, uri, *name;
81 ngx_dir_t dir;
82 ngx_uint_t n, level;
83 ngx_array_t names;
84 ngx_http_random_index_loc_conf_t *rlcf;
86 if (r->uri.data[r->uri.len - 1] != '/') {
87 return NGX_DECLINED;
90 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
91 return NGX_DECLINED;
94 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_random_index_module);
96 if (!rlcf->enable) {
97 return NGX_DECLINED;
100 #if (NGX_HAVE_D_TYPE)
101 len = NGX_DIR_MASK_LEN;
102 #else
103 len = NGX_HTTP_RANDOM_INDEX_PREALLOCATE;
104 #endif
106 last = ngx_http_map_uri_to_path(r, &path, &root, len);
107 if (last == NULL) {
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) {
120 err = ngx_errno;
122 if (err == NGX_ENOENT
123 || err == NGX_ENOTDIR
124 || err == NGX_ENAMETOOLONG)
126 level = NGX_LOG_ERR;
127 rc = NGX_HTTP_NOT_FOUND;
129 } else if (err == NGX_EACCES) {
130 level = NGX_LOG_ERR;
131 rc = NGX_HTTP_FORBIDDEN;
133 } else {
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);
141 return rc;
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] = '/';
151 for ( ;; ) {
152 ngx_set_errno(0);
154 if (ngx_read_dir(&dir) == NGX_ERROR) {
155 err = ngx_errno;
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);
163 break;
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] == '.') {
170 continue;
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);
189 *last++ = '/';
192 ngx_cpystrn(last, ngx_de_name(&dir), len + 1);
194 if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) {
195 err = ngx_errno;
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",
206 filename);
207 return ngx_http_random_index_error(r, &dir, &path);
212 if (!ngx_de_is_file(&dir)) {
213 continue;
216 name = ngx_array_push(&names);
217 if (name == NULL) {
218 return ngx_http_random_index_error(r, &dir, &path);
221 name->len = len;
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);
236 n = names.nelts;
238 if (n == 0) {
239 return NGX_DECLINED;
242 name = names.elts;
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);
260 static ngx_int_t
261 ngx_http_random_index_error(ngx_http_request_t *r, ngx_dir_t *dir,
262 ngx_str_t *name)
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;
273 static void *
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));
279 if (conf == NULL) {
280 return NULL;
283 conf->enable = NGX_CONF_UNSET;
285 return conf;
289 static char *
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);
297 return NGX_CONF_OK;
301 static ngx_int_t
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);
310 if (h == NULL) {
311 return NGX_ERROR;
314 *h = ngx_http_random_index_handler;
316 return NGX_OK;