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