3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
19 ngx_chain_t **last_out;
20 } ngx_http_autoindex_ctx_t;
32 } ngx_http_autoindex_entry_t;
38 ngx_flag_t exact_size;
39 } ngx_http_autoindex_loc_conf_t;
42 #define NGX_HTTP_AUTOINDEX_PREALLOCATE 50
44 #define NGX_HTTP_AUTOINDEX_NAME_LEN 50
47 static int ngx_libc_cdecl ngx_http_autoindex_cmp_entries(const void *one,
49 static ngx_int_t ngx_http_autoindex_error(ngx_http_request_t *r,
50 ngx_dir_t *dir, ngx_str_t *name);
51 static ngx_int_t ngx_http_autoindex_init(ngx_conf_t *cf);
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,
54 void *parent, void *child);
57 static ngx_command_t ngx_http_autoindex_commands[] = {
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),
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),
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),
84 static ngx_http_module_t ngx_http_autoindex_module_ctx = {
85 NULL, /* preconfiguration */
86 ngx_http_autoindex_init, /* postconfiguration */
88 NULL, /* create main configuration */
89 NULL, /* init main configuration */
91 NULL, /* create server configuration */
92 NULL, /* merge server configuration */
94 ngx_http_autoindex_create_loc_conf, /* create location configration */
95 ngx_http_autoindex_merge_loc_conf /* merge location configration */
99 ngx_module_t ngx_http_autoindex_module = {
101 &ngx_http_autoindex_module_ctx, /* module context */
102 ngx_http_autoindex_commands, /* module directives */
103 NGX_HTTP_MODULE, /* module type */
104 NULL, /* init master */
105 NULL, /* init module */
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
115 static u_char title[] =
117 "<head><title>Index of "
121 static u_char header[] =
122 "</title></head>" CRLF
123 "<body bgcolor=\"white\">" CRLF
127 static u_char tail[] =
134 ngx_http_autoindex_handler(ngx_http_request_t *r)
136 u_char *last, *filename, scale;
138 size_t len, utf_len, allocated, root;
150 ngx_http_autoindex_entry_t *entry;
151 ngx_http_autoindex_loc_conf_t *alcf;
153 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
154 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
156 if (r->uri.data[r->uri.len - 1] != '/') {
161 if (r->zero_in_uri) {
165 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
169 alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module);
175 /* NGX_DIR_MASK_LEN is lesser than NGX_HTTP_AUTOINDEX_PREALLOCATE */
177 last = ngx_http_map_uri_to_path(r, &path, &root,
178 NGX_HTTP_AUTOINDEX_PREALLOCATE);
180 return NGX_HTTP_INTERNAL_SERVER_ERROR;
183 allocated = path.len;
184 path.len = last - path.data;
188 path.data[path.len] = '\0';
190 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
191 "http autoindex: \"%s\"", path.data);
193 if (ngx_open_dir(&path, &dir) == NGX_ERROR) {
196 if (err == NGX_ENOENT
197 || err == NGX_ENOTDIR
198 || err == NGX_ENAMETOOLONG)
201 rc = NGX_HTTP_NOT_FOUND;
203 } else if (err == NGX_EACCES) {
205 rc = NGX_HTTP_FORBIDDEN;
208 level = NGX_LOG_CRIT;
209 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
212 ngx_log_error(level, r->connection->log, err,
213 ngx_open_dir_n " \"%s\" failed", path.data);
218 #if (NGX_SUPPRESS_WARN)
220 /* MSVC thinks 'entries' may be used without having been initialized */
221 ngx_memzero(&entries, sizeof(ngx_array_t));
225 /* TODO: pool should be temporary pool */
228 if (ngx_array_init(&entries, pool, 40, sizeof(ngx_http_autoindex_entry_t))
231 return ngx_http_autoindex_error(r, &dir, &path);
234 r->headers_out.status = NGX_HTTP_OK;
235 r->headers_out.content_type_len = sizeof("text/html") - 1;
236 r->headers_out.content_type.len = sizeof("text/html") - 1;
237 r->headers_out.content_type.data = (u_char *) "text/html";
239 rc = ngx_http_send_header(r);
241 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
242 if (ngx_close_dir(&dir) == NGX_ERROR) {
243 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
244 ngx_close_dir_n " \"%V\" failed", &path);
250 filename = path.data;
251 filename[path.len] = '/';
256 if (ngx_read_dir(&dir) == NGX_ERROR) {
259 if (err != NGX_ENOMOREFILES) {
260 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
261 ngx_read_dir_n " \"%V\" failed", &path);
262 return ngx_http_autoindex_error(r, &dir, &path);
268 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
269 "http autoindex file: \"%s\"", ngx_de_name(&dir));
271 len = ngx_de_namelen(&dir);
273 if (ngx_de_name(&dir)[0] == '.') {
277 if (!dir.valid_info) {
279 /* 1 byte for '/' and 1 byte for terminating '\0' */
281 if (path.len + 1 + len + 1 > allocated) {
282 allocated = path.len + 1 + len + 1
283 + NGX_HTTP_AUTOINDEX_PREALLOCATE;
285 filename = ngx_pnalloc(pool, allocated);
286 if (filename == NULL) {
287 return ngx_http_autoindex_error(r, &dir, &path);
290 last = ngx_cpystrn(filename, path.data, path.len + 1);
294 ngx_cpystrn(last, ngx_de_name(&dir), len + 1);
296 if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) {
299 if (err != NGX_ENOENT) {
300 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
301 ngx_de_info_n " \"%s\" failed", filename);
303 if (err == NGX_EACCES) {
307 return ngx_http_autoindex_error(r, &dir, &path);
310 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) {
311 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
312 ngx_de_link_info_n " \"%s\" failed",
314 return ngx_http_autoindex_error(r, &dir, &path);
319 entry = ngx_array_push(&entries);
321 return ngx_http_autoindex_error(r, &dir, &path);
324 entry->name.len = len;
326 entry->name.data = ngx_pnalloc(pool, len + 1);
327 if (entry->name.data == NULL) {
328 return ngx_http_autoindex_error(r, &dir, &path);
331 ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1);
333 entry->escape = 2 * ngx_escape_uri(NULL, ngx_de_name(&dir), len,
337 entry->utf_len = ngx_utf8_length(entry->name.data, entry->name.len);
339 entry->utf_len = len;
342 entry->dir = ngx_de_is_dir(&dir);
343 entry->mtime = ngx_de_mtime(&dir);
344 entry->size = ngx_de_size(&dir);
347 if (ngx_close_dir(&dir) == NGX_ERROR) {
348 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
349 ngx_close_dir_n " \"%s\" failed", &path);
352 len = sizeof(title) - 1
356 + sizeof("</h1>") - 1
357 + sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1
358 + sizeof("</pre><hr>") - 1
361 entry = entries.elts;
362 for (i = 0; i < entries.nelts; i++) {
363 len += sizeof("<a href=\"") - 1
364 + entry[i].name.len + entry[i].escape
365 + 1 /* 1 is for "/" */
367 + entry[i].name.len - entry[i].utf_len
368 + NGX_HTTP_AUTOINDEX_NAME_LEN + sizeof(">") - 2
370 + sizeof(" 28-Sep-1970 12:00 ") - 1
371 + 20 /* the file size */
375 b = ngx_create_temp_buf(r->pool, len);
377 return NGX_HTTP_INTERNAL_SERVER_ERROR;
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);
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);
392 b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF,
393 sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1);
395 tp = ngx_timeofday();
397 for (i = 0; i < entries.nelts; i++) {
398 b->last = ngx_cpymem(b->last, "<a href=\"", sizeof("<a href=\"") - 1);
400 if (entry[i].escape) {
401 ngx_escape_uri(b->last, entry[i].name.data, entry[i].name.len,
404 b->last += entry[i].name.len + entry[i].escape;
407 b->last = ngx_cpymem(b->last, entry[i].name.data,
418 len = entry[i].utf_len;
420 if (entry[i].name.len != len) {
421 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) {
422 utf_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3 + 1;
425 utf_len = NGX_HTTP_AUTOINDEX_NAME_LEN + 1;
428 b->last = ngx_utf8_cpystrn(b->last, entry[i].name.data,
429 utf_len, entry[i].name.len + 1);
433 b->last = ngx_cpystrn(b->last, entry[i].name.data,
434 NGX_HTTP_AUTOINDEX_NAME_LEN + 1);
438 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) {
439 b->last = ngx_cpymem(last, "..></a>", sizeof("..></a>") - 1);
442 if (entry[i].dir && NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) {
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;
454 ngx_gmtime(entry[i].mtime + tp->gmtoff * 60 * alcf->localtime, &tm);
456 b->last = ngx_sprintf(b->last, "%02d-%s-%d %02d:%02d ",
458 months[tm.ngx_tm_mon - 1],
463 if (alcf->exact_size) {
465 b->last = ngx_cpymem(b->last, " -",
468 b->last = ngx_sprintf(b->last, "%19O", entry[i].size);
473 b->last = ngx_cpymem(b->last, " -",
477 length = entry[i].size;
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))
488 } else if (length > 1024 * 1024 - 1) {
489 size = (ngx_int_t) (length / (1024 * 1024));
490 if ((length % (1024 * 1024)) > (1024 * 1024 / 2 - 1)) {
495 } else if (length > 9999) {
496 size = (ngx_int_t) (length / 1024);
497 if (length % 1024 > 511) {
503 size = (ngx_int_t) length;
508 b->last = ngx_sprintf(b->last, "%6i%c", size, scale);
511 b->last = ngx_sprintf(b->last, " %6i", size);
520 /* TODO: free temporary pool */
522 b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1);
524 b->last = ngx_cpymem(b->last, tail, sizeof(tail) - 1);
530 b->last_in_chain = 1;
535 return ngx_http_output_filter(r, &out);
539 static int ngx_libc_cdecl
540 ngx_http_autoindex_cmp_entries(const void *one, const void *two)
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;
545 if (first->dir && !second->dir) {
546 /* move the directories to the start */
550 if (!first->dir && second->dir) {
551 /* move the directories to the start */
555 return (int) ngx_strcmp(first->name.data, second->name.data);
562 ngx_http_autoindex_alloc(ngx_http_autoindex_ctx_t *ctx, size_t size)
568 if ((size_t) (ctx->buf->end - ctx->buf->last) >= size) {
572 ctx->size += ctx->buf->last - ctx->buf->pos;
575 ctx->buf = ngx_create_temp_buf(ctx->pool, ctx->alloc_size);
576 if (ctx->buf == NULL) {
580 cl = ngx_alloc_chain_link(ctx->pool);
589 ctx->last_out = &cl->next;
598 ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name)
600 if (ngx_close_dir(dir) == NGX_ERROR) {
601 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
602 ngx_close_dir_n " \"%V\" failed", name);
605 return NGX_HTTP_INTERNAL_SERVER_ERROR;
610 ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf)
612 ngx_http_autoindex_loc_conf_t *conf;
614 conf = ngx_palloc(cf->pool, sizeof(ngx_http_autoindex_loc_conf_t));
616 return NGX_CONF_ERROR;
619 conf->enable = NGX_CONF_UNSET;
620 conf->localtime = NGX_CONF_UNSET;
621 conf->exact_size = NGX_CONF_UNSET;
628 ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
630 ngx_http_autoindex_loc_conf_t *prev = parent;
631 ngx_http_autoindex_loc_conf_t *conf = child;
633 ngx_conf_merge_value(conf->enable, prev->enable, 0);
634 ngx_conf_merge_value(conf->localtime, prev->localtime, 0);
635 ngx_conf_merge_value(conf->exact_size, prev->exact_size, 1);
642 ngx_http_autoindex_init(ngx_conf_t *cf)
644 ngx_http_handler_pt *h;
645 ngx_http_core_main_conf_t *cmcf;
647 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
649 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
654 *h = ngx_http_autoindex_handler;