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
|
|
143 /* TODO: Win32 */
|
|
144 if (r->zero_in_uri) {
|
|
145 return NGX_DECLINED;
|
|
146 }
|
|
147
|
|
148 log = r->connection->log;
|
|
149
|
|
150 /*
|
58
|
151 * we use context because the handler supports an async file opening,
|
|
152 * and may be called several times
|
50
|
153 */
|
|
154
|
|
155 ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);
|
|
156
|
|
157 ctx = ngx_http_get_module_ctx(r, ngx_http_index_module);
|
|
158 if (ctx == NULL) {
|
|
159
|
|
160 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_index_ctx_t));
|
|
161 if (ctx == NULL) {
|
|
162 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
163 }
|
|
164
|
|
165 ngx_http_set_ctx(r, ctx, ngx_http_index_module);
|
58
|
166 }
|
50
|
167
|
58
|
168 index = ilcf->indices->elts;
|
|
169 for (i = ctx->current; i < ilcf->indices->nelts; i++) {
|
|
170
|
|
171 if (index[i].lengths == NULL) {
|
50
|
172
|
58
|
173 if (index[i].name.data[0] == '/') {
|
|
174 return ngx_http_internal_redirect(r, &index[i].name, &r->args);
|
|
175 }
|
50
|
176
|
58
|
177 len = ilcf->max_index_len;
|
|
178 ctx->index.len = index[i].name.len;
|
50
|
179
|
58
|
180 } else {
|
|
181 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
|
50
|
182
|
58
|
183 e.ip = index[i].lengths->elts;
|
|
184 e.request = r;
|
122
|
185 e.flushed = 1;
|
58
|
186
|
112
|
187 /* 1 byte for terminating '\0' */
|
106
|
188
|
112
|
189 len = 1;
|
50
|
190
|
58
|
191 while (*(uintptr_t *) e.ip) {
|
|
192 lcode = *(ngx_http_script_len_code_pt *) e.ip;
|
|
193 len += lcode(&e);
|
|
194 }
|
50
|
195
|
58
|
196 ctx->index.len = len;
|
112
|
197
|
|
198 /* 16 bytes are preallocation */
|
|
199
|
|
200 len += 16;
|
58
|
201 }
|
50
|
202
|
230
|
203 if (len > (size_t) (ctx->path.data + ctx->path.len - ctx->index.data)) {
|
106
|
204
|
248
|
205 last = ngx_http_map_uri_to_path(r, &ctx->path, &ctx->root, len);
|
106
|
206 if (last == NULL) {
|
|
207 return NGX_ERROR;
|
50
|
208 }
|
106
|
209
|
|
210 ctx->index.data = last;
|
50
|
211 }
|
|
212
|
58
|
213 if (index[i].values == NULL) {
|
106
|
214
|
|
215 /* index[i].name.len includes the terminating '\0' */
|
|
216
|
|
217 ngx_memcpy(ctx->index.data, index[i].name.data, index[i].name.len);
|
50
|
218
|
58
|
219 } else {
|
|
220 e.ip = index[i].values->elts;
|
|
221 e.pos = ctx->index.data;
|
50
|
222
|
58
|
223 while (*(uintptr_t *) e.ip) {
|
|
224 code = *(ngx_http_script_code_pt *) e.ip;
|
|
225 code((ngx_http_script_engine_t *) &e);
|
50
|
226 }
|
|
227
|
58
|
228 if (*ctx->index.data == '/') {
|
|
229 ctx->index.len--;
|
|
230 return ngx_http_internal_redirect(r, &ctx->index, &r->args);
|
50
|
231 }
|
|
232
|
58
|
233 *e.pos++ = '\0';
|
50
|
234 }
|
|
235
|
106
|
236 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
237 "open index \"%s\"", ctx->path.data);
|
50
|
238
|
112
|
239 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t));
|
|
240 if (cln == NULL) {
|
|
241 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
242 }
|
|
243
|
234
|
244 fd = ngx_open_file(ctx->path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN);
|
50
|
245
|
|
246 if (fd == (ngx_fd_t) NGX_AGAIN) {
|
58
|
247 ctx->current = i;
|
50
|
248 return NGX_AGAIN;
|
|
249 }
|
|
250
|
|
251 if (fd == NGX_INVALID_FILE) {
|
|
252 err = ngx_errno;
|
|
253
|
|
254 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, err,
|
106
|
255 ngx_open_file_n " \"%s\" failed", ctx->path.data);
|
50
|
256
|
|
257 if (err == NGX_ENOTDIR) {
|
|
258 return ngx_http_index_error(r, ctx, err);
|
|
259
|
|
260 } else if (err == NGX_EACCES) {
|
|
261 return ngx_http_index_error(r, ctx, err);
|
|
262 }
|
|
263
|
|
264 if (!ctx->tested) {
|
|
265 rc = ngx_http_index_test_dir(r, ctx);
|
|
266
|
|
267 if (rc != NGX_OK) {
|
|
268 return rc;
|
|
269 }
|
|
270
|
|
271 ctx->tested = 1;
|
|
272 }
|
|
273
|
|
274 if (err == NGX_ENOENT) {
|
|
275 continue;
|
|
276 }
|
|
277
|
|
278 ngx_log_error(NGX_LOG_ERR, log, err,
|
106
|
279 ngx_open_file_n " \"%s\" failed", ctx->path.data);
|
50
|
280
|
|
281 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
282 }
|
|
283
|
112
|
284 cln->handler = ngx_pool_cleanup_file;
|
|
285 clnf = cln->data;
|
50
|
286
|
112
|
287 clnf->fd = fd;
|
|
288 clnf->name = ctx->path.data;
|
|
289 clnf->log = r->pool->log;
|
50
|
290
|
106
|
291 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
50
|
292
|
106
|
293 uri.len = r->uri.len + ctx->index.len - 1;
|
|
294
|
|
295 if (!clcf->alias) {
|
248
|
296 uri.data = ctx->path.data + ctx->root;
|
106
|
297
|
|
298 } else {
|
|
299 uri.data = ngx_palloc(r->pool, uri.len);
|
|
300 if (uri.data == NULL) {
|
|
301 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
302 }
|
|
303
|
122
|
304 last = ngx_copy(uri.data, r->uri.data, r->uri.len);
|
106
|
305 ngx_memcpy(last, ctx->index.data, ctx->index.len - 1);
|
50
|
306 }
|
|
307
|
106
|
308 return ngx_http_internal_redirect(r, &uri, &r->args);
|
50
|
309 }
|
|
310
|
|
311 return NGX_DECLINED;
|
|
312 }
|
|
313
|
|
314
|
58
|
315 static ngx_int_t
|
|
316 ngx_http_index_test_dir(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx)
|
|
317 {
|
252
|
318 u_char c;
|
|
319 ngx_uint_t i;
|
58
|
320 ngx_err_t err;
|
|
321 ngx_file_info_t fi;
|
|
322
|
252
|
323 c = *(ctx->index.data - 1);
|
|
324 i = (c == '/') ? 1 : 0;
|
|
325 *(ctx->index.data - i) = '\0';
|
50
|
326
|
|
327 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
106
|
328 "http index check dir: \"%s\"", ctx->path.data);
|
50
|
329
|
106
|
330 if (ngx_file_info(ctx->path.data, &fi) == -1) {
|
50
|
331
|
|
332 err = ngx_errno;
|
|
333
|
|
334 if (err == NGX_ENOENT) {
|
252
|
335 *(ctx->index.data - i) = c;
|
50
|
336 return ngx_http_index_error(r, ctx, err);
|
|
337 }
|
|
338
|
|
339 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
|
106
|
340 ngx_file_info_n " \"%s\" failed", ctx->path.data);
|
50
|
341
|
|
342 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
343 }
|
|
344
|
252
|
345 *(ctx->index.data - i) = c;
|
50
|
346
|
58
|
347 if (ngx_is_dir(&fi)) {
|
50
|
348 return NGX_OK;
|
|
349 }
|
|
350
|
58
|
351 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
106
|
352 "\"%s\" is not a directory", ctx->path.data);
|
58
|
353
|
|
354 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
50
|
355 }
|
|
356
|
|
357
|
58
|
358 static ngx_int_t
|
|
359 ngx_http_index_error(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx,
|
|
360 ngx_err_t err)
|
50
|
361 {
|
|
362 if (err == NGX_EACCES) {
|
|
363 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
106
|
364 "\"%s\" is forbidden", ctx->path.data);
|
126
|
365
|
50
|
366 return NGX_HTTP_FORBIDDEN;
|
|
367 }
|
|
368
|
|
369 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
|
106
|
370 "\"%s\" is not found", ctx->path.data);
|
58
|
371
|
50
|
372 return NGX_HTTP_NOT_FOUND;
|
|
373 }
|
|
374
|
|
375
|
58
|
376 static void *
|
|
377 ngx_http_index_create_loc_conf(ngx_conf_t *cf)
|
|
378 {
|
|
379 ngx_http_index_loc_conf_t *conf;
|
|
380
|
|
381 conf = ngx_palloc(cf->pool, sizeof(ngx_http_index_loc_conf_t));
|
|
382 if (conf == NULL) {
|
|
383 return NGX_CONF_ERROR;
|
|
384 }
|
|
385
|
|
386 conf->indices = NULL;
|
230
|
387 conf->max_index_len = 0;
|
58
|
388
|
|
389 return conf;
|
|
390 }
|
|
391
|
|
392
|
|
393 static char *
|
|
394 ngx_http_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
395 {
|
|
396 ngx_http_index_loc_conf_t *prev = parent;
|
|
397 ngx_http_index_loc_conf_t *conf = child;
|
|
398
|
|
399 ngx_http_index_t *index;
|
|
400
|
|
401 if (conf->indices == NULL) {
|
|
402 conf->indices = prev->indices;
|
|
403 conf->max_index_len = prev->max_index_len;
|
|
404 }
|
|
405
|
|
406 if (conf->indices == NULL) {
|
|
407 conf->indices = ngx_array_create(cf->pool, 1, sizeof(ngx_http_index_t));
|
|
408 if (conf->indices == NULL) {
|
|
409 return NGX_CONF_ERROR;
|
|
410 }
|
|
411
|
|
412 index = ngx_array_push(conf->indices);
|
|
413 if (index == NULL) {
|
|
414 return NGX_CONF_ERROR;
|
|
415 }
|
|
416
|
|
417 index->name.len = sizeof(NGX_HTTP_DEFAULT_INDEX);
|
|
418 index->name.data = (u_char *) NGX_HTTP_DEFAULT_INDEX;
|
|
419 index->lengths = NULL;
|
|
420 index->values = NULL;
|
|
421
|
|
422 conf->max_index_len = sizeof(NGX_HTTP_DEFAULT_INDEX);
|
|
423
|
|
424 return NGX_CONF_OK;
|
|
425 }
|
|
426
|
|
427 return NGX_CONF_OK;
|
|
428 }
|
|
429
|
|
430
|
230
|
431 static ngx_int_t
|
|
432 ngx_http_index_init(ngx_conf_t *cf)
|
|
433 {
|
|
434 ngx_http_handler_pt *h;
|
|
435 ngx_http_core_main_conf_t *cmcf;
|
|
436
|
|
437 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
438
|
|
439 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
|
|
440 if (h == NULL) {
|
|
441 return NGX_ERROR;
|
|
442 }
|
|
443
|
|
444 *h = ngx_http_index_handler;
|
|
445
|
|
446 return NGX_OK;
|
|
447 }
|
|
448
|
|
449
|
58
|
450 /* TODO: warn about duplicate indices */
|
|
451
|
|
452 static char *
|
|
453 ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
454 {
|
|
455 ngx_http_index_loc_conf_t *ilcf = conf;
|
|
456
|
230
|
457 ngx_str_t *value;
|
58
|
458 ngx_uint_t i, n;
|
|
459 ngx_http_index_t *index;
|
|
460 ngx_http_script_compile_t sc;
|
|
461
|
|
462 if (ilcf->indices == NULL) {
|
|
463 ilcf->indices = ngx_array_create(cf->pool, 2, sizeof(ngx_http_index_t));
|
|
464 if (ilcf->indices == NULL) {
|
|
465 return NGX_CONF_ERROR;
|
|
466 }
|
|
467 }
|
|
468
|
|
469 value = cf->args->elts;
|
|
470
|
|
471 for (i = 1; i < cf->args->nelts; i++) {
|
|
472 if (value[i].data[0] == '/' && i != cf->args->nelts - 1) {
|
|
473 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
474 "only the last index in \"index\" directive "
|
|
475 "may be absolute");
|
|
476 return NGX_CONF_ERROR;
|
|
477 }
|
|
478
|
|
479 if (value[i].len == 0) {
|
|
480 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
481 "index \"%V\" in \"index\" directive is invalid",
|
|
482 &value[1]);
|
|
483 return NGX_CONF_ERROR;
|
|
484 }
|
|
485
|
|
486 index = ngx_array_push(ilcf->indices);
|
|
487 if (index == NULL) {
|
|
488 return NGX_CONF_ERROR;
|
|
489 }
|
|
490
|
|
491 index->name.len = value[i].len;
|
|
492 index->name.data = value[i].data;
|
|
493 index->lengths = NULL;
|
|
494 index->values = NULL;
|
|
495
|
|
496 n = ngx_http_script_variables_count(&value[i]);
|
|
497
|
|
498 if (n == 0) {
|
230
|
499 if (ilcf->max_index_len < index->name.len) {
|
58
|
500 ilcf->max_index_len = index->name.len;
|
|
501 }
|
|
502
|
122
|
503 /* include the terminating '\0' to the length to use ngx_copy() */
|
106
|
504 index->name.len++;
|
|
505
|
58
|
506 continue;
|
|
507 }
|
|
508
|
|
509 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
510
|
|
511 sc.cf = cf;
|
|
512 sc.source = &value[i];
|
|
513 sc.lengths = &index->lengths;
|
|
514 sc.values = &index->values;
|
|
515 sc.variables = n;
|
|
516 sc.complete_lengths = 1;
|
|
517 sc.complete_values = 1;
|
|
518
|
|
519 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
520 return NGX_CONF_ERROR;
|
|
521 }
|
|
522 }
|
|
523
|
|
524 return NGX_CONF_OK;
|
|
525 }
|