Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_index_module.c @ 879:804514cb0758
bump version to 0.5.0
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 27 Nov 2006 14:34:42 +0000 |
parents | a608349951cf |
children | ff762576da78 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
401
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
401
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
401
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
401
diff
changeset
|
5 |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
98
diff
changeset
|
7 #include <ngx_config.h> |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
98
diff
changeset
|
8 #include <ngx_core.h> |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
98
diff
changeset
|
9 #include <ngx_http.h> |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
98
diff
changeset
|
10 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
98
diff
changeset
|
11 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
98
diff
changeset
|
12 typedef struct { |
509 | 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 */ | |
469 | 21 size_t max_index_len; |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
22 } ngx_http_index_loc_conf_t; |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
98
diff
changeset
|
23 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
98
diff
changeset
|
24 |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
25 typedef struct { |
509 | 26 ngx_uint_t current; |
27 | |
557 | 28 ngx_str_t path; |
509 | 29 ngx_str_t index; |
30 | |
773 | 31 size_t root; |
32 | |
509 | 33 ngx_uint_t tested; /* unsigned tested:1 */ |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
34 } ngx_http_index_ctx_t; |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
35 |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
36 |
293
ec3c049681fd
nginx-0.0.3-2004-03-19-08:25:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
292
diff
changeset
|
37 #define NGX_HTTP_DEFAULT_INDEX "index.html" |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
38 |
10
4f3879d9b6f6
nginx-0.0.1-2002-09-11-19:18:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
1
diff
changeset
|
39 |
199
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
40 static ngx_int_t ngx_http_index_test_dir(ngx_http_request_t *r, |
509 | 41 ngx_http_index_ctx_t *ctx); |
199
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
42 static ngx_int_t ngx_http_index_error(ngx_http_request_t *r, |
509 | 43 ngx_http_index_ctx_t *ctx, ngx_err_t err); |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
44 |
681 | 45 static ngx_int_t ngx_http_index_init(ngx_conf_t *cf); |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
46 static void *ngx_http_index_create_loc_conf(ngx_conf_t *cf); |
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
47 static char *ngx_http_index_merge_loc_conf(ngx_conf_t *cf, |
509 | 48 void *parent, void *child); |
41
59e7c7f30d49
nginx-0.0.1-2002-12-26-19:26:23 import
Igor Sysoev <igor@sysoev.ru>
parents:
32
diff
changeset
|
49 static char *ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, |
509 | 50 void *conf); |
10
4f3879d9b6f6
nginx-0.0.1-2002-09-11-19:18:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
1
diff
changeset
|
51 |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
24
diff
changeset
|
52 |
199
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
53 static ngx_command_t ngx_http_index_commands[] = { |
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
54 |
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
55 { ngx_string("index"), |
509 | 56 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, |
199
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
57 ngx_http_index_set_index, |
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
58 NGX_HTTP_LOC_CONF_OFFSET, |
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
59 0, |
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
60 NULL }, |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
24
diff
changeset
|
61 |
287
35a6a9df2d25
nginx-0.0.2-2004-03-12-19:57:08 import
Igor Sysoev <igor@sysoev.ru>
parents:
257
diff
changeset
|
62 #if (NGX_HTTP_CACHE) |
35a6a9df2d25
nginx-0.0.2-2004-03-12-19:57:08 import
Igor Sysoev <igor@sysoev.ru>
parents:
257
diff
changeset
|
63 |
199
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
64 { ngx_string("index_cache"), |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
65 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE3, |
199
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
66 ngx_http_set_cache_slot, |
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
67 NGX_HTTP_LOC_CONF_OFFSET, |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
68 offsetof(ngx_http_index_loc_conf_t, index_cache), |
199
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
69 NULL }, |
41
59e7c7f30d49
nginx-0.0.1-2002-12-26-19:26:23 import
Igor Sysoev <igor@sysoev.ru>
parents:
32
diff
changeset
|
70 |
287
35a6a9df2d25
nginx-0.0.2-2004-03-12-19:57:08 import
Igor Sysoev <igor@sysoev.ru>
parents:
257
diff
changeset
|
71 #endif |
35a6a9df2d25
nginx-0.0.2-2004-03-12-19:57:08 import
Igor Sysoev <igor@sysoev.ru>
parents:
257
diff
changeset
|
72 |
199
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
73 ngx_null_command |
41
59e7c7f30d49
nginx-0.0.1-2002-12-26-19:26:23 import
Igor Sysoev <igor@sysoev.ru>
parents:
32
diff
changeset
|
74 }; |
59e7c7f30d49
nginx-0.0.1-2002-12-26-19:26:23 import
Igor Sysoev <igor@sysoev.ru>
parents:
32
diff
changeset
|
75 |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
24
diff
changeset
|
76 |
667 | 77 static ngx_http_module_t ngx_http_index_module_ctx = { |
509 | 78 NULL, /* preconfiguration */ |
681 | 79 ngx_http_index_init, /* postconfiguration */ |
177
4db54fdbcbe7
nginx-0.0.1-2003-11-10-20:17:31 import
Igor Sysoev <igor@sysoev.ru>
parents:
148
diff
changeset
|
80 |
91
637625a2acdb
nginx-0.0.1-2003-05-19-20:39:14 import
Igor Sysoev <igor@sysoev.ru>
parents:
90
diff
changeset
|
81 NULL, /* create main configuration */ |
637625a2acdb
nginx-0.0.1-2003-05-19-20:39:14 import
Igor Sysoev <igor@sysoev.ru>
parents:
90
diff
changeset
|
82 NULL, /* init main configuration */ |
69
e43f406e4525
nginx-0.0.1-2003-03-20-19:09:44 import
Igor Sysoev <igor@sysoev.ru>
parents:
59
diff
changeset
|
83 |
91
637625a2acdb
nginx-0.0.1-2003-05-19-20:39:14 import
Igor Sysoev <igor@sysoev.ru>
parents:
90
diff
changeset
|
84 NULL, /* create server configuration */ |
637625a2acdb
nginx-0.0.1-2003-05-19-20:39:14 import
Igor Sysoev <igor@sysoev.ru>
parents:
90
diff
changeset
|
85 NULL, /* merge server configuration */ |
637625a2acdb
nginx-0.0.1-2003-05-19-20:39:14 import
Igor Sysoev <igor@sysoev.ru>
parents:
90
diff
changeset
|
86 |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
87 ngx_http_index_create_loc_conf, /* create location configration */ |
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
88 ngx_http_index_merge_loc_conf /* merge location configration */ |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
24
diff
changeset
|
89 }; |
10
4f3879d9b6f6
nginx-0.0.1-2002-09-11-19:18:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
1
diff
changeset
|
90 |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
91 |
41
59e7c7f30d49
nginx-0.0.1-2002-12-26-19:26:23 import
Igor Sysoev <igor@sysoev.ru>
parents:
32
diff
changeset
|
92 ngx_module_t ngx_http_index_module = { |
509 | 93 NGX_MODULE_V1, |
90
37530da31268
nginx-0.0.1-2003-05-16-19:27:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
89
diff
changeset
|
94 &ngx_http_index_module_ctx, /* module context */ |
10
4f3879d9b6f6
nginx-0.0.1-2002-09-11-19:18:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
1
diff
changeset
|
95 ngx_http_index_commands, /* module directives */ |
96
a23d010f356d
nginx-0.0.1-2003-05-27-16:18:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
91
diff
changeset
|
96 NGX_HTTP_MODULE, /* module type */ |
541 | 97 NULL, /* init master */ |
681 | 98 NULL, /* init module */ |
541 | 99 NULL, /* init process */ |
100 NULL, /* init thread */ | |
101 NULL, /* exit thread */ | |
102 NULL, /* exit process */ | |
103 NULL, /* exit master */ | |
104 NGX_MODULE_V1_PADDING | |
10
4f3879d9b6f6
nginx-0.0.1-2002-09-11-19:18:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
1
diff
changeset
|
105 }; |
4f3879d9b6f6
nginx-0.0.1-2002-09-11-19:18:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
1
diff
changeset
|
106 |
4f3879d9b6f6
nginx-0.0.1-2002-09-11-19:18:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
1
diff
changeset
|
107 |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
108 /* |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
109 * Try to open the first index file before the test of the directory existence |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
110 * because the valid requests should be many more than invalid ones. |
501 | 111 * If open() would fail, then stat() should be more quickly because some data |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
112 * is already cached in the kernel. |
501 | 113 * Besides, Win32 has ERROR_PATH_NOT_FOUND (NGX_ENOTDIR). |
114 * Unix has ENOTDIR error, although it less helpfull - it points only | |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
115 * that path contains the usual file in place of the directory. |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
116 */ |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
117 |
509 | 118 static ngx_int_t |
119 ngx_http_index_handler(ngx_http_request_t *r) | |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
120 { |
557 | 121 u_char *last; |
509 | 122 size_t len; |
123 ngx_fd_t fd; | |
124 ngx_int_t rc; | |
125 ngx_err_t err; | |
557 | 126 ngx_str_t uri; |
509 | 127 ngx_log_t *log; |
128 ngx_uint_t i; | |
129 ngx_http_index_t *index; | |
130 ngx_http_index_ctx_t *ctx; | |
563 | 131 ngx_pool_cleanup_t *cln; |
132 ngx_pool_cleanup_file_t *clnf; | |
509 | 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; | |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
138 |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
139 if (r->uri.data[r->uri.len - 1] != '/') { |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
140 return NGX_DECLINED; |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
141 } |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
142 |
489 | 143 /* TODO: Win32 */ |
144 if (r->zero_in_uri) { | |
145 return NGX_DECLINED; | |
146 } | |
147 | |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
148 log = r->connection->log; |
199
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
149 |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
150 /* |
509 | 151 * we use context because the handler supports an async file opening, |
152 * and may be called several times | |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
153 */ |
199
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
154 |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
155 ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module); |
199
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
156 |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
157 ctx = ngx_http_get_module_ctx(r, ngx_http_index_module); |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
158 if (ctx == NULL) { |
501 | 159 |
160 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_index_ctx_t)); | |
161 if (ctx == NULL) { | |
162 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
163 } | |
164 | |
165 ngx_http_set_ctx(r, ctx, ngx_http_index_module); | |
509 | 166 } |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
167 |
509 | 168 index = ilcf->indices->elts; |
169 for (i = ctx->current; i < ilcf->indices->nelts; i++) { | |
170 | |
171 if (index[i].lengths == NULL) { | |
287
35a6a9df2d25
nginx-0.0.2-2004-03-12-19:57:08 import
Igor Sysoev <igor@sysoev.ru>
parents:
257
diff
changeset
|
172 |
509 | 173 if (index[i].name.data[0] == '/') { |
174 return ngx_http_internal_redirect(r, &index[i].name, &r->args); | |
175 } | |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
176 |
509 | 177 len = ilcf->max_index_len; |
178 ctx->index.len = index[i].name.len; | |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
179 |
509 | 180 } else { |
181 ngx_memzero(&e, sizeof(ngx_http_script_engine_t)); | |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
182 |
509 | 183 e.ip = index[i].lengths->elts; |
184 e.request = r; | |
573 | 185 e.flushed = 1; |
509 | 186 |
563 | 187 /* 1 byte for terminating '\0' */ |
557 | 188 |
563 | 189 len = 1; |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
190 |
509 | 191 while (*(uintptr_t *) e.ip) { |
192 lcode = *(ngx_http_script_len_code_pt *) e.ip; | |
193 len += lcode(&e); | |
194 } | |
44
0e81ac0bb3e2
nginx-0.0.1-2003-01-09-08:36:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
43
diff
changeset
|
195 |
509 | 196 ctx->index.len = len; |
563 | 197 |
198 /* 16 bytes are preallocation */ | |
199 | |
200 len += 16; | |
509 | 201 } |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
202 |
681 | 203 if (len > (size_t) (ctx->path.data + ctx->path.len - ctx->index.data)) { |
557 | 204 |
773 | 205 last = ngx_http_map_uri_to_path(r, &ctx->path, &ctx->root, len); |
557 | 206 if (last == NULL) { |
207 return NGX_ERROR; | |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
208 } |
557 | 209 |
210 ctx->index.data = last; | |
287
35a6a9df2d25
nginx-0.0.2-2004-03-12-19:57:08 import
Igor Sysoev <igor@sysoev.ru>
parents:
257
diff
changeset
|
211 } |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
212 |
509 | 213 if (index[i].values == NULL) { |
557 | 214 |
215 /* index[i].name.len includes the terminating '\0' */ | |
216 | |
217 ngx_memcpy(ctx->index.data, index[i].name.data, index[i].name.len); | |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
218 |
509 | 219 } else { |
220 e.ip = index[i].values->elts; | |
221 e.pos = ctx->index.data; | |
293
ec3c049681fd
nginx-0.0.3-2004-03-19-08:25:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
292
diff
changeset
|
222 |
509 | 223 while (*(uintptr_t *) e.ip) { |
224 code = *(ngx_http_script_code_pt *) e.ip; | |
225 code((ngx_http_script_engine_t *) &e); | |
396
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
356
diff
changeset
|
226 } |
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
356
diff
changeset
|
227 |
509 | 228 if (*ctx->index.data == '/') { |
229 ctx->index.len--; | |
230 return ngx_http_internal_redirect(r, &ctx->index, &r->args); | |
396
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
356
diff
changeset
|
231 } |
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
356
diff
changeset
|
232 |
509 | 233 *e.pos++ = '\0'; |
293
ec3c049681fd
nginx-0.0.3-2004-03-19-08:25:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
292
diff
changeset
|
234 } |
ec3c049681fd
nginx-0.0.3-2004-03-19-08:25:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
292
diff
changeset
|
235 |
557 | 236 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, |
237 "open index \"%s\"", ctx->path.data); | |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
238 |
563 | 239 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t)); |
240 if (cln == NULL) { | |
241 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
242 } | |
243 | |
691
fd8a64f5dd3b
backout O_NOATIME support, it requires CAP_FOWNER capability
Igor Sysoev <igor@sysoev.ru>
parents:
685
diff
changeset
|
244 fd = ngx_open_file(ctx->path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN); |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
245 |
148
5afee0074707
nginx-0.0.1-2003-10-17-00:19:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
144
diff
changeset
|
246 if (fd == (ngx_fd_t) NGX_AGAIN) { |
509 | 247 ctx->current = i; |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
248 return NGX_AGAIN; |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
249 } |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
250 |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
24
diff
changeset
|
251 if (fd == NGX_INVALID_FILE) { |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
252 err = ngx_errno; |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
253 |
400
69e851f83522
nginx-0.0.8-2004-07-26-20:21:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
254 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, err, |
557 | 255 ngx_open_file_n " \"%s\" failed", ctx->path.data); |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
256 |
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
257 if (err == NGX_ENOTDIR) { |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
258 return ngx_http_index_error(r, ctx, err); |
47
d81326c3b21b
nginx-0.0.1-2003-01-15-10:02:27 import
Igor Sysoev <igor@sysoev.ru>
parents:
46
diff
changeset
|
259 |
d81326c3b21b
nginx-0.0.1-2003-01-15-10:02:27 import
Igor Sysoev <igor@sysoev.ru>
parents:
46
diff
changeset
|
260 } else if (err == NGX_EACCES) { |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
261 return ngx_http_index_error(r, ctx, err); |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
262 } |
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
263 |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
264 if (!ctx->tested) { |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
265 rc = ngx_http_index_test_dir(r, ctx); |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
266 |
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
267 if (rc != NGX_OK) { |
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
268 return rc; |
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
269 } |
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
270 |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
271 ctx->tested = 1; |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
272 } |
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
273 |
41
59e7c7f30d49
nginx-0.0.1-2002-12-26-19:26:23 import
Igor Sysoev <igor@sysoev.ru>
parents:
32
diff
changeset
|
274 if (err == NGX_ENOENT) { |
10
4f3879d9b6f6
nginx-0.0.1-2002-09-11-19:18:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
1
diff
changeset
|
275 continue; |
41
59e7c7f30d49
nginx-0.0.1-2002-12-26-19:26:23 import
Igor Sysoev <igor@sysoev.ru>
parents:
32
diff
changeset
|
276 } |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
277 |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
278 ngx_log_error(NGX_LOG_ERR, log, err, |
557 | 279 ngx_open_file_n " \"%s\" failed", ctx->path.data); |
10
4f3879d9b6f6
nginx-0.0.1-2002-09-11-19:18:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
1
diff
changeset
|
280 |
4f3879d9b6f6
nginx-0.0.1-2002-09-11-19:18:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
1
diff
changeset
|
281 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
282 } |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
283 |
563 | 284 cln->handler = ngx_pool_cleanup_file; |
285 clnf = cln->data; | |
45
f1ee46c036a4
nginx-0.0.1-2003-01-10-09:09:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
44
diff
changeset
|
286 |
563 | 287 clnf->fd = fd; |
288 clnf->name = ctx->path.data; | |
289 clnf->log = r->pool->log; | |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
290 |
557 | 291 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
201
267ea1d98683
nginx-0.0.1-2003-11-30-23:03:18 import
Igor Sysoev <igor@sysoev.ru>
parents:
200
diff
changeset
|
292 |
557 | 293 uri.len = r->uri.len + ctx->index.len - 1; |
294 | |
295 if (!clcf->alias) { | |
773 | 296 uri.data = ctx->path.data + ctx->root; |
557 | 297 |
298 } else { | |
299 uri.data = ngx_palloc(r->pool, uri.len); | |
300 if (uri.data == NULL) { | |
301 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
302 } | |
303 | |
573 | 304 last = ngx_copy(uri.data, r->uri.data, r->uri.len); |
557 | 305 ngx_memcpy(last, ctx->index.data, ctx->index.len - 1); |
199
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
306 } |
a65b630b3a66
nginx-0.0.1-2003-11-28-11:40:40 import
Igor Sysoev <igor@sysoev.ru>
parents:
186
diff
changeset
|
307 |
557 | 308 return ngx_http_internal_redirect(r, &uri, &r->args); |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
309 } |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
310 |
10
4f3879d9b6f6
nginx-0.0.1-2002-09-11-19:18:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
1
diff
changeset
|
311 return NGX_DECLINED; |
0
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
312 } |
4eff17414a43
nginx-0.0.1-2002-08-06-20:39:45 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
313 |
24
77c7629a2627
nginx-0.0.1-2002-12-10-21:05:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
11
diff
changeset
|
314 |
509 | 315 static ngx_int_t |
316 ngx_http_index_test_dir(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx) | |
317 { | |
813
a608349951cf
fix alias without trailing slash
Igor Sysoev <igor@sysoev.ru>
parents:
773
diff
changeset
|
318 u_char c; |
a608349951cf
fix alias without trailing slash
Igor Sysoev <igor@sysoev.ru>
parents:
773
diff
changeset
|
319 ngx_uint_t i; |
509 | 320 ngx_err_t err; |
321 ngx_file_info_t fi; | |
322 | |
813
a608349951cf
fix alias without trailing slash
Igor Sysoev <igor@sysoev.ru>
parents:
773
diff
changeset
|
323 c = *(ctx->index.data - 1); |
a608349951cf
fix alias without trailing slash
Igor Sysoev <igor@sysoev.ru>
parents:
773
diff
changeset
|
324 i = (c == '/') ? 1 : 0; |
a608349951cf
fix alias without trailing slash
Igor Sysoev <igor@sysoev.ru>
parents:
773
diff
changeset
|
325 *(ctx->index.data - i) = '\0'; |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
326 |
257
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
202
diff
changeset
|
327 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
557 | 328 "http index check dir: \"%s\"", ctx->path.data); |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
329 |
557 | 330 if (ngx_file_info(ctx->path.data, &fi) == -1) { |
47
d81326c3b21b
nginx-0.0.1-2003-01-15-10:02:27 import
Igor Sysoev <igor@sysoev.ru>
parents:
46
diff
changeset
|
331 |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
332 err = ngx_errno; |
47
d81326c3b21b
nginx-0.0.1-2003-01-15-10:02:27 import
Igor Sysoev <igor@sysoev.ru>
parents:
46
diff
changeset
|
333 |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
334 if (err == NGX_ENOENT) { |
813
a608349951cf
fix alias without trailing slash
Igor Sysoev <igor@sysoev.ru>
parents:
773
diff
changeset
|
335 *(ctx->index.data - i) = c; |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
336 return ngx_http_index_error(r, ctx, err); |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
337 } |
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
338 |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
339 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, |
557 | 340 ngx_file_info_n " \"%s\" failed", ctx->path.data); |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
341 |
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
342 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
343 } |
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
344 |
813
a608349951cf
fix alias without trailing slash
Igor Sysoev <igor@sysoev.ru>
parents:
773
diff
changeset
|
345 *(ctx->index.data - i) = c; |
47
d81326c3b21b
nginx-0.0.1-2003-01-15-10:02:27 import
Igor Sysoev <igor@sysoev.ru>
parents:
46
diff
changeset
|
346 |
509 | 347 if (ngx_is_dir(&fi)) { |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
348 return NGX_OK; |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
349 } |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
350 |
509 | 351 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, |
557 | 352 "\"%s\" is not a directory", ctx->path.data); |
509 | 353 |
354 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
355 } |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
356 |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
357 |
509 | 358 static ngx_int_t |
359 ngx_http_index_error(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx, | |
360 ngx_err_t err) | |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
361 { |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
362 if (err == NGX_EACCES) { |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
363 ngx_log_error(NGX_LOG_ERR, r->connection->log, err, |
557 | 364 "\"%s\" is forbidden", ctx->path.data); |
577 | 365 |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
366 return NGX_HTTP_FORBIDDEN; |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
367 } |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
368 |
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
369 ngx_log_error(NGX_LOG_ERR, r->connection->log, err, |
557 | 370 "\"%s\" is not found", ctx->path.data); |
509 | 371 |
144
ef8c87afcfc5
nginx-0.0.1-2003-10-12-20:49:16 import
Igor Sysoev <igor@sysoev.ru>
parents:
119
diff
changeset
|
372 return NGX_HTTP_NOT_FOUND; |
46
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
373 } |
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
374 |
f84a648211f4
nginx-0.0.1-2003-01-10-20:45:47 import
Igor Sysoev <igor@sysoev.ru>
parents:
45
diff
changeset
|
375 |
509 | 376 static void * |
377 ngx_http_index_create_loc_conf(ngx_conf_t *cf) | |
378 { | |
379 ngx_http_index_loc_conf_t *conf; | |
380 | |
381 conf = ngx_palloc(cf->pool, sizeof(ngx_http_index_loc_conf_t)); | |
382 if (conf == NULL) { | |
383 return NGX_CONF_ERROR; | |
384 } | |
385 | |
386 conf->indices = NULL; | |
681 | 387 conf->max_index_len = 0; |
509 | 388 |
389 return conf; | |
390 } | |
391 | |
392 | |
393 static char * | |
394 ngx_http_index_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
395 { | |
396 ngx_http_index_loc_conf_t *prev = parent; | |
397 ngx_http_index_loc_conf_t *conf = child; | |
398 | |
399 ngx_http_index_t *index; | |
400 | |
401 if (conf->indices == NULL) { | |
402 conf->indices = prev->indices; | |
403 conf->max_index_len = prev->max_index_len; | |
404 } | |
405 | |
406 if (conf->indices == NULL) { | |
407 conf->indices = ngx_array_create(cf->pool, 1, sizeof(ngx_http_index_t)); | |
408 if (conf->indices == NULL) { | |
409 return NGX_CONF_ERROR; | |
410 } | |
411 | |
412 index = ngx_array_push(conf->indices); | |
413 if (index == NULL) { | |
414 return NGX_CONF_ERROR; | |
415 } | |
416 | |
417 index->name.len = sizeof(NGX_HTTP_DEFAULT_INDEX); | |
418 index->name.data = (u_char *) NGX_HTTP_DEFAULT_INDEX; | |
419 index->lengths = NULL; | |
420 index->values = NULL; | |
421 | |
422 conf->max_index_len = sizeof(NGX_HTTP_DEFAULT_INDEX); | |
423 | |
424 return NGX_CONF_OK; | |
425 } | |
426 | |
427 return NGX_CONF_OK; | |
428 } | |
429 | |
430 | |
681 | 431 static ngx_int_t |
432 ngx_http_index_init(ngx_conf_t *cf) | |
433 { | |
434 ngx_http_handler_pt *h; | |
435 ngx_http_core_main_conf_t *cmcf; | |
436 | |
437 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
438 | |
439 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
440 if (h == NULL) { | |
441 return NGX_ERROR; | |
442 } | |
443 | |
444 *h = ngx_http_index_handler; | |
445 | |
446 return NGX_OK; | |
447 } | |
448 | |
449 | |
509 | 450 /* TODO: warn about duplicate indices */ |
451 | |
452 static char * | |
453 ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
454 { | |
455 ngx_http_index_loc_conf_t *ilcf = conf; | |
456 | |
681 | 457 ngx_str_t *value; |
509 | 458 ngx_uint_t i, n; |
459 ngx_http_index_t *index; | |
460 ngx_http_script_compile_t sc; | |
461 | |
462 if (ilcf->indices == NULL) { | |
463 ilcf->indices = ngx_array_create(cf->pool, 2, sizeof(ngx_http_index_t)); | |
464 if (ilcf->indices == NULL) { | |
465 return NGX_CONF_ERROR; | |
466 } | |
467 } | |
468 | |
469 value = cf->args->elts; | |
470 | |
471 for (i = 1; i < cf->args->nelts; i++) { | |
472 if (value[i].data[0] == '/' && i != cf->args->nelts - 1) { | |
473 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
474 "only the last index in \"index\" directive " | |
475 "may be absolute"); | |
476 return NGX_CONF_ERROR; | |
477 } | |
478 | |
479 if (value[i].len == 0) { | |
480 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
481 "index \"%V\" in \"index\" directive is invalid", | |
482 &value[1]); | |
483 return NGX_CONF_ERROR; | |
484 } | |
485 | |
486 index = ngx_array_push(ilcf->indices); | |
487 if (index == NULL) { | |
488 return NGX_CONF_ERROR; | |
489 } | |
490 | |
491 index->name.len = value[i].len; | |
492 index->name.data = value[i].data; | |
493 index->lengths = NULL; | |
494 index->values = NULL; | |
495 | |
496 n = ngx_http_script_variables_count(&value[i]); | |
497 | |
498 if (n == 0) { | |
681 | 499 if (ilcf->max_index_len < index->name.len) { |
509 | 500 ilcf->max_index_len = index->name.len; |
501 } | |
502 | |
573 | 503 /* include the terminating '\0' to the length to use ngx_copy() */ |
557 | 504 index->name.len++; |
505 | |
509 | 506 continue; |
507 } | |
508 | |
509 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
510 | |
511 sc.cf = cf; | |
512 sc.source = &value[i]; | |
513 sc.lengths = &index->lengths; | |
514 sc.values = &index->values; | |
515 sc.variables = n; | |
516 sc.complete_lengths = 1; | |
517 sc.complete_values = 1; | |
518 | |
519 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
520 return NGX_CONF_ERROR; | |
521 } | |
522 } | |
523 | |
524 return NGX_CONF_OK; | |
525 } |