annotate src/http/modules/ngx_http_random_index_module.c @ 2258:5b4680865e68

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