Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_autoindex_module.c @ 3525:eb156d98a9fa
fix delay in limit_req
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 24 May 2010 07:43:39 +0000 |
parents | dd1570b6f237 |
children | 84905c7b2aa7 |
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; |
2889
0bb8c54f4c45
refactor ngx_http_charset_header_filter()
Igor Sysoev <igor@sysoev.ru>
parents:
2849
diff
changeset
|
148 ngx_uint_t i, level, utf8; |
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; |
3516
dd1570b6f237
ngx_str_set() and ngx_str_null()
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
238 ngx_str_set(&r->headers_out.content_type, "text/html"); |
523 | 239 |
240 rc = ngx_http_send_header(r); | |
241 | |
242 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
|
243 if (ngx_close_dir(&dir) == NGX_ERROR) { |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
244 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
|
245 ngx_close_dir_n " \"%V\" failed", &path); |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
246 } |
090e391f53db
fix file leak for HEAD requests
Igor Sysoev <igor@sysoev.ru>
parents:
842
diff
changeset
|
247 |
523 | 248 return rc; |
249 } | |
250 | |
557 | 251 filename = path.data; |
252 filename[path.len] = '/'; | |
457 | 253 |
2889
0bb8c54f4c45
refactor ngx_http_charset_header_filter()
Igor Sysoev <igor@sysoev.ru>
parents:
2849
diff
changeset
|
254 if (r->headers_out.charset.len == 5 |
0bb8c54f4c45
refactor ngx_http_charset_header_filter()
Igor Sysoev <igor@sysoev.ru>
parents:
2849
diff
changeset
|
255 && ngx_strncasecmp(r->headers_out.charset.data, (u_char *) "utf-8", 5) |
0bb8c54f4c45
refactor ngx_http_charset_header_filter()
Igor Sysoev <igor@sysoev.ru>
parents:
2849
diff
changeset
|
256 == 0) |
0bb8c54f4c45
refactor ngx_http_charset_header_filter()
Igor Sysoev <igor@sysoev.ru>
parents:
2849
diff
changeset
|
257 { |
0bb8c54f4c45
refactor ngx_http_charset_header_filter()
Igor Sysoev <igor@sysoev.ru>
parents:
2849
diff
changeset
|
258 utf8 = 1; |
0bb8c54f4c45
refactor ngx_http_charset_header_filter()
Igor Sysoev <igor@sysoev.ru>
parents:
2849
diff
changeset
|
259 |
0bb8c54f4c45
refactor ngx_http_charset_header_filter()
Igor Sysoev <igor@sysoev.ru>
parents:
2849
diff
changeset
|
260 } else { |
0bb8c54f4c45
refactor ngx_http_charset_header_filter()
Igor Sysoev <igor@sysoev.ru>
parents:
2849
diff
changeset
|
261 utf8 = 0; |
0bb8c54f4c45
refactor ngx_http_charset_header_filter()
Igor Sysoev <igor@sysoev.ru>
parents:
2849
diff
changeset
|
262 } |
0bb8c54f4c45
refactor ngx_http_charset_header_filter()
Igor Sysoev <igor@sysoev.ru>
parents:
2849
diff
changeset
|
263 |
457 | 264 for ( ;; ) { |
265 ngx_set_errno(0); | |
266 | |
267 if (ngx_read_dir(&dir) == NGX_ERROR) { | |
268 err = ngx_errno; | |
269 | |
501 | 270 if (err != NGX_ENOMOREFILES) { |
457 | 271 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, |
557 | 272 ngx_read_dir_n " \"%V\" failed", &path); |
273 return ngx_http_autoindex_error(r, &dir, &path); | |
457 | 274 } |
275 | |
577 | 276 break; |
457 | 277 } |
278 | |
279 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
280 "http autoindex file: \"%s\"", ngx_de_name(&dir)); | |
281 | |
282 len = ngx_de_namelen(&dir); | |
283 | |
547 | 284 if (ngx_de_name(&dir)[0] == '.') { |
459 | 285 continue; |
286 } | |
287 | |
457 | 288 if (!dir.valid_info) { |
289 | |
557 | 290 /* 1 byte for '/' and 1 byte for terminating '\0' */ |
457 | 291 |
557 | 292 if (path.len + 1 + len + 1 > allocated) { |
293 allocated = path.len + 1 + len + 1 | |
294 + NGX_HTTP_AUTOINDEX_PREALLOCATE; | |
295 | |
2049 | 296 filename = ngx_pnalloc(pool, allocated); |
557 | 297 if (filename == NULL) { |
298 return ngx_http_autoindex_error(r, &dir, &path); | |
457 | 299 } |
300 | |
557 | 301 last = ngx_cpystrn(filename, path.data, path.len + 1); |
457 | 302 *last++ = '/'; |
303 } | |
304 | |
305 ngx_cpystrn(last, ngx_de_name(&dir), len + 1); | |
306 | |
557 | 307 if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) { |
497 | 308 err = ngx_errno; |
309 | |
310 if (err != NGX_ENOENT) { | |
311 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, | |
557 | 312 ngx_de_info_n " \"%s\" failed", filename); |
2371
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
313 |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
314 if (err == NGX_EACCES) { |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
315 continue; |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
316 } |
b438ffe54e34
skip protected symlinks in autoindex
Igor Sysoev <igor@sysoev.ru>
parents:
2125
diff
changeset
|
317 |
557 | 318 return ngx_http_autoindex_error(r, &dir, &path); |
497 | 319 } |
320 | |
557 | 321 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) { |
497 | 322 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, |
323 ngx_de_link_info_n " \"%s\" failed", | |
557 | 324 filename); |
325 return ngx_http_autoindex_error(r, &dir, &path); | |
497 | 326 } |
457 | 327 } |
328 } | |
329 | |
501 | 330 entry = ngx_array_push(&entries); |
331 if (entry == NULL) { | |
557 | 332 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 333 } |
334 | |
577 | 335 entry->name.len = len; |
525 | 336 |
2049 | 337 entry->name.data = ngx_pnalloc(pool, len + 1); |
501 | 338 if (entry->name.data == NULL) { |
557 | 339 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 340 } |
461 | 341 |
457 | 342 ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1); |
343 | |
529 | 344 entry->escape = 2 * ngx_escape_uri(NULL, ngx_de_name(&dir), len, |
345 NGX_ESCAPE_HTML); | |
346 | |
2889
0bb8c54f4c45
refactor ngx_http_charset_header_filter()
Igor Sysoev <igor@sysoev.ru>
parents:
2849
diff
changeset
|
347 if (utf8) { |
2125
8e4b9d2acde8
rename ngx_utf_...() to ngx_utf8_...()
Igor Sysoev <igor@sysoev.ru>
parents:
2120
diff
changeset
|
348 entry->utf_len = ngx_utf8_length(entry->name.data, entry->name.len); |
525 | 349 } else { |
350 entry->utf_len = len; | |
351 } | |
352 | |
2849
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
353 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
|
354 |
457 | 355 entry->dir = ngx_de_is_dir(&dir); |
356 entry->mtime = ngx_de_mtime(&dir); | |
357 entry->size = ngx_de_size(&dir); | |
358 } | |
359 | |
360 if (ngx_close_dir(&dir) == NGX_ERROR) { | |
361 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
557 | 362 ngx_close_dir_n " \"%s\" failed", &path); |
457 | 363 } |
364 | |
365 len = sizeof(title) - 1 | |
366 + r->uri.len | |
367 + sizeof(header) - 1 | |
368 + r->uri.len | |
369 + sizeof("</h1>") - 1 | |
370 + sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1 | |
371 + sizeof("</pre><hr>") - 1 | |
372 + sizeof(tail) - 1; | |
373 | |
374 entry = entries.elts; | |
375 for (i = 0; i < entries.nelts; i++) { | |
376 len += sizeof("<a href=\"") - 1 | |
529 | 377 + entry[i].name.len + entry[i].escape |
525 | 378 + 1 /* 1 is for "/" */ |
379 + sizeof("\">") - 1 | |
2849
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
380 + entry[i].name.len - entry[i].utf_len + entry[i].colon * 2 |
525 | 381 + NGX_HTTP_AUTOINDEX_NAME_LEN + sizeof(">") - 2 |
382 + sizeof("</a>") - 1 | |
383 + sizeof(" 28-Sep-1970 12:00 ") - 1 | |
529 | 384 + 20 /* the file size */ |
525 | 385 + 2; |
457 | 386 } |
387 | |
501 | 388 b = ngx_create_temp_buf(r->pool, len); |
389 if (b == NULL) { | |
457 | 390 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
391 } | |
392 | |
393 if (entries.nelts > 1) { | |
394 ngx_qsort(entry, (size_t) entries.nelts, | |
395 sizeof(ngx_http_autoindex_entry_t), | |
396 ngx_http_autoindex_cmp_entries); | |
397 } | |
398 | |
399 b->last = ngx_cpymem(b->last, title, sizeof(title) - 1); | |
400 b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); | |
401 b->last = ngx_cpymem(b->last, header, sizeof(header) - 1); | |
402 b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); | |
403 b->last = ngx_cpymem(b->last, "</h1>", sizeof("</h1>") - 1); | |
404 | |
405 b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF, | |
406 sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1); | |
407 | |
563 | 408 tp = ngx_timeofday(); |
409 | |
457 | 410 for (i = 0; i < entries.nelts; i++) { |
411 b->last = ngx_cpymem(b->last, "<a href=\"", sizeof("<a href=\"") - 1); | |
461 | 412 |
2849
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
413 if (entry[i].colon) { |
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
414 *b->last++ = '.'; |
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
415 *b->last++ = '/'; |
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
416 } |
6a62bed048cd
fix colon in file name for ngx_http_autoindex_module
Igor Sysoev <igor@sysoev.ru>
parents:
2721
diff
changeset
|
417 |
461 | 418 if (entry[i].escape) { |
419 ngx_escape_uri(b->last, entry[i].name.data, entry[i].name.len, | |
420 NGX_ESCAPE_HTML); | |
421 | |
422 b->last += entry[i].name.len + entry[i].escape; | |
423 | |
424 } else { | |
425 b->last = ngx_cpymem(b->last, entry[i].name.data, | |
426 entry[i].name.len); | |
427 } | |
457 | 428 |
429 if (entry[i].dir) { | |
430 *b->last++ = '/'; | |
431 } | |
432 | |
433 *b->last++ = '"'; | |
434 *b->last++ = '>'; | |
435 | |
525 | 436 len = entry[i].utf_len; |
457 | 437 |
2120 | 438 if (entry[i].name.len != len) { |
527 | 439 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
2120 | 440 utf_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3 + 1; |
527 | 441 |
442 } else { | |
2120 | 443 utf_len = NGX_HTTP_AUTOINDEX_NAME_LEN + 1; |
527 | 444 } |
445 | |
2125
8e4b9d2acde8
rename ngx_utf_...() to ngx_utf8_...()
Igor Sysoev <igor@sysoev.ru>
parents:
2120
diff
changeset
|
446 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
|
447 utf_len, entry[i].name.len + 1); |
527 | 448 last = b->last; |
449 | |
450 } else { | |
451 b->last = ngx_cpystrn(b->last, entry[i].name.data, | |
452 NGX_HTTP_AUTOINDEX_NAME_LEN + 1); | |
453 last = b->last - 3; | |
454 } | |
455 | |
457 | 456 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
527 | 457 b->last = ngx_cpymem(last, "..></a>", sizeof("..></a>") - 1); |
457 | 458 |
459 } else { | |
460 if (entry[i].dir && NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) { | |
461 *b->last++ = '/'; | |
462 len++; | |
463 } | |
464 | |
465 b->last = ngx_cpymem(b->last, "</a>", sizeof("</a>") - 1); | |
466 ngx_memset(b->last, ' ', NGX_HTTP_AUTOINDEX_NAME_LEN - len); | |
467 b->last += NGX_HTTP_AUTOINDEX_NAME_LEN - len; | |
468 } | |
469 | |
470 *b->last++ = ' '; | |
471 | |
563 | 472 ngx_gmtime(entry[i].mtime + tp->gmtoff * 60 * alcf->localtime, &tm); |
457 | 473 |
474 b->last = ngx_sprintf(b->last, "%02d-%s-%d %02d:%02d ", | |
475 tm.ngx_tm_mday, | |
476 months[tm.ngx_tm_mon - 1], | |
477 tm.ngx_tm_year, | |
478 tm.ngx_tm_hour, | |
479 tm.ngx_tm_min); | |
480 | |
527 | 481 if (alcf->exact_size) { |
482 if (entry[i].dir) { | |
483 b->last = ngx_cpymem(b->last, " -", | |
484 sizeof(" -") - 1); | |
485 } else { | |
486 b->last = ngx_sprintf(b->last, "%19O", entry[i].size); | |
577 | 487 } |
457 | 488 |
489 } else { | |
527 | 490 if (entry[i].dir) { |
547 | 491 b->last = ngx_cpymem(b->last, " -", |
492 sizeof(" -") - 1); | |
527 | 493 |
494 } else { | |
495 length = entry[i].size; | |
496 | |
497 if (length > 1024 * 1024 * 1024 - 1) { | |
498 size = (ngx_int_t) (length / (1024 * 1024 * 1024)); | |
499 if ((length % (1024 * 1024 * 1024)) | |
500 > (1024 * 1024 * 1024 / 2 - 1)) | |
577 | 501 { |
527 | 502 size++; |
503 } | |
504 scale = 'G'; | |
505 | |
506 } else if (length > 1024 * 1024 - 1) { | |
507 size = (ngx_int_t) (length / (1024 * 1024)); | |
508 if ((length % (1024 * 1024)) > (1024 * 1024 / 2 - 1)) { | |
509 size++; | |
510 } | |
511 scale = 'M'; | |
512 | |
513 } else if (length > 9999) { | |
514 size = (ngx_int_t) (length / 1024); | |
515 if (length % 1024 > 511) { | |
516 size++; | |
517 } | |
518 scale = 'K'; | |
519 | |
520 } else { | |
521 size = (ngx_int_t) length; | |
547 | 522 scale = '\0'; |
527 | 523 } |
524 | |
547 | 525 if (scale) { |
526 b->last = ngx_sprintf(b->last, "%6i%c", size, scale); | |
527 | 527 |
547 | 528 } else { |
529 b->last = ngx_sprintf(b->last, " %6i", size); | |
527 | 530 } |
531 } | |
457 | 532 } |
533 | |
534 *b->last++ = CR; | |
535 *b->last++ = LF; | |
536 } | |
537 | |
459 | 538 /* TODO: free temporary pool */ |
539 | |
457 | 540 b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1); |
541 | |
542 b->last = ngx_cpymem(b->last, tail, sizeof(tail) - 1); | |
543 | |
597 | 544 if (r == r->main) { |
457 | 545 b->last_buf = 1; |
546 } | |
547 | |
509 | 548 b->last_in_chain = 1; |
549 | |
457 | 550 out.buf = b; |
551 out.next = NULL; | |
552 | |
553 return ngx_http_output_filter(r, &out); | |
554 } | |
555 | |
556 | |
503 | 557 static int ngx_libc_cdecl |
499 | 558 ngx_http_autoindex_cmp_entries(const void *one, const void *two) |
457 | 559 { |
560 ngx_http_autoindex_entry_t *first = (ngx_http_autoindex_entry_t *) one; | |
561 ngx_http_autoindex_entry_t *second = (ngx_http_autoindex_entry_t *) two; | |
562 | |
563 if (first->dir && !second->dir) { | |
564 /* move the directories to the start */ | |
565 return -1; | |
566 } | |
567 | |
568 if (!first->dir && second->dir) { | |
569 /* move the directories to the start */ | |
570 return 1; | |
571 } | |
572 | |
573 return (int) ngx_strcmp(first->name.data, second->name.data); | |
574 } | |
575 | |
576 | |
577 #if 0 | |
578 | |
499 | 579 static ngx_buf_t * |
580 ngx_http_autoindex_alloc(ngx_http_autoindex_ctx_t *ctx, size_t size) | |
457 | 581 { |
582 ngx_chain_t *cl; | |
583 | |
584 if (ctx->buf) { | |
585 | |
586 if ((size_t) (ctx->buf->end - ctx->buf->last) >= size) { | |
587 return ctx->buf; | |
588 } | |
589 | |
590 ctx->size += ctx->buf->last - ctx->buf->pos; | |
591 } | |
592 | |
501 | 593 ctx->buf = ngx_create_temp_buf(ctx->pool, ctx->alloc_size); |
594 if (ctx->buf == NULL) { | |
457 | 595 return NULL; |
596 } | |
597 | |
501 | 598 cl = ngx_alloc_chain_link(ctx->pool); |
599 if (cl == NULL) { | |
457 | 600 return NULL; |
601 } | |
602 | |
603 cl->buf = ctx->buf; | |
604 cl->next = NULL; | |
605 | |
606 *ctx->last_out = cl; | |
607 ctx->last_out = &cl->next; | |
608 | |
609 return ctx->buf; | |
610 } | |
611 | |
612 #endif | |
613 | |
614 | |
499 | 615 static ngx_int_t |
557 | 616 ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name) |
457 | 617 { |
618 if (ngx_close_dir(dir) == NGX_ERROR) { | |
619 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
557 | 620 ngx_close_dir_n " \"%V\" failed", name); |
457 | 621 } |
622 | |
623 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
624 } | |
625 | |
626 | |
499 | 627 static void * |
628 ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf) | |
457 | 629 { |
630 ngx_http_autoindex_loc_conf_t *conf; | |
631 | |
632 conf = ngx_palloc(cf->pool, sizeof(ngx_http_autoindex_loc_conf_t)); | |
633 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
|
634 return NULL; |
457 | 635 } |
636 | |
637 conf->enable = NGX_CONF_UNSET; | |
519 | 638 conf->localtime = NGX_CONF_UNSET; |
527 | 639 conf->exact_size = NGX_CONF_UNSET; |
457 | 640 |
641 return conf; | |
642 } | |
643 | |
644 | |
499 | 645 static char * |
646 ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
457 | 647 { |
648 ngx_http_autoindex_loc_conf_t *prev = parent; | |
649 ngx_http_autoindex_loc_conf_t *conf = child; | |
650 | |
651 ngx_conf_merge_value(conf->enable, prev->enable, 0); | |
519 | 652 ngx_conf_merge_value(conf->localtime, prev->localtime, 0); |
527 | 653 ngx_conf_merge_value(conf->exact_size, prev->exact_size, 1); |
457 | 654 |
655 return NGX_CONF_OK; | |
656 } | |
681 | 657 |
658 | |
659 static ngx_int_t | |
660 ngx_http_autoindex_init(ngx_conf_t *cf) | |
661 { | |
662 ngx_http_handler_pt *h; | |
663 ngx_http_core_main_conf_t *cmcf; | |
664 | |
665 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
666 | |
667 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
668 if (h == NULL) { | |
669 return NGX_ERROR; | |
670 } | |
671 | |
672 *h = ngx_http_autoindex_handler; | |
673 | |
674 return NGX_OK; | |
675 } |