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