Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_autoindex_module.c @ 879:804514cb0758
bump version to 0.5.0
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 27 Nov 2006 14:34:42 +0000 |
parents | d8e2613a2b55 |
children | 090e391f53db |
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; |
773 | 138 size_t len, copy, 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; |
184 path.len = last - path.data - 1; | |
185 path.data[path.len] = '\0'; | |
457 | 186 |
187 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
557 | 188 "http autoindex: \"%s\"", path.data); |
457 | 189 |
557 | 190 if (ngx_open_dir(&path, &dir) == NGX_ERROR) { |
457 | 191 err = ngx_errno; |
192 | |
543 | 193 if (err == NGX_ENOENT |
194 || err == NGX_ENOTDIR | |
195 || err == NGX_ENAMETOOLONG) | |
196 { | |
457 | 197 level = NGX_LOG_ERR; |
198 rc = NGX_HTTP_NOT_FOUND; | |
199 | |
200 } else if (err == NGX_EACCES) { | |
201 level = NGX_LOG_ERR; | |
202 rc = NGX_HTTP_FORBIDDEN; | |
203 | |
204 } else { | |
205 level = NGX_LOG_CRIT; | |
206 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
207 } | |
208 | |
209 ngx_log_error(level, r->connection->log, err, | |
557 | 210 ngx_open_dir_n " \"%s\" failed", path.data); |
457 | 211 |
212 return rc; | |
213 } | |
214 | |
215 #if (NGX_SUPPRESS_WARN) | |
557 | 216 |
457 | 217 /* MSVC thinks 'entries' may be used without having been initialized */ |
218 ngx_memzero(&entries, sizeof(ngx_array_t)); | |
557 | 219 |
457 | 220 #endif |
221 | |
557 | 222 /* TODO: pool should be temporary pool */ |
223 pool = r->pool; | |
224 | |
225 if (ngx_array_init(&entries, pool, 40, sizeof(ngx_http_autoindex_entry_t)) | |
226 != NGX_OK) | |
457 | 227 { |
557 | 228 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 229 } |
230 | |
523 | 231 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
|
232 r->headers_out.content_type_len = sizeof("text/html") - 1; |
523 | 233 r->headers_out.content_type.len = sizeof("text/html") - 1; |
234 r->headers_out.content_type.data = (u_char *) "text/html"; | |
235 | |
236 rc = ngx_http_send_header(r); | |
237 | |
238 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
239 return rc; | |
240 } | |
241 | |
557 | 242 filename = path.data; |
243 filename[path.len] = '/'; | |
457 | 244 |
245 for ( ;; ) { | |
246 ngx_set_errno(0); | |
247 | |
248 if (ngx_read_dir(&dir) == NGX_ERROR) { | |
249 err = ngx_errno; | |
250 | |
501 | 251 if (err != NGX_ENOMOREFILES) { |
457 | 252 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, |
557 | 253 ngx_read_dir_n " \"%V\" failed", &path); |
254 return ngx_http_autoindex_error(r, &dir, &path); | |
457 | 255 } |
256 | |
577 | 257 break; |
457 | 258 } |
259 | |
260 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
261 "http autoindex file: \"%s\"", ngx_de_name(&dir)); | |
262 | |
263 len = ngx_de_namelen(&dir); | |
264 | |
547 | 265 if (ngx_de_name(&dir)[0] == '.') { |
459 | 266 continue; |
267 } | |
268 | |
457 | 269 if (!dir.valid_info) { |
270 | |
557 | 271 /* 1 byte for '/' and 1 byte for terminating '\0' */ |
457 | 272 |
557 | 273 if (path.len + 1 + len + 1 > allocated) { |
274 allocated = path.len + 1 + len + 1 | |
275 + NGX_HTTP_AUTOINDEX_PREALLOCATE; | |
276 | |
277 filename = ngx_palloc(pool, allocated); | |
278 if (filename == NULL) { | |
279 return ngx_http_autoindex_error(r, &dir, &path); | |
457 | 280 } |
281 | |
557 | 282 last = ngx_cpystrn(filename, path.data, path.len + 1); |
457 | 283 *last++ = '/'; |
284 } | |
285 | |
286 ngx_cpystrn(last, ngx_de_name(&dir), len + 1); | |
287 | |
557 | 288 if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) { |
497 | 289 err = ngx_errno; |
290 | |
291 if (err != NGX_ENOENT) { | |
292 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, | |
557 | 293 ngx_de_info_n " \"%s\" failed", filename); |
294 return ngx_http_autoindex_error(r, &dir, &path); | |
497 | 295 } |
296 | |
557 | 297 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) { |
497 | 298 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, |
299 ngx_de_link_info_n " \"%s\" failed", | |
557 | 300 filename); |
301 return ngx_http_autoindex_error(r, &dir, &path); | |
497 | 302 } |
457 | 303 } |
304 } | |
305 | |
501 | 306 entry = ngx_array_push(&entries); |
307 if (entry == NULL) { | |
557 | 308 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 309 } |
310 | |
577 | 311 entry->name.len = len; |
525 | 312 |
529 | 313 entry->name.data = ngx_palloc(pool, len + 1); |
501 | 314 if (entry->name.data == NULL) { |
557 | 315 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 316 } |
461 | 317 |
457 | 318 ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1); |
319 | |
529 | 320 entry->escape = 2 * ngx_escape_uri(NULL, ngx_de_name(&dir), len, |
321 NGX_ESCAPE_HTML); | |
322 | |
525 | 323 if (r->utf8) { |
657 | 324 entry->utf_len = ngx_utf_length(entry->name.data, entry->name.len); |
525 | 325 } else { |
326 entry->utf_len = len; | |
327 } | |
328 | |
457 | 329 entry->dir = ngx_de_is_dir(&dir); |
330 entry->mtime = ngx_de_mtime(&dir); | |
331 entry->size = ngx_de_size(&dir); | |
332 } | |
333 | |
334 if (ngx_close_dir(&dir) == NGX_ERROR) { | |
335 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
557 | 336 ngx_close_dir_n " \"%s\" failed", &path); |
457 | 337 } |
338 | |
339 len = sizeof(title) - 1 | |
340 + r->uri.len | |
341 + sizeof(header) - 1 | |
342 + r->uri.len | |
343 + sizeof("</h1>") - 1 | |
344 + sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1 | |
345 + sizeof("</pre><hr>") - 1 | |
346 + sizeof(tail) - 1; | |
347 | |
348 entry = entries.elts; | |
349 for (i = 0; i < entries.nelts; i++) { | |
350 len += sizeof("<a href=\"") - 1 | |
529 | 351 + entry[i].name.len + entry[i].escape |
525 | 352 + 1 /* 1 is for "/" */ |
353 + sizeof("\">") - 1 | |
354 + entry[i].name.len - entry[i].utf_len | |
355 + NGX_HTTP_AUTOINDEX_NAME_LEN + sizeof(">") - 2 | |
356 + sizeof("</a>") - 1 | |
357 + sizeof(" 28-Sep-1970 12:00 ") - 1 | |
529 | 358 + 20 /* the file size */ |
525 | 359 + 2; |
457 | 360 } |
361 | |
501 | 362 b = ngx_create_temp_buf(r->pool, len); |
363 if (b == NULL) { | |
457 | 364 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
365 } | |
366 | |
367 if (entries.nelts > 1) { | |
368 ngx_qsort(entry, (size_t) entries.nelts, | |
369 sizeof(ngx_http_autoindex_entry_t), | |
370 ngx_http_autoindex_cmp_entries); | |
371 } | |
372 | |
373 b->last = ngx_cpymem(b->last, title, sizeof(title) - 1); | |
374 b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); | |
375 b->last = ngx_cpymem(b->last, header, sizeof(header) - 1); | |
376 b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); | |
377 b->last = ngx_cpymem(b->last, "</h1>", sizeof("</h1>") - 1); | |
378 | |
379 b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF, | |
380 sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1); | |
381 | |
563 | 382 tp = ngx_timeofday(); |
383 | |
457 | 384 for (i = 0; i < entries.nelts; i++) { |
385 b->last = ngx_cpymem(b->last, "<a href=\"", sizeof("<a href=\"") - 1); | |
461 | 386 |
387 if (entry[i].escape) { | |
388 ngx_escape_uri(b->last, entry[i].name.data, entry[i].name.len, | |
389 NGX_ESCAPE_HTML); | |
390 | |
391 b->last += entry[i].name.len + entry[i].escape; | |
392 | |
393 } else { | |
394 b->last = ngx_cpymem(b->last, entry[i].name.data, | |
395 entry[i].name.len); | |
396 } | |
457 | 397 |
398 if (entry[i].dir) { | |
399 *b->last++ = '/'; | |
400 } | |
401 | |
402 *b->last++ = '"'; | |
403 *b->last++ = '>'; | |
404 | |
525 | 405 len = entry[i].utf_len; |
457 | 406 |
529 | 407 if (entry[i].name.len - len) { |
527 | 408 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
409 copy = NGX_HTTP_AUTOINDEX_NAME_LEN - 3 + 1; | |
410 | |
411 } else { | |
412 copy = NGX_HTTP_AUTOINDEX_NAME_LEN + 1; | |
413 } | |
414 | |
415 b->last = ngx_utf_cpystrn(b->last, entry[i].name.data, copy); | |
416 last = b->last; | |
417 | |
418 } else { | |
419 b->last = ngx_cpystrn(b->last, entry[i].name.data, | |
420 NGX_HTTP_AUTOINDEX_NAME_LEN + 1); | |
421 last = b->last - 3; | |
422 } | |
423 | |
457 | 424 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
527 | 425 b->last = ngx_cpymem(last, "..></a>", sizeof("..></a>") - 1); |
457 | 426 |
427 } else { | |
428 if (entry[i].dir && NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) { | |
429 *b->last++ = '/'; | |
430 len++; | |
431 } | |
432 | |
433 b->last = ngx_cpymem(b->last, "</a>", sizeof("</a>") - 1); | |
434 ngx_memset(b->last, ' ', NGX_HTTP_AUTOINDEX_NAME_LEN - len); | |
435 b->last += NGX_HTTP_AUTOINDEX_NAME_LEN - len; | |
436 } | |
437 | |
438 *b->last++ = ' '; | |
439 | |
563 | 440 ngx_gmtime(entry[i].mtime + tp->gmtoff * 60 * alcf->localtime, &tm); |
457 | 441 |
442 b->last = ngx_sprintf(b->last, "%02d-%s-%d %02d:%02d ", | |
443 tm.ngx_tm_mday, | |
444 months[tm.ngx_tm_mon - 1], | |
445 tm.ngx_tm_year, | |
446 tm.ngx_tm_hour, | |
447 tm.ngx_tm_min); | |
448 | |
527 | 449 if (alcf->exact_size) { |
450 if (entry[i].dir) { | |
451 b->last = ngx_cpymem(b->last, " -", | |
452 sizeof(" -") - 1); | |
453 } else { | |
454 b->last = ngx_sprintf(b->last, "%19O", entry[i].size); | |
577 | 455 } |
457 | 456 |
457 } else { | |
527 | 458 if (entry[i].dir) { |
547 | 459 b->last = ngx_cpymem(b->last, " -", |
460 sizeof(" -") - 1); | |
527 | 461 |
462 } else { | |
463 length = entry[i].size; | |
464 | |
465 if (length > 1024 * 1024 * 1024 - 1) { | |
466 size = (ngx_int_t) (length / (1024 * 1024 * 1024)); | |
467 if ((length % (1024 * 1024 * 1024)) | |
468 > (1024 * 1024 * 1024 / 2 - 1)) | |
577 | 469 { |
527 | 470 size++; |
471 } | |
472 scale = 'G'; | |
473 | |
474 } else if (length > 1024 * 1024 - 1) { | |
475 size = (ngx_int_t) (length / (1024 * 1024)); | |
476 if ((length % (1024 * 1024)) > (1024 * 1024 / 2 - 1)) { | |
477 size++; | |
478 } | |
479 scale = 'M'; | |
480 | |
481 } else if (length > 9999) { | |
482 size = (ngx_int_t) (length / 1024); | |
483 if (length % 1024 > 511) { | |
484 size++; | |
485 } | |
486 scale = 'K'; | |
487 | |
488 } else { | |
489 size = (ngx_int_t) length; | |
547 | 490 scale = '\0'; |
527 | 491 } |
492 | |
547 | 493 if (scale) { |
494 b->last = ngx_sprintf(b->last, "%6i%c", size, scale); | |
527 | 495 |
547 | 496 } else { |
497 b->last = ngx_sprintf(b->last, " %6i", size); | |
527 | 498 } |
499 } | |
457 | 500 } |
501 | |
502 *b->last++ = CR; | |
503 *b->last++ = LF; | |
504 } | |
505 | |
459 | 506 /* TODO: free temporary pool */ |
507 | |
457 | 508 b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1); |
509 | |
510 b->last = ngx_cpymem(b->last, tail, sizeof(tail) - 1); | |
511 | |
597 | 512 if (r == r->main) { |
457 | 513 b->last_buf = 1; |
514 } | |
515 | |
509 | 516 b->last_in_chain = 1; |
517 | |
457 | 518 out.buf = b; |
519 out.next = NULL; | |
520 | |
521 return ngx_http_output_filter(r, &out); | |
522 } | |
523 | |
524 | |
503 | 525 static int ngx_libc_cdecl |
499 | 526 ngx_http_autoindex_cmp_entries(const void *one, const void *two) |
457 | 527 { |
528 ngx_http_autoindex_entry_t *first = (ngx_http_autoindex_entry_t *) one; | |
529 ngx_http_autoindex_entry_t *second = (ngx_http_autoindex_entry_t *) two; | |
530 | |
531 if (first->dir && !second->dir) { | |
532 /* move the directories to the start */ | |
533 return -1; | |
534 } | |
535 | |
536 if (!first->dir && second->dir) { | |
537 /* move the directories to the start */ | |
538 return 1; | |
539 } | |
540 | |
541 return (int) ngx_strcmp(first->name.data, second->name.data); | |
542 } | |
543 | |
544 | |
545 #if 0 | |
546 | |
499 | 547 static ngx_buf_t * |
548 ngx_http_autoindex_alloc(ngx_http_autoindex_ctx_t *ctx, size_t size) | |
457 | 549 { |
550 ngx_chain_t *cl; | |
551 | |
552 if (ctx->buf) { | |
553 | |
554 if ((size_t) (ctx->buf->end - ctx->buf->last) >= size) { | |
555 return ctx->buf; | |
556 } | |
557 | |
558 ctx->size += ctx->buf->last - ctx->buf->pos; | |
559 } | |
560 | |
501 | 561 ctx->buf = ngx_create_temp_buf(ctx->pool, ctx->alloc_size); |
562 if (ctx->buf == NULL) { | |
457 | 563 return NULL; |
564 } | |
565 | |
501 | 566 cl = ngx_alloc_chain_link(ctx->pool); |
567 if (cl == NULL) { | |
457 | 568 return NULL; |
569 } | |
570 | |
571 cl->buf = ctx->buf; | |
572 cl->next = NULL; | |
573 | |
574 *ctx->last_out = cl; | |
575 ctx->last_out = &cl->next; | |
576 | |
577 return ctx->buf; | |
578 } | |
579 | |
580 #endif | |
581 | |
582 | |
499 | 583 static ngx_int_t |
557 | 584 ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name) |
457 | 585 { |
586 if (ngx_close_dir(dir) == NGX_ERROR) { | |
587 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
557 | 588 ngx_close_dir_n " \"%V\" failed", name); |
457 | 589 } |
590 | |
591 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
592 } | |
593 | |
594 | |
499 | 595 static void * |
596 ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf) | |
457 | 597 { |
598 ngx_http_autoindex_loc_conf_t *conf; | |
599 | |
600 conf = ngx_palloc(cf->pool, sizeof(ngx_http_autoindex_loc_conf_t)); | |
601 if (conf == NULL) { | |
602 return NGX_CONF_ERROR; | |
603 } | |
604 | |
605 conf->enable = NGX_CONF_UNSET; | |
519 | 606 conf->localtime = NGX_CONF_UNSET; |
527 | 607 conf->exact_size = NGX_CONF_UNSET; |
457 | 608 |
609 return conf; | |
610 } | |
611 | |
612 | |
499 | 613 static char * |
614 ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
457 | 615 { |
616 ngx_http_autoindex_loc_conf_t *prev = parent; | |
617 ngx_http_autoindex_loc_conf_t *conf = child; | |
618 | |
619 ngx_conf_merge_value(conf->enable, prev->enable, 0); | |
519 | 620 ngx_conf_merge_value(conf->localtime, prev->localtime, 0); |
527 | 621 ngx_conf_merge_value(conf->exact_size, prev->exact_size, 1); |
457 | 622 |
623 return NGX_CONF_OK; | |
624 } | |
681 | 625 |
626 | |
627 static ngx_int_t | |
628 ngx_http_autoindex_init(ngx_conf_t *cf) | |
629 { | |
630 ngx_http_handler_pt *h; | |
631 ngx_http_core_main_conf_t *cmcf; | |
632 | |
633 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
634 | |
635 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
636 if (h == NULL) { | |
637 return NGX_ERROR; | |
638 } | |
639 | |
640 *h = ngx_http_autoindex_handler; | |
641 | |
642 return NGX_OK; | |
643 } |