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>
35 /* plugin config for all request/connections */
52 unsigned short dir_listing
;
53 unsigned short hide_dot_files
;
54 unsigned short hide_readme_file
;
55 unsigned short encode_readme
;
56 unsigned short hide_header_file
;
57 unsigned short encode_header
;
58 unsigned short auto_layout
;
60 excludes_buffer
*excludes
;
74 buffer
*content_charset
;
76 plugin_config
**config_storage
;
81 static excludes_buffer
*excludes_buffer_init(void) {
84 exb
= calloc(1, sizeof(*exb
));
89 static int excludes_buffer_append(excludes_buffer
*exb
, buffer
*string
) {
95 if (!string
) return -1;
101 exb
->ptr
= malloc(exb
->size
* sizeof(*exb
->ptr
));
103 for(i
= 0; i
< exb
->size
; i
++) {
104 exb
->ptr
[i
] = calloc(1, sizeof(**exb
->ptr
));
106 } else if (exb
->used
== exb
->size
) {
109 exb
->ptr
= realloc(exb
->ptr
, exb
->size
* sizeof(*exb
->ptr
));
111 for(i
= exb
->used
; i
< exb
->size
; i
++) {
112 exb
->ptr
[i
] = calloc(1, sizeof(**exb
->ptr
));
117 if (NULL
== (exb
->ptr
[exb
->used
]->regex
= pcre_compile(string
->ptr
, 0,
118 &errptr
, &erroff
, NULL
))) {
122 exb
->ptr
[exb
->used
]->string
= buffer_init();
123 buffer_copy_buffer(exb
->ptr
[exb
->used
]->string
, string
);
136 static void excludes_buffer_free(excludes_buffer
*exb
) {
140 for (i
= 0; i
< exb
->size
; i
++) {
141 if (exb
->ptr
[i
]->regex
) pcre_free(exb
->ptr
[i
]->regex
);
142 if (exb
->ptr
[i
]->string
) buffer_free(exb
->ptr
[i
]->string
);
146 if (exb
->ptr
) free(exb
->ptr
);
152 /* init the plugin data */
153 INIT_FUNC(mod_dirlisting_init
) {
156 p
= calloc(1, sizeof(*p
));
158 p
->tmp_buf
= buffer_init();
159 p
->content_charset
= buffer_init();
164 /* detroy the plugin data */
165 FREE_FUNC(mod_dirlisting_free
) {
166 plugin_data
*p
= p_d
;
170 if (!p
) return HANDLER_GO_ON
;
172 if (p
->config_storage
) {
174 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
175 plugin_config
*s
= p
->config_storage
[i
];
179 excludes_buffer_free(s
->excludes
);
180 buffer_free(s
->show_readme
);
181 buffer_free(s
->show_header
);
182 buffer_free(s
->external_css
);
183 buffer_free(s
->external_js
);
184 buffer_free(s
->encoding
);
185 buffer_free(s
->set_footer
);
189 free(p
->config_storage
);
192 buffer_free(p
->tmp_buf
);
193 buffer_free(p
->content_charset
);
197 return HANDLER_GO_ON
;
200 /* handle plugin config and check values */
202 #define CONFIG_EXCLUDE "dir-listing.exclude"
203 #define CONFIG_ACTIVATE "dir-listing.activate"
204 #define CONFIG_HIDE_DOTFILES "dir-listing.hide-dotfiles"
205 #define CONFIG_EXTERNAL_CSS "dir-listing.external-css"
206 #define CONFIG_EXTERNAL_JS "dir-listing.external-js"
207 #define CONFIG_ENCODING "dir-listing.encoding"
208 #define CONFIG_SHOW_README "dir-listing.show-readme"
209 #define CONFIG_HIDE_README_FILE "dir-listing.hide-readme-file"
210 #define CONFIG_SHOW_HEADER "dir-listing.show-header"
211 #define CONFIG_HIDE_HEADER_FILE "dir-listing.hide-header-file"
212 #define CONFIG_DIR_LISTING "server.dir-listing"
213 #define CONFIG_SET_FOOTER "dir-listing.set-footer"
214 #define CONFIG_ENCODE_README "dir-listing.encode-readme"
215 #define CONFIG_ENCODE_HEADER "dir-listing.encode-header"
216 #define CONFIG_AUTO_LAYOUT "dir-listing.auto-layout"
219 SETDEFAULTS_FUNC(mod_dirlisting_set_defaults
) {
220 plugin_data
*p
= p_d
;
223 config_values_t cv
[] = {
224 { CONFIG_EXCLUDE
, NULL
, T_CONFIG_LOCAL
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
225 { CONFIG_ACTIVATE
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
226 { CONFIG_HIDE_DOTFILES
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
227 { CONFIG_EXTERNAL_CSS
, NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
228 { CONFIG_ENCODING
, NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 4 */
229 { CONFIG_SHOW_README
, NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 5 */
230 { CONFIG_HIDE_README_FILE
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 6 */
231 { CONFIG_SHOW_HEADER
, NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 7 */
232 { CONFIG_HIDE_HEADER_FILE
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 8 */
233 { CONFIG_DIR_LISTING
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 9 */
234 { CONFIG_SET_FOOTER
, NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 10 */
235 { CONFIG_ENCODE_README
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 11 */
236 { CONFIG_ENCODE_HEADER
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 12 */
237 { CONFIG_AUTO_LAYOUT
, NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 13 */
238 { CONFIG_EXTERNAL_JS
, NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 14 */
240 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
243 if (!p
) return HANDLER_ERROR
;
245 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
247 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
248 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
250 data_unset
*du_excludes
;
252 s
= calloc(1, sizeof(plugin_config
));
253 s
->excludes
= excludes_buffer_init();
255 s
->show_readme
= buffer_init();
256 s
->show_header
= buffer_init();
257 s
->external_css
= buffer_init();
258 s
->external_js
= buffer_init();
259 s
->hide_dot_files
= 1;
260 s
->hide_readme_file
= 0;
261 s
->hide_header_file
= 0;
262 s
->encode_readme
= 1;
263 s
->encode_header
= 1;
266 s
->encoding
= buffer_init();
267 s
->set_footer
= buffer_init();
269 cv
[0].destination
= s
->excludes
;
270 cv
[1].destination
= &(s
->dir_listing
);
271 cv
[2].destination
= &(s
->hide_dot_files
);
272 cv
[3].destination
= s
->external_css
;
273 cv
[4].destination
= s
->encoding
;
274 cv
[5].destination
= s
->show_readme
;
275 cv
[6].destination
= &(s
->hide_readme_file
);
276 cv
[7].destination
= s
->show_header
;
277 cv
[8].destination
= &(s
->hide_header_file
);
278 cv
[9].destination
= &(s
->dir_listing
); /* old name */
279 cv
[10].destination
= s
->set_footer
;
280 cv
[11].destination
= &(s
->encode_readme
);
281 cv
[12].destination
= &(s
->encode_header
);
282 cv
[13].destination
= &(s
->auto_layout
);
283 cv
[14].destination
= s
->external_js
;
285 p
->config_storage
[i
] = s
;
287 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
288 return HANDLER_ERROR
;
291 if (NULL
!= (du_excludes
= array_get_element(config
->value
, CONFIG_EXCLUDE
))) {
292 array
*excludes_list
;
295 if (du_excludes
->type
!= TYPE_ARRAY
) {
296 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
297 "unexpected type for key: ", CONFIG_EXCLUDE
, "array of strings");
298 return HANDLER_ERROR
;
301 excludes_list
= ((data_array
*)du_excludes
)->value
;
304 if (excludes_list
->used
> 0) {
305 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
306 "pcre support is missing for: ", CONFIG_EXCLUDE
, ", please install libpcre and the headers");
307 return HANDLER_ERROR
;
310 for (j
= 0; j
< excludes_list
->used
; j
++) {
311 data_unset
*du_exclude
= excludes_list
->data
[j
];
313 if (du_exclude
->type
!= TYPE_STRING
) {
314 log_error_write(srv
, __FILE__
, __LINE__
, "sssbs",
315 "unexpected type for key: ", CONFIG_EXCLUDE
, "[",
316 du_exclude
->key
, "](string)");
317 return HANDLER_ERROR
;
320 if (0 != excludes_buffer_append(s
->excludes
, ((data_string
*)(du_exclude
))->value
)) {
321 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
322 "pcre-compile failed for", ((data_string
*)(du_exclude
))->value
);
323 return HANDLER_ERROR
;
329 if (!buffer_string_is_empty(s
->show_readme
)) {
330 if (buffer_is_equal_string(s
->show_readme
, CONST_STR_LEN("enable"))) {
331 buffer_copy_string_len(s
->show_readme
, CONST_STR_LEN("README.txt"));
333 else if (buffer_is_equal_string(s
->show_readme
, CONST_STR_LEN("disable"))) {
334 buffer_string_set_length(s
->show_readme
, 0);
338 if (!buffer_string_is_empty(s
->show_header
)) {
339 if (buffer_is_equal_string(s
->show_header
, CONST_STR_LEN("enable"))) {
340 buffer_copy_string_len(s
->show_header
, CONST_STR_LEN("HEADER.txt"));
342 else if (buffer_is_equal_string(s
->show_header
, CONST_STR_LEN("disable"))) {
343 buffer_string_set_length(s
->show_header
, 0);
348 return HANDLER_GO_ON
;
353 static int mod_dirlisting_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
355 plugin_config
*s
= p
->config_storage
[0];
360 PATCH(hide_dot_files
);
363 PATCH(hide_readme_file
);
365 PATCH(hide_header_file
);
368 PATCH(encode_readme
);
369 PATCH(encode_header
);
372 /* skip the first, the global context */
373 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
374 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
375 s
= p
->config_storage
[i
];
377 /* condition didn't match */
378 if (!config_check_cond(srv
, con
, dc
)) continue;
381 for (j
= 0; j
< dc
->value
->used
; j
++) {
382 data_unset
*du
= dc
->value
->data
[j
];
384 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_ACTIVATE
)) ||
385 buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_DIR_LISTING
))) {
387 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_HIDE_DOTFILES
))) {
388 PATCH(hide_dot_files
);
389 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_EXTERNAL_CSS
))) {
391 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_EXTERNAL_JS
))) {
393 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_ENCODING
))) {
395 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_SHOW_README
))) {
397 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_HIDE_README_FILE
))) {
398 PATCH(hide_readme_file
);
399 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_SHOW_HEADER
))) {
401 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_HIDE_HEADER_FILE
))) {
402 PATCH(hide_header_file
);
403 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_SET_FOOTER
))) {
405 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_EXCLUDE
))) {
407 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_ENCODE_README
))) {
408 PATCH(encode_readme
);
409 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_ENCODE_HEADER
))) {
410 PATCH(encode_header
);
411 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN(CONFIG_AUTO_LAYOUT
))) {
433 #define DIRLIST_ENT_NAME(ent) ((char*)(ent) + sizeof(dirls_entry_t))
434 #define DIRLIST_BLOB_SIZE 16
436 /* simple combsort algorithm */
437 static void http_dirls_sort(dirls_entry_t
**ent
, int num
) {
444 gap
= (gap
* 10) / 13;
445 if (gap
== 9 || gap
== 10)
451 for (i
= 0; i
< num
- gap
; i
++) {
453 if (strcmp(DIRLIST_ENT_NAME(ent
[i
]), DIRLIST_ENT_NAME(ent
[j
])) > 0) {
461 } while (gap
> 1 || swapped
);
464 /* buffer must be able to hold "999.9K"
465 * conversion is simple but not perfect
467 static int http_list_directory_sizefmt(char *buf
, size_t bufsz
, off_t size
) {
468 const char unit
[] = " KMGTPE"; /* Kilo, Mega, Giga, Tera, Peta, Exa */
469 const char *u
= unit
; /* u will always increment at least once */
479 remain
= (int) size
& 1023;
482 if ((size
& (~0 ^ 1023)) == 0)
495 li_itostrn(buf
, bufsz
, size
);
496 buflen
= strlen(buf
);
497 if (buflen
+ 3 >= bufsz
) return buflen
;
499 buf
[buflen
+1] = remain
+ '0';
501 buf
[buflen
+3] = '\0';
506 /* don't want to block when open()ing a fifo */
507 #if defined(O_NONBLOCK)
508 # define FIFO_NONBLOCK O_NONBLOCK
510 # define FIFO_NONBLOCK 0
513 static void http_list_directory_include_file(buffer
*out
, buffer
*path
, const char *classname
, int encode
) {
514 int fd
= open(path
->ptr
, O_RDONLY
| FIFO_NONBLOCK
);
518 if (-1 == fd
) return;
521 buffer_append_string_len(out
, CONST_STR_LEN("<pre class=\""));
522 buffer_append_string(out
, classname
);
523 buffer_append_string_len(out
, CONST_STR_LEN("\">"));
526 while ((rd
= read(fd
, buf
, sizeof(buf
))) > 0) {
528 buffer_append_string_encoded(out
, buf
, (size_t)rd
, ENCODING_MINIMAL_XML
);
530 buffer_append_string_len(out
, buf
, (size_t)rd
);
536 buffer_append_string_len(out
, CONST_STR_LEN("</pre>"));
540 /* portions copied from mod_status
541 * modified and specialized for stable dirlist sorting by name */
542 static const char js_simple_table_resort
[] = \
543 "var click_column;\n" \
544 "var name_column = 0;\n" \
545 "var date_column = 1;\n" \
546 "var size_column = 2;\n" \
547 "var type_column = 3;\n" \
548 "var prev_span = null;\n" \
550 "if (typeof(String.prototype.localeCompare) === 'undefined') {\n" \
551 " String.prototype.localeCompare = function(str, locale, options) {\n" \
552 " return ((this == str) ? 0 : ((this > str) ? 1 : -1));\n" \
556 "if (typeof(String.prototype.toLocaleUpperCase) === 'undefined') {\n" \
557 " String.prototype.toLocaleUpperCase = function() {\n" \
558 " return this.toUpperCase();\n" \
562 "function get_inner_text(el) {\n" \
563 " if((typeof el == 'string')||(typeof el == 'undefined'))\n" \
565 " if(el.innerText)\n" \
566 " return el.innerText;\n" \
568 " var str = \"\";\n" \
569 " var cs = el.childNodes;\n" \
570 " var l = cs.length;\n" \
571 " for (i=0;i<l;i++) {\n" \
572 " if (cs[i].nodeType==1) str += get_inner_text(cs[i]);\n" \
573 " else if (cs[i].nodeType==3) str += cs[i].nodeValue;\n" \
579 "function isdigit(c) {\n" \
580 " return (c >= '0' && c <= '9');\n" \
583 "function unit_multiplier(unit) {\n" \
584 " return (unit=='K') ? 1000\n" \
585 " : (unit=='M') ? 1000000\n" \
586 " : (unit=='G') ? 1000000000\n" \
587 " : (unit=='T') ? 1000000000000\n" \
588 " : (unit=='P') ? 1000000000000000\n" \
589 " : (unit=='E') ? 1000000000000000000 : 1;\n" \
592 "function sortfn_then_by_name(a,b,sort_column) {\n" \
593 " if (sort_column == name_column || sort_column == type_column) {\n" \
594 " var ad = (a.cells[type_column].innerHTML === 'Directory');\n" \
595 " var bd = (b.cells[type_column].innerHTML === 'Directory');\n" \
596 " if (ad != bd) return (ad ? -1 : 1);\n" \
598 " var at = get_inner_text(a.cells[sort_column]);\n" \
599 " var bt = get_inner_text(b.cells[sort_column]);\n" \
601 " if (a.cells[sort_column].className == 'int') {\n" \
602 " cmp = parseInt(at)-parseInt(bt);\n" \
603 " } else if (sort_column == date_column) {\n" \
604 " cmp = Date.parse(at.replace(/-/g, '/'))\n" \
605 " - Date.parse(bt.replace(/-/g, '/'));\n" \
606 " var ad = isdigit(at.substr(0,1));\n" \
607 " var bd = isdigit(bt.substr(0,1));\n" \
608 " if (ad != bd) return (!ad ? -1 : 1);\n" \
609 " } else if (sort_column == size_column) {\n" \
610 " var ai = parseInt(at, 10) * unit_multiplier(at.substr(-1,1));\n" \
611 " var bi = parseInt(bt, 10) * unit_multiplier(bt.substr(-1,1));\n" \
612 " if (at.substr(0,1) == '-') ai = -1;\n" \
613 " if (bt.substr(0,1) == '-') bi = -1;\n" \
614 " cmp = ai - bi;\n" \
616 " cmp = at.toLocaleUpperCase().localeCompare(bt.toLocaleUpperCase());\n" \
617 " if (0 != cmp) return cmp;\n" \
618 " cmp = at.localeCompare(bt);\n" \
620 " if (0 != cmp || sort_column == name_column) return cmp;\n" \
621 " return sortfn_then_by_name(a,b,name_column);\n" \
624 "function sortfn(a,b) {\n" \
625 " return sortfn_then_by_name(a,b,click_column);\n" \
628 "function resort(lnk) {\n" \
629 " var span = lnk.childNodes[1];\n" \
630 " var table = lnk.parentNode.parentNode.parentNode.parentNode;\n" \
631 " var rows = new Array();\n" \
632 " for (j=1;j<table.rows.length;j++)\n" \
633 " rows[j-1] = table.rows[j];\n" \
634 " click_column = lnk.parentNode.cellIndex;\n" \
635 " rows.sort(sortfn);\n" \
637 " if (prev_span != null) prev_span.innerHTML = '';\n" \
638 " if (span.getAttribute('sortdir')=='down') {\n" \
639 " span.innerHTML = '↑';\n" \
640 " span.setAttribute('sortdir','up');\n" \
641 " rows.reverse();\n" \
643 " span.innerHTML = '↓';\n" \
644 " span.setAttribute('sortdir','down');\n" \
646 " for (i=0;i<rows.length;i++)\n" \
647 " table.tBodies[0].appendChild(rows[i]);\n" \
648 " prev_span = span;\n" \
651 /* portions copied from mod_dirlist (lighttpd2) */
652 static const char js_simple_table_init_sort
[] = \
654 "function init_sort(init_sort_column, ascending) {\n" \
655 " var tables = document.getElementsByTagName(\"table\");\n" \
656 " for (var i = 0; i < tables.length; i++) {\n" \
657 " var table = tables[i];\n" \
658 " //var c = table.getAttribute(\"class\")\n" \
659 " //if (-1 != c.split(\" \").indexOf(\"sort\")) {\n" \
660 " var row = table.rows[0].cells;\n" \
661 " for (var j = 0; j < row.length; j++) {\n" \
662 " var n = row[j];\n" \
663 " if (n.childNodes.length == 1 && n.childNodes[0].nodeType == 3) {\n" \
664 " var link = document.createElement(\"a\");\n" \
665 " var title = n.childNodes[0].nodeValue.replace(/:$/, \"\");\n" \
666 " link.appendChild(document.createTextNode(title));\n" \
667 " link.setAttribute(\"href\", \"#\");\n" \
668 " link.setAttribute(\"class\", \"sortheader\");\n" \
669 " link.setAttribute(\"onclick\", \"resort(this);return false;\");\n" \
670 " var arrow = document.createElement(\"span\");\n" \
671 " arrow.setAttribute(\"class\", \"sortarrow\");\n" \
672 " arrow.appendChild(document.createTextNode(\":\"));\n" \
673 " link.appendChild(arrow)\n" \
674 " n.replaceChild(link, n.firstChild);\n" \
677 " var lnk = row[init_sort_column].firstChild;\n" \
678 " if (ascending) {\n" \
679 " var span = lnk.childNodes[1];\n" \
680 " span.setAttribute('sortdir','down');\n" \
687 static void http_dirlist_append_js_table_resort (buffer
*b
, connection
*con
) {
689 char ascending
= '0';
690 if (!buffer_string_is_empty(con
->uri
.query
)) {
691 const char *qs
= con
->uri
.query
->ptr
;
693 if (qs
[0] == 'C' && qs
[1] == '=') {
695 case 'N': col
= '0'; break;
696 case 'M': col
= '1'; break;
697 case 'S': col
= '2'; break;
699 case 'D': col
= '3'; break;
703 else if (qs
[0] == 'O' && qs
[1] == '=') {
705 case 'A': ascending
= '1'; break;
706 case 'D': ascending
= '0'; break;
710 } while ((qs
= strchr(qs
, '&')) && *++qs
);
713 buffer_append_string_len(b
, CONST_STR_LEN("\n<script type=\"text/javascript\">\n// <!--\n\n"));
714 buffer_append_string_len(b
, js_simple_table_resort
, sizeof(js_simple_table_resort
)-1);
715 buffer_append_string_len(b
, js_simple_table_init_sort
, sizeof(js_simple_table_init_sort
)-1);
716 buffer_append_string_len(b
, CONST_STR_LEN("\ninit_sort("));
717 buffer_append_string_len(b
, &col
, 1);
718 buffer_append_string_len(b
, CONST_STR_LEN(", "));
719 buffer_append_string_len(b
, &ascending
, 1);
720 buffer_append_string_len(b
, CONST_STR_LEN(");\n\n// -->\n</script>\n\n"));
723 static void http_list_directory_header(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*out
) {
726 if (p
->conf
.auto_layout
) {
727 buffer_append_string_len(out
, CONST_STR_LEN(
728 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
729 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n"
733 buffer_append_string_encoded(out
, CONST_BUF_LEN(con
->uri
.path
), ENCODING_MINIMAL_XML
);
734 buffer_append_string_len(out
, CONST_STR_LEN("</title>\n"));
736 if (!buffer_string_is_empty(p
->conf
.external_css
)) {
737 buffer_append_string_len(out
, CONST_STR_LEN("<link rel=\"stylesheet\" type=\"text/css\" href=\""));
738 buffer_append_string_buffer(out
, p
->conf
.external_css
);
739 buffer_append_string_len(out
, CONST_STR_LEN("\" />\n"));
741 buffer_append_string_len(out
, CONST_STR_LEN(
742 "<style type=\"text/css\">\n"
743 "a, a:active {text-decoration: none; color: blue;}\n"
744 "a:visited {color: #48468F;}\n"
745 "a:hover, a:focus {text-decoration: underline; color: red;}\n"
746 "body {background-color: #F5F5F5;}\n"
747 "h2 {margin-bottom: 12px;}\n"
748 "table {margin-left: 12px;}\n"
750 " font: 90% monospace;"
754 " font-weight: bold;"
755 " padding-right: 14px;"
756 " padding-bottom: 3px;"
758 "td {padding-right: 14px;}\n"
759 "td.s, th.s {text-align: right;}\n"
761 " background-color: white;"
762 " border-top: 1px solid #646464;"
763 " border-bottom: 1px solid #646464;"
764 " padding-top: 10px;"
765 " padding-bottom: 14px;"
768 " font: 90% monospace;"
776 buffer_append_string_len(out
, CONST_STR_LEN("</head>\n<body>\n"));
779 if (!buffer_string_is_empty(p
->conf
.show_header
)) {
780 /* if we have a HEADER file, display it in <pre class="header"></pre> */
782 buffer_copy_buffer(p
->tmp_buf
, con
->physical
.path
);
783 buffer_append_slash(p
->tmp_buf
);
784 buffer_append_string_buffer(p
->tmp_buf
, p
->conf
.show_header
);
786 http_list_directory_include_file(out
, p
->tmp_buf
, "header", p
->conf
.encode_header
);
789 buffer_append_string_len(out
, CONST_STR_LEN("<h2>Index of "));
790 buffer_append_string_encoded(out
, CONST_BUF_LEN(con
->uri
.path
), ENCODING_MINIMAL_XML
);
791 buffer_append_string_len(out
, CONST_STR_LEN(
793 "<div class=\"list\">\n"
794 "<table summary=\"Directory Listing\" cellpadding=\"0\" cellspacing=\"0\">\n"
797 "<th class=\"n\">Name</th>"
798 "<th class=\"m\">Last Modified</th>"
799 "<th class=\"s\">Size</th>"
800 "<th class=\"t\">Type</th>"
805 "<td class=\"n\"><a href=\"../\">..</a>/</td>"
806 "<td class=\"m\"> </td>"
807 "<td class=\"s\">- </td>"
808 "<td class=\"t\">Directory</td>"
813 static void http_list_directory_footer(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*out
) {
816 buffer_append_string_len(out
, CONST_STR_LEN(
822 if (!buffer_string_is_empty(p
->conf
.show_readme
)) {
823 /* if we have a README file, display it in <pre class="readme"></pre> */
825 buffer_copy_buffer(p
->tmp_buf
, con
->physical
.path
);
826 buffer_append_slash(p
->tmp_buf
);
827 buffer_append_string_buffer(p
->tmp_buf
, p
->conf
.show_readme
);
829 http_list_directory_include_file(out
, p
->tmp_buf
, "readme", p
->conf
.encode_readme
);
832 if(p
->conf
.auto_layout
) {
834 if (!buffer_string_is_empty(p
->conf
.external_js
)) {
835 buffer_append_string_len(out
, CONST_STR_LEN("<script type=\"text/javascript\" src=\""));
836 buffer_append_string_buffer(out
, p
->conf
.external_js
);
837 buffer_append_string_len(out
, CONST_STR_LEN("\" />\n"));
838 } else if (buffer_is_empty(p
->conf
.external_js
)) {
839 http_dirlist_append_js_table_resort(out
, con
);
842 buffer_append_string_len(out
, CONST_STR_LEN(
843 "<div class=\"foot\">"
846 if (!buffer_string_is_empty(p
->conf
.set_footer
)) {
847 buffer_append_string_buffer(out
, p
->conf
.set_footer
);
849 buffer_append_string_buffer(out
, con
->conf
.server_tag
);
852 buffer_append_string_len(out
, CONST_STR_LEN(
860 static int http_list_directory(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*dir
) {
865 char *path
, *path_file
;
867 int hide_dotfiles
= p
->conf
.hide_dot_files
;
868 dirls_list_t dirs
, files
, *list
;
870 char sizebuf
[sizeof("999.9K")];
871 char datebuf
[sizeof("2005-Jan-01 22:23:24")];
873 const char *content_type
;
875 #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
879 #ifdef HAVE_LOCALTIME_R
883 if (buffer_string_is_empty(dir
)) return -1;
885 i
= buffer_string_length(dir
);
888 if (0 >= (name_max
= pathconf(dir
->ptr
, _PC_NAME_MAX
))) {
889 /* some broken fs (fuse) return 0 instead of -1 */
893 name_max
= 255; /* stupid default */
896 #elif defined __WIN32
897 name_max
= FILENAME_MAX
;
902 path
= malloc(i
+ name_max
+ 1);
903 force_assert(NULL
!= path
);
904 memcpy(path
, dir
->ptr
, i
+1);
905 path_file
= path
+ i
;
907 if (NULL
== (dp
= opendir(path
))) {
908 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
909 "opendir failed:", dir
, strerror(errno
));
915 dirs
.ent
= (dirls_entry_t
**) malloc(sizeof(dirls_entry_t
*) * DIRLIST_BLOB_SIZE
);
916 force_assert(dirs
.ent
);
917 dirs
.size
= DIRLIST_BLOB_SIZE
;
919 files
.ent
= (dirls_entry_t
**) malloc(sizeof(dirls_entry_t
*) * DIRLIST_BLOB_SIZE
);
920 force_assert(files
.ent
);
921 files
.size
= DIRLIST_BLOB_SIZE
;
924 while ((dent
= readdir(dp
)) != NULL
) {
925 unsigned short exclude_match
= 0;
927 if (dent
->d_name
[0] == '.') {
930 if (dent
->d_name
[1] == '\0')
932 if (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0')
936 if (p
->conf
.hide_readme_file
&& !buffer_string_is_empty(p
->conf
.show_readme
)) {
937 if (strcmp(dent
->d_name
, p
->conf
.show_readme
->ptr
) == 0)
940 if (p
->conf
.hide_header_file
&& !buffer_string_is_empty(p
->conf
.show_header
)) {
941 if (strcmp(dent
->d_name
, p
->conf
.show_header
->ptr
) == 0)
945 /* compare d_name against excludes array
946 * elements, skipping any that match.
949 for(i
= 0; i
< p
->conf
.excludes
->used
; i
++) {
953 pcre
*regex
= p
->conf
.excludes
->ptr
[i
]->regex
;
955 if ((n
= pcre_exec(regex
, NULL
, dent
->d_name
,
956 strlen(dent
->d_name
), 0, 0, ovec
, 3 * N
)) < 0) {
957 if (n
!= PCRE_ERROR_NOMATCH
) {
958 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
959 "execution error while matching:", n
);
961 /* aborting would require a lot of manual cleanup here.
962 * skip instead (to not leak names that break pcre matching)
979 i
= strlen(dent
->d_name
);
981 /* NOTE: the manual says, d_name is never more than NAME_MAX
982 * so this should actually not be a buffer-overflow-risk
984 if (i
> (size_t)name_max
) continue;
986 memcpy(path_file
, dent
->d_name
, i
+ 1);
987 if (stat(path
, &st
) != 0)
991 if (S_ISDIR(st
.st_mode
))
994 if (list
->used
== list
->size
) {
995 list
->size
+= DIRLIST_BLOB_SIZE
;
996 list
->ent
= (dirls_entry_t
**) realloc(list
->ent
, sizeof(dirls_entry_t
*) * list
->size
);
997 force_assert(list
->ent
);
1000 tmp
= (dirls_entry_t
*) malloc(sizeof(dirls_entry_t
) + 1 + i
);
1001 tmp
->mtime
= st
.st_mtime
;
1002 tmp
->size
= st
.st_size
;
1004 memcpy(DIRLIST_ENT_NAME(tmp
), dent
->d_name
, i
+ 1);
1006 list
->ent
[list
->used
++] = tmp
;
1010 if (dirs
.used
) http_dirls_sort(dirs
.ent
, dirs
.used
);
1012 if (files
.used
) http_dirls_sort(files
.ent
, files
.used
);
1014 out
= buffer_init();
1015 buffer_copy_string_len(out
, CONST_STR_LEN("<?xml version=\"1.0\" encoding=\""));
1016 if (buffer_string_is_empty(p
->conf
.encoding
)) {
1017 buffer_append_string_len(out
, CONST_STR_LEN("iso-8859-1"));
1019 buffer_append_string_buffer(out
, p
->conf
.encoding
);
1021 buffer_append_string_len(out
, CONST_STR_LEN("\"?>\n"));
1022 http_list_directory_header(srv
, con
, p
, out
);
1025 for (i
= 0; i
< dirs
.used
; i
++) {
1028 #ifdef HAVE_LOCALTIME_R
1029 localtime_r(&(tmp
->mtime
), &tm
);
1030 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", &tm
);
1032 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", localtime(&(tmp
->mtime
)));
1035 buffer_append_string_len(out
, CONST_STR_LEN("<tr class=\"d\"><td class=\"n\"><a href=\""));
1036 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_REL_URI_PART
);
1037 buffer_append_string_len(out
, CONST_STR_LEN("/\">"));
1038 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_MINIMAL_XML
);
1039 buffer_append_string_len(out
, CONST_STR_LEN("</a>/</td><td class=\"m\">"));
1040 buffer_append_string_len(out
, datebuf
, sizeof(datebuf
) - 1);
1041 buffer_append_string_len(out
, CONST_STR_LEN("</td><td class=\"s\">- </td><td class=\"t\">Directory</td></tr>\n"));
1047 for (i
= 0; i
< files
.used
; i
++) {
1050 content_type
= NULL
;
1051 #if defined(HAVE_XATTR)
1052 if (con
->conf
.use_xattr
) {
1053 memcpy(path_file
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
+ 1);
1054 attrlen
= sizeof(attrval
) - 1;
1055 if (attr_get(path
, srv
->srvconf
.xattr_name
->ptr
, attrval
, &attrlen
, 0) == 0) {
1056 attrval
[attrlen
] = '\0';
1057 content_type
= attrval
;
1060 #elif defined(HAVE_EXTATTR)
1061 if (con
->conf
.use_xattr
) {
1062 memcpy(path_file
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
+ 1);
1063 if(-1 != (attrlen
= extattr_get_file(path
, EXTATTR_NAMESPACE_USER
, srv
->srvconf
.xattr_name
->ptr
, attrval
, sizeof(attrval
)-1))) {
1064 attrval
[attrlen
] = '\0';
1065 content_type
= attrval
;
1070 if (content_type
== NULL
) {
1071 content_type
= "application/octet-stream";
1072 for (k
= 0; k
< con
->conf
.mimetypes
->used
; k
++) {
1073 data_string
*ds
= (data_string
*)con
->conf
.mimetypes
->data
[k
];
1076 if (buffer_is_empty(ds
->key
))
1079 ct_len
= buffer_string_length(ds
->key
);
1080 if (tmp
->namelen
< ct_len
)
1083 if (0 == strncasecmp(DIRLIST_ENT_NAME(tmp
) + tmp
->namelen
- ct_len
, ds
->key
->ptr
, ct_len
)) {
1084 content_type
= ds
->value
->ptr
;
1090 #ifdef HAVE_LOCALTIME_R
1091 localtime_r(&(tmp
->mtime
), &tm
);
1092 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", &tm
);
1094 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", localtime(&(tmp
->mtime
)));
1096 http_list_directory_sizefmt(sizebuf
, sizeof(sizebuf
), tmp
->size
);
1098 buffer_append_string_len(out
, CONST_STR_LEN("<tr><td class=\"n\"><a href=\""));
1099 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_REL_URI_PART
);
1100 buffer_append_string_len(out
, CONST_STR_LEN("\">"));
1101 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_MINIMAL_XML
);
1102 buffer_append_string_len(out
, CONST_STR_LEN("</a></td><td class=\"m\">"));
1103 buffer_append_string_len(out
, datebuf
, sizeof(datebuf
) - 1);
1104 buffer_append_string_len(out
, CONST_STR_LEN("</td><td class=\"s\">"));
1105 buffer_append_string(out
, sizebuf
);
1106 buffer_append_string_len(out
, CONST_STR_LEN("</td><td class=\"t\">"));
1107 buffer_append_string(out
, content_type
);
1108 buffer_append_string_len(out
, CONST_STR_LEN("</td></tr>\n"));
1117 http_list_directory_footer(srv
, con
, p
, out
);
1119 /* Insert possible charset to Content-Type */
1120 if (buffer_string_is_empty(p
->conf
.encoding
)) {
1121 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
1123 buffer_copy_string_len(p
->content_charset
, CONST_STR_LEN("text/html; charset="));
1124 buffer_append_string_buffer(p
->content_charset
, p
->conf
.encoding
);
1125 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(p
->content_charset
));
1128 con
->file_finished
= 1;
1129 chunkqueue_append_buffer(con
->write_queue
, out
);
1137 URIHANDLER_FUNC(mod_dirlisting_subrequest
) {
1138 plugin_data
*p
= p_d
;
1139 stat_cache_entry
*sce
= NULL
;
1143 /* we only handle GET and HEAD */
1144 switch(con
->request
.http_method
) {
1145 case HTTP_METHOD_GET
:
1146 case HTTP_METHOD_HEAD
:
1149 return HANDLER_GO_ON
;
1152 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
1154 if (buffer_is_empty(con
->physical
.path
)) return HANDLER_GO_ON
;
1155 if (buffer_is_empty(con
->uri
.path
)) return HANDLER_GO_ON
;
1156 if (con
->uri
.path
->ptr
[buffer_string_length(con
->uri
.path
) - 1] != '/') return HANDLER_GO_ON
;
1158 mod_dirlisting_patch_connection(srv
, con
, p
);
1160 if (!p
->conf
.dir_listing
) return HANDLER_GO_ON
;
1162 if (con
->conf
.log_request_handling
) {
1163 log_error_write(srv
, __FILE__
, __LINE__
, "s", "-- handling the request as Dir-Listing");
1164 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "URI :", con
->uri
.path
);
1167 if (HANDLER_ERROR
== stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) {
1168 log_error_write(srv
, __FILE__
, __LINE__
, "SB", "stat_cache_get_entry failed: ", con
->physical
.path
);
1172 if (!S_ISDIR(sce
->st
.st_mode
)) return HANDLER_GO_ON
;
1174 if (http_list_directory(srv
, con
, p
, con
->physical
.path
)) {
1175 /* dirlisting failed */
1176 con
->http_status
= 403;
1179 buffer_reset(con
->physical
.path
);
1182 return HANDLER_FINISHED
;
1185 /* this function is called at dlopen() time and inits the callbacks */
1187 int mod_dirlisting_plugin_init(plugin
*p
);
1188 int mod_dirlisting_plugin_init(plugin
*p
) {
1189 p
->version
= LIGHTTPD_VERSION_ID
;
1190 p
->name
= buffer_init_string("dirlisting");
1192 p
->init
= mod_dirlisting_init
;
1193 p
->handle_subrequest_start
= mod_dirlisting_subrequest
;
1194 p
->set_defaults
= mod_dirlisting_set_defaults
;
1195 p
->cleanup
= mod_dirlisting_free
;