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