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