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