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