Mercurial > hg > nginx
annotate src/http/modules/ngx_http_autoindex_module.c @ 5943:631dee7bfd4e
Autoindex: rendering code moved to a separate function.
No functional changes.
author | Valentin Bartenev <vbart@nginx.com> |
---|---|
date | Fri, 12 Dec 2014 20:25:28 +0300 |
parents | 2cfc095a607a |
children | 33c08d7e2915 |
rev | line source |
---|---|
457 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
457 | 5 */ |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_http.h> | |
11 | |
12 | |
13 #if 0 | |
14 | |
15 typedef struct { | |
16 ngx_buf_t *buf; | |
17 size_t size; | |
18 ngx_pool_t *pool; | |
19 size_t alloc_size; | |
20 ngx_chain_t **last_out; | |
21 } ngx_http_autoindex_ctx_t; | |
22 | |
23 #endif | |
24 | |
25 | |
26 typedef struct { | |
27 ngx_str_t name; | |
525 | 28 size_t utf_len; |
2849
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
29 size_t escape; |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
30 size_t escape_html; |
2849
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
31 |
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
32 unsigned dir:1; |
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
33 |
457 | 34 time_t mtime; |
35 off_t size; | |
36 } ngx_http_autoindex_entry_t; | |
37 | |
38 | |
39 typedef struct { | |
40 ngx_flag_t enable; | |
519 | 41 ngx_flag_t localtime; |
527 | 42 ngx_flag_t exact_size; |
457 | 43 } ngx_http_autoindex_loc_conf_t; |
44 | |
45 | |
557 | 46 #define NGX_HTTP_AUTOINDEX_PREALLOCATE 50 |
47 | |
48 #define NGX_HTTP_AUTOINDEX_NAME_LEN 50 | |
457 | 49 |
50 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
51 static ngx_buf_t *ngx_http_autoindex_html(ngx_http_request_t *r, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
52 ngx_array_t *entries); |
503 | 53 static int ngx_libc_cdecl ngx_http_autoindex_cmp_entries(const void *one, |
54 const void *two); | |
499 | 55 static ngx_int_t ngx_http_autoindex_error(ngx_http_request_t *r, |
557 | 56 ngx_dir_t *dir, ngx_str_t *name); |
681 | 57 static ngx_int_t ngx_http_autoindex_init(ngx_conf_t *cf); |
457 | 58 static void *ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf); |
59 static char *ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, | |
499 | 60 void *parent, void *child); |
457 | 61 |
62 | |
63 static ngx_command_t ngx_http_autoindex_commands[] = { | |
64 | |
65 { ngx_string("autoindex"), | |
66 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
67 ngx_conf_set_flag_slot, | |
68 NGX_HTTP_LOC_CONF_OFFSET, | |
69 offsetof(ngx_http_autoindex_loc_conf_t, enable), | |
70 NULL }, | |
71 | |
519 | 72 { ngx_string("autoindex_localtime"), |
73 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
74 ngx_conf_set_flag_slot, | |
75 NGX_HTTP_LOC_CONF_OFFSET, | |
76 offsetof(ngx_http_autoindex_loc_conf_t, localtime), | |
77 NULL }, | |
78 | |
527 | 79 { ngx_string("autoindex_exact_size"), |
80 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
81 ngx_conf_set_flag_slot, | |
82 NGX_HTTP_LOC_CONF_OFFSET, | |
83 offsetof(ngx_http_autoindex_loc_conf_t, exact_size), | |
84 NULL }, | |
85 | |
457 | 86 ngx_null_command |
87 }; | |
88 | |
89 | |
667 | 90 static ngx_http_module_t ngx_http_autoindex_module_ctx = { |
509 | 91 NULL, /* preconfiguration */ |
681 | 92 ngx_http_autoindex_init, /* postconfiguration */ |
457 | 93 |
94 NULL, /* create main configuration */ | |
95 NULL, /* init main configuration */ | |
96 | |
97 NULL, /* create server configuration */ | |
98 NULL, /* merge server configuration */ | |
99 | |
4499
778ef9c3fd2d
Fixed spelling in single-line comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
100 ngx_http_autoindex_create_loc_conf, /* create location configuration */ |
778ef9c3fd2d
Fixed spelling in single-line comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
101 ngx_http_autoindex_merge_loc_conf /* merge location configuration */ |
457 | 102 }; |
103 | |
104 | |
105 ngx_module_t ngx_http_autoindex_module = { | |
509 | 106 NGX_MODULE_V1, |
577 | 107 &ngx_http_autoindex_module_ctx, /* module context */ |
457 | 108 ngx_http_autoindex_commands, /* module directives */ |
109 NGX_HTTP_MODULE, /* module type */ | |
541 | 110 NULL, /* init master */ |
681 | 111 NULL, /* init module */ |
541 | 112 NULL, /* init process */ |
113 NULL, /* init thread */ | |
114 NULL, /* exit thread */ | |
115 NULL, /* exit process */ | |
116 NULL, /* exit master */ | |
117 NGX_MODULE_V1_PADDING | |
457 | 118 }; |
119 | |
120 | |
499 | 121 static ngx_int_t |
122 ngx_http_autoindex_handler(ngx_http_request_t *r) | |
457 | 123 { |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
124 u_char *last, *filename; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
125 size_t len, allocated, root; |
457 | 126 ngx_err_t err; |
127 ngx_buf_t *b; | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
128 ngx_int_t rc; |
557 | 129 ngx_str_t path; |
457 | 130 ngx_dir_t dir; |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
131 ngx_uint_t level; |
457 | 132 ngx_pool_t *pool; |
557 | 133 ngx_chain_t out; |
457 | 134 ngx_array_t entries; |
135 ngx_http_autoindex_entry_t *entry; | |
136 ngx_http_autoindex_loc_conf_t *alcf; | |
137 | |
138 if (r->uri.data[r->uri.len - 1] != '/') { | |
139 return NGX_DECLINED; | |
140 } | |
141 | |
645 | 142 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) { |
637 | 143 return NGX_DECLINED; |
144 } | |
145 | |
457 | 146 alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module); |
147 | |
148 if (!alcf->enable) { | |
149 return NGX_DECLINED; | |
150 } | |
151 | |
557 | 152 /* NGX_DIR_MASK_LEN is lesser than NGX_HTTP_AUTOINDEX_PREALLOCATE */ |
457 | 153 |
773 | 154 last = ngx_http_map_uri_to_path(r, &path, &root, |
155 NGX_HTTP_AUTOINDEX_PREALLOCATE); | |
557 | 156 if (last == NULL) { |
157 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
457 | 158 } |
159 | |
557 | 160 allocated = path.len; |
1627 | 161 path.len = last - path.data; |
162 if (path.len > 1) { | |
163 path.len--; | |
164 } | |
557 | 165 path.data[path.len] = '\0'; |
457 | 166 |
167 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
557 | 168 "http autoindex: \"%s\"", path.data); |
457 | 169 |
557 | 170 if (ngx_open_dir(&path, &dir) == NGX_ERROR) { |
457 | 171 err = ngx_errno; |
172 | |
543 | 173 if (err == NGX_ENOENT |
174 || err == NGX_ENOTDIR | |
175 || err == NGX_ENAMETOOLONG) | |
176 { | |
457 | 177 level = NGX_LOG_ERR; |
178 rc = NGX_HTTP_NOT_FOUND; | |
179 | |
180 } else if (err == NGX_EACCES) { | |
181 level = NGX_LOG_ERR; | |
182 rc = NGX_HTTP_FORBIDDEN; | |
183 | |
184 } else { | |
185 level = NGX_LOG_CRIT; | |
186 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
187 } | |
188 | |
189 ngx_log_error(level, r->connection->log, err, | |
557 | 190 ngx_open_dir_n " \"%s\" failed", path.data); |
457 | 191 |
192 return rc; | |
193 } | |
194 | |
195 #if (NGX_SUPPRESS_WARN) | |
557 | 196 |
457 | 197 /* MSVC thinks 'entries' may be used without having been initialized */ |
198 ngx_memzero(&entries, sizeof(ngx_array_t)); | |
557 | 199 |
457 | 200 #endif |
201 | |
557 | 202 /* TODO: pool should be temporary pool */ |
203 pool = r->pool; | |
204 | |
205 if (ngx_array_init(&entries, pool, 40, sizeof(ngx_http_autoindex_entry_t)) | |
206 != NGX_OK) | |
457 | 207 { |
557 | 208 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 209 } |
210 | |
523 | 211 r->headers_out.status = NGX_HTTP_OK; |
842
d8e2613a2b55
charset could not be set for ngx_http_autoindex_module responses
Igor Sysoev <igor@sysoev.ru>
parents:
773
diff
changeset
|
212 r->headers_out.content_type_len = sizeof("text/html") - 1; |
3516
dd1570b6f237
ngx_str_set() and ngx_str_null()
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
213 ngx_str_set(&r->headers_out.content_type, "text/html"); |
5497
2cfc095a607a
Fixed setting of content type in some cases.
Ruslan Ermilov <ru@nginx.com>
parents:
5333
diff
changeset
|
214 r->headers_out.content_type_lowcase = NULL; |
523 | 215 |
216 rc = ngx_http_send_header(r); | |
217 | |
218 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
1499
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
219 if (ngx_close_dir(&dir) == NGX_ERROR) { |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
220 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
221 ngx_close_dir_n " \"%V\" failed", &path); |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
222 } |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
223 |
523 | 224 return rc; |
225 } | |
226 | |
557 | 227 filename = path.data; |
228 filename[path.len] = '/'; | |
457 | 229 |
230 for ( ;; ) { | |
231 ngx_set_errno(0); | |
232 | |
233 if (ngx_read_dir(&dir) == NGX_ERROR) { | |
234 err = ngx_errno; | |
235 | |
501 | 236 if (err != NGX_ENOMOREFILES) { |
457 | 237 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, |
557 | 238 ngx_read_dir_n " \"%V\" failed", &path); |
239 return ngx_http_autoindex_error(r, &dir, &path); | |
457 | 240 } |
241 | |
577 | 242 break; |
457 | 243 } |
244 | |
245 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
246 "http autoindex file: \"%s\"", ngx_de_name(&dir)); | |
247 | |
248 len = ngx_de_namelen(&dir); | |
249 | |
547 | 250 if (ngx_de_name(&dir)[0] == '.') { |
459 | 251 continue; |
252 } | |
253 | |
457 | 254 if (!dir.valid_info) { |
255 | |
557 | 256 /* 1 byte for '/' and 1 byte for terminating '\0' */ |
457 | 257 |
557 | 258 if (path.len + 1 + len + 1 > allocated) { |
259 allocated = path.len + 1 + len + 1 | |
260 + NGX_HTTP_AUTOINDEX_PREALLOCATE; | |
261 | |
2049 | 262 filename = ngx_pnalloc(pool, allocated); |
557 | 263 if (filename == NULL) { |
264 return ngx_http_autoindex_error(r, &dir, &path); | |
457 | 265 } |
266 | |
557 | 267 last = ngx_cpystrn(filename, path.data, path.len + 1); |
457 | 268 *last++ = '/'; |
269 } | |
270 | |
271 ngx_cpystrn(last, ngx_de_name(&dir), len + 1); | |
272 | |
557 | 273 if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) { |
497 | 274 err = ngx_errno; |
275 | |
5333
e8bca8397625
Autoindex: improved ngx_de_info() error handling.
Sergey Kandaurov <pluknet@nginx.com>
parents:
5332
diff
changeset
|
276 if (err != NGX_ENOENT && err != NGX_ELOOP) { |
497 | 277 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, |
557 | 278 ngx_de_info_n " \"%s\" failed", filename); |
2371
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
279 |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
280 if (err == NGX_EACCES) { |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
281 continue; |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
282 } |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
283 |
557 | 284 return ngx_http_autoindex_error(r, &dir, &path); |
497 | 285 } |
286 | |
557 | 287 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) { |
497 | 288 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, |
289 ngx_de_link_info_n " \"%s\" failed", | |
557 | 290 filename); |
291 return ngx_http_autoindex_error(r, &dir, &path); | |
497 | 292 } |
457 | 293 } |
294 } | |
295 | |
501 | 296 entry = ngx_array_push(&entries); |
297 if (entry == NULL) { | |
557 | 298 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 299 } |
300 | |
577 | 301 entry->name.len = len; |
525 | 302 |
2049 | 303 entry->name.data = ngx_pnalloc(pool, len + 1); |
501 | 304 if (entry->name.data == NULL) { |
557 | 305 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 306 } |
461 | 307 |
457 | 308 ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1); |
309 | |
310 entry->dir = ngx_de_is_dir(&dir); | |
311 entry->mtime = ngx_de_mtime(&dir); | |
312 entry->size = ngx_de_size(&dir); | |
313 } | |
314 | |
315 if (ngx_close_dir(&dir) == NGX_ERROR) { | |
316 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
5218 | 317 ngx_close_dir_n " \"%V\" failed", &path); |
457 | 318 } |
319 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
320 if (entries.nelts > 1) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
321 ngx_qsort(entries.elts, (size_t) entries.nelts, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
322 sizeof(ngx_http_autoindex_entry_t), |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
323 ngx_http_autoindex_cmp_entries); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
324 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
325 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
326 b = ngx_http_autoindex_html(r, &entries); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
327 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
328 if (b == NULL) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
329 return NGX_ERROR; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
330 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
331 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
332 /* TODO: free temporary pool */ |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
333 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
334 if (r == r->main) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
335 b->last_buf = 1; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
336 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
337 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
338 b->last_in_chain = 1; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
339 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
340 out.buf = b; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
341 out.next = NULL; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
342 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
343 return ngx_http_output_filter(r, &out); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
344 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
345 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
346 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
347 static ngx_buf_t * |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
348 ngx_http_autoindex_html(ngx_http_request_t *r, ngx_array_t *entries) |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
349 { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
350 u_char *last, scale; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
351 off_t length; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
352 size_t len, char_len, escape_html; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
353 ngx_tm_t tm; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
354 ngx_buf_t *b; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
355 ngx_int_t size; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
356 ngx_uint_t i, utf8; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
357 ngx_time_t *tp; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
358 ngx_http_autoindex_entry_t *entry; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
359 ngx_http_autoindex_loc_conf_t *alcf; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
360 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
361 static u_char title[] = |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
362 "<html>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
363 "<head><title>Index of " |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
364 ; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
365 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
366 static u_char header[] = |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
367 "</title></head>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
368 "<body bgcolor=\"white\">" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
369 "<h1>Index of " |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
370 ; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
371 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
372 static u_char tail[] = |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
373 "</body>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
374 "</html>" CRLF |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
375 ; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
376 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
377 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
378 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
379 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
380 if (r->headers_out.charset.len == 5 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
381 && ngx_strncasecmp(r->headers_out.charset.data, (u_char *) "utf-8", 5) |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
382 == 0) |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
383 { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
384 utf8 = 1; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
385 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
386 } else { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
387 utf8 = 0; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
388 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
389 |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
390 escape_html = ngx_escape_html(NULL, r->uri.data, r->uri.len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
391 |
457 | 392 len = sizeof(title) - 1 |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
393 + r->uri.len + escape_html |
457 | 394 + sizeof(header) - 1 |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
395 + r->uri.len + escape_html |
457 | 396 + sizeof("</h1>") - 1 |
397 + sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1 | |
398 + sizeof("</pre><hr>") - 1 | |
399 + sizeof(tail) - 1; | |
400 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
401 entry = entries->elts; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
402 for (i = 0; i < entries->nelts; i++) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
403 entry[i].escape = 2 * ngx_escape_uri(NULL, entry[i].name.data, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
404 entry[i].name.len, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
405 NGX_ESCAPE_URI_COMPONENT); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
406 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
407 entry[i].escape_html = ngx_escape_html(NULL, entry[i].name.data, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
408 entry[i].name.len); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
409 |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
410 if (utf8) { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
411 entry[i].utf_len = ngx_utf8_length(entry[i].name.data, |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
412 entry[i].name.len); |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
413 } else { |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
414 entry[i].utf_len = entry[i].name.len; |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
415 } |
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
416 |
457 | 417 len += sizeof("<a href=\"") - 1 |
529 | 418 + entry[i].name.len + entry[i].escape |
525 | 419 + 1 /* 1 is for "/" */ |
420 + sizeof("\">") - 1 | |
4192
61e4af19df9f
Autoindex: escape '?' in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
3527
diff
changeset
|
421 + entry[i].name.len - entry[i].utf_len |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
422 + entry[i].escape_html |
525 | 423 + NGX_HTTP_AUTOINDEX_NAME_LEN + sizeof(">") - 2 |
424 + sizeof("</a>") - 1 | |
425 + sizeof(" 28-Sep-1970 12:00 ") - 1 | |
529 | 426 + 20 /* the file size */ |
525 | 427 + 2; |
457 | 428 } |
429 | |
501 | 430 b = ngx_create_temp_buf(r->pool, len); |
431 if (b == NULL) { | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
432 return NULL; |
457 | 433 } |
434 | |
435 b->last = ngx_cpymem(b->last, title, sizeof(title) - 1); | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
436 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
437 if (escape_html) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
438 b->last = (u_char *) ngx_escape_html(b->last, r->uri.data, r->uri.len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
439 b->last = ngx_cpymem(b->last, header, sizeof(header) - 1); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
440 b->last = (u_char *) ngx_escape_html(b->last, r->uri.data, r->uri.len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
441 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
442 } else { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
443 b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
444 b->last = ngx_cpymem(b->last, header, sizeof(header) - 1); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
445 b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
446 } |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
447 |
457 | 448 b->last = ngx_cpymem(b->last, "</h1>", sizeof("</h1>") - 1); |
449 | |
450 b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF, | |
451 sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1); | |
452 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
453 alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module); |
563 | 454 tp = ngx_timeofday(); |
455 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
456 for (i = 0; i < entries->nelts; i++) { |
457 | 457 b->last = ngx_cpymem(b->last, "<a href=\"", sizeof("<a href=\"") - 1); |
461 | 458 |
459 if (entry[i].escape) { | |
460 ngx_escape_uri(b->last, entry[i].name.data, entry[i].name.len, | |
4192
61e4af19df9f
Autoindex: escape '?' in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
3527
diff
changeset
|
461 NGX_ESCAPE_URI_COMPONENT); |
461 | 462 |
463 b->last += entry[i].name.len + entry[i].escape; | |
464 | |
465 } else { | |
466 b->last = ngx_cpymem(b->last, entry[i].name.data, | |
467 entry[i].name.len); | |
468 } | |
457 | 469 |
470 if (entry[i].dir) { | |
471 *b->last++ = '/'; | |
472 } | |
473 | |
474 *b->last++ = '"'; | |
475 *b->last++ = '>'; | |
476 | |
525 | 477 len = entry[i].utf_len; |
457 | 478 |
2120 | 479 if (entry[i].name.len != len) { |
527 | 480 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
481 char_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3 + 1; |
527 | 482 |
483 } else { | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
484 char_len = NGX_HTTP_AUTOINDEX_NAME_LEN + 1; |
527 | 485 } |
486 | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
487 last = b->last; |
2125
8e4b9d2acde8
rename ngx_utf_...() to ngx_utf8_...()
Igor Sysoev <igor@sysoev.ru>
parents:
2120
diff
changeset
|
488 b->last = ngx_utf8_cpystrn(b->last, entry[i].name.data, |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
489 char_len, entry[i].name.len + 1); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
490 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
491 if (entry[i].escape_html) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
492 b->last = (u_char *) ngx_escape_html(last, entry[i].name.data, |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
493 b->last - last); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
494 } |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
495 |
527 | 496 last = b->last; |
497 | |
498 } else { | |
4193
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
499 if (entry[i].escape_html) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
500 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
501 char_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
502 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
503 } else { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
504 char_len = len; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
505 } |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
506 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
507 b->last = (u_char *) ngx_escape_html(b->last, |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
508 entry[i].name.data, char_len); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
509 last = b->last; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
510 |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
511 } else { |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
512 b->last = ngx_cpystrn(b->last, entry[i].name.data, |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
513 NGX_HTTP_AUTOINDEX_NAME_LEN + 1); |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
514 last = b->last - 3; |
63aa6ab94630
Autoindex: escape html in file names.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4192
diff
changeset
|
515 } |
527 | 516 } |
517 | |
457 | 518 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
527 | 519 b->last = ngx_cpymem(last, "..></a>", sizeof("..></a>") - 1); |
457 | 520 |
521 } else { | |
522 if (entry[i].dir && NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) { | |
523 *b->last++ = '/'; | |
524 len++; | |
525 } | |
526 | |
527 b->last = ngx_cpymem(b->last, "</a>", sizeof("</a>") - 1); | |
5069
e9d188e295cf
Fixed false memset warning on Linux with -O3 (ticket #275).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
528 |
e9d188e295cf
Fixed false memset warning on Linux with -O3 (ticket #275).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
529 if (NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) { |
e9d188e295cf
Fixed false memset warning on Linux with -O3 (ticket #275).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
530 ngx_memset(b->last, ' ', NGX_HTTP_AUTOINDEX_NAME_LEN - len); |
e9d188e295cf
Fixed false memset warning on Linux with -O3 (ticket #275).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
531 b->last += NGX_HTTP_AUTOINDEX_NAME_LEN - len; |
e9d188e295cf
Fixed false memset warning on Linux with -O3 (ticket #275).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4499
diff
changeset
|
532 } |
457 | 533 } |
534 | |
535 *b->last++ = ' '; | |
536 | |
563 | 537 ngx_gmtime(entry[i].mtime + tp->gmtoff * 60 * alcf->localtime, &tm); |
457 | 538 |
539 b->last = ngx_sprintf(b->last, "%02d-%s-%d %02d:%02d ", | |
540 tm.ngx_tm_mday, | |
541 months[tm.ngx_tm_mon - 1], | |
542 tm.ngx_tm_year, | |
543 tm.ngx_tm_hour, | |
544 tm.ngx_tm_min); | |
545 | |
527 | 546 if (alcf->exact_size) { |
547 if (entry[i].dir) { | |
548 b->last = ngx_cpymem(b->last, " -", | |
549 sizeof(" -") - 1); | |
550 } else { | |
551 b->last = ngx_sprintf(b->last, "%19O", entry[i].size); | |
577 | 552 } |
457 | 553 |
554 } else { | |
527 | 555 if (entry[i].dir) { |
547 | 556 b->last = ngx_cpymem(b->last, " -", |
557 sizeof(" -") - 1); | |
527 | 558 |
559 } else { | |
560 length = entry[i].size; | |
561 | |
562 if (length > 1024 * 1024 * 1024 - 1) { | |
563 size = (ngx_int_t) (length / (1024 * 1024 * 1024)); | |
564 if ((length % (1024 * 1024 * 1024)) | |
565 > (1024 * 1024 * 1024 / 2 - 1)) | |
577 | 566 { |
527 | 567 size++; |
568 } | |
569 scale = 'G'; | |
570 | |
571 } else if (length > 1024 * 1024 - 1) { | |
572 size = (ngx_int_t) (length / (1024 * 1024)); | |
573 if ((length % (1024 * 1024)) > (1024 * 1024 / 2 - 1)) { | |
574 size++; | |
575 } | |
576 scale = 'M'; | |
577 | |
578 } else if (length > 9999) { | |
579 size = (ngx_int_t) (length / 1024); | |
580 if (length % 1024 > 511) { | |
581 size++; | |
582 } | |
583 scale = 'K'; | |
584 | |
585 } else { | |
586 size = (ngx_int_t) length; | |
547 | 587 scale = '\0'; |
527 | 588 } |
589 | |
547 | 590 if (scale) { |
591 b->last = ngx_sprintf(b->last, "%6i%c", size, scale); | |
527 | 592 |
547 | 593 } else { |
594 b->last = ngx_sprintf(b->last, " %6i", size); | |
527 | 595 } |
596 } | |
457 | 597 } |
598 | |
599 *b->last++ = CR; | |
600 *b->last++ = LF; | |
601 } | |
602 | |
603 b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1); | |
604 | |
605 b->last = ngx_cpymem(b->last, tail, sizeof(tail) - 1); | |
606 | |
5943
631dee7bfd4e
Autoindex: rendering code moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
5497
diff
changeset
|
607 return b; |
457 | 608 } |
609 | |
610 | |
503 | 611 static int ngx_libc_cdecl |
499 | 612 ngx_http_autoindex_cmp_entries(const void *one, const void *two) |
457 | 613 { |
614 ngx_http_autoindex_entry_t *first = (ngx_http_autoindex_entry_t *) one; | |
615 ngx_http_autoindex_entry_t *second = (ngx_http_autoindex_entry_t *) two; | |
616 | |
617 if (first->dir && !second->dir) { | |
618 /* move the directories to the start */ | |
619 return -1; | |
620 } | |
621 | |
622 if (!first->dir && second->dir) { | |
623 /* move the directories to the start */ | |
624 return 1; | |
625 } | |
626 | |
627 return (int) ngx_strcmp(first->name.data, second->name.data); | |
628 } | |
629 | |
630 | |
631 #if 0 | |
632 | |
499 | 633 static ngx_buf_t * |
634 ngx_http_autoindex_alloc(ngx_http_autoindex_ctx_t *ctx, size_t size) | |
457 | 635 { |
636 ngx_chain_t *cl; | |
637 | |
638 if (ctx->buf) { | |
639 | |
640 if ((size_t) (ctx->buf->end - ctx->buf->last) >= size) { | |
641 return ctx->buf; | |
642 } | |
643 | |
644 ctx->size += ctx->buf->last - ctx->buf->pos; | |
645 } | |
646 | |
501 | 647 ctx->buf = ngx_create_temp_buf(ctx->pool, ctx->alloc_size); |
648 if (ctx->buf == NULL) { | |
457 | 649 return NULL; |
650 } | |
651 | |
501 | 652 cl = ngx_alloc_chain_link(ctx->pool); |
653 if (cl == NULL) { | |
457 | 654 return NULL; |
655 } | |
656 | |
657 cl->buf = ctx->buf; | |
658 cl->next = NULL; | |
659 | |
660 *ctx->last_out = cl; | |
661 ctx->last_out = &cl->next; | |
662 | |
663 return ctx->buf; | |
664 } | |
665 | |
666 #endif | |
667 | |
668 | |
499 | 669 static ngx_int_t |
557 | 670 ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name) |
457 | 671 { |
672 if (ngx_close_dir(dir) == NGX_ERROR) { | |
673 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
557 | 674 ngx_close_dir_n " \"%V\" failed", name); |
457 | 675 } |
676 | |
5332
1a9700ef9725
Autoindex: return NGX_ERROR on error if headers were sent.
Sergey Kandaurov <pluknet@nginx.com>
parents:
5218
diff
changeset
|
677 return r->header_sent ? NGX_ERROR : NGX_HTTP_INTERNAL_SERVER_ERROR; |
457 | 678 } |
679 | |
680 | |
499 | 681 static void * |
682 ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf) | |
457 | 683 { |
684 ngx_http_autoindex_loc_conf_t *conf; | |
685 | |
686 conf = ngx_palloc(cf->pool, sizeof(ngx_http_autoindex_loc_conf_t)); | |
687 if (conf == NULL) { | |
2912
c7d57b539248
return NULL instead of NGX_CONF_ERROR on a create conf failure
Igor Sysoev <igor@sysoev.ru>
parents:
2889
diff
changeset
|
688 return NULL; |
457 | 689 } |
690 | |
691 conf->enable = NGX_CONF_UNSET; | |
519 | 692 conf->localtime = NGX_CONF_UNSET; |
527 | 693 conf->exact_size = NGX_CONF_UNSET; |
457 | 694 |
695 return conf; | |
696 } | |
697 | |
698 | |
499 | 699 static char * |
700 ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
457 | 701 { |
702 ngx_http_autoindex_loc_conf_t *prev = parent; | |
703 ngx_http_autoindex_loc_conf_t *conf = child; | |
704 | |
705 ngx_conf_merge_value(conf->enable, prev->enable, 0); | |
519 | 706 ngx_conf_merge_value(conf->localtime, prev->localtime, 0); |
527 | 707 ngx_conf_merge_value(conf->exact_size, prev->exact_size, 1); |
457 | 708 |
709 return NGX_CONF_OK; | |
710 } | |
681 | 711 |
712 | |
713 static ngx_int_t | |
714 ngx_http_autoindex_init(ngx_conf_t *cf) | |
715 { | |
716 ngx_http_handler_pt *h; | |
717 ngx_http_core_main_conf_t *cmcf; | |
718 | |
719 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
720 | |
721 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
722 if (h == NULL) { | |
723 return NGX_ERROR; | |
724 } | |
725 | |
726 *h = ngx_http_autoindex_handler; | |
727 | |
728 return NGX_OK; | |
729 } |