10 #include "stat_cache.h"
24 * this is a dirlisting for a lighttpd plugin
27 #ifdef HAVE_ATTR_ATTRIBUTES_H
28 #include <attr/attributes.h>
31 #ifdef HAVE_SYS_EXTATTR_H
32 #include <sys/extattr.h>
37 /* plugin config for all request/connections */
54 unsigned short dir_listing
;
55 unsigned short hide_dot_files
;
56 unsigned short show_readme
;
57 unsigned short hide_readme_file
;
58 unsigned short encode_readme
;
59 unsigned short show_header
;
60 unsigned short hide_header_file
;
61 unsigned short encode_header
;
62 unsigned short auto_layout
;
64 excludes_buffer
*excludes
;
75 buffer
*content_charset
;
77 plugin_config
**config_storage
;
82 static excludes_buffer
*excludes_buffer_init(void) {
85 exb
= calloc(1, sizeof(*exb
));
90 static int excludes_buffer_append(excludes_buffer
*exb
, buffer
*string
) {
96 if (!string
) return -1;
102 exb
->ptr
= malloc(exb
->size
* sizeof(*exb
->ptr
));
104 for(i
= 0; i
< exb
->size
; i
++) {
105 exb
->ptr
[i
] = calloc(1, sizeof(**exb
->ptr
));
107 } else if (exb
->used
== exb
->size
) {
110 exb
->ptr
= realloc(exb
->ptr
, exb
->size
* sizeof(*exb
->ptr
));
112 for(i
= exb
->used
; i
< exb
->size
; i
++) {
113 exb
->ptr
[i
] = calloc(1, sizeof(**exb
->ptr
));
118 if (NULL
== (exb
->ptr
[exb
->used
]->regex
= pcre_compile(string
->ptr
, 0,
119 &errptr
, &erroff
, NULL
))) {
123 exb
->ptr
[exb
->used
]->string
= buffer_init();
124 buffer_copy_buffer(exb
->ptr
[exb
->used
]->string
, string
);
137 static void excludes_buffer_free(excludes_buffer
*exb
) {
141 for (i
= 0; i
< exb
->size
; i
++) {
142 if (exb
->ptr
[i
]->regex
) pcre_free(exb
->ptr
[i
]->regex
);
143 if (exb
->ptr
[i
]->string
) buffer_free(exb
->ptr
[i
]->string
);
147 if (exb
->ptr
) free(exb
->ptr
);
153 /* init the plugin data */
154 INIT_FUNC(mod_dirlisting_init
) {
157 p
= calloc(1, sizeof(*p
));
159 p
->tmp_buf
= buffer_init();
160 p
->content_charset
= buffer_init();
165 /* detroy the plugin data */
166 FREE_FUNC(mod_dirlisting_free
) {
167 plugin_data
*p
= p_d
;
171 if (!p
) return HANDLER_GO_ON
;
173 if (p
->config_storage
) {
175 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
176 plugin_config
*s
= p
->config_storage
[i
];
180 excludes_buffer_free(s
->excludes
);
181 buffer_free(s
->external_css
);
182 buffer_free(s
->encoding
);
183 buffer_free(s
->set_footer
);
187 free(p
->config_storage
);
190 buffer_free(p
->tmp_buf
);
191 buffer_free(p
->content_charset
);
195 return HANDLER_GO_ON
;
198 /* handle plugin config and check values */
200 #define CONFIG_EXCLUDE "dir-listing.exclude"
201 #define CONFIG_ACTIVATE "dir-listing.activate"
202 #define CONFIG_HIDE_DOTFILES "dir-listing.hide-dotfiles"
203 #define CONFIG_EXTERNAL_CSS "dir-listing.external-css"
204 #define CONFIG_ENCODING "dir-listing.encoding"
205 #define CONFIG_SHOW_README "dir-listing.show-readme"
206 #define CONFIG_HIDE_README_FILE "dir-listing.hide-readme-file"
207 #define CONFIG_SHOW_HEADER "dir-listing.show-header"
208 #define CONFIG_HIDE_HEADER_FILE "dir-listing.hide-header-file"
209 #define CONFIG_DIR_LISTING "server.dir-listing"
210 #define CONFIG_SET_FOOTER "dir-listing.set-footer"
211 #define CONFIG_ENCODE_README "dir-listing.encode-readme"
212 #define CONFIG_ENCODE_HEADER "dir-listing.encode-header"
213 #define CONFIG_AUTO_LAYOUT "dir-listing.auto-layout"
216 SETDEFAULTS_FUNC(mod_dirlisting_set_defaults
) {
217 plugin_data
*p
= p_d
;
220 config_values_t cv
[] = {
221 { CONFIG_EXCLUDE
, NULL
, T_CONFIG_LOCAL
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
222 { CONFIG_ACTIVATE
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
223 { CONFIG_HIDE_DOTFILES
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
224 { CONFIG_EXTERNAL_CSS
, NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
225 { CONFIG_ENCODING
, NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 4 */
226 { CONFIG_SHOW_README
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 5 */
227 { CONFIG_HIDE_README_FILE
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 6 */
228 { CONFIG_SHOW_HEADER
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 7 */
229 { CONFIG_HIDE_HEADER_FILE
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 8 */
230 { CONFIG_DIR_LISTING
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 9 */
231 { CONFIG_SET_FOOTER
, NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 10 */
232 { CONFIG_ENCODE_README
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 11 */
233 { CONFIG_ENCODE_HEADER
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 12 */
234 { CONFIG_AUTO_LAYOUT
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 13 */
236 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
239 if (!p
) return HANDLER_ERROR
;
241 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
243 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
244 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
246 data_unset
*du_excludes
;
248 s
= calloc(1, sizeof(plugin_config
));
249 s
->excludes
= excludes_buffer_init();
251 s
->external_css
= buffer_init();
252 s
->hide_dot_files
= 1;
254 s
->hide_readme_file
= 0;
256 s
->hide_header_file
= 0;
257 s
->encode_readme
= 1;
258 s
->encode_header
= 1;
261 s
->encoding
= buffer_init();
262 s
->set_footer
= buffer_init();
264 cv
[0].destination
= s
->excludes
;
265 cv
[1].destination
= &(s
->dir_listing
);
266 cv
[2].destination
= &(s
->hide_dot_files
);
267 cv
[3].destination
= s
->external_css
;
268 cv
[4].destination
= s
->encoding
;
269 cv
[5].destination
= &(s
->show_readme
);
270 cv
[6].destination
= &(s
->hide_readme_file
);
271 cv
[7].destination
= &(s
->show_header
);
272 cv
[8].destination
= &(s
->hide_header_file
);
273 cv
[9].destination
= &(s
->dir_listing
); /* old name */
274 cv
[10].destination
= s
->set_footer
;
275 cv
[11].destination
= &(s
->encode_readme
);
276 cv
[12].destination
= &(s
->encode_header
);
277 cv
[13].destination
= &(s
->auto_layout
);
279 p
->config_storage
[i
] = s
;
281 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
282 return HANDLER_ERROR
;
285 if (NULL
!= (du_excludes
= array_get_element(config
->value
, CONFIG_EXCLUDE
))) {
286 array
*excludes_list
;
289 if (du_excludes
->type
!= TYPE_ARRAY
) {
290 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
291 "unexpected type for key: ", CONFIG_EXCLUDE
, "array of strings");
292 return HANDLER_ERROR
;
295 excludes_list
= ((data_array
*)du_excludes
)->value
;
298 if (excludes_list
->used
> 0) {
299 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
300 "pcre support is missing for: ", CONFIG_EXCLUDE
, ", please install libpcre and the headers");
301 return HANDLER_ERROR
;
304 for (j
= 0; j
< excludes_list
->used
; j
++) {
305 data_unset
*du_exclude
= excludes_list
->data
[j
];
307 if (du_exclude
->type
!= TYPE_STRING
) {
308 log_error_write(srv
, __FILE__
, __LINE__
, "sssbs",
309 "unexpected type for key: ", CONFIG_EXCLUDE
, "[",
310 du_exclude
->key
, "](string)");
311 return HANDLER_ERROR
;
314 if (0 != excludes_buffer_append(s
->excludes
, ((data_string
*)(du_exclude
))->value
)) {
315 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
316 "pcre-compile failed for", ((data_string
*)(du_exclude
))->value
);
317 return HANDLER_ERROR
;
324 return HANDLER_GO_ON
;
329 static int mod_dirlisting_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
331 plugin_config
*s
= p
->config_storage
[0];
335 PATCH(hide_dot_files
);
338 PATCH(hide_readme_file
);
340 PATCH(hide_header_file
);
343 PATCH(encode_readme
);
344 PATCH(encode_header
);
347 /* skip the first, the global context */
348 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
349 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
350 s
= p
->config_storage
[i
];
352 /* condition didn't match */
353 if (!config_check_cond(srv
, con
, dc
)) continue;
356 for (j
= 0; j
< dc
->value
->used
; j
++) {
357 data_unset
*du
= dc
->value
->data
[j
];
359 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_ACTIVATE
)) ||
360 buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_DIR_LISTING
))) {
362 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_HIDE_DOTFILES
))) {
363 PATCH(hide_dot_files
);
364 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_EXTERNAL_CSS
))) {
366 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_ENCODING
))) {
368 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_SHOW_README
))) {
370 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_HIDE_README_FILE
))) {
371 PATCH(hide_readme_file
);
372 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_SHOW_HEADER
))) {
374 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_HIDE_HEADER_FILE
))) {
375 PATCH(hide_header_file
);
376 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_SET_FOOTER
))) {
378 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_EXCLUDE
))) {
380 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_ENCODE_README
))) {
381 PATCH(encode_readme
);
382 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_ENCODE_HEADER
))) {
383 PATCH(encode_header
);
384 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_AUTO_LAYOUT
))) {
406 #define DIRLIST_ENT_NAME(ent) ((char*)(ent) + sizeof(dirls_entry_t))
407 #define DIRLIST_BLOB_SIZE 16
409 /* simple combsort algorithm */
410 static void http_dirls_sort(dirls_entry_t
**ent
, int num
) {
417 gap
= (gap
* 10) / 13;
418 if (gap
== 9 || gap
== 10)
424 for (i
= 0; i
< num
- gap
; i
++) {
426 if (strcmp(DIRLIST_ENT_NAME(ent
[i
]), DIRLIST_ENT_NAME(ent
[j
])) > 0) {
434 } while (gap
> 1 || swapped
);
437 /* buffer must be able to hold "999.9K"
438 * conversion is simple but not perfect
440 static int http_list_directory_sizefmt(char *buf
, size_t bufsz
, off_t size
) {
441 const char unit
[] = "KMGTPE"; /* Kilo, Mega, Tera, Peta, Exa */
442 const char *u
= unit
- 1; /* u will always increment at least once */
452 remain
= (int) size
& 1023;
455 if ((size
& (~0 ^ 1023)) == 0)
468 li_itostrn(buf
, bufsz
, size
);
469 buflen
= strlen(buf
);
470 if (buflen
+ 3 >= bufsz
) return buflen
;
472 buf
[buflen
+1] = remain
+ '0';
474 buf
[buflen
+3] = '\0';
479 static void http_list_directory_header(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*out
) {
482 if (p
->conf
.auto_layout
) {
483 buffer_append_string_len(out
, CONST_STR_LEN(
484 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
485 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n"
489 buffer_append_string_encoded(out
, CONST_BUF_LEN(con
->uri
.path
), ENCODING_MINIMAL_XML
);
490 buffer_append_string_len(out
, CONST_STR_LEN("</title>\n"));
492 if (!buffer_string_is_empty(p
->conf
.external_css
)) {
493 buffer_append_string_len(out
, CONST_STR_LEN("<link rel=\"stylesheet\" type=\"text/css\" href=\""));
494 buffer_append_string_buffer(out
, p
->conf
.external_css
);
495 buffer_append_string_len(out
, CONST_STR_LEN("\" />\n"));
497 buffer_append_string_len(out
, CONST_STR_LEN(
498 "<style type=\"text/css\">\n"
499 "a, a:active {text-decoration: none; color: blue;}\n"
500 "a:visited {color: #48468F;}\n"
501 "a:hover, a:focus {text-decoration: underline; color: red;}\n"
502 "body {background-color: #F5F5F5;}\n"
503 "h2 {margin-bottom: 12px;}\n"
504 "table {margin-left: 12px;}\n"
506 " font: 90% monospace;"
510 " font-weight: bold;"
511 " padding-right: 14px;"
512 " padding-bottom: 3px;"
514 "td {padding-right: 14px;}\n"
515 "td.s, th.s {text-align: right;}\n"
517 " background-color: white;"
518 " border-top: 1px solid #646464;"
519 " border-bottom: 1px solid #646464;"
520 " padding-top: 10px;"
521 " padding-bottom: 14px;"
524 " font: 90% monospace;"
532 buffer_append_string_len(out
, CONST_STR_LEN("</head>\n<body>\n"));
536 if (p
->conf
.show_header
) {
538 /* if we have a HEADER file, display it in <pre class="header"></pre> */
540 buffer_copy_buffer(p
->tmp_buf
, con
->physical
.path
);
541 buffer_append_slash(p
->tmp_buf
);
542 buffer_append_string_len(p
->tmp_buf
, CONST_STR_LEN("HEADER.txt"));
544 if (-1 != stream_open(&s
, p
->tmp_buf
)) {
545 if (p
->conf
.encode_header
) {
546 buffer_append_string_len(out
, CONST_STR_LEN("<pre class=\"header\">"));
547 buffer_append_string_encoded(out
, s
.start
, s
.size
, ENCODING_MINIMAL_XML
);
548 buffer_append_string_len(out
, CONST_STR_LEN("</pre>"));
550 buffer_append_string_len(out
, s
.start
, s
.size
);
556 buffer_append_string_len(out
, CONST_STR_LEN("<h2>Index of "));
557 buffer_append_string_encoded(out
, CONST_BUF_LEN(con
->uri
.path
), ENCODING_MINIMAL_XML
);
558 buffer_append_string_len(out
, CONST_STR_LEN(
560 "<div class=\"list\">\n"
561 "<table summary=\"Directory Listing\" cellpadding=\"0\" cellspacing=\"0\">\n"
564 "<th class=\"n\">Name</th>"
565 "<th class=\"m\">Last Modified</th>"
566 "<th class=\"s\">Size</th>"
567 "<th class=\"t\">Type</th>"
572 "<td class=\"n\"><a href=\"../\">Parent Directory</a>/</td>"
573 "<td class=\"m\"> </td>"
574 "<td class=\"s\">- </td>"
575 "<td class=\"t\">Directory</td>"
580 static void http_list_directory_footer(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*out
) {
583 buffer_append_string_len(out
, CONST_STR_LEN(
589 if (p
->conf
.show_readme
) {
591 /* if we have a README file, display it in <pre class="readme"></pre> */
593 buffer_copy_buffer(p
->tmp_buf
, con
->physical
.path
);
594 buffer_append_slash(p
->tmp_buf
);
595 buffer_append_string_len(p
->tmp_buf
, CONST_STR_LEN("README.txt"));
597 if (-1 != stream_open(&s
, p
->tmp_buf
)) {
598 if (p
->conf
.encode_readme
) {
599 buffer_append_string_len(out
, CONST_STR_LEN("<pre class=\"readme\">"));
600 buffer_append_string_encoded(out
, s
.start
, s
.size
, ENCODING_MINIMAL_XML
);
601 buffer_append_string_len(out
, CONST_STR_LEN("</pre>"));
603 buffer_append_string_len(out
, s
.start
, s
.size
);
609 if(p
->conf
.auto_layout
) {
610 buffer_append_string_len(out
, CONST_STR_LEN(
611 "<div class=\"foot\">"
614 if (!buffer_string_is_empty(p
->conf
.set_footer
)) {
615 buffer_append_string_buffer(out
, p
->conf
.set_footer
);
616 } else if (buffer_is_empty(con
->conf
.server_tag
)) {
617 buffer_append_string_len(out
, CONST_STR_LEN(PACKAGE_DESC
));
619 buffer_append_string_buffer(out
, con
->conf
.server_tag
);
622 buffer_append_string_len(out
, CONST_STR_LEN(
630 static int http_list_directory(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*dir
) {
635 char *path
, *path_file
;
637 int hide_dotfiles
= p
->conf
.hide_dot_files
;
638 dirls_list_t dirs
, files
, *list
;
640 char sizebuf
[sizeof("999.9K")];
641 char datebuf
[sizeof("2005-Jan-01 22:23:24")];
643 const char *content_type
;
645 #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
649 #ifdef HAVE_LOCALTIME_R
653 if (buffer_string_is_empty(dir
)) return -1;
655 i
= buffer_string_length(dir
);
658 if (0 >= (name_max
= pathconf(dir
->ptr
, _PC_NAME_MAX
))) {
659 /* some broken fs (fuse) return 0 instead of -1 */
663 name_max
= 255; /* stupid default */
666 #elif defined __WIN32
667 name_max
= FILENAME_MAX
;
672 path
= malloc(buffer_string_length(dir
) + name_max
+ 1);
673 force_assert(NULL
!= path
);
674 strcpy(path
, dir
->ptr
);
675 path_file
= path
+ i
;
677 if (NULL
== (dp
= opendir(path
))) {
678 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
679 "opendir failed:", dir
, strerror(errno
));
685 dirs
.ent
= (dirls_entry_t
**) malloc(sizeof(dirls_entry_t
*) * DIRLIST_BLOB_SIZE
);
686 force_assert(dirs
.ent
);
687 dirs
.size
= DIRLIST_BLOB_SIZE
;
689 files
.ent
= (dirls_entry_t
**) malloc(sizeof(dirls_entry_t
*) * DIRLIST_BLOB_SIZE
);
690 force_assert(files
.ent
);
691 files
.size
= DIRLIST_BLOB_SIZE
;
694 while ((dent
= readdir(dp
)) != NULL
) {
695 unsigned short exclude_match
= 0;
697 if (dent
->d_name
[0] == '.') {
700 if (dent
->d_name
[1] == '\0')
702 if (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0')
706 if (p
->conf
.hide_readme_file
) {
707 if (strcmp(dent
->d_name
, "README.txt") == 0)
710 if (p
->conf
.hide_header_file
) {
711 if (strcmp(dent
->d_name
, "HEADER.txt") == 0)
715 /* compare d_name against excludes array
716 * elements, skipping any that match.
719 for(i
= 0; i
< p
->conf
.excludes
->used
; i
++) {
723 pcre
*regex
= p
->conf
.excludes
->ptr
[i
]->regex
;
725 if ((n
= pcre_exec(regex
, NULL
, dent
->d_name
,
726 strlen(dent
->d_name
), 0, 0, ovec
, 3 * N
)) < 0) {
727 if (n
!= PCRE_ERROR_NOMATCH
) {
728 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
729 "execution error while matching:", n
);
731 /* aborting would require a lot of manual cleanup here.
732 * skip instead (to not leak names that break pcre matching)
749 i
= strlen(dent
->d_name
);
751 /* NOTE: the manual says, d_name is never more than NAME_MAX
752 * so this should actually not be a buffer-overflow-risk
754 if (i
> (size_t)name_max
) continue;
756 memcpy(path_file
, dent
->d_name
, i
+ 1);
757 if (stat(path
, &st
) != 0)
761 if (S_ISDIR(st
.st_mode
))
764 if (list
->used
== list
->size
) {
765 list
->size
+= DIRLIST_BLOB_SIZE
;
766 list
->ent
= (dirls_entry_t
**) realloc(list
->ent
, sizeof(dirls_entry_t
*) * list
->size
);
767 force_assert(list
->ent
);
770 tmp
= (dirls_entry_t
*) malloc(sizeof(dirls_entry_t
) + 1 + i
);
771 tmp
->mtime
= st
.st_mtime
;
772 tmp
->size
= st
.st_size
;
774 memcpy(DIRLIST_ENT_NAME(tmp
), dent
->d_name
, i
+ 1);
776 list
->ent
[list
->used
++] = tmp
;
780 if (dirs
.used
) http_dirls_sort(dirs
.ent
, dirs
.used
);
782 if (files
.used
) http_dirls_sort(files
.ent
, files
.used
);
785 buffer_copy_string_len(out
, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\""));
786 if (buffer_string_is_empty(p
->conf
.encoding
)) {
787 buffer_append_string_len(out
, CONST_STR_LEN("iso-8859-1"));
789 buffer_append_string_buffer(out
, p
->conf
.encoding
);
791 buffer_append_string_len(out
, CONST_STR_LEN("\"?>\n"));
792 http_list_directory_header(srv
, con
, p
, out
);
795 for (i
= 0; i
< dirs
.used
; i
++) {
798 #ifdef HAVE_LOCALTIME_R
799 localtime_r(&(tmp
->mtime
), &tm
);
800 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", &tm
);
802 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", localtime(&(tmp
->mtime
)));
805 buffer_append_string_len(out
, CONST_STR_LEN("<tr class=\"d\"><td class=\"n\"><a href=\""));
806 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_REL_URI_PART
);
807 buffer_append_string_len(out
, CONST_STR_LEN("/\">"));
808 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_MINIMAL_XML
);
809 buffer_append_string_len(out
, CONST_STR_LEN("</a>/</td><td class=\"m\">"));
810 buffer_append_string_len(out
, datebuf
, sizeof(datebuf
) - 1);
811 buffer_append_string_len(out
, CONST_STR_LEN("</td><td class=\"s\">- </td><td class=\"t\">Directory</td></tr>\n"));
817 for (i
= 0; i
< files
.used
; i
++) {
821 #if defined(HAVE_XATTR)
822 if (con
->conf
.use_xattr
) {
823 memcpy(path_file
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
+ 1);
824 attrlen
= sizeof(attrval
) - 1;
825 if (attr_get(path
, srv
->srvconf
.xattr_name
->ptr
, attrval
, &attrlen
, 0) == 0) {
826 attrval
[attrlen
] = '\0';
827 content_type
= attrval
;
830 #elif defined(HAVE_EXTATTR)
831 if (con
->conf
.use_xattr
) {
832 memcpy(path_file
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
+ 1);
833 if(-1 != (attrlen
= extattr_get_file(path
, EXTATTR_NAMESPACE_USER
, srv
->srvconf
.xattr_name
->ptr
, attrval
, sizeof(attrval
)-1))) {
834 attrval
[attrlen
] = '\0';
835 content_type
= attrval
;
840 if (content_type
== NULL
) {
841 content_type
= "application/octet-stream";
842 for (k
= 0; k
< con
->conf
.mimetypes
->used
; k
++) {
843 data_string
*ds
= (data_string
*)con
->conf
.mimetypes
->data
[k
];
846 if (buffer_is_empty(ds
->key
))
849 ct_len
= buffer_string_length(ds
->key
);
850 if (tmp
->namelen
< ct_len
)
853 if (0 == strncasecmp(DIRLIST_ENT_NAME(tmp
) + tmp
->namelen
- ct_len
, ds
->key
->ptr
, ct_len
)) {
854 content_type
= ds
->value
->ptr
;
860 #ifdef HAVE_LOCALTIME_R
861 localtime_r(&(tmp
->mtime
), &tm
);
862 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", &tm
);
864 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", localtime(&(tmp
->mtime
)));
866 http_list_directory_sizefmt(sizebuf
, sizeof(sizebuf
), tmp
->size
);
868 buffer_append_string_len(out
, CONST_STR_LEN("<tr><td class=\"n\"><a href=\""));
869 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_REL_URI_PART
);
870 buffer_append_string_len(out
, CONST_STR_LEN("\">"));
871 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_MINIMAL_XML
);
872 buffer_append_string_len(out
, CONST_STR_LEN("</a></td><td class=\"m\">"));
873 buffer_append_string_len(out
, datebuf
, sizeof(datebuf
) - 1);
874 buffer_append_string_len(out
, CONST_STR_LEN("</td><td class=\"s\">"));
875 buffer_append_string(out
, sizebuf
);
876 buffer_append_string_len(out
, CONST_STR_LEN("</td><td class=\"t\">"));
877 buffer_append_string(out
, content_type
);
878 buffer_append_string_len(out
, CONST_STR_LEN("</td></tr>\n"));
887 http_list_directory_footer(srv
, con
, p
, out
);
889 /* Insert possible charset to Content-Type */
890 if (buffer_string_is_empty(p
->conf
.encoding
)) {
891 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
893 buffer_copy_string_len(p
->content_charset
, CONST_STR_LEN("text/html; charset="));
894 buffer_append_string_buffer(p
->content_charset
, p
->conf
.encoding
);
895 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(p
->content_charset
));
898 con
->file_finished
= 1;
899 chunkqueue_append_buffer(con
->write_queue
, out
);
907 URIHANDLER_FUNC(mod_dirlisting_subrequest
) {
908 plugin_data
*p
= p_d
;
909 stat_cache_entry
*sce
= NULL
;
913 /* we only handle GET, POST and HEAD */
914 switch(con
->request
.http_method
) {
915 case HTTP_METHOD_GET
:
916 case HTTP_METHOD_POST
:
917 case HTTP_METHOD_HEAD
:
920 return HANDLER_GO_ON
;
923 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
925 if (buffer_is_empty(con
->physical
.path
)) return HANDLER_GO_ON
;
926 if (buffer_is_empty(con
->uri
.path
)) return HANDLER_GO_ON
;
927 if (con
->uri
.path
->ptr
[buffer_string_length(con
->uri
.path
) - 1] != '/') return HANDLER_GO_ON
;
929 mod_dirlisting_patch_connection(srv
, con
, p
);
931 if (!p
->conf
.dir_listing
) return HANDLER_GO_ON
;
933 if (con
->conf
.log_request_handling
) {
934 log_error_write(srv
, __FILE__
, __LINE__
, "s", "-- handling the request as Dir-Listing");
935 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "URI :", con
->uri
.path
);
938 if (HANDLER_ERROR
== stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) {
939 log_error_write(srv
, __FILE__
, __LINE__
, "SB", "stat_cache_get_entry failed: ", con
->physical
.path
);
943 if (!S_ISDIR(sce
->st
.st_mode
)) return HANDLER_GO_ON
;
945 if (http_list_directory(srv
, con
, p
, con
->physical
.path
)) {
946 /* dirlisting failed */
947 con
->http_status
= 403;
950 buffer_reset(con
->physical
.path
);
953 return HANDLER_FINISHED
;
956 /* this function is called at dlopen() time and inits the callbacks */
958 int mod_dirlisting_plugin_init(plugin
*p
);
959 int mod_dirlisting_plugin_init(plugin
*p
) {
960 p
->version
= LIGHTTPD_VERSION_ID
;
961 p
->name
= buffer_init_string("dirlisting");
963 p
->init
= mod_dirlisting_init
;
964 p
->handle_subrequest_start
= mod_dirlisting_subrequest
;
965 p
->set_defaults
= mod_dirlisting_set_defaults
;
966 p
->cleanup
= mod_dirlisting_free
;