annotate src/http/modules/ngx_http_random_index_module.c @ 489:549994537f15 NGINX_0_7_52

nginx 0.7.52 *) Feature: the first native Windows binary release. *) Bugfix: in processing HEAD method while caching. *) Bugfix: in processing the "If-Modified-Since", "If-Range", etc. client request header lines while caching. *) Bugfix: now the "Set-Cookie" and "P3P" header lines are hidden in cacheable responses. *) Bugfix: if nginx was built with the ngx_http_perl_module and with a perl which supports threads, then during a master process exit the message "panic: MUTEX_LOCK" might be issued. *) Bugfix: nginx could not be built --without-http-cache; the bug had appeared in 0.7.48. *) Bugfix: nginx could not be built on platforms different from i386, amd64, sparc, and ppc; the bug had appeared in 0.7.42.
author Igor Sysoev <http://sysoev.ru>
date Mon, 20 Apr 2009 00:00:00 +0400
parents 79c5df00501e
children f39b9e29530d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
412
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
1
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
2 /*
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
3 * Copyright (C) Igor Sysoev
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
4 */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
5
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
6
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
7 #include <ngx_config.h>
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
8 #include <ngx_core.h>
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
9 #include <ngx_http.h>
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
10
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
11
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
12 typedef struct {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
13 ngx_flag_t enable;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
14 } ngx_http_random_index_loc_conf_t;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
15
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
16
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
17 #define NGX_HTTP_RANDOM_INDEX_PREALLOCATE 50
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
18
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
19
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
20 static ngx_int_t ngx_http_random_index_error(ngx_http_request_t *r,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
21 ngx_dir_t *dir, ngx_str_t *name);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
22 static ngx_int_t ngx_http_random_index_init(ngx_conf_t *cf);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
23 static void *ngx_http_random_index_create_loc_conf(ngx_conf_t *cf);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
24 static char *ngx_http_random_index_merge_loc_conf(ngx_conf_t *cf,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
25 void *parent, void *child);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
26
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
27
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
28 static ngx_command_t ngx_http_random_index_commands[] = {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
29
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
30 { ngx_string("random_index"),
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
31 NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
32 ngx_conf_set_flag_slot,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
33 NGX_HTTP_LOC_CONF_OFFSET,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
34 offsetof(ngx_http_random_index_loc_conf_t, enable),
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
35 NULL },
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
36
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
37 ngx_null_command
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
38 };
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
39
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
40
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
41 static ngx_http_module_t ngx_http_random_index_module_ctx = {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
42 NULL, /* preconfiguration */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
43 ngx_http_random_index_init, /* postconfiguration */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
44
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
45 NULL, /* create main configuration */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
46 NULL, /* init main configuration */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
47
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
48 NULL, /* create server configuration */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
49 NULL, /* merge server configuration */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
50
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
51 ngx_http_random_index_create_loc_conf, /* create location configration */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
52 ngx_http_random_index_merge_loc_conf /* merge location configration */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
53 };
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
54
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
55
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
56 ngx_module_t ngx_http_random_index_module = {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
57 NGX_MODULE_V1,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
58 &ngx_http_random_index_module_ctx, /* module context */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
59 ngx_http_random_index_commands, /* module directives */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
60 NGX_HTTP_MODULE, /* module type */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
61 NULL, /* init master */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
62 NULL, /* init module */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
63 NULL, /* init process */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
64 NULL, /* init thread */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
65 NULL, /* exit thread */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
66 NULL, /* exit process */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
67 NULL, /* exit master */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
68 NGX_MODULE_V1_PADDING
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
69 };
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
70
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
71
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
72 static ngx_int_t
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
73 ngx_http_random_index_handler(ngx_http_request_t *r)
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
74 {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
75 u_char *last, *filename;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
76 size_t len, allocated, root;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
77 ngx_err_t err;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
78 ngx_int_t rc;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
79 ngx_str_t path, uri, *name;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
80 ngx_dir_t dir;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
81 ngx_uint_t n, level;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
82 ngx_array_t names;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
83 ngx_http_random_index_loc_conf_t *rlcf;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
84
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
85 if (r->uri.data[r->uri.len - 1] != '/') {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
86 return NGX_DECLINED;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
87 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
88
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
89 if (r->zero_in_uri) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
90 return NGX_DECLINED;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
91 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
92
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
93 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
94 return NGX_DECLINED;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
95 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
96
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
97 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_random_index_module);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
98
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
99 if (!rlcf->enable) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
100 return NGX_DECLINED;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
101 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
102
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
103 #if (NGX_HAVE_D_TYPE)
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
104 len = NGX_DIR_MASK_LEN;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
105 #else
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
106 len = NGX_HTTP_RANDOM_INDEX_PREALLOCATE;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
107 #endif
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
108
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
109 last = ngx_http_map_uri_to_path(r, &path, &root, len);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
110 if (last == NULL) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
111 return NGX_HTTP_INTERNAL_SERVER_ERROR;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
112 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
113
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
114 allocated = path.len;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
115
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
116 path.len = last - path.data - 1;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
117 path.data[path.len] = '\0';
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
118
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
119 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
120 "http random index: \"%s\"", path.data);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
121
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
122 if (ngx_open_dir(&path, &dir) == NGX_ERROR) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
123 err = ngx_errno;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
124
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
125 if (err == NGX_ENOENT
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
126 || err == NGX_ENOTDIR
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
127 || err == NGX_ENAMETOOLONG)
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
128 {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
129 level = NGX_LOG_ERR;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
130 rc = NGX_HTTP_NOT_FOUND;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
131
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
132 } else if (err == NGX_EACCES) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
133 level = NGX_LOG_ERR;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
134 rc = NGX_HTTP_FORBIDDEN;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
135
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
136 } else {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
137 level = NGX_LOG_CRIT;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
138 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
139 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
140
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
141 ngx_log_error(level, r->connection->log, err,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
142 ngx_open_dir_n " \"%s\" failed", path.data);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
143
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
144 return rc;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
145 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
146
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
147 if (ngx_array_init(&names, r->pool, 32, sizeof(ngx_str_t)) != NGX_OK) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
148 return ngx_http_random_index_error(r, &dir, &path);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
149 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
150
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
151 filename = path.data;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
152 filename[path.len] = '/';
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
153
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
154 for ( ;; ) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
155 ngx_set_errno(0);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
156
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
157 if (ngx_read_dir(&dir) == NGX_ERROR) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
158 err = ngx_errno;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
159
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
160 if (err != NGX_ENOMOREFILES) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
161 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
162 ngx_read_dir_n " \"%V\" failed", &path);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
163 return ngx_http_random_index_error(r, &dir, &path);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
164 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
165
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
166 break;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
167 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
168
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
169 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
170 "http random index file: \"%s\"", ngx_de_name(&dir));
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
171
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
172 if (ngx_de_name(&dir)[0] == '.') {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
173 continue;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
174 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
175
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
176 len = ngx_de_namelen(&dir);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
177
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
178 if (!dir.valid_type) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
179
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
180 /* 1 byte for '/' and 1 byte for terminating '\0' */
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
181
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
182 if (path.len + 1 + len + 1 > allocated) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
183 allocated = path.len + 1 + len + 1
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
184 + NGX_HTTP_RANDOM_INDEX_PREALLOCATE;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
185
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
186 filename = ngx_pnalloc(r->pool, allocated);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
187 if (filename == NULL) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
188 return ngx_http_random_index_error(r, &dir, &path);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
189 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
190
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
191 last = ngx_cpystrn(filename, path.data, path.len + 1);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
192 *last++ = '/';
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
193 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
194
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
195 ngx_cpystrn(last, ngx_de_name(&dir), len + 1);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
196
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
197 if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
198 err = ngx_errno;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
199
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
200 if (err != NGX_ENOENT) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
201 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
202 ngx_de_info_n " \"%s\" failed", filename);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
203 return ngx_http_random_index_error(r, &dir, &path);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
204 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
205
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
206 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
207 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
208 ngx_de_link_info_n " \"%s\" failed",
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
209 filename);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
210 return ngx_http_random_index_error(r, &dir, &path);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
211 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
212 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
213 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
214
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
215 if (!ngx_de_is_file(&dir)) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
216 continue;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
217 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
218
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
219 name = ngx_array_push(&names);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
220 if (name == NULL) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
221 return ngx_http_random_index_error(r, &dir, &path);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
222 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
223
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
224 name->len = len;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
225
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
226 name->data = ngx_pnalloc(r->pool, len);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
227 if (name->data == NULL) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
228 return ngx_http_random_index_error(r, &dir, &path);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
229 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
230
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
231 ngx_memcpy(name->data, ngx_de_name(&dir), len);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
232 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
233
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
234 if (ngx_close_dir(&dir) == NGX_ERROR) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
235 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
236 ngx_close_dir_n " \"%s\" failed", &path);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
237 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
238
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
239 n = names.nelts;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
240
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
241 if (n == 0) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
242 return NGX_DECLINED;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
243 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
244
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
245 name = names.elts;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
246
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
247 n = (ngx_uint_t) (((uint64_t) ngx_random() * n) / 0x80000000);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
248
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
249 uri.len = r->uri.len + name[n].len;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
250
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
251 uri.data = ngx_pnalloc(r->pool, uri.len);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
252 if (uri.data == NULL) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
253 return NGX_HTTP_INTERNAL_SERVER_ERROR;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
254 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
255
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
256 last = ngx_copy(uri.data, r->uri.data, r->uri.len);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
257 ngx_memcpy(last, name[n].data, name[n].len);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
258
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
259 return ngx_http_internal_redirect(r, &uri, &r->args);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
260 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
261
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
262
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
263 static ngx_int_t
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
264 ngx_http_random_index_error(ngx_http_request_t *r, ngx_dir_t *dir,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
265 ngx_str_t *name)
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
266 {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
267 if (ngx_close_dir(dir) == NGX_ERROR) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
268 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
269 ngx_close_dir_n " \"%V\" failed", name);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
270 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
271
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
272 return NGX_HTTP_INTERNAL_SERVER_ERROR;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
273 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
274
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
275
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
276 static void *
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
277 ngx_http_random_index_create_loc_conf(ngx_conf_t *cf)
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
278 {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
279 ngx_http_random_index_loc_conf_t *conf;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
280
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
281 conf = ngx_palloc(cf->pool, sizeof(ngx_http_random_index_loc_conf_t));
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
282 if (conf == NULL) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
283 return NGX_CONF_ERROR;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
284 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
285
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
286 conf->enable = NGX_CONF_UNSET;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
287
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
288 return conf;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
289 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
290
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
291
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
292 static char *
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
293 ngx_http_random_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
294 {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
295 ngx_http_random_index_loc_conf_t *prev = parent;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
296 ngx_http_random_index_loc_conf_t *conf = child;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
297
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
298 ngx_conf_merge_value(conf->enable, prev->enable, 0);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
299
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
300 return NGX_CONF_OK;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
301 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
302
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
303
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
304 static ngx_int_t
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
305 ngx_http_random_index_init(ngx_conf_t *cf)
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
306 {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
307 ngx_http_handler_pt *h;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
308 ngx_http_core_main_conf_t *cmcf;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
309
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
310 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
311
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
312 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
313 if (h == NULL) {
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
314 return NGX_ERROR;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
315 }
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
316
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
317 *h = ngx_http_random_index_handler;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
318
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
319 return NGX_OK;
79c5df00501e nginx 0.7.15
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
320 }