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