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