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