50
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 typedef struct {
|
58
|
13 ngx_str_t name;
|
|
14 ngx_array_t *lengths;
|
|
15 ngx_array_t *values;
|
|
16 } ngx_http_index_t;
|
|
17
|
|
18
|
|
19 typedef struct {
|
|
20 ngx_array_t *indices; /* array of ngx_http_index_t */
|
50
|
21 size_t max_index_len;
|
|
22 } ngx_http_index_loc_conf_t;
|
|
23
|
|
24
|
|
25 typedef struct {
|
58
|
26 ngx_uint_t current;
|
|
27
|
106
|
28 ngx_str_t path;
|
58
|
29 ngx_str_t index;
|
|
30
|
248
|
31 size_t root;
|
|
32
|
58
|
33 ngx_uint_t tested; /* unsigned tested:1 */
|
50
|
34 } ngx_http_index_ctx_t;
|
|
35
|
|
36
|
|
37 #define NGX_HTTP_DEFAULT_INDEX "index.html"
|
|
38
|
|
39
|
|
40 static ngx_int_t ngx_http_index_test_dir(ngx_http_request_t *r,
|
58
|
41 ngx_http_index_ctx_t *ctx);
|
50
|
42 static ngx_int_t ngx_http_index_error(ngx_http_request_t *r,
|
58
|
43 ngx_http_index_ctx_t *ctx, ngx_err_t err);
|
50
|
44
|
230
|
45 static ngx_int_t ngx_http_index_init(ngx_conf_t *cf);
|
50
|
46 static void *ngx_http_index_create_loc_conf(ngx_conf_t *cf);
|
|
47 static char *ngx_http_index_merge_loc_conf(ngx_conf_t *cf,
|
58
|
48 void *parent, void *child);
|
50
|
49 static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
|
58
|
50 void *conf);
|
50
|
51
|
|
52
|
|
53 static ngx_command_t ngx_http_index_commands[] = {
|
|
54
|
|
55 { ngx_string("index"),
|
58
|
56 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
50
|
57 ngx_http_index_set_index,
|
|
58 NGX_HTTP_LOC_CONF_OFFSET,
|
|
59 0,
|
|
60 NULL },
|
|
61
|
|
62 #if (NGX_HTTP_CACHE)
|
|
63
|
|
64 { ngx_string("index_cache"),
|
|
65 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE3,
|
|
66 ngx_http_set_cache_slot,
|
|
67 NGX_HTTP_LOC_CONF_OFFSET,
|
|
68 offsetof(ngx_http_index_loc_conf_t, index_cache),
|
|
69 NULL },
|
|
70
|
|
71 #endif
|
|
72
|
|
73 ngx_null_command
|
|
74 };
|
|
75
|
|
76
|
216
|
77 static ngx_http_module_t ngx_http_index_module_ctx = {
|
58
|
78 NULL, /* preconfiguration */
|
230
|
79 ngx_http_index_init, /* postconfiguration */
|
50
|
80
|
|
81 NULL, /* create main configuration */
|
|
82 NULL, /* init main configuration */
|
|
83
|
|
84 NULL, /* create server configuration */
|
|
85 NULL, /* merge server configuration */
|
|
86
|
|
87 ngx_http_index_create_loc_conf, /* create location configration */
|
|
88 ngx_http_index_merge_loc_conf /* merge location configration */
|
|
89 };
|
|
90
|
|
91
|
|
92 ngx_module_t ngx_http_index_module = {
|
58
|
93 NGX_MODULE_V1,
|
50
|
94 &ngx_http_index_module_ctx, /* module context */
|
|
95 ngx_http_index_commands, /* module directives */
|
|
96 NGX_HTTP_MODULE, /* module type */
|
90
|
97 NULL, /* init master */
|
230
|
98 NULL, /* init module */
|
90
|
99 NULL, /* init process */
|
|
100 NULL, /* init thread */
|
|
101 NULL, /* exit thread */
|
|
102 NULL, /* exit process */
|
|
103 NULL, /* exit master */
|
|
104 NGX_MODULE_V1_PADDING
|
50
|
105 };
|
|
106
|
|
107
|
|
108 /*
|
|
109 * Try to open the first index file before the test of the directory existence
|
|
110 * because the valid requests should be many more than invalid ones.
|
|
111 * If open() would fail, then stat() should be more quickly because some data
|
|
112 * is already cached in the kernel.
|
|
113 * Besides, Win32 has ERROR_PATH_NOT_FOUND (NGX_ENOTDIR).
|
|
114 * Unix has ENOTDIR error, although it less helpfull - it points only
|
|
115 * that path contains the usual file in place of the directory.
|
|
116 */
|
|
117
|
58
|
118 static ngx_int_t
|
|
119 ngx_http_index_handler(ngx_http_request_t *r)
|
50
|
120 {
|
106
|
121 u_char *last;
|
58
|
122 size_t len;
|
|
123 ngx_fd_t fd;
|
|
124 ngx_int_t rc;
|
|
125 ngx_err_t err;
|
106
|
126 ngx_str_t uri;
|
58
|
127 ngx_log_t *log;
|
|
128 ngx_uint_t i;
|
|
129 ngx_http_index_t *index;
|
|
130 ngx_http_index_ctx_t *ctx;
|
112
|
131 ngx_pool_cleanup_t *cln;
|
|
132 ngx_pool_cleanup_file_t *clnf;
|
58
|
133 ngx_http_script_code_pt code;
|
|
134 ngx_http_script_engine_t e;
|
|
135 ngx_http_core_loc_conf_t *clcf;
|
|
136 ngx_http_index_loc_conf_t *ilcf;
|
|
137 ngx_http_script_len_code_pt lcode;
|
50
|
138
|
|
139 if (r->uri.data[r->uri.len - 1] != '/') {
|
|
140 return NGX_DECLINED;
|
|
141 }
|
|
142
|
272
|
143 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
|
|
144 return NGX_DECLINED;
|
|
145 }
|
|
146
|
50
|
147 /* TODO: Win32 */
|
|
148 if (r->zero_in_uri) {
|
|
149 return NGX_DECLINED;
|
|
150 }
|
|
151
|
|
152 log = r->connection->log;
|
|
153
|
|
154 /*
|
58
|
155 * we use context because the handler supports an async file opening,
|
|
156 * and may be called several times
|
50
|
157 */
|
|
158
|
|
159 ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);
|
|
160
|
|
161 ctx = ngx_http_get_module_ctx(r, ngx_http_index_module);
|
|
162 if (ctx == NULL) {
|
|
163
|
|
164 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_index_ctx_t));
|
|
165 if (ctx == NULL) {
|
|
166 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
167 }
|
|
168
|
|
169 ngx_http_set_ctx(r, ctx, ngx_http_index_module);
|
58
|
170 }
|
50
|
171
|
58
|
172 index = ilcf->indices->elts;
|
|
173 for (i = ctx->current; i < ilcf->indices->nelts; i++) {
|
|
174
|
|
175 if (index[i].lengths == NULL) {
|
50
|
176
|
58
|
177 if (index[i].name.data[0] == '/') {
|
|
178 return ngx_http_internal_redirect(r, &index[i].name, &r->args);
|
|
179 }
|
50
|
180
|
58
|
181 len = ilcf->max_index_len;
|
|
182 ctx->index.len = index[i].name.len;
|
50
|
183
|
58
|
184 } else {
|
|
185 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
|
50
|
186
|
58
|
187 e.ip = index[i].lengths->elts;
|
|
188 e.request = r;
|
122
|
189 e.flushed = 1;
|
58
|
190
|
112
|
191 /* 1 byte for terminating '\0' */
|
106
|
192
|
112
|
193 len = 1;
|
50
|
194
|
58
|
195 while (*(uintptr_t *) e.ip) {
|
|
196 lcode = *(ngx_http_script_len_code_pt *) e.ip;
|
|
197 len += lcode(&e);
|
|
198 }
|
50
|
199
|
58
|
200 ctx->index.len = len;
|
112
|
201
|
|
202 /* 16 bytes are preallocation */
|
|
203
|
|
204 len += 16;
|
58
|
205 }
|
50
|
206
|
230
|
207 if (len > (size_t) (ctx->path.data + ctx->path.len - ctx->index.data)) {
|
106
|
208
|
248
|
209 last = ngx_http_map_uri_to_path(r, &ctx->path, &ctx->root, len);
|
106
|
210 if (last == NULL) {
|
|
211 return NGX_ERROR;
|
50
|
212 }
|
106
|
213
|
|
214 ctx->index.data = last;
|
50
|
215 }
|
|
216
|
58
|
217 if (index[i].values == NULL) {
|
106
|
218
|
|
219 /* index[i].name.len includes the terminating '\0' */
|
|
220
|
|
221 ngx_memcpy(ctx->index.data, index[i].name.data, index[i].name.len);
|
50
|
222
|
58
|
223 } else {
|
|
224 e.ip = index[i].values->elts;
|
|
225 e.pos = ctx->index.data;
|
50
|
226
|
58
|
227 while (*(uintptr_t *) e.ip) {
|
|
228 code = *(ngx_http_script_code_pt *) e.ip;
|
|
229 code((ngx_http_script_engine_t *) &e);
|
50
|
230 }
|
|
231
|
58
|
232 if (*ctx->index.data == '/') {
|
|
233 ctx->index.len--;
|
|
234 return ngx_http_internal_redirect(r, &ctx->index, &r->args);
|
50
|
235 }
|
|
236
|
58
|
237 *e.pos++ = '\0';
|
50
|
238 }
|
|
239
|
106
|
240 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
241 "open index \"%s\"", ctx->path.data);
|
50
|
242
|
112
|
243 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t));
|
|
244 if (cln == NULL) {
|
|
245 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
246 }
|
|
247
|
234
|
248 fd = ngx_open_file(ctx->path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN);
|
50
|
249
|
|
250 if (fd == (ngx_fd_t) NGX_AGAIN) {
|
58
|
251 ctx->current = i;
|
50
|
252 return NGX_AGAIN;
|
|
253 }
|
|
254
|
|
255 if (fd == NGX_INVALID_FILE) {
|
|
256 err = ngx_errno;
|
|
257
|
|
258 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, err,
|
106
|
259 ngx_open_file_n " \"%s\" failed", ctx->path.data);
|
50
|
260
|
|
261 if (err == NGX_ENOTDIR) {
|
|
262 return ngx_http_index_error(r, ctx, err);
|
|
263
|
|
264 } else if (err == NGX_EACCES) {
|
|
265 return ngx_http_index_error(r, ctx, err);
|
|
266 }
|
|
267
|
|
268 if (!ctx->tested) {
|
|
269 rc = ngx_http_index_test_dir(r, ctx);
|
|
270
|
|
271 if (rc != NGX_OK) {
|
|
272 return rc;
|
|
273 }
|
|
274
|
|
275 ctx->tested = 1;
|
|
276 }
|
|
277
|
|
278 if (err == NGX_ENOENT) {
|
|
279 continue;
|
|
280 }
|
|
281
|
|
282 ngx_log_error(NGX_LOG_ERR, log, err,
|
106
|
283 ngx_open_file_n " \"%s\" failed", ctx->path.data);
|
50
|
284
|
|
285 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
286 }
|
|
287
|
112
|
288 cln->handler = ngx_pool_cleanup_file;
|
|
289 clnf = cln->data;
|
50
|
290
|
112
|
291 clnf->fd = fd;
|
|
292 clnf->name = ctx->path.data;
|
|
293 clnf->log = r->pool->log;
|
50
|
294
|
106
|
295 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
50
|
296
|
106
|
297 uri.len = r->uri.len + ctx->index.len - 1;
|
|
298
|
|
299 if (!clcf->alias) {
|
248
|
300 uri.data = ctx->path.data + ctx->root;
|
106
|
301
|
|
302 } else {
|
|
303 uri.data = ngx_palloc(r->pool, uri.len);
|
|
304 if (uri.data == NULL) {
|
|
305 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
306 }
|
|
307
|
122
|
308 last = ngx_copy(uri.data, r->uri.data, r->uri.len);
|
106
|
309 ngx_memcpy(last, ctx->index.data, ctx->index.len - 1);
|
50
|
310 }
|
|
311
|
106
|
312 return ngx_http_internal_redirect(r, &uri, &r->args);
|
50
|
313 }
|
|
314
|
|
315 return NGX_DECLINED;
|
|
316 }
|
|
317
|
|
318
|
58
|
319 static ngx_int_t
|
|
320 ngx_http_index_test_dir(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx)
|
|
321 {
|
252
|
322 u_char c;
|
|
323 ngx_uint_t i;
|
58
|
324 ngx_err_t err;
|
|
325 ngx_file_info_t fi;
|
|
326
|
252
|
327 c = *(ctx->index.data - 1);
|
|
328 i = (c == '/') ? 1 : 0;
|
|
329 *(ctx->index.data - i) = '\0';
|
50
|
330
|
|
331 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
106
|
332 "http index check dir: \"%s\"", ctx->path.data);
|
50
|
333
|
106
|
334 if (ngx_file_info(ctx->path.data, &fi) == -1) {
|
50
|
335
|
|
336 err = ngx_errno;
|
|
337
|
|
338 if (err == NGX_ENOENT) {
|
252
|
339 *(ctx->index.data - i) = c;
|
50
|
340 return ngx_http_index_error(r, ctx, err);
|
|
341 }
|
|
342
|
|
343 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
|
106
|
344 ngx_file_info_n " \"%s\" failed", ctx->path.data);
|
50
|
345
|
|
346 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
347 }
|
|
348
|
252
|
349 *(ctx->index.data - i) = c;
|
50
|
350
|
58
|
351 if (ngx_is_dir(&fi)) {
|
50
|
352 return NGX_OK;
|
|
353 }
|
|
354
|
58
|
355 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
106
|
356 "\"%s\" is not a directory", ctx->path.data);
|
58
|
357
|
|
358 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
50
|
359 }
|
|
360
|
|
361
|
58
|
362 static ngx_int_t
|
|
363 ngx_http_index_error(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx,
|
|
364 ngx_err_t err)
|
50
|
365 {
|
|
366 if (err == NGX_EACCES) {
|
|
367 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
106
|
368 "\"%s\" is forbidden", ctx->path.data);
|
126
|
369
|
50
|
370 return NGX_HTTP_FORBIDDEN;
|
|
371 }
|
|
372
|
|
373 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
106
|
374 "\"%s\" is not found", ctx->path.data);
|
58
|
375
|
50
|
376 return NGX_HTTP_NOT_FOUND;
|
|
377 }
|
|
378
|
|
379
|
58
|
380 static void *
|
|
381 ngx_http_index_create_loc_conf(ngx_conf_t *cf)
|
|
382 {
|
|
383 ngx_http_index_loc_conf_t *conf;
|
|
384
|
|
385 conf = ngx_palloc(cf->pool, sizeof(ngx_http_index_loc_conf_t));
|
|
386 if (conf == NULL) {
|
|
387 return NGX_CONF_ERROR;
|
|
388 }
|
|
389
|
|
390 conf->indices = NULL;
|
230
|
391 conf->max_index_len = 0;
|
58
|
392
|
|
393 return conf;
|
|
394 }
|
|
395
|
|
396
|
|
397 static char *
|
|
398 ngx_http_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
399 {
|
|
400 ngx_http_index_loc_conf_t *prev = parent;
|
|
401 ngx_http_index_loc_conf_t *conf = child;
|
|
402
|
|
403 ngx_http_index_t *index;
|
|
404
|
|
405 if (conf->indices == NULL) {
|
|
406 conf->indices = prev->indices;
|
|
407 conf->max_index_len = prev->max_index_len;
|
|
408 }
|
|
409
|
|
410 if (conf->indices == NULL) {
|
|
411 conf->indices = ngx_array_create(cf->pool, 1, sizeof(ngx_http_index_t));
|
|
412 if (conf->indices == NULL) {
|
|
413 return NGX_CONF_ERROR;
|
|
414 }
|
|
415
|
|
416 index = ngx_array_push(conf->indices);
|
|
417 if (index == NULL) {
|
|
418 return NGX_CONF_ERROR;
|
|
419 }
|
|
420
|
|
421 index->name.len = sizeof(NGX_HTTP_DEFAULT_INDEX);
|
|
422 index->name.data = (u_char *) NGX_HTTP_DEFAULT_INDEX;
|
|
423 index->lengths = NULL;
|
|
424 index->values = NULL;
|
|
425
|
|
426 conf->max_index_len = sizeof(NGX_HTTP_DEFAULT_INDEX);
|
|
427
|
|
428 return NGX_CONF_OK;
|
|
429 }
|
|
430
|
|
431 return NGX_CONF_OK;
|
|
432 }
|
|
433
|
|
434
|
230
|
435 static ngx_int_t
|
|
436 ngx_http_index_init(ngx_conf_t *cf)
|
|
437 {
|
|
438 ngx_http_handler_pt *h;
|
|
439 ngx_http_core_main_conf_t *cmcf;
|
|
440
|
|
441 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
442
|
|
443 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
444 if (h == NULL) {
|
|
445 return NGX_ERROR;
|
|
446 }
|
|
447
|
|
448 *h = ngx_http_index_handler;
|
|
449
|
|
450 return NGX_OK;
|
|
451 }
|
|
452
|
|
453
|
58
|
454 /* TODO: warn about duplicate indices */
|
|
455
|
|
456 static char *
|
|
457 ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
458 {
|
|
459 ngx_http_index_loc_conf_t *ilcf = conf;
|
|
460
|
230
|
461 ngx_str_t *value;
|
58
|
462 ngx_uint_t i, n;
|
|
463 ngx_http_index_t *index;
|
|
464 ngx_http_script_compile_t sc;
|
|
465
|
|
466 if (ilcf->indices == NULL) {
|
|
467 ilcf->indices = ngx_array_create(cf->pool, 2, sizeof(ngx_http_index_t));
|
|
468 if (ilcf->indices == NULL) {
|
|
469 return NGX_CONF_ERROR;
|
|
470 }
|
|
471 }
|
|
472
|
|
473 value = cf->args->elts;
|
|
474
|
|
475 for (i = 1; i < cf->args->nelts; i++) {
|
|
476 if (value[i].data[0] == '/' && i != cf->args->nelts - 1) {
|
|
477 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
478 "only the last index in \"index\" directive "
|
|
479 "may be absolute");
|
|
480 return NGX_CONF_ERROR;
|
|
481 }
|
|
482
|
|
483 if (value[i].len == 0) {
|
|
484 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
485 "index \"%V\" in \"index\" directive is invalid",
|
|
486 &value[1]);
|
|
487 return NGX_CONF_ERROR;
|
|
488 }
|
|
489
|
|
490 index = ngx_array_push(ilcf->indices);
|
|
491 if (index == NULL) {
|
|
492 return NGX_CONF_ERROR;
|
|
493 }
|
|
494
|
|
495 index->name.len = value[i].len;
|
|
496 index->name.data = value[i].data;
|
|
497 index->lengths = NULL;
|
|
498 index->values = NULL;
|
|
499
|
|
500 n = ngx_http_script_variables_count(&value[i]);
|
|
501
|
|
502 if (n == 0) {
|
230
|
503 if (ilcf->max_index_len < index->name.len) {
|
58
|
504 ilcf->max_index_len = index->name.len;
|
|
505 }
|
|
506
|
122
|
507 /* include the terminating '\0' to the length to use ngx_copy() */
|
106
|
508 index->name.len++;
|
|
509
|
58
|
510 continue;
|
|
511 }
|
|
512
|
|
513 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
514
|
|
515 sc.cf = cf;
|
|
516 sc.source = &value[i];
|
|
517 sc.lengths = &index->lengths;
|
|
518 sc.values = &index->values;
|
|
519 sc.variables = n;
|
|
520 sc.complete_lengths = 1;
|
|
521 sc.complete_values = 1;
|
|
522
|
|
523 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
524 return NGX_CONF_ERROR;
|
|
525 }
|
|
526 }
|
|
527
|
|
528 return NGX_CONF_OK;
|
|
529 }
|