Mercurial > hg > nginx
annotate src/http/modules/ngx_http_autoindex_module.c @ 1960:1609b3c3d604
fix memory leak on resolver query send failure
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 09 Apr 2008 14:26:08 +0000 |
parents | bf5521329069 |
children | 2a92804f4109 d7d3a72c07d8 |
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; |
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 | |
285 filename = ngx_palloc(pool, allocated); | |
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); |
302 return ngx_http_autoindex_error(r, &dir, &path); | |
497 | 303 } |
304 | |
557 | 305 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) { |
497 | 306 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, |
307 ngx_de_link_info_n " \"%s\" failed", | |
557 | 308 filename); |
309 return ngx_http_autoindex_error(r, &dir, &path); | |
497 | 310 } |
457 | 311 } |
312 } | |
313 | |
501 | 314 entry = ngx_array_push(&entries); |
315 if (entry == NULL) { | |
557 | 316 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 317 } |
318 | |
577 | 319 entry->name.len = len; |
525 | 320 |
529 | 321 entry->name.data = ngx_palloc(pool, len + 1); |
501 | 322 if (entry->name.data == NULL) { |
557 | 323 return ngx_http_autoindex_error(r, &dir, &path); |
457 | 324 } |
461 | 325 |
457 | 326 ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1); |
327 | |
529 | 328 entry->escape = 2 * ngx_escape_uri(NULL, ngx_de_name(&dir), len, |
329 NGX_ESCAPE_HTML); | |
330 | |
525 | 331 if (r->utf8) { |
657 | 332 entry->utf_len = ngx_utf_length(entry->name.data, entry->name.len); |
525 | 333 } else { |
334 entry->utf_len = len; | |
335 } | |
336 | |
457 | 337 entry->dir = ngx_de_is_dir(&dir); |
338 entry->mtime = ngx_de_mtime(&dir); | |
339 entry->size = ngx_de_size(&dir); | |
340 } | |
341 | |
342 if (ngx_close_dir(&dir) == NGX_ERROR) { | |
343 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
557 | 344 ngx_close_dir_n " \"%s\" failed", &path); |
457 | 345 } |
346 | |
347 len = sizeof(title) - 1 | |
348 + r->uri.len | |
349 + sizeof(header) - 1 | |
350 + r->uri.len | |
351 + sizeof("</h1>") - 1 | |
352 + sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1 | |
353 + sizeof("</pre><hr>") - 1 | |
354 + sizeof(tail) - 1; | |
355 | |
356 entry = entries.elts; | |
357 for (i = 0; i < entries.nelts; i++) { | |
358 len += sizeof("<a href=\"") - 1 | |
529 | 359 + entry[i].name.len + entry[i].escape |
525 | 360 + 1 /* 1 is for "/" */ |
361 + sizeof("\">") - 1 | |
362 + entry[i].name.len - entry[i].utf_len | |
363 + NGX_HTTP_AUTOINDEX_NAME_LEN + sizeof(">") - 2 | |
364 + sizeof("</a>") - 1 | |
365 + sizeof(" 28-Sep-1970 12:00 ") - 1 | |
529 | 366 + 20 /* the file size */ |
525 | 367 + 2; |
457 | 368 } |
369 | |
501 | 370 b = ngx_create_temp_buf(r->pool, len); |
371 if (b == NULL) { | |
457 | 372 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
373 } | |
374 | |
375 if (entries.nelts > 1) { | |
376 ngx_qsort(entry, (size_t) entries.nelts, | |
377 sizeof(ngx_http_autoindex_entry_t), | |
378 ngx_http_autoindex_cmp_entries); | |
379 } | |
380 | |
381 b->last = ngx_cpymem(b->last, title, sizeof(title) - 1); | |
382 b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); | |
383 b->last = ngx_cpymem(b->last, header, sizeof(header) - 1); | |
384 b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); | |
385 b->last = ngx_cpymem(b->last, "</h1>", sizeof("</h1>") - 1); | |
386 | |
387 b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF, | |
388 sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1); | |
389 | |
563 | 390 tp = ngx_timeofday(); |
391 | |
457 | 392 for (i = 0; i < entries.nelts; i++) { |
393 b->last = ngx_cpymem(b->last, "<a href=\"", sizeof("<a href=\"") - 1); | |
461 | 394 |
395 if (entry[i].escape) { | |
396 ngx_escape_uri(b->last, entry[i].name.data, entry[i].name.len, | |
397 NGX_ESCAPE_HTML); | |
398 | |
399 b->last += entry[i].name.len + entry[i].escape; | |
400 | |
401 } else { | |
402 b->last = ngx_cpymem(b->last, entry[i].name.data, | |
403 entry[i].name.len); | |
404 } | |
457 | 405 |
406 if (entry[i].dir) { | |
407 *b->last++ = '/'; | |
408 } | |
409 | |
410 *b->last++ = '"'; | |
411 *b->last++ = '>'; | |
412 | |
525 | 413 len = entry[i].utf_len; |
457 | 414 |
529 | 415 if (entry[i].name.len - len) { |
527 | 416 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
417 copy = NGX_HTTP_AUTOINDEX_NAME_LEN - 3 + 1; | |
418 | |
419 } else { | |
420 copy = NGX_HTTP_AUTOINDEX_NAME_LEN + 1; | |
421 } | |
422 | |
423 b->last = ngx_utf_cpystrn(b->last, entry[i].name.data, copy); | |
424 last = b->last; | |
425 | |
426 } else { | |
427 b->last = ngx_cpystrn(b->last, entry[i].name.data, | |
428 NGX_HTTP_AUTOINDEX_NAME_LEN + 1); | |
429 last = b->last - 3; | |
430 } | |
431 | |
457 | 432 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { |
527 | 433 b->last = ngx_cpymem(last, "..></a>", sizeof("..></a>") - 1); |
457 | 434 |
435 } else { | |
436 if (entry[i].dir && NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) { | |
437 *b->last++ = '/'; | |
438 len++; | |
439 } | |
440 | |
441 b->last = ngx_cpymem(b->last, "</a>", sizeof("</a>") - 1); | |
442 ngx_memset(b->last, ' ', NGX_HTTP_AUTOINDEX_NAME_LEN - len); | |
443 b->last += NGX_HTTP_AUTOINDEX_NAME_LEN - len; | |
444 } | |
445 | |
446 *b->last++ = ' '; | |
447 | |
563 | 448 ngx_gmtime(entry[i].mtime + tp->gmtoff * 60 * alcf->localtime, &tm); |
457 | 449 |
450 b->last = ngx_sprintf(b->last, "%02d-%s-%d %02d:%02d ", | |
451 tm.ngx_tm_mday, | |
452 months[tm.ngx_tm_mon - 1], | |
453 tm.ngx_tm_year, | |
454 tm.ngx_tm_hour, | |
455 tm.ngx_tm_min); | |
456 | |
527 | 457 if (alcf->exact_size) { |
458 if (entry[i].dir) { | |
459 b->last = ngx_cpymem(b->last, " -", | |
460 sizeof(" -") - 1); | |
461 } else { | |
462 b->last = ngx_sprintf(b->last, "%19O", entry[i].size); | |
577 | 463 } |
457 | 464 |
465 } else { | |
527 | 466 if (entry[i].dir) { |
547 | 467 b->last = ngx_cpymem(b->last, " -", |
468 sizeof(" -") - 1); | |
527 | 469 |
470 } else { | |
471 length = entry[i].size; | |
472 | |
473 if (length > 1024 * 1024 * 1024 - 1) { | |
474 size = (ngx_int_t) (length / (1024 * 1024 * 1024)); | |
475 if ((length % (1024 * 1024 * 1024)) | |
476 > (1024 * 1024 * 1024 / 2 - 1)) | |
577 | 477 { |
527 | 478 size++; |
479 } | |
480 scale = 'G'; | |
481 | |
482 } else if (length > 1024 * 1024 - 1) { | |
483 size = (ngx_int_t) (length / (1024 * 1024)); | |
484 if ((length % (1024 * 1024)) > (1024 * 1024 / 2 - 1)) { | |
485 size++; | |
486 } | |
487 scale = 'M'; | |
488 | |
489 } else if (length > 9999) { | |
490 size = (ngx_int_t) (length / 1024); | |
491 if (length % 1024 > 511) { | |
492 size++; | |
493 } | |
494 scale = 'K'; | |
495 | |
496 } else { | |
497 size = (ngx_int_t) length; | |
547 | 498 scale = '\0'; |
527 | 499 } |
500 | |
547 | 501 if (scale) { |
502 b->last = ngx_sprintf(b->last, "%6i%c", size, scale); | |
527 | 503 |
547 | 504 } else { |
505 b->last = ngx_sprintf(b->last, " %6i", size); | |
527 | 506 } |
507 } | |
457 | 508 } |
509 | |
510 *b->last++ = CR; | |
511 *b->last++ = LF; | |
512 } | |
513 | |
459 | 514 /* TODO: free temporary pool */ |
515 | |
457 | 516 b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1); |
517 | |
518 b->last = ngx_cpymem(b->last, tail, sizeof(tail) - 1); | |
519 | |
597 | 520 if (r == r->main) { |
457 | 521 b->last_buf = 1; |
522 } | |
523 | |
509 | 524 b->last_in_chain = 1; |
525 | |
457 | 526 out.buf = b; |
527 out.next = NULL; | |
528 | |
529 return ngx_http_output_filter(r, &out); | |
530 } | |
531 | |
532 | |
503 | 533 static int ngx_libc_cdecl |
499 | 534 ngx_http_autoindex_cmp_entries(const void *one, const void *two) |
457 | 535 { |
536 ngx_http_autoindex_entry_t *first = (ngx_http_autoindex_entry_t *) one; | |
537 ngx_http_autoindex_entry_t *second = (ngx_http_autoindex_entry_t *) two; | |
538 | |
539 if (first->dir && !second->dir) { | |
540 /* move the directories to the start */ | |
541 return -1; | |
542 } | |
543 | |
544 if (!first->dir && second->dir) { | |
545 /* move the directories to the start */ | |
546 return 1; | |
547 } | |
548 | |
549 return (int) ngx_strcmp(first->name.data, second->name.data); | |
550 } | |
551 | |
552 | |
553 #if 0 | |
554 | |
499 | 555 static ngx_buf_t * |
556 ngx_http_autoindex_alloc(ngx_http_autoindex_ctx_t *ctx, size_t size) | |
457 | 557 { |
558 ngx_chain_t *cl; | |
559 | |
560 if (ctx->buf) { | |
561 | |
562 if ((size_t) (ctx->buf->end - ctx->buf->last) >= size) { | |
563 return ctx->buf; | |
564 } | |
565 | |
566 ctx->size += ctx->buf->last - ctx->buf->pos; | |
567 } | |
568 | |
501 | 569 ctx->buf = ngx_create_temp_buf(ctx->pool, ctx->alloc_size); |
570 if (ctx->buf == NULL) { | |
457 | 571 return NULL; |
572 } | |
573 | |
501 | 574 cl = ngx_alloc_chain_link(ctx->pool); |
575 if (cl == NULL) { | |
457 | 576 return NULL; |
577 } | |
578 | |
579 cl->buf = ctx->buf; | |
580 cl->next = NULL; | |
581 | |
582 *ctx->last_out = cl; | |
583 ctx->last_out = &cl->next; | |
584 | |
585 return ctx->buf; | |
586 } | |
587 | |
588 #endif | |
589 | |
590 | |
499 | 591 static ngx_int_t |
557 | 592 ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name) |
457 | 593 { |
594 if (ngx_close_dir(dir) == NGX_ERROR) { | |
595 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
557 | 596 ngx_close_dir_n " \"%V\" failed", name); |
457 | 597 } |
598 | |
599 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
600 } | |
601 | |
602 | |
499 | 603 static void * |
604 ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf) | |
457 | 605 { |
606 ngx_http_autoindex_loc_conf_t *conf; | |
607 | |
608 conf = ngx_palloc(cf->pool, sizeof(ngx_http_autoindex_loc_conf_t)); | |
609 if (conf == NULL) { | |
610 return NGX_CONF_ERROR; | |
611 } | |
612 | |
613 conf->enable = NGX_CONF_UNSET; | |
519 | 614 conf->localtime = NGX_CONF_UNSET; |
527 | 615 conf->exact_size = NGX_CONF_UNSET; |
457 | 616 |
617 return conf; | |
618 } | |
619 | |
620 | |
499 | 621 static char * |
622 ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
457 | 623 { |
624 ngx_http_autoindex_loc_conf_t *prev = parent; | |
625 ngx_http_autoindex_loc_conf_t *conf = child; | |
626 | |
627 ngx_conf_merge_value(conf->enable, prev->enable, 0); | |
519 | 628 ngx_conf_merge_value(conf->localtime, prev->localtime, 0); |
527 | 629 ngx_conf_merge_value(conf->exact_size, prev->exact_size, 1); |
457 | 630 |
631 return NGX_CONF_OK; | |
632 } | |
681 | 633 |
634 | |
635 static ngx_int_t | |
636 ngx_http_autoindex_init(ngx_conf_t *cf) | |
637 { | |
638 ngx_http_handler_pt *h; | |
639 ngx_http_core_main_conf_t *cmcf; | |
640 | |
641 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
642 | |
643 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
644 if (h == NULL) { | |
645 return NGX_ERROR; | |
646 } | |
647 | |
648 *h = ngx_http_autoindex_handler; | |
649 | |
650 return NGX_OK; | |
651 } |