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