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