comparison src/http/modules/ngx_http_index_handler.c @ 24:77c7629a2627

nginx-0.0.1-2002-12-10-21:05:12 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 10 Dec 2002 18:05:12 +0000
parents f323b4f74e4a
children 53cb81681040
comparison
equal deleted inserted replaced
23:f540a63026c9 24:77c7629a2627
9 #include <ngx_http_config.h> 9 #include <ngx_http_config.h>
10 #include <ngx_http_index_handler.h> 10 #include <ngx_http_index_handler.h>
11 11
12 12
13 static void *ngx_http_index_create_conf(ngx_pool_t *pool); 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); 14 static void *ngx_http_index_merge_conf(ngx_pool_t *p,
15 void *parent, void *child);
16 static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf,
17 ngx_str_t *value);
15 18
16 static ngx_command_t ngx_http_index_commands[]; 19 static ngx_command_t ngx_http_index_commands[];
17 20
18 21
19 ngx_http_module_t ngx_http_index_module = { 22 ngx_http_module_t ngx_http_index_module = {
20 NGX_HTTP_MODULE, 23 NGX_HTTP_MODULE,
24
21 NULL, /* create server config */ 25 NULL, /* create server config */
22 ngx_http_index_create_conf, /* create location config */ 26 ngx_http_index_create_conf, /* create location config */
23 ngx_http_index_commands, /* module directives */ 27 ngx_http_index_commands, /* module directives */
28
24 NULL, /* init module */ 29 NULL, /* init module */
30 NULL, /* translate handler */
31
25 NULL, /* init output body filter */ 32 NULL, /* init output body filter */
26 }; 33 };
27 34
28 35
29 static ngx_command_t ngx_http_index_commands[] = { 36 static ngx_command_t ngx_http_index_commands[] = {
34 41
35 {NULL} 42 {NULL}
36 43
37 }; 44 };
38 45
46
39 int ngx_http_index_handler(ngx_http_request_t *r) 47 int ngx_http_index_handler(ngx_http_request_t *r)
40 { 48 {
41 int index_len, i; 49 int i;
42 char *name, *loc, *file; 50 char *name, *file;
51 ngx_str_t loc, *index;
43 ngx_err_t err; 52 ngx_err_t err;
44 ngx_fd_t fd; 53 ngx_fd_t fd;
45 54
46 ngx_http_index_file_t *index;
47 ngx_http_index_conf_t *cf; 55 ngx_http_index_conf_t *cf;
48 56
49 cf = (ngx_http_index_conf_t *) 57 cf = (ngx_http_index_conf_t *)
50 ngx_get_module_loc_conf(r, ngx_http_index_module); 58 ngx_get_module_loc_conf(r, ngx_http_index_module);
51 59
52 index_len = (*(r->uri_end - 1) == '/') ? cf->max_index_len : 0;
53
54 ngx_test_null(name, 60 ngx_test_null(name,
55 ngx_palloc(r->pool, r->uri_end - r->uri_start + index_len 61 ngx_palloc(r->pool,
56 + r->server->doc_root_len), 62 r->server->doc_root_len + r->uri.len
63 + cf->max_index_len),
57 NGX_HTTP_INTERNAL_SERVER_ERROR); 64 NGX_HTTP_INTERNAL_SERVER_ERROR);
58 65
59 loc = ngx_cpystrn(name, r->server->doc_root, r->server->doc_root_len); 66 loc.data = ngx_cpystrn(name, r->server->doc_root, r->server->doc_root_len);
60 file = ngx_cpystrn(loc, r->uri_start, r->uri_end - r->uri_start + 1); 67 file = ngx_cpystrn(loc.data, r->uri.data, r->uri.len + 1);
61 68
62 index = (ngx_http_index_file_t *) cf->indices->elts; 69 index = (ngx_str_t *) cf->indices->elts;
63 for (i = 0; i < cf->indices->nelts; i++) { 70 for (i = 0; i < cf->indices->nelts; i++) {
64 ngx_memcpy(file, index[i].name, index[i].len); 71 ngx_memcpy(file, index[i].data, index[i].len + 1);
65 72
66 fd = ngx_open_file(name, NGX_FILE_RDONLY); 73 fd = ngx_open_file(name, NGX_FILE_RDONLY);
67 if (fd == -1) { 74 if (fd == -1) {
68 err = ngx_errno; 75 err = ngx_errno;
69 if (err == NGX_ENOENT) 76 if (err == NGX_ENOENT)
73 ngx_open_file_n " %s failed", name); 80 ngx_open_file_n " %s failed", name);
74 81
75 return NGX_HTTP_INTERNAL_SERVER_ERROR; 82 return NGX_HTTP_INTERNAL_SERVER_ERROR;
76 } 83 }
77 84
78 r->filename = name; 85 r->filename.len = r->server->doc_root_len + r->uri.len + index[i].len;
86 r->filename.data = name;
79 r->fd = fd; 87 r->fd = fd;
80 88
89 loc.len = r->uri.len + index[i].len;
81 return ngx_http_internal_redirect(r, loc); 90 return ngx_http_internal_redirect(r, loc);
82 } 91 }
83 92
84 return NGX_DECLINED; 93 return NGX_DECLINED;
85 } 94 }
95
86 96
87 static void *ngx_http_index_create_conf(ngx_pool_t *pool) 97 static void *ngx_http_index_create_conf(ngx_pool_t *pool)
88 { 98 {
89 ngx_http_index_conf_t *conf; 99 ngx_http_index_conf_t *conf;
90 100
91 ngx_test_null(conf, ngx_pcalloc(pool, sizeof(ngx_http_index_conf_t)), NULL); 101 ngx_test_null(conf, ngx_pcalloc(pool, sizeof(ngx_http_index_conf_t)), NULL);
92 102
93 ngx_test_null(conf->indices, 103 ngx_test_null(conf->indices,
94 ngx_create_array(pool, sizeof(ngx_http_index_file_t), 3), 104 ngx_create_array(pool, sizeof(ngx_str_t), 3),
95 NULL); 105 NULL);
96 106
97 return conf; 107 return conf;
98 } 108 }
99 109
110
100 static void *ngx_http_index_merge_conf(ngx_pool_t *p, void *parent, void *child) 111 static void *ngx_http_index_merge_conf(ngx_pool_t *p, void *parent, void *child)
101 { 112 {
102 ngx_http_index_conf_t *prev = (ngx_http_index_conf_t *) parent; 113 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; 114 ngx_http_index_conf_t *conf = (ngx_http_index_conf_t *) child;
104 ngx_http_index_file_t *index; 115 ngx_str_t *index;
105 116
106 if (conf->max_index_len == 0) { 117 if (conf->max_index_len == 0) {
107 if (prev->max_index_len != 0) 118 if (prev->max_index_len != 0)
108 return prev; 119 return prev;
109 120
110 ngx_test_null(index, ngx_push_array(conf->indices), NULL); 121 ngx_test_null(index, ngx_push_array(conf->indices), NULL);
111 index->name = NGX_HTTP_INDEX; 122 index->len = sizeof(NGX_HTTP_INDEX) - 1;
112 conf->max_index_len = index->len = sizeof(NGX_HTTP_INDEX) + 1; 123 index->data = NGX_HTTP_INDEX;
124 conf->max_index_len = sizeof(NGX_HTTP_INDEX);
113 } 125 }
114 126
115 return conf; 127 return conf;
116 } 128 }
117 129
118 static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf, char *value) 130
131 static char *ngx_http_index_set_index(ngx_pool_t *p, void *conf,
132 ngx_str_t *value)
119 { 133 {
120 ngx_http_index_conf_t *cf = (ngx_http_index_conf_t *) conf; 134 ngx_http_index_conf_t *cf = (ngx_http_index_conf_t *) conf;
121 ngx_http_index_file_t *index; 135 ngx_str_t *index;
122 136
123 ngx_test_null(index, ngx_push_array(cf->indices), NULL); 137 ngx_test_null(index, ngx_push_array(cf->indices), NULL);
124 index->name = value; 138 index->len = value->len;
125 index->len = strlen(value) + 1; 139 index->data = value->data;
126 140
127 if (cf->max_index_len < index->len) 141 if (cf->max_index_len < index->len)
128 cf->max_index_len = index->len; 142 cf->max_index_len = index->len;
129 143
130 return NULL; 144 return NULL;