blob: 80e4d19d9dcab1a6d189460913778d65fc82b28a [file] [log] [blame]
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00001
Igor Sysoev7578ec92003-06-02 15:24:30 +00002#include <ngx_config.h>
3#include <ngx_core.h>
4#include <ngx_http.h>
5
6
7typedef struct {
Igor Sysoev877df632003-11-28 08:40:40 +00008 ngx_array_t indices;
9 size_t max_index_len;
Igor Sysoev865c1502003-11-30 20:03:18 +000010 ngx_http_cache_hash_t *index_cache;
11} ngx_http_index_loc_conf_t;
Igor Sysoev7578ec92003-06-02 15:24:30 +000012
13
Igor Sysoev0a280a32003-10-12 16:49:16 +000014typedef struct {
Igor Sysoev10a543a2004-03-16 07:10:12 +000015 ngx_uint_t index;
16 u_char *last;
Igor Sysoev865c1502003-11-30 20:03:18 +000017 ngx_str_t path;
18 ngx_str_t redirect;
19 ngx_http_cache_t *cache;
20 unsigned tested:1;
Igor Sysoev0a280a32003-10-12 16:49:16 +000021} ngx_http_index_ctx_t;
22
23
Igor Sysoevae02c192004-03-19 05:25:53 +000024#define NGX_HTTP_DEFAULT_INDEX "index.html"
Igor Sysoeve0268b92002-09-11 15:18:33 +000025
26
Igor Sysoev877df632003-11-28 08:40:40 +000027static ngx_int_t ngx_http_index_test_dir(ngx_http_request_t *r,
28 ngx_http_index_ctx_t *ctx);
29static ngx_int_t ngx_http_index_error(ngx_http_request_t *r,
30 ngx_http_index_ctx_t *ctx, ngx_err_t err);
Igor Sysoev0a280a32003-10-12 16:49:16 +000031
Igor Sysoev2f657222004-06-16 15:32:11 +000032static ngx_int_t ngx_http_index_init(ngx_cycle_t *cycle);
Igor Sysoev865c1502003-11-30 20:03:18 +000033static void *ngx_http_index_create_loc_conf(ngx_conf_t *cf);
34static char *ngx_http_index_merge_loc_conf(ngx_conf_t *cf,
Igor Sysoev890fc962003-07-20 21:15:59 +000035 void *parent, void *child);
Igor Sysoev207ed5a2002-12-26 16:26:23 +000036static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoev890fc962003-07-20 21:15:59 +000037 void *conf);
Igor Sysoeve0268b92002-09-11 15:18:33 +000038
Igor Sysoev42feecb2002-12-15 06:25:09 +000039
Igor Sysoev877df632003-11-28 08:40:40 +000040static ngx_command_t ngx_http_index_commands[] = {
Igor Sysoev42feecb2002-12-15 06:25:09 +000041
Igor Sysoev877df632003-11-28 08:40:40 +000042 { ngx_string("index"),
43 NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
44 ngx_http_index_set_index,
45 NGX_HTTP_LOC_CONF_OFFSET,
46 0,
47 NULL },
Igor Sysoev42feecb2002-12-15 06:25:09 +000048
Igor Sysoev67f88e92004-03-12 16:57:08 +000049#if (NGX_HTTP_CACHE)
50
Igor Sysoev877df632003-11-28 08:40:40 +000051 { ngx_string("index_cache"),
Igor Sysoev865c1502003-11-30 20:03:18 +000052 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE3,
Igor Sysoev877df632003-11-28 08:40:40 +000053 ngx_http_set_cache_slot,
54 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev865c1502003-11-30 20:03:18 +000055 offsetof(ngx_http_index_loc_conf_t, index_cache),
Igor Sysoev877df632003-11-28 08:40:40 +000056 NULL },
57
Igor Sysoev67f88e92004-03-12 16:57:08 +000058#endif
59
Igor Sysoev877df632003-11-28 08:40:40 +000060 ngx_null_command
Igor Sysoev42feecb2002-12-15 06:25:09 +000061};
Igor Sysoeve0268b92002-09-11 15:18:33 +000062
63
Igor Sysoev207ed5a2002-12-26 16:26:23 +000064ngx_http_module_t ngx_http_index_module_ctx = {
Igor Sysoev78329332003-11-10 17:17:31 +000065 NULL, /* pre conf */
66
Igor Sysoeva9830112003-05-19 16:39:14 +000067 NULL, /* create main configuration */
68 NULL, /* init main configuration */
Igor Sysoevdc479b42003-03-20 16:09:44 +000069
Igor Sysoeva9830112003-05-19 16:39:14 +000070 NULL, /* create server configuration */
71 NULL, /* merge server configuration */
72
Igor Sysoev865c1502003-11-30 20:03:18 +000073 ngx_http_index_create_loc_conf, /* create location configration */
74 ngx_http_index_merge_loc_conf /* merge location configration */
Igor Sysoev207ed5a2002-12-26 16:26:23 +000075};
76
77
78ngx_module_t ngx_http_index_module = {
Igor Sysoev6253ca12003-05-27 12:18:54 +000079 NGX_MODULE,
Igor Sysoev207ed5a2002-12-26 16:26:23 +000080 &ngx_http_index_module_ctx, /* module context */
81 ngx_http_index_commands, /* module directives */
Igor Sysoev6253ca12003-05-27 12:18:54 +000082 NGX_HTTP_MODULE, /* module type */
Igor Sysoev9d9f58f2003-07-02 18:51:41 +000083 ngx_http_index_init, /* init module */
Igor Sysoev340b03b2003-07-04 15:10:33 +000084 NULL /* init child */
Igor Sysoeve0268b92002-09-11 15:18:33 +000085};
86
87
Igor Sysoeve79c6ac2003-01-10 17:45:47 +000088/*
Igor Sysoev0a280a32003-10-12 16:49:16 +000089 * Try to open the first index file before the test of the directory existence
90 * because the valid requests should be many more than invalid ones.
91 * If open() failed then stat() should be more quickly because some data
92 * is already cached in the kernel.
93 * Besides Win32 has ERROR_PATH_NOT_FOUND (NGX_ENOTDIR).
94 * Unix has ENOTDIR error, although it less helpfull - it shows only
95 * that path contains the usual file in place of the directory.
96 */
Igor Sysoeve79c6ac2003-01-10 17:45:47 +000097
Igor Sysoev2f657222004-06-16 15:32:11 +000098ngx_int_t ngx_http_index_handler(ngx_http_request_t *r)
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000099{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000100 u_char *name;
Igor Sysoev865c1502003-11-30 20:03:18 +0000101 ngx_fd_t fd;
102 ngx_int_t rc;
103 ngx_str_t *index;
104 ngx_err_t err;
105 ngx_log_t *log;
106 ngx_http_index_ctx_t *ctx;
107 ngx_http_core_loc_conf_t *clcf;
108 ngx_http_index_loc_conf_t *ilcf;
Igor Sysoev3646a162004-03-14 20:46:25 +0000109#if (NGX_HTTP_CACHE)
110 uint32_t crc;
111#endif
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000112
Igor Sysoev0a280a32003-10-12 16:49:16 +0000113 if (r->uri.data[r->uri.len - 1] != '/') {
114 return NGX_DECLINED;
115 }
116
Igor Sysoev865c1502003-11-30 20:03:18 +0000117 log = r->connection->log;
118
119 /*
120 * we use context because the handler supports an async file opening
121 * and thus can be called several times
122 */
123
124 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
Igor Sysoev877df632003-11-28 08:40:40 +0000125 ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);
126
Igor Sysoev0a280a32003-10-12 16:49:16 +0000127 ctx = ngx_http_get_module_ctx(r, ngx_http_index_module);
128 if (ctx == NULL) {
129 ngx_http_create_ctx(r, ctx, ngx_http_index_module,
130 sizeof(ngx_http_index_ctx_t),
131 NGX_HTTP_INTERNAL_SERVER_ERROR);
Igor Sysoev0a280a32003-10-12 16:49:16 +0000132
Igor Sysoev67f88e92004-03-12 16:57:08 +0000133#if (NGX_HTTP_CACHE)
134
Igor Sysoev865c1502003-11-30 20:03:18 +0000135 if (ilcf->index_cache) {
136 ctx->cache = ngx_http_cache_get(ilcf->index_cache, NULL,
137 &r->uri, &crc);
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000138
Igor Sysoev865c1502003-11-30 20:03:18 +0000139 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
140 "http index cache get: " PTR_FMT, ctx->cache);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000141
Igor Sysoev4fc368f2003-12-01 16:28:14 +0000142 if (ctx->cache && !ctx->cache->expired) {
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000143
Igor Sysoev865c1502003-11-30 20:03:18 +0000144 ctx->cache->accessed = ngx_cached_time;
145
146 ctx->redirect.len = ctx->cache->data.value.len;
147 ctx->redirect.data = ngx_palloc(r->pool, ctx->redirect.len + 1);
148 if (ctx->redirect.data == NULL) {
149 ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
150 return NGX_HTTP_INTERNAL_SERVER_ERROR;
151 }
152
153 ngx_memcpy(ctx->redirect.data, ctx->cache->data.value.data,
154 ctx->redirect.len + 1);
155 ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
156
157 return ngx_http_internal_redirect(r, &ctx->redirect, NULL);
158 }
Igor Sysoev865c1502003-11-30 20:03:18 +0000159 }
160
Igor Sysoev67f88e92004-03-12 16:57:08 +0000161#endif
162
Igor Sysoevae02c192004-03-19 05:25:53 +0000163 ctx->path.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len
164 + ilcf->max_index_len
165 - clcf->alias * clcf->name.len);
166 if (ctx->path.data == NULL) {
Igor Sysoev865c1502003-11-30 20:03:18 +0000167 return NGX_HTTP_INTERNAL_SERVER_ERROR;
168 }
169
Igor Sysoev10a543a2004-03-16 07:10:12 +0000170 ctx->redirect.data = ngx_cpymem(ctx->path.data, clcf->root.data,
171 clcf->root.len);
Igor Sysoevae02c192004-03-19 05:25:53 +0000172
173 if (clcf->alias) {
174 ctx->last = ngx_cpystrn(ctx->redirect.data,
175 r->uri.data + clcf->name.len,
176 r->uri.len + 1 - clcf->name.len);
177
178 /*
179 * aliases usually have trailling "/",
180 * set it in the start of the possible redirect
181 */
182
183 if (*ctx->redirect.data != '/') {
184 ctx->redirect.data--;
185 }
186
187 } else {
188 ctx->last = ngx_cpystrn(ctx->redirect.data, r->uri.data,
189 r->uri.len + 1);
190 }
Igor Sysoev0a280a32003-10-12 16:49:16 +0000191 }
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000192
Igor Sysoevae02c192004-03-19 05:25:53 +0000193 ctx->path.len = ctx->last - ctx->path.data;
194
Igor Sysoev877df632003-11-28 08:40:40 +0000195 index = ilcf->indices.elts;
196 for (/* void */; ctx->index < ilcf->indices.nelts; ctx->index++) {
Igor Sysoevb2620632003-01-10 06:09:20 +0000197
Igor Sysoev0a280a32003-10-12 16:49:16 +0000198 if (index[ctx->index].data[0] == '/') {
199 name = index[ctx->index].data;
Igor Sysoevb2620632003-01-10 06:09:20 +0000200
201 } else {
Igor Sysoev865c1502003-11-30 20:03:18 +0000202 ngx_memcpy(ctx->last, index[ctx->index].data,
203 index[ctx->index].len + 1);
204 name = ctx->path.data;
Igor Sysoevb2620632003-01-10 06:09:20 +0000205 }
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000206
Igor Sysoev7578ec92003-06-02 15:24:30 +0000207 fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN);
Igor Sysoev0a280a32003-10-12 16:49:16 +0000208
Igor Sysoevd404c972003-10-16 20:19:16 +0000209 if (fd == (ngx_fd_t) NGX_AGAIN) {
Igor Sysoev0a280a32003-10-12 16:49:16 +0000210 return NGX_AGAIN;
211 }
212
Igor Sysoev42feecb2002-12-15 06:25:09 +0000213 if (fd == NGX_INVALID_FILE) {
Igor Sysoeve0268b92002-09-11 15:18:33 +0000214 err = ngx_errno;
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000215
Igor Sysoev865c1502003-11-30 20:03:18 +0000216 ngx_log_error(NGX_LOG_DEBUG, log, err,
217 "debug: " ngx_open_file_n " %s failed", name);
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000218
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000219 if (err == NGX_ENOTDIR) {
Igor Sysoev0a280a32003-10-12 16:49:16 +0000220 return ngx_http_index_error(r, ctx, err);
Igor Sysoevad22e012003-01-15 07:02:27 +0000221
222 } else if (err == NGX_EACCES) {
Igor Sysoev0a280a32003-10-12 16:49:16 +0000223 return ngx_http_index_error(r, ctx, err);
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000224 }
225
Igor Sysoev0a280a32003-10-12 16:49:16 +0000226 if (!ctx->tested) {
227 rc = ngx_http_index_test_dir(r, ctx);
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000228
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000229 if (rc != NGX_OK) {
230 return rc;
231 }
232
Igor Sysoev0a280a32003-10-12 16:49:16 +0000233 ctx->tested = 1;
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000234 }
235
Igor Sysoev207ed5a2002-12-26 16:26:23 +0000236 if (err == NGX_ENOENT) {
Igor Sysoeve0268b92002-09-11 15:18:33 +0000237 continue;
Igor Sysoev207ed5a2002-12-26 16:26:23 +0000238 }
Igor Sysoeve0268b92002-09-11 15:18:33 +0000239
Igor Sysoev865c1502003-11-30 20:03:18 +0000240 ngx_log_error(NGX_LOG_ERR, log, err,
Igor Sysoeve0268b92002-09-11 15:18:33 +0000241 ngx_open_file_n " %s failed", name);
242
243 return NGX_HTTP_INTERNAL_SERVER_ERROR;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000244 }
Igor Sysoeve0268b92002-09-11 15:18:33 +0000245
Igor Sysoev865c1502003-11-30 20:03:18 +0000246
247 /* STUB: open file cache */
248
Igor Sysoev6253ca12003-05-27 12:18:54 +0000249 r->file.name.data = name;
250 r->file.fd = fd;
Igor Sysoeve0268b92002-09-11 15:18:33 +0000251
Igor Sysoev0a280a32003-10-12 16:49:16 +0000252 if (index[ctx->index].data[0] == '/') {
253 r->file.name.len = index[ctx->index].len;
Igor Sysoev865c1502003-11-30 20:03:18 +0000254 ctx->redirect.len = index[ctx->index].len;
255 ctx->redirect.data = index[ctx->index].data;
Igor Sysoevb2620632003-01-10 06:09:20 +0000256
257 } else {
Igor Sysoev865c1502003-11-30 20:03:18 +0000258 ctx->redirect.len = r->uri.len + index[ctx->index].len;
Igor Sysoev10a543a2004-03-16 07:10:12 +0000259 r->file.name.len = clcf->root.len + r->uri.len
Igor Sysoev0a280a32003-10-12 16:49:16 +0000260 + index[ctx->index].len;
Igor Sysoevb2620632003-01-10 06:09:20 +0000261 }
262
Igor Sysoev865c1502003-11-30 20:03:18 +0000263 /**/
Igor Sysoev877df632003-11-28 08:40:40 +0000264
Igor Sysoev865c1502003-11-30 20:03:18 +0000265
Igor Sysoev67f88e92004-03-12 16:57:08 +0000266#if (NGX_HTTP_CACHE)
267
Igor Sysoev865c1502003-11-30 20:03:18 +0000268 if (ilcf->index_cache) {
269
270 if (ctx->cache) {
271 if (ctx->redirect.len == ctx->cache->data.value.len
272 && ngx_memcmp(ctx->cache->data.value.data,
273 ctx->redirect.data, ctx->redirect.len) == 0)
Igor Sysoev877df632003-11-28 08:40:40 +0000274 {
Igor Sysoev865c1502003-11-30 20:03:18 +0000275 ctx->cache->accessed = ngx_cached_time;
276 ctx->cache->updated = ngx_cached_time;
277 ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
Igor Sysoev877df632003-11-28 08:40:40 +0000278
Igor Sysoev865c1502003-11-30 20:03:18 +0000279 return ngx_http_internal_redirect(r, &ctx->redirect, NULL);
Igor Sysoev877df632003-11-28 08:40:40 +0000280 }
281 }
282
Igor Sysoev865c1502003-11-30 20:03:18 +0000283 ctx->redirect.len++;
284 ctx->cache = ngx_http_cache_alloc(ilcf->index_cache, ctx->cache,
285 NULL, &r->uri, crc,
286 &ctx->redirect, log);
287 ctx->redirect.len--;
Igor Sysoev877df632003-11-28 08:40:40 +0000288
Igor Sysoev865c1502003-11-30 20:03:18 +0000289 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
290 "http index cache alloc: " PTR_FMT, ctx->cache);
Igor Sysoev877df632003-11-28 08:40:40 +0000291
Igor Sysoev865c1502003-11-30 20:03:18 +0000292 if (ctx->cache) {
293 ctx->cache->fd = NGX_INVALID_FILE;
294 ctx->cache->accessed = ngx_cached_time;
295 ctx->cache->last_modified = 0;
296 ctx->cache->updated = ngx_cached_time;
Igor Sysoev865c1502003-11-30 20:03:18 +0000297 ctx->cache->memory = 1;
298 ngx_http_cache_unlock(ilcf->index_cache, ctx->cache, log);
Igor Sysoev877df632003-11-28 08:40:40 +0000299 }
300 }
301
Igor Sysoev67f88e92004-03-12 16:57:08 +0000302#endif
303
Igor Sysoev865c1502003-11-30 20:03:18 +0000304 return ngx_http_internal_redirect(r, &ctx->redirect, NULL);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000305 }
306
Igor Sysoeve0268b92002-09-11 15:18:33 +0000307 return NGX_DECLINED;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000308}
309
Igor Sysoevb0869052002-12-10 18:05:12 +0000310
Igor Sysoev877df632003-11-28 08:40:40 +0000311static ngx_int_t ngx_http_index_test_dir(ngx_http_request_t *r,
312 ngx_http_index_ctx_t *ctx)
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000313{
Igor Sysoev0a280a32003-10-12 16:49:16 +0000314 ngx_err_t err;
315
Igor Sysoev865c1502003-11-30 20:03:18 +0000316 ctx->path.data[ctx->path.len - 1] = '\0';
317 ctx->path.data[ctx->path.len] = '\0';
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000318
Igor Sysoev54498db2004-02-11 17:08:49 +0000319 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
320 "http check dir: \"%s\"", ctx->path.data);
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000321
Igor Sysoev865c1502003-11-30 20:03:18 +0000322 if (ngx_file_info(ctx->path.data, &r->file.info) == -1) {
Igor Sysoevad22e012003-01-15 07:02:27 +0000323
Igor Sysoev0a280a32003-10-12 16:49:16 +0000324 err = ngx_errno;
Igor Sysoevad22e012003-01-15 07:02:27 +0000325
Igor Sysoev0a280a32003-10-12 16:49:16 +0000326 if (err == NGX_ENOENT) {
Igor Sysoev865c1502003-11-30 20:03:18 +0000327 ctx->path.data[ctx->path.len - 1] = '/';
Igor Sysoev0a280a32003-10-12 16:49:16 +0000328 return ngx_http_index_error(r, ctx, err);
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000329 }
330
Igor Sysoev0a280a32003-10-12 16:49:16 +0000331 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
Igor Sysoev865c1502003-11-30 20:03:18 +0000332 ngx_file_info_n " %s failed", ctx->path.data);
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000333
334 return NGX_HTTP_INTERNAL_SERVER_ERROR;
335 }
336
Igor Sysoev865c1502003-11-30 20:03:18 +0000337 ctx->path.data[ctx->path.len - 1] = '/';
Igor Sysoevad22e012003-01-15 07:02:27 +0000338
Igor Sysoevf2e676a2003-11-16 21:49:42 +0000339 if (ngx_is_dir(&r->file.info)) {
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000340 return NGX_OK;
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000341 }
Igor Sysoev0a280a32003-10-12 16:49:16 +0000342
343 /* THINK: not reached ??? */
344 return ngx_http_index_error(r, ctx, 0);
345}
346
347
Igor Sysoev877df632003-11-28 08:40:40 +0000348static ngx_int_t ngx_http_index_error(ngx_http_request_t *r,
349 ngx_http_index_ctx_t *ctx, ngx_err_t err)
Igor Sysoev0a280a32003-10-12 16:49:16 +0000350{
351 if (err == NGX_EACCES) {
352 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
Igor Sysoev865c1502003-11-30 20:03:18 +0000353 "\"%s\" is forbidden", ctx->path.data);
Igor Sysoev0a280a32003-10-12 16:49:16 +0000354
355 return NGX_HTTP_FORBIDDEN;
356 }
357
358 ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
Igor Sysoev865c1502003-11-30 20:03:18 +0000359 "\"%s\" is not found", ctx->path.data);
Igor Sysoev0a280a32003-10-12 16:49:16 +0000360 return NGX_HTTP_NOT_FOUND;
Igor Sysoeve79c6ac2003-01-10 17:45:47 +0000361}
362
363
Igor Sysoev2f657222004-06-16 15:32:11 +0000364static ngx_int_t ngx_http_index_init(ngx_cycle_t *cycle)
Igor Sysoevb2620632003-01-10 06:09:20 +0000365{
Igor Sysoev9d9f58f2003-07-02 18:51:41 +0000366 ngx_http_handler_pt *h;
367 ngx_http_conf_ctx_t *ctx;
368 ngx_http_core_main_conf_t *cmcf;
Igor Sysoevb2620632003-01-10 06:09:20 +0000369
Igor Sysoev9d9f58f2003-07-02 18:51:41 +0000370 ctx = (ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index];
371 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
372
Igor Sysoev865c1502003-11-30 20:03:18 +0000373 h = ngx_push_array(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
374 if (h == NULL) {
375 return NGX_ERROR;
376 }
377
Igor Sysoevb2620632003-01-10 06:09:20 +0000378 *h = ngx_http_index_handler;
379
380 return NGX_OK;
381}
382
383
Igor Sysoev865c1502003-11-30 20:03:18 +0000384static void *ngx_http_index_create_loc_conf(ngx_conf_t *cf)
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000385{
Igor Sysoev865c1502003-11-30 20:03:18 +0000386 ngx_http_index_loc_conf_t *conf;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000387
Igor Sysoev865c1502003-11-30 20:03:18 +0000388 ngx_test_null(conf, ngx_palloc(cf->pool, sizeof(ngx_http_index_loc_conf_t)),
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000389 NGX_CONF_ERROR);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000390
Igor Sysoev890fc962003-07-20 21:15:59 +0000391 ngx_init_array(conf->indices, cf->pool, 3, sizeof(ngx_str_t),
392 NGX_CONF_ERROR);
Igor Sysoev6253ca12003-05-27 12:18:54 +0000393 conf->max_index_len = 0;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000394
Igor Sysoev865c1502003-11-30 20:03:18 +0000395 conf->index_cache = NULL;
Igor Sysoev877df632003-11-28 08:40:40 +0000396
Igor Sysoeve0268b92002-09-11 15:18:33 +0000397 return conf;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000398}
399
Igor Sysoevb0869052002-12-10 18:05:12 +0000400
Igor Sysoev6253ca12003-05-27 12:18:54 +0000401/* TODO: remove duplicate indices */
402
Igor Sysoev865c1502003-11-30 20:03:18 +0000403static char *ngx_http_index_merge_loc_conf(ngx_conf_t *cf,
404 void *parent, void *child)
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000405{
Igor Sysoev865c1502003-11-30 20:03:18 +0000406 ngx_http_index_loc_conf_t *prev = parent;
407 ngx_http_index_loc_conf_t *conf = child;
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000408
Igor Sysoev10a543a2004-03-16 07:10:12 +0000409 ngx_uint_t i;
Igor Sysoev6253ca12003-05-27 12:18:54 +0000410 ngx_str_t *index, *prev_index;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000411
Igor Sysoeve0268b92002-09-11 15:18:33 +0000412 if (conf->max_index_len == 0) {
Igor Sysoev207ed5a2002-12-26 16:26:23 +0000413 if (prev->max_index_len != 0) {
Igor Sysoev865c1502003-11-30 20:03:18 +0000414 ngx_memcpy(conf, prev, sizeof(ngx_http_index_loc_conf_t));
Igor Sysoev6253ca12003-05-27 12:18:54 +0000415 return NGX_CONF_OK;
Igor Sysoev207ed5a2002-12-26 16:26:23 +0000416 }
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000417
Igor Sysoev6253ca12003-05-27 12:18:54 +0000418 ngx_test_null(index, ngx_push_array(&conf->indices), NGX_CONF_ERROR);
419 index->len = sizeof(NGX_HTTP_DEFAULT_INDEX) - 1;
Igor Sysoevae02c192004-03-19 05:25:53 +0000420 index->data = (u_char *) NGX_HTTP_DEFAULT_INDEX;
Igor Sysoev6253ca12003-05-27 12:18:54 +0000421 conf->max_index_len = sizeof(NGX_HTTP_DEFAULT_INDEX);
422
423 return NGX_CONF_OK;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000424 }
Igor Sysoeve0268b92002-09-11 15:18:33 +0000425
Igor Sysoev6253ca12003-05-27 12:18:54 +0000426 if (prev->max_index_len != 0) {
427
428 prev_index = prev->indices.elts;
429 for (i = 0; i < prev->indices.nelts; i++) {
430 ngx_test_null(index, ngx_push_array(&conf->indices),
431 NGX_CONF_ERROR);
432 index->len = prev_index[i].len;
433 index->data = prev_index[i].data;
434 }
435 }
436
437 if (conf->max_index_len < prev->max_index_len) {
438 conf->max_index_len = prev->max_index_len;
439 }
440
Igor Sysoev865c1502003-11-30 20:03:18 +0000441 if (conf->index_cache == NULL) {
442 conf->index_cache = prev->index_cache;
Igor Sysoev877df632003-11-28 08:40:40 +0000443 }
444
Igor Sysoev6253ca12003-05-27 12:18:54 +0000445 return NGX_CONF_OK;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000446}
Igor Sysoev6253ca12003-05-27 12:18:54 +0000447
448
Igor Sysoev13933252003-05-29 13:02:09 +0000449/* TODO: warn about duplicate indices */
Igor Sysoevb0869052002-12-10 18:05:12 +0000450
Igor Sysoev207ed5a2002-12-26 16:26:23 +0000451static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoev6253ca12003-05-27 12:18:54 +0000452 void *conf)
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000453{
Igor Sysoev865c1502003-11-30 20:03:18 +0000454 ngx_http_index_loc_conf_t *ilcf = conf;
Igor Sysoev6253ca12003-05-27 12:18:54 +0000455
Igor Sysoev10a543a2004-03-16 07:10:12 +0000456 ngx_uint_t i;
Igor Sysoev207ed5a2002-12-26 16:26:23 +0000457 ngx_str_t *index, *value;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000458
Igor Sysoev6253ca12003-05-27 12:18:54 +0000459 value = cf->args->elts;
460
Igor Sysoev877df632003-11-28 08:40:40 +0000461 if (value[1].data[0] == '/' && ilcf->indices.nelts == 0) {
Igor Sysoev8e1fbe62003-07-18 14:44:05 +0000462 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
463 "first index \"%s\" in \"%s\" directive "
464 "must not be absolute",
465 value[1].data, cmd->name.data);
466 return NGX_CONF_ERROR;
Igor Sysoev6253ca12003-05-27 12:18:54 +0000467 }
468
Igor Sysoev207ed5a2002-12-26 16:26:23 +0000469 for (i = 1; i < cf->args->nelts; i++) {
Igor Sysoev6253ca12003-05-27 12:18:54 +0000470 if (value[i].len == 0) {
Igor Sysoev8e1fbe62003-07-18 14:44:05 +0000471 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
472 "index \"%s\" in \"%s\" directive is invalid",
473 value[1].data, cmd->name.data);
474 return NGX_CONF_ERROR;
Igor Sysoev6253ca12003-05-27 12:18:54 +0000475 }
476
Igor Sysoev877df632003-11-28 08:40:40 +0000477 ngx_test_null(index, ngx_push_array(&ilcf->indices), NGX_CONF_ERROR);
Igor Sysoev207ed5a2002-12-26 16:26:23 +0000478 index->len = value[i].len;
479 index->data = value[i].data;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000480
Igor Sysoev877df632003-11-28 08:40:40 +0000481 if (ilcf->max_index_len < index->len + 1) {
482 ilcf->max_index_len = index->len + 1;
Igor Sysoev207ed5a2002-12-26 16:26:23 +0000483 }
484 }
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000485
Igor Sysoev13933252003-05-29 13:02:09 +0000486 return NGX_CONF_OK;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000487}