comparison 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
comparison
equal deleted inserted replaced
2257:74d270c8821e 2258:5b4680865e68
17 #define NGX_HTTP_RANDOM_INDEX_PREALLOCATE 50 17 #define NGX_HTTP_RANDOM_INDEX_PREALLOCATE 50
18 18
19 19
20 static ngx_int_t ngx_http_random_index_error(ngx_http_request_t *r, 20 static ngx_int_t ngx_http_random_index_error(ngx_http_request_t *r,
21 ngx_dir_t *dir, ngx_str_t *name); 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); 22 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, 23 static char *ngx_http_random_index_merge_loc_conf(ngx_conf_t *cf,
25 void *parent, void *child); 24 void *parent, void *child);
25 static ngx_int_t ngx_http_random_index_add_variable(ngx_conf_t *cf);
26 static ngx_int_t ngx_http_random_index_init(ngx_conf_t *cf);
26 27
27 28
28 static ngx_command_t ngx_http_random_index_commands[] = { 29 static ngx_command_t ngx_http_random_index_commands[] = {
29 30
30 { ngx_string("random_index"), 31 { ngx_string("random_index"),
37 ngx_null_command 38 ngx_null_command
38 }; 39 };
39 40
40 41
41 static ngx_http_module_t ngx_http_random_index_module_ctx = { 42 static ngx_http_module_t ngx_http_random_index_module_ctx = {
42 NULL, /* preconfiguration */ 43 ngx_http_random_index_add_variable, /* preconfiguration */
43 ngx_http_random_index_init, /* postconfiguration */ 44 ngx_http_random_index_init, /* postconfiguration */
44 45
45 NULL, /* create main configuration */ 46 NULL, /* create main configuration */
46 NULL, /* init main configuration */ 47 NULL, /* init main configuration */
47 48
67 NULL, /* exit master */ 68 NULL, /* exit master */
68 NGX_MODULE_V1_PADDING 69 NGX_MODULE_V1_PADDING
69 }; 70 };
70 71
71 72
73 static ngx_str_t ngx_http_random_index = ngx_string("random_index");
74 static ngx_int_t ngx_random_index_variable_index;
75
76
72 static ngx_int_t 77 static ngx_int_t
73 ngx_http_random_index_handler(ngx_http_request_t *r) 78 ngx_http_random_index_handler(ngx_http_request_t *r)
74 { 79 {
75 u_char *last, *filename; 80 u_char *last, *filename;
76 size_t len, allocated, root; 81 size_t len, allocated, root;
78 ngx_int_t rc; 83 ngx_int_t rc;
79 ngx_str_t path, uri, *name; 84 ngx_str_t path, uri, *name;
80 ngx_dir_t dir; 85 ngx_dir_t dir;
81 ngx_uint_t n, level; 86 ngx_uint_t n, level;
82 ngx_array_t names; 87 ngx_array_t names;
88 ngx_http_variable_value_t *v;
83 ngx_http_random_index_loc_conf_t *rlcf; 89 ngx_http_random_index_loc_conf_t *rlcf;
84 90
85 if (r->uri.data[r->uri.len - 1] != '/') { 91 if (r->uri.data[r->uri.len - 1] != '/') {
86 return NGX_DECLINED; 92 return NGX_DECLINED;
87 } 93 }
255 } 261 }
256 262
257 last = ngx_copy(uri.data, r->uri.data, r->uri.len); 263 last = ngx_copy(uri.data, r->uri.data, r->uri.len);
258 ngx_memcpy(last, name[n].data, name[n].len); 264 ngx_memcpy(last, name[n].data, name[n].len);
259 265
266 v = &r->variables[ngx_random_index_variable_index];
267
268 v->len = name[n].len;
269 v->valid = 1;
270 v->no_cacheable = 0;
271 v->not_found = 0;
272 v->data = name[n].data;
273
260 return ngx_http_internal_redirect(r, &uri, &r->args); 274 return ngx_http_internal_redirect(r, &uri, &r->args);
261 } 275 }
262 276
263 277
264 static ngx_int_t 278 static ngx_int_t
272 286
273 return NGX_HTTP_INTERNAL_SERVER_ERROR; 287 return NGX_HTTP_INTERNAL_SERVER_ERROR;
274 } 288 }
275 289
276 290
291 static ngx_int_t
292 ngx_http_random_index_variable(ngx_http_request_t *r,
293 ngx_http_variable_value_t *v, uintptr_t data)
294 {
295 /*
296 * the "random_index" directive stores index file name directly inside
297 * r->variables[] because request context is not preserved while
298 * an internal redirection
299 */
300
301 v->not_found = 1;
302
303 return NGX_OK;
304 }
305
306
277 static void * 307 static void *
278 ngx_http_random_index_create_loc_conf(ngx_conf_t *cf) 308 ngx_http_random_index_create_loc_conf(ngx_conf_t *cf)
279 { 309 {
280 ngx_http_random_index_loc_conf_t *conf; 310 ngx_http_random_index_loc_conf_t *conf;
281 311
301 return NGX_CONF_OK; 331 return NGX_CONF_OK;
302 } 332 }
303 333
304 334
305 static ngx_int_t 335 static ngx_int_t
336 ngx_http_random_index_add_variable(ngx_conf_t *cf)
337 {
338 ngx_int_t index;
339 ngx_http_variable_t *var;
340
341 var = ngx_http_add_variable(cf, &ngx_http_random_index,
342 NGX_HTTP_VAR_NOHASH);
343 if (var == NULL) {
344 return NGX_ERROR;
345 }
346
347 index = ngx_http_get_variable_index(cf, &ngx_http_random_index);
348 if (index == NGX_ERROR) {
349 return NGX_ERROR;
350 }
351
352 ngx_random_index_variable_index = index;
353
354 var->get_handler = ngx_http_random_index_variable;
355
356 return NGX_OK;
357 }
358
359
360 static ngx_int_t
306 ngx_http_random_index_init(ngx_conf_t *cf) 361 ngx_http_random_index_init(ngx_conf_t *cf)
307 { 362 {
308 ngx_http_handler_pt *h; 363 ngx_http_handler_pt *h;
309 ngx_http_core_main_conf_t *cmcf; 364 ngx_http_core_main_conf_t *cmcf;
310 365