10 #include "stat_cache.h"
25 * this is a dirlisting for a lighttpd plugin
28 #ifdef HAVE_ATTR_ATTRIBUTES_H
29 #include <attr/attributes.h>
32 #ifdef HAVE_SYS_EXTATTR_H
33 #include <sys/extattr.h>
38 /* plugin config for all request/connections */
55 unsigned short dir_listing
;
56 unsigned short hide_dot_files
;
57 unsigned short show_readme
;
58 unsigned short hide_readme_file
;
59 unsigned short encode_readme
;
60 unsigned short show_header
;
61 unsigned short hide_header_file
;
62 unsigned short encode_header
;
63 unsigned short auto_layout
;
65 excludes_buffer
*excludes
;
76 buffer
*content_charset
;
78 plugin_config
**config_storage
;
83 static excludes_buffer
*excludes_buffer_init(void) {
86 exb
= calloc(1, sizeof(*exb
));
91 static int excludes_buffer_append(excludes_buffer
*exb
, buffer
*string
) {
97 if (!string
) return -1;
103 exb
->ptr
= malloc(exb
->size
* sizeof(*exb
->ptr
));
105 for(i
= 0; i
< exb
->size
; i
++) {
106 exb
->ptr
[i
] = calloc(1, sizeof(**exb
->ptr
));
108 } else if (exb
->used
== exb
->size
) {
111 exb
->ptr
= realloc(exb
->ptr
, exb
->size
* sizeof(*exb
->ptr
));
113 for(i
= exb
->used
; i
< exb
->size
; i
++) {
114 exb
->ptr
[i
] = calloc(1, sizeof(**exb
->ptr
));
119 if (NULL
== (exb
->ptr
[exb
->used
]->regex
= pcre_compile(string
->ptr
, 0,
120 &errptr
, &erroff
, NULL
))) {
124 exb
->ptr
[exb
->used
]->string
= buffer_init();
125 buffer_copy_buffer(exb
->ptr
[exb
->used
]->string
, string
);
138 static void excludes_buffer_free(excludes_buffer
*exb
) {
142 for (i
= 0; i
< exb
->size
; i
++) {
143 if (exb
->ptr
[i
]->regex
) pcre_free(exb
->ptr
[i
]->regex
);
144 if (exb
->ptr
[i
]->string
) buffer_free(exb
->ptr
[i
]->string
);
148 if (exb
->ptr
) free(exb
->ptr
);
154 /* init the plugin data */
155 INIT_FUNC(mod_dirlisting_init
) {
158 p
= calloc(1, sizeof(*p
));
160 p
->tmp_buf
= buffer_init();
161 p
->content_charset
= buffer_init();
166 /* detroy the plugin data */
167 FREE_FUNC(mod_dirlisting_free
) {
168 plugin_data
*p
= p_d
;
172 if (!p
) return HANDLER_GO_ON
;
174 if (p
->config_storage
) {
176 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
177 plugin_config
*s
= p
->config_storage
[i
];
181 excludes_buffer_free(s
->excludes
);
182 buffer_free(s
->external_css
);
183 buffer_free(s
->encoding
);
184 buffer_free(s
->set_footer
);
188 free(p
->config_storage
);
191 buffer_free(p
->tmp_buf
);
192 buffer_free(p
->content_charset
);
196 return HANDLER_GO_ON
;
199 /* handle plugin config and check values */
201 #define CONFIG_EXCLUDE "dir-listing.exclude"
202 #define CONFIG_ACTIVATE "dir-listing.activate"
203 #define CONFIG_HIDE_DOTFILES "dir-listing.hide-dotfiles"
204 #define CONFIG_EXTERNAL_CSS "dir-listing.external-css"
205 #define CONFIG_ENCODING "dir-listing.encoding"
206 #define CONFIG_SHOW_README "dir-listing.show-readme"
207 #define CONFIG_HIDE_README_FILE "dir-listing.hide-readme-file"
208 #define CONFIG_SHOW_HEADER "dir-listing.show-header"
209 #define CONFIG_HIDE_HEADER_FILE "dir-listing.hide-header-file"
210 #define CONFIG_DIR_LISTING "server.dir-listing"
211 #define CONFIG_SET_FOOTER "dir-listing.set-footer"
212 #define CONFIG_ENCODE_README "dir-listing.encode-readme"
213 #define CONFIG_ENCODE_HEADER "dir-listing.encode-header"
214 #define CONFIG_AUTO_LAYOUT "dir-listing.auto-layout"
217 SETDEFAULTS_FUNC(mod_dirlisting_set_defaults
) {
218 plugin_data
*p
= p_d
;
221 config_values_t cv
[] = {
222 { CONFIG_EXCLUDE
, NULL
, T_CONFIG_LOCAL
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
223 { CONFIG_ACTIVATE
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
224 { CONFIG_HIDE_DOTFILES
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
225 { CONFIG_EXTERNAL_CSS
, NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
226 { CONFIG_ENCODING
, NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 4 */
227 { CONFIG_SHOW_README
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 5 */
228 { CONFIG_HIDE_README_FILE
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 6 */
229 { CONFIG_SHOW_HEADER
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 7 */
230 { CONFIG_HIDE_HEADER_FILE
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 8 */
231 { CONFIG_DIR_LISTING
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 9 */
232 { CONFIG_SET_FOOTER
, NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 10 */
233 { CONFIG_ENCODE_README
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 11 */
234 { CONFIG_ENCODE_HEADER
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 12 */
235 { CONFIG_AUTO_LAYOUT
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 13 */
237 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
240 if (!p
) return HANDLER_ERROR
;
242 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
244 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
245 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
247 data_unset
*du_excludes
;
249 s
= calloc(1, sizeof(plugin_config
));
250 s
->excludes
= excludes_buffer_init();
252 s
->external_css
= buffer_init();
253 s
->hide_dot_files
= 1;
255 s
->hide_readme_file
= 0;
257 s
->hide_header_file
= 0;
258 s
->encode_readme
= 1;
259 s
->encode_header
= 1;
262 s
->encoding
= buffer_init();
263 s
->set_footer
= buffer_init();
265 cv
[0].destination
= s
->excludes
;
266 cv
[1].destination
= &(s
->dir_listing
);
267 cv
[2].destination
= &(s
->hide_dot_files
);
268 cv
[3].destination
= s
->external_css
;
269 cv
[4].destination
= s
->encoding
;
270 cv
[5].destination
= &(s
->show_readme
);
271 cv
[6].destination
= &(s
->hide_readme_file
);
272 cv
[7].destination
= &(s
->show_header
);
273 cv
[8].destination
= &(s
->hide_header_file
);
274 cv
[9].destination
= &(s
->dir_listing
); /* old name */
275 cv
[10].destination
= s
->set_footer
;
276 cv
[11].destination
= &(s
->encode_readme
);
277 cv
[12].destination
= &(s
->encode_header
);
278 cv
[13].destination
= &(s
->auto_layout
);
280 p
->config_storage
[i
] = s
;
282 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
283 return HANDLER_ERROR
;
286 if (NULL
!= (du_excludes
= array_get_element(config
->value
, CONFIG_EXCLUDE
))) {
287 array
*excludes_list
;
290 if (du_excludes
->type
!= TYPE_ARRAY
) {
291 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
292 "unexpected type for key: ", CONFIG_EXCLUDE
, "array of strings");
293 return HANDLER_ERROR
;
296 excludes_list
= ((data_array
*)du_excludes
)->value
;
299 if (excludes_list
->used
> 0) {
300 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
301 "pcre support is missing for: ", CONFIG_EXCLUDE
, ", please install libpcre and the headers");
302 return HANDLER_ERROR
;
305 for (j
= 0; j
< excludes_list
->used
; j
++) {
306 data_unset
*du_exclude
= excludes_list
->data
[j
];
308 if (du_exclude
->type
!= TYPE_STRING
) {
309 log_error_write(srv
, __FILE__
, __LINE__
, "sssbs",
310 "unexpected type for key: ", CONFIG_EXCLUDE
, "[",
311 du_exclude
->key
, "](string)");
312 return HANDLER_ERROR
;
315 if (0 != excludes_buffer_append(s
->excludes
, ((data_string
*)(du_exclude
))->value
)) {
316 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
317 "pcre-compile failed for", ((data_string
*)(du_exclude
))->value
);
318 return HANDLER_ERROR
;
325 return HANDLER_GO_ON
;
330 static int mod_dirlisting_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
332 plugin_config
*s
= p
->config_storage
[0];
336 PATCH(hide_dot_files
);
339 PATCH(hide_readme_file
);
341 PATCH(hide_header_file
);
344 PATCH(encode_readme
);
345 PATCH(encode_header
);
348 /* skip the first, the global context */
349 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
350 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
351 s
= p
->config_storage
[i
];
353 /* condition didn't match */
354 if (!config_check_cond(srv
, con
, dc
)) continue;
357 for (j
= 0; j
< dc
->value
->used
; j
++) {
358 data_unset
*du
= dc
->value
->data
[j
];
360 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_ACTIVATE
)) ||
361 buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_DIR_LISTING
))) {
363 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_HIDE_DOTFILES
))) {
364 PATCH(hide_dot_files
);
365 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_EXTERNAL_CSS
))) {
367 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_ENCODING
))) {
369 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_SHOW_README
))) {
371 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_HIDE_README_FILE
))) {
372 PATCH(hide_readme_file
);
373 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_SHOW_HEADER
))) {
375 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_HIDE_HEADER_FILE
))) {
376 PATCH(hide_header_file
);
377 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_SET_FOOTER
))) {
379 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_EXCLUDE
))) {
381 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_ENCODE_README
))) {
382 PATCH(encode_readme
);
383 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_ENCODE_HEADER
))) {
384 PATCH(encode_header
);
385 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_AUTO_LAYOUT
))) {
407 #define DIRLIST_ENT_NAME(ent) ((char*)(ent) + sizeof(dirls_entry_t))
408 #define DIRLIST_BLOB_SIZE 16
410 /* simple combsort algorithm */
411 static void http_dirls_sort(dirls_entry_t
**ent
, int num
) {
418 gap
= (gap
* 10) / 13;
419 if (gap
== 9 || gap
== 10)
425 for (i
= 0; i
< num
- gap
; i
++) {
427 if (strcmp(DIRLIST_ENT_NAME(ent
[i
]), DIRLIST_ENT_NAME(ent
[j
])) > 0) {
435 } while (gap
> 1 || swapped
);
438 /* buffer must be able to hold "999.9K"
439 * conversion is simple but not perfect
441 static int http_list_directory_sizefmt(char *buf
, size_t bufsz
, off_t size
) {
442 const char unit
[] = "KMGTPE"; /* Kilo, Mega, Tera, Peta, Exa */
443 const char *u
= unit
- 1; /* u will always increment at least once */
453 remain
= (int) size
& 1023;
456 if ((size
& (~0 ^ 1023)) == 0)
469 li_itostrn(buf
, bufsz
, size
);
470 buflen
= strlen(buf
);
471 if (buflen
+ 3 >= bufsz
) return buflen
;
473 buf
[buflen
+1] = remain
+ '0';
475 buf
[buflen
+3] = '\0';
480 /* don't want to block when open()ing a fifo */
481 #if defined(O_NONBLOCK)
482 # define FIFO_NONBLOCK O_NONBLOCK
484 # define FIFO_NONBLOCK 0
487 static void http_list_directory_include_file(buffer
*out
, buffer
*path
, const char *classname
, int encode
) {
488 int fd
= open(path
->ptr
, O_RDONLY
| FIFO_NONBLOCK
);
492 if (-1 == fd
) return;
495 buffer_append_string_len(out
, CONST_STR_LEN("<pre class=\""));
496 buffer_append_string(out
, classname
);
497 buffer_append_string_len(out
, CONST_STR_LEN("\">"));
500 while ((rd
= read(fd
, buf
, sizeof(buf
))) > 0) {
502 buffer_append_string_encoded(out
, buf
, (size_t)rd
, ENCODING_MINIMAL_XML
);
504 buffer_append_string_len(out
, buf
, (size_t)rd
);
509 buffer_append_string_len(out
, CONST_STR_LEN("</pre>"));
513 static void http_list_directory_header(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*out
) {
516 if (p
->conf
.auto_layout
) {
517 buffer_append_string_len(out
, CONST_STR_LEN(
518 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
519 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n"
523 buffer_append_string_encoded(out
, CONST_BUF_LEN(con
->uri
.path
), ENCODING_MINIMAL_XML
);
524 buffer_append_string_len(out
, CONST_STR_LEN("</title>\n"));
526 if (!buffer_string_is_empty(p
->conf
.external_css
)) {
527 buffer_append_string_len(out
, CONST_STR_LEN("<link rel=\"stylesheet\" type=\"text/css\" href=\""));
528 buffer_append_string_buffer(out
, p
->conf
.external_css
);
529 buffer_append_string_len(out
, CONST_STR_LEN("\" />\n"));
531 buffer_append_string_len(out
, CONST_STR_LEN(
532 "<style type=\"text/css\">\n"
533 "a, a:active {text-decoration: none; color: blue;}\n"
534 "a:visited {color: #48468F;}\n"
535 "a:hover, a:focus {text-decoration: underline; color: red;}\n"
536 "body {background-color: #F5F5F5;}\n"
537 "h2 {margin-bottom: 12px;}\n"
538 "table {margin-left: 12px;}\n"
540 " font: 90% monospace;"
544 " font-weight: bold;"
545 " padding-right: 14px;"
546 " padding-bottom: 3px;"
548 "td {padding-right: 14px;}\n"
549 "td.s, th.s {text-align: right;}\n"
551 " background-color: white;"
552 " border-top: 1px solid #646464;"
553 " border-bottom: 1px solid #646464;"
554 " padding-top: 10px;"
555 " padding-bottom: 14px;"
558 " font: 90% monospace;"
566 buffer_append_string_len(out
, CONST_STR_LEN("</head>\n<body>\n"));
570 if (p
->conf
.show_header
) {
571 /* if we have a HEADER file, display it in <pre class="header"></pre> */
573 buffer_copy_buffer(p
->tmp_buf
, con
->physical
.path
);
574 buffer_append_slash(p
->tmp_buf
);
575 buffer_append_string_len(p
->tmp_buf
, CONST_STR_LEN("HEADER.txt"));
577 http_list_directory_include_file(out
, p
->tmp_buf
, "header", p
->conf
.encode_header
);
580 buffer_append_string_len(out
, CONST_STR_LEN("<h2>Index of "));
581 buffer_append_string_encoded(out
, CONST_BUF_LEN(con
->uri
.path
), ENCODING_MINIMAL_XML
);
582 buffer_append_string_len(out
, CONST_STR_LEN(
584 "<div class=\"list\">\n"
585 "<table summary=\"Directory Listing\" cellpadding=\"0\" cellspacing=\"0\">\n"
588 "<th class=\"n\">Name</th>"
589 "<th class=\"m\">Last Modified</th>"
590 "<th class=\"s\">Size</th>"
591 "<th class=\"t\">Type</th>"
596 "<td class=\"n\"><a href=\"../\">Parent Directory</a>/</td>"
597 "<td class=\"m\"> </td>"
598 "<td class=\"s\">- </td>"
599 "<td class=\"t\">Directory</td>"
604 static void http_list_directory_footer(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*out
) {
607 buffer_append_string_len(out
, CONST_STR_LEN(
613 if (p
->conf
.show_readme
) {
614 /* if we have a README file, display it in <pre class="readme"></pre> */
616 buffer_copy_buffer(p
->tmp_buf
, con
->physical
.path
);
617 buffer_append_slash(p
->tmp_buf
);
618 buffer_append_string_len(p
->tmp_buf
, CONST_STR_LEN("README.txt"));
620 http_list_directory_include_file(out
, p
->tmp_buf
, "readme", p
->conf
.encode_readme
);
623 if(p
->conf
.auto_layout
) {
624 buffer_append_string_len(out
, CONST_STR_LEN(
625 "<div class=\"foot\">"
628 if (!buffer_string_is_empty(p
->conf
.set_footer
)) {
629 buffer_append_string_buffer(out
, p
->conf
.set_footer
);
630 } else if (buffer_is_empty(con
->conf
.server_tag
)) {
631 buffer_append_string_len(out
, CONST_STR_LEN(PACKAGE_DESC
));
633 buffer_append_string_buffer(out
, con
->conf
.server_tag
);
636 buffer_append_string_len(out
, CONST_STR_LEN(
644 static int http_list_directory(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*dir
) {
649 char *path
, *path_file
;
651 int hide_dotfiles
= p
->conf
.hide_dot_files
;
652 dirls_list_t dirs
, files
, *list
;
654 char sizebuf
[sizeof("999.9K")];
655 char datebuf
[sizeof("2005-Jan-01 22:23:24")];
657 const char *content_type
;
659 #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
663 #ifdef HAVE_LOCALTIME_R
667 if (buffer_string_is_empty(dir
)) return -1;
669 i
= buffer_string_length(dir
);
672 if (0 >= (name_max
= pathconf(dir
->ptr
, _PC_NAME_MAX
))) {
673 /* some broken fs (fuse) return 0 instead of -1 */
677 name_max
= 255; /* stupid default */
680 #elif defined __WIN32
681 name_max
= FILENAME_MAX
;
686 path
= malloc(buffer_string_length(dir
) + name_max
+ 1);
687 force_assert(NULL
!= path
);
688 strcpy(path
, dir
->ptr
);
689 path_file
= path
+ i
;
691 if (NULL
== (dp
= opendir(path
))) {
692 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
693 "opendir failed:", dir
, strerror(errno
));
699 dirs
.ent
= (dirls_entry_t
**) malloc(sizeof(dirls_entry_t
*) * DIRLIST_BLOB_SIZE
);
700 force_assert(dirs
.ent
);
701 dirs
.size
= DIRLIST_BLOB_SIZE
;
703 files
.ent
= (dirls_entry_t
**) malloc(sizeof(dirls_entry_t
*) * DIRLIST_BLOB_SIZE
);
704 force_assert(files
.ent
);
705 files
.size
= DIRLIST_BLOB_SIZE
;
708 while ((dent
= readdir(dp
)) != NULL
) {
709 unsigned short exclude_match
= 0;
711 if (dent
->d_name
[0] == '.') {
714 if (dent
->d_name
[1] == '\0')
716 if (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0')
720 if (p
->conf
.hide_readme_file
) {
721 if (strcmp(dent
->d_name
, "README.txt") == 0)
724 if (p
->conf
.hide_header_file
) {
725 if (strcmp(dent
->d_name
, "HEADER.txt") == 0)
729 /* compare d_name against excludes array
730 * elements, skipping any that match.
733 for(i
= 0; i
< p
->conf
.excludes
->used
; i
++) {
737 pcre
*regex
= p
->conf
.excludes
->ptr
[i
]->regex
;
739 if ((n
= pcre_exec(regex
, NULL
, dent
->d_name
,
740 strlen(dent
->d_name
), 0, 0, ovec
, 3 * N
)) < 0) {
741 if (n
!= PCRE_ERROR_NOMATCH
) {
742 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
743 "execution error while matching:", n
);
745 /* aborting would require a lot of manual cleanup here.
746 * skip instead (to not leak names that break pcre matching)
763 i
= strlen(dent
->d_name
);
765 /* NOTE: the manual says, d_name is never more than NAME_MAX
766 * so this should actually not be a buffer-overflow-risk
768 if (i
> (size_t)name_max
) continue;
770 memcpy(path_file
, dent
->d_name
, i
+ 1);
771 if (stat(path
, &st
) != 0)
775 if (S_ISDIR(st
.st_mode
))
778 if (list
->used
== list
->size
) {
779 list
->size
+= DIRLIST_BLOB_SIZE
;
780 list
->ent
= (dirls_entry_t
**) realloc(list
->ent
, sizeof(dirls_entry_t
*) * list
->size
);
781 force_assert(list
->ent
);
784 tmp
= (dirls_entry_t
*) malloc(sizeof(dirls_entry_t
) + 1 + i
);
785 tmp
->mtime
= st
.st_mtime
;
786 tmp
->size
= st
.st_size
;
788 memcpy(DIRLIST_ENT_NAME(tmp
), dent
->d_name
, i
+ 1);
790 list
->ent
[list
->used
++] = tmp
;
794 if (dirs
.used
) http_dirls_sort(dirs
.ent
, dirs
.used
);
796 if (files
.used
) http_dirls_sort(files
.ent
, files
.used
);
799 buffer_copy_string_len(out
, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\""));
800 if (buffer_string_is_empty(p
->conf
.encoding
)) {
801 buffer_append_string_len(out
, CONST_STR_LEN("iso-8859-1"));
803 buffer_append_string_buffer(out
, p
->conf
.encoding
);
805 buffer_append_string_len(out
, CONST_STR_LEN("\"?>\n"));
806 http_list_directory_header(srv
, con
, p
, out
);
809 for (i
= 0; i
< dirs
.used
; i
++) {
812 #ifdef HAVE_LOCALTIME_R
813 localtime_r(&(tmp
->mtime
), &tm
);
814 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", &tm
);
816 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", localtime(&(tmp
->mtime
)));
819 buffer_append_string_len(out
, CONST_STR_LEN("<tr class=\"d\"><td class=\"n\"><a href=\""));
820 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_REL_URI_PART
);
821 buffer_append_string_len(out
, CONST_STR_LEN("/\">"));
822 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_MINIMAL_XML
);
823 buffer_append_string_len(out
, CONST_STR_LEN("</a>/</td><td class=\"m\">"));
824 buffer_append_string_len(out
, datebuf
, sizeof(datebuf
) - 1);
825 buffer_append_string_len(out
, CONST_STR_LEN("</td><td class=\"s\">- </td><td class=\"t\">Directory</td></tr>\n"));
831 for (i
= 0; i
< files
.used
; i
++) {
835 #if defined(HAVE_XATTR)
836 if (con
->conf
.use_xattr
) {
837 memcpy(path_file
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
+ 1);
838 attrlen
= sizeof(attrval
) - 1;
839 if (attr_get(path
, srv
->srvconf
.xattr_name
->ptr
, attrval
, &attrlen
, 0) == 0) {
840 attrval
[attrlen
] = '\0';
841 content_type
= attrval
;
844 #elif defined(HAVE_EXTATTR)
845 if (con
->conf
.use_xattr
) {
846 memcpy(path_file
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
+ 1);
847 if(-1 != (attrlen
= extattr_get_file(path
, EXTATTR_NAMESPACE_USER
, srv
->srvconf
.xattr_name
->ptr
, attrval
, sizeof(attrval
)-1))) {
848 attrval
[attrlen
] = '\0';
849 content_type
= attrval
;
854 if (content_type
== NULL
) {
855 content_type
= "application/octet-stream";
856 for (k
= 0; k
< con
->conf
.mimetypes
->used
; k
++) {
857 data_string
*ds
= (data_string
*)con
->conf
.mimetypes
->data
[k
];
860 if (buffer_is_empty(ds
->key
))
863 ct_len
= buffer_string_length(ds
->key
);
864 if (tmp
->namelen
< ct_len
)
867 if (0 == strncasecmp(DIRLIST_ENT_NAME(tmp
) + tmp
->namelen
- ct_len
, ds
->key
->ptr
, ct_len
)) {
868 content_type
= ds
->value
->ptr
;
874 #ifdef HAVE_LOCALTIME_R
875 localtime_r(&(tmp
->mtime
), &tm
);
876 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", &tm
);
878 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", localtime(&(tmp
->mtime
)));
880 http_list_directory_sizefmt(sizebuf
, sizeof(sizebuf
), tmp
->size
);
882 buffer_append_string_len(out
, CONST_STR_LEN("<tr><td class=\"n\"><a href=\""));
883 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_REL_URI_PART
);
884 buffer_append_string_len(out
, CONST_STR_LEN("\">"));
885 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_MINIMAL_XML
);
886 buffer_append_string_len(out
, CONST_STR_LEN("</a></td><td class=\"m\">"));
887 buffer_append_string_len(out
, datebuf
, sizeof(datebuf
) - 1);
888 buffer_append_string_len(out
, CONST_STR_LEN("</td><td class=\"s\">"));
889 buffer_append_string(out
, sizebuf
);
890 buffer_append_string_len(out
, CONST_STR_LEN("</td><td class=\"t\">"));
891 buffer_append_string(out
, content_type
);
892 buffer_append_string_len(out
, CONST_STR_LEN("</td></tr>\n"));
901 http_list_directory_footer(srv
, con
, p
, out
);
903 /* Insert possible charset to Content-Type */
904 if (buffer_string_is_empty(p
->conf
.encoding
)) {
905 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
907 buffer_copy_string_len(p
->content_charset
, CONST_STR_LEN("text/html; charset="));
908 buffer_append_string_buffer(p
->content_charset
, p
->conf
.encoding
);
909 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(p
->content_charset
));
912 con
->file_finished
= 1;
913 chunkqueue_append_buffer(con
->write_queue
, out
);
921 URIHANDLER_FUNC(mod_dirlisting_subrequest
) {
922 plugin_data
*p
= p_d
;
923 stat_cache_entry
*sce
= NULL
;
927 /* we only handle GET, POST and HEAD */
928 switch(con
->request
.http_method
) {
929 case HTTP_METHOD_GET
:
930 case HTTP_METHOD_POST
:
931 case HTTP_METHOD_HEAD
:
934 return HANDLER_GO_ON
;
937 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
939 if (buffer_is_empty(con
->physical
.path
)) return HANDLER_GO_ON
;
940 if (buffer_is_empty(con
->uri
.path
)) return HANDLER_GO_ON
;
941 if (con
->uri
.path
->ptr
[buffer_string_length(con
->uri
.path
) - 1] != '/') return HANDLER_GO_ON
;
943 mod_dirlisting_patch_connection(srv
, con
, p
);
945 if (!p
->conf
.dir_listing
) return HANDLER_GO_ON
;
947 if (con
->conf
.log_request_handling
) {
948 log_error_write(srv
, __FILE__
, __LINE__
, "s", "-- handling the request as Dir-Listing");
949 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "URI :", con
->uri
.path
);
952 if (HANDLER_ERROR
== stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) {
953 log_error_write(srv
, __FILE__
, __LINE__
, "SB", "stat_cache_get_entry failed: ", con
->physical
.path
);
957 if (!S_ISDIR(sce
->st
.st_mode
)) return HANDLER_GO_ON
;
959 if (http_list_directory(srv
, con
, p
, con
->physical
.path
)) {
960 /* dirlisting failed */
961 con
->http_status
= 403;
964 buffer_reset(con
->physical
.path
);
967 return HANDLER_FINISHED
;
970 /* this function is called at dlopen() time and inits the callbacks */
972 int mod_dirlisting_plugin_init(plugin
*p
);
973 int mod_dirlisting_plugin_init(plugin
*p
) {
974 p
->version
= LIGHTTPD_VERSION_ID
;
975 p
->name
= buffer_init_string("dirlisting");
977 p
->init
= mod_dirlisting_init
;
978 p
->handle_subrequest_start
= mod_dirlisting_subrequest
;
979 p
->set_defaults
= mod_dirlisting_set_defaults
;
980 p
->cleanup
= mod_dirlisting_free
;