Mercurial > hg > nginx
comparison src/http/modules/ngx_http_index_handler.c @ 45:f1ee46c036a4
nginx-0.0.1-2003-01-10-09:09:20 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 10 Jan 2003 06:09:20 +0000 |
parents | 0e81ac0bb3e2 |
children | f84a648211f4 |
comparison
equal
deleted
inserted
replaced
44:0e81ac0bb3e2 | 45:f1ee46c036a4 |
---|---|
11 #include <ngx_http_config.h> | 11 #include <ngx_http_config.h> |
12 #include <ngx_http_core_module.h> | 12 #include <ngx_http_core_module.h> |
13 #include <ngx_http_index_handler.h> | 13 #include <ngx_http_index_handler.h> |
14 | 14 |
15 | 15 |
16 static int ngx_http_index_init(ngx_pool_t *pool); | |
16 static void *ngx_http_index_create_conf(ngx_pool_t *pool); | 17 static void *ngx_http_index_create_conf(ngx_pool_t *pool); |
17 static char *ngx_http_index_merge_conf(ngx_pool_t *p, | 18 static char *ngx_http_index_merge_conf(ngx_pool_t *p, |
18 void *parent, void *child); | 19 void *parent, void *child); |
19 static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, | 20 static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, |
20 char *conf); | 21 char *conf); |
53 ngx_module_t ngx_http_index_module = { | 54 ngx_module_t ngx_http_index_module = { |
54 0, /* module index */ | 55 0, /* module index */ |
55 &ngx_http_index_module_ctx, /* module context */ | 56 &ngx_http_index_module_ctx, /* module context */ |
56 ngx_http_index_commands, /* module directives */ | 57 ngx_http_index_commands, /* module directives */ |
57 NGX_HTTP_MODULE_TYPE, /* module type */ | 58 NGX_HTTP_MODULE_TYPE, /* module type */ |
58 NULL /* init module */ | 59 ngx_http_index_init /* init module */ |
59 }; | 60 }; |
60 | 61 |
61 | 62 |
62 int ngx_http_index_handler(ngx_http_request_t *r) | 63 int ngx_http_index_handler(ngx_http_request_t *r) |
63 { | 64 { |
64 int i; | 65 int i, len; |
65 char *name, *file; | 66 char *name, *file; |
66 ngx_str_t loc, *index; | 67 ngx_str_t loc, *index; |
67 ngx_err_t err; | 68 ngx_err_t err; |
68 ngx_fd_t fd; | 69 ngx_fd_t fd; |
69 | 70 |
74 ngx_http_get_module_loc_conf(r, ngx_http_index_module_ctx); | 75 ngx_http_get_module_loc_conf(r, ngx_http_index_module_ctx); |
75 | 76 |
76 core_cf = (ngx_http_core_loc_conf_t *) | 77 core_cf = (ngx_http_core_loc_conf_t *) |
77 ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx); | 78 ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx); |
78 | 79 |
79 ngx_test_null(name, | 80 ngx_test_null(r->path.data, |
80 ngx_palloc(r->pool, | 81 ngx_palloc(r->pool, |
81 core_cf->doc_root.len + r->uri.len | 82 core_cf->doc_root.len + r->uri.len |
82 + cf->max_index_len), | 83 + cf->max_index_len), |
83 NGX_HTTP_INTERNAL_SERVER_ERROR); | 84 NGX_HTTP_INTERNAL_SERVER_ERROR); |
84 | 85 |
85 loc.data = ngx_cpystrn(name, core_cf->doc_root.data, | 86 loc.data = ngx_cpystrn(r->path.data, core_cf->doc_root.data, |
86 core_cf->doc_root.len + 1); | 87 core_cf->doc_root.len + 1); |
87 file = ngx_cpystrn(loc.data, r->uri.data, r->uri.len + 1); | 88 file = ngx_cpystrn(loc.data, r->uri.data, r->uri.len + 1); |
89 r->path.len = file - r->path.data; | |
88 | 90 |
89 index = (ngx_str_t *) cf->indices->elts; | 91 index = (ngx_str_t *) cf->indices->elts; |
90 for (i = 0; i < cf->indices->nelts; i++) { | 92 for (i = 0; i < cf->indices->nelts; i++) { |
91 ngx_memcpy(file, index[i].data, index[i].len + 1); | 93 |
94 if (index[i].data[0] != '/') { | |
95 if (!r->path_not_found) { | |
96 continue; | |
97 } | |
98 | |
99 ngx_memcpy(file, index[i].data, index[i].len + 1); | |
100 name = r->path.data; | |
101 | |
102 } else { | |
103 name = index[i].data; | |
104 } | |
92 | 105 |
93 fd = ngx_open_file(name, NGX_FILE_RDONLY); | 106 fd = ngx_open_file(name, NGX_FILE_RDONLY); |
94 if (fd == NGX_INVALID_FILE) { | 107 if (fd == NGX_INVALID_FILE) { |
95 err = ngx_errno; | 108 err = ngx_errno; |
96 if (err == NGX_ENOENT) { | 109 if (err == NGX_ENOENT) { |
97 continue; | 110 continue; |
98 } | 111 } |
99 #if (WIN32) | 112 #if (WIN32) |
100 if (err == ERROR_PATH_NOT_FOUND) { | 113 if (err == ERROR_PATH_NOT_FOUND) { |
114 r->path_not_found = 1; | |
115 continue; | |
116 } | |
117 #else | |
118 if (err == NGX_ENOTDIR) { | |
119 r->path_not_found = 1; | |
101 continue; | 120 continue; |
102 } | 121 } |
103 #endif | 122 #endif |
104 | 123 |
105 ngx_log_error(NGX_LOG_ERR, r->connection->log, err, | 124 ngx_log_error(NGX_LOG_ERR, r->connection->log, err, |
106 ngx_open_file_n " %s failed", name); | 125 ngx_open_file_n " %s failed", name); |
107 | 126 |
108 return NGX_HTTP_INTERNAL_SERVER_ERROR; | 127 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
109 } | 128 } |
110 | 129 |
111 r->file.name.len = core_cf->doc_root.len + r->uri.len + index[i].len; | |
112 r->file.name.data = name; | 130 r->file.name.data = name; |
113 r->file.fd = fd; | 131 r->file.fd = fd; |
114 | 132 |
115 loc.len = r->uri.len + index[i].len; | 133 if (index[i].data[0] == '/') { |
134 r->file.name.len = index[i].len; | |
135 loc.len = index[i].len; | |
136 loc.data = index[i].data; | |
137 | |
138 } else { | |
139 loc.len = r->uri.len + index[i].len; | |
140 r->file.name.len = core_cf->doc_root.len + r->uri.len | |
141 + index[i].len; | |
142 } | |
143 | |
116 return ngx_http_internal_redirect(r, loc); | 144 return ngx_http_internal_redirect(r, loc); |
117 } | 145 } |
118 | 146 |
119 return NGX_DECLINED; | 147 return NGX_DECLINED; |
148 } | |
149 | |
150 | |
151 static int ngx_http_index_init(ngx_pool_t *pool) | |
152 { | |
153 ngx_http_handler_pt *h; | |
154 | |
155 ngx_test_null(h, ngx_push_array(&ngx_http_index_handlers), NGX_ERROR); | |
156 | |
157 *h = ngx_http_index_handler; | |
158 | |
159 return NGX_OK; | |
120 } | 160 } |
121 | 161 |
122 | 162 |
123 static void *ngx_http_index_create_conf(ngx_pool_t *pool) | 163 static void *ngx_http_index_create_conf(ngx_pool_t *pool) |
124 { | 164 { |