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