comparison src/http/modules/ngx_http_index_handler.c @ 10:4f3879d9b6f6

nginx-0.0.1-2002-09-11-19:18:33 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 11 Sep 2002 15:18:33 +0000
parents d220029ac7f3
children f323b4f74e4a
comparison
equal deleted inserted replaced
9:6f58641241bb 10:4f3879d9b6f6
1 1
2 #include <ngx_config.h> 2 #include <ngx_config.h>
3 #include <ngx_core.h>
4 #include <ngx_errno.h>
5 #include <ngx_string.h>
6 #include <ngx_files.h>
7 #include <ngx_config_command.h>
8 #include <ngx_http.h>
9 #include <ngx_http_config.h>
10 #include <ngx_http_index_handler.h>
3 11
4 #include <ngx_strings.h>
5 #include <ngx_open.h>
6 #include <ngx_stat.h>
7 12
8 #include <ngx_http.h> 13 static void *ngx_http_index_create_conf(ngx_pool_t *pool);
14 static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf, char *value);
15
16 static ngx_command_t ngx_http_index_commands[];
17
18
19 ngx_http_module_t ngx_http_index_module = {
20 NGX_HTTP_MODULE,
21 NULL, /* create server config */
22 ngx_http_index_create_conf, /* create location config */
23 ngx_http_index_commands, /* module directives */
24 NULL, /* init module */
25 NULL, /* init output body filter */
26 };
27
28
29 static ngx_command_t ngx_http_index_commands[] = {
30
31 {"index", ngx_http_index_set_index, NULL,
32 NGX_HTTP_LOC_CONF, NGX_CONF_ITERATE,
33 "set index files"},
34
35 {NULL}
36
37 };
9 38
10 int ngx_http_index_handler(ngx_http_request_t *r) 39 int ngx_http_index_handler(ngx_http_request_t *r)
11 { 40 {
12 int index_len, err, i; 41 int index_len, i;
13 char *name, *loc, *file 42 char *name, *loc, *file;
14 ngx_file_t fd; 43 ngx_err_t err;
44 ngx_fd_t fd;
15 45
16 ngx_http_index_t *index; 46 ngx_http_index_file_t *index;
17 ngx_http_index_handler_loc_conf_t *cf; 47 ngx_http_index_conf_t *cf;
18 48
19 cf = (ngx_http_index_handler_loc_conf_t *) 49 cf = (ngx_http_index_conf_t *)
20 ngx_get_module_loc_conf(r, &ngx_http_index_handler_module); 50 ngx_get_module_loc_conf(r, ngx_http_index_module);
21 51
22 index_len = (*(r->uri_end - 1) == '/') ? cf->max_index_len : 0; 52 index_len = (*(r->uri_end - 1) == '/') ? cf->max_index_len : 0;
23 53
24 ngx_test_null(name, 54 ngx_test_null(name,
25 ngx_palloc(r->pool, r->uri_end - r->uri_start + index_len 55 ngx_palloc(r->pool, r->uri_end - r->uri_start + index_len
27 NGX_HTTP_INTERNAL_SERVER_ERROR); 57 NGX_HTTP_INTERNAL_SERVER_ERROR);
28 58
29 loc = ngx_cpystrn(name, r->server->doc_root, r->server->doc_root_len); 59 loc = ngx_cpystrn(name, r->server->doc_root, r->server->doc_root_len);
30 file = ngx_cpystrn(loc, r->uri_start, r->uri_end - r->uri_start + 1); 60 file = ngx_cpystrn(loc, r->uri_start, r->uri_end - r->uri_start + 1);
31 61
32 /* URI without / on the end - check directory */ 62 index = (ngx_http_index_file_t *) cf->indices->elts;
33 if (index_len == 0) { 63 for (i = 0; i < cf->indices->nelts; i++) {
64 ngx_memcpy(file, index[i].name, index[i].len);
34 65
35 if (ngx_stat(name, &r->stat) == -1) { 66 fd = ngx_open_file(name, NGX_FILE_RDONLY);
67 if (fd == -1) {
36 err = ngx_errno; 68 err = ngx_errno;
37 ngx_log_error(GX_LOG_ERR, r->connection->log, err, 69 if (err == NGX_ENOENT)
38 "ngx_http_handler: " ngx_stat_n " %s failed", name); 70 continue;
39 71
40 if (err == NGX_ENOENT) 72 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
41 return NGX_HTTP_NOT_FOUND; 73 ngx_open_file_n " %s failed", name);
42 else 74
43 return NGX_HTTP_INTERNAL_SERVER_ERROR; 75 return NGX_HTTP_INTERNAL_SERVER_ERROR;
44 } 76 }
45 77
46 if (ngx_is_dir(r->stat)) { 78 r->filename = name;
47 *file++ = '/'; 79 r->fd = fd;
48 *file = '\0';
49 r->headers_out->location = loc;
50 return NGX_HTTP_MOVED_PERMANENTLY;
51 }
52 80
53 r->file = name; 81 return ngx_http_internal_redirect(r, loc);
54 r->stat_valid = 1;
55
56 return NGX_OK;
57 } 82 }
58 83
59 /* look for index file */ 84 return NGX_DECLINED;
60 index = (ngx_http_index_t *) cf->indices->elts; 85 }
61 for (i = 0; i < cf->indices->nelts; i++) {
62 ngx_memcpy(file, index[i].name; index[i].len);
63 86
64 fd = ngx_open(name, O_RDONLY); 87 static void *ngx_http_index_create_conf(ngx_pool_t *pool)
65 if (fd != -1) { 88 {
66 r->file = name; 89 ngx_http_index_conf_t *conf;
67 r->fd = fd; 90
68 return NGX_OK; 91 ngx_test_null(conf, ngx_pcalloc(pool, sizeof(ngx_http_index_conf_t)), NULL);
69 } 92
93 ngx_test_null(conf->indices,
94 ngx_create_array(pool, sizeof(ngx_http_index_file_t), 3),
95 NULL);
96
97 return conf;
98 }
99
100 static void *ngx_http_index_merge_conf(ngx_pool_t *p, void *parent, void *child)
101 {
102 ngx_http_index_conf_t *prev = (ngx_http_index_conf_t *) parent;
103 ngx_http_index_conf_t *conf = (ngx_http_index_conf_t *) child;
104 ngx_http_index_file_t *index;
105
106 if (conf->max_index_len == 0) {
107 if (prev->max_index_len != 0)
108 return prev;
109
110 ngx_test_null(index, ngx_push_array(conf->indices), NULL);
111 index->name = NGX_HTTP_INDEX;
112 conf->max_index_len = index->len = sizeof(NGX_HTTP_INDEX);
70 } 113 }
71 114
72 return NGX_HTTP_FORBIDDEN; 115 return conf;
73 } 116 }
74 117
75 /* 118 static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf, char *value)
119 {
120 ngx_http_index_conf_t *cf = (ngx_http_index_conf_t *) conf;
121 ngx_http_index_file_t *index;
76 122
77 static void *ngx_create_index_config() 123 ngx_test_null(index, ngx_push_array(cf->indices), NULL);
78 { 124 index->name = value;
79 ngx_http_index_handler_loc_conf_t *cf; 125 index->len = strlen(value);
80 126
81 ngx_check_null(cf, ngx_alloc(p, sizeof(ngx_http_index_handler_loc_conf)), 127 if (cf->max_index_len < index->len)
82 NULL); 128 cf->max_index_len = index->len;
83 129
84 cf->indices = ngx_create_array(p, sizeof(ngx_http_index_t), 5); 130 return NULL;
85 if (cf->indices == NULL)
86 return NULL;
87
88 cf->max_index_len = 0;
89
90 return cf;
91 } 131 }
92
93 static void *ngx_merge_index_config()
94 {
95 if (p->indices->nelts > 0) {
96
97 copy and check dups
98
99 if (c->max_index_len < c->max_index_len)
100 c->max_index_len < c->max_index_len);
101 }
102 }
103
104 static void *ngx_set_index()
105 {
106 if (*conf == NULL) {
107 cf = ngx_create_index_conf();
108 if (cf == NULL)
109 return "can not create config";
110 }
111
112 while (args) {
113 index = ngx_push_array(cf->indices);
114 index->name = arg;
115 index->len = ngx_strlen(arg) + 1;
116
117 if (cf->max_index_len < index->len)
118 cf->max_index_len = index->len;
119 }
120
121 *conf = cf;
122 }
123
124 */