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(
732 if (!buffer_string_is_empty(p
->conf
.encoding
)) {
733 buffer_append_string_len(out
, CONST_STR_LEN("<meta charset=\""));
734 buffer_append_string_buffer(out
, p
->conf
.encoding
);
735 buffer_append_string_len(out
, CONST_STR_LEN("\">\n"));
737 buffer_append_string_len(out
, CONST_STR_LEN("<title>Index of "));
738 buffer_append_string_encoded(out
, CONST_BUF_LEN(con
->uri
.path
), ENCODING_MINIMAL_XML
);
739 buffer_append_string_len(out
, CONST_STR_LEN("</title>\n"));
741 if (!buffer_string_is_empty(p
->conf
.external_css
)) {
742 buffer_append_string_len(out
, CONST_STR_LEN("<meta name=\"viewport\" content=\"initial-scale=1\">"));
743 buffer_append_string_len(out
, CONST_STR_LEN("<link rel=\"stylesheet\" type=\"text/css\" href=\""));
744 buffer_append_string_buffer(out
, p
->conf
.external_css
);
745 buffer_append_string_len(out
, CONST_STR_LEN("\">\n"));
747 buffer_append_string_len(out
, CONST_STR_LEN(
748 "<style type=\"text/css\">\n"
749 "a, a:active {text-decoration: none; color: blue;}\n"
750 "a:visited {color: #48468F;}\n"
751 "a:hover, a:focus {text-decoration: underline; color: red;}\n"
752 "body {background-color: #F5F5F5;}\n"
753 "h2 {margin-bottom: 12px;}\n"
754 "table {margin-left: 12px;}\n"
756 " font: 90% monospace;"
760 " font-weight: bold;"
761 " padding-right: 14px;"
762 " padding-bottom: 3px;"
764 "td {padding-right: 14px;}\n"
765 "td.s, th.s {text-align: right;}\n"
767 " background-color: white;"
768 " border-top: 1px solid #646464;"
769 " border-bottom: 1px solid #646464;"
770 " padding-top: 10px;"
771 " padding-bottom: 14px;"
774 " font: 90% monospace;"
782 buffer_append_string_len(out
, CONST_STR_LEN("</head>\n<body>\n"));
785 if (!buffer_string_is_empty(p
->conf
.show_header
)) {
786 /* if we have a HEADER file, display it in <pre class="header"></pre> */
788 buffer_copy_buffer(p
->tmp_buf
, con
->physical
.path
);
789 buffer_append_slash(p
->tmp_buf
);
790 buffer_append_string_buffer(p
->tmp_buf
, p
->conf
.show_header
);
792 http_list_directory_include_file(out
, p
->tmp_buf
, "header", p
->conf
.encode_header
);
795 buffer_append_string_len(out
, CONST_STR_LEN("<h2>Index of "));
796 buffer_append_string_encoded(out
, CONST_BUF_LEN(con
->uri
.path
), ENCODING_MINIMAL_XML
);
797 buffer_append_string_len(out
, CONST_STR_LEN(
799 "<div class=\"list\">\n"
800 "<table summary=\"Directory Listing\" cellpadding=\"0\" cellspacing=\"0\">\n"
803 "<th class=\"n\">Name</th>"
804 "<th class=\"m\">Last Modified</th>"
805 "<th class=\"s\">Size</th>"
806 "<th class=\"t\">Type</th>"
811 "<td class=\"n\"><a href=\"../\">..</a>/</td>"
812 "<td class=\"m\"> </td>"
813 "<td class=\"s\">- </td>"
814 "<td class=\"t\">Directory</td>"
819 static void http_list_directory_footer(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*out
) {
822 buffer_append_string_len(out
, CONST_STR_LEN(
828 if (!buffer_string_is_empty(p
->conf
.show_readme
)) {
829 /* if we have a README file, display it in <pre class="readme"></pre> */
831 buffer_copy_buffer(p
->tmp_buf
, con
->physical
.path
);
832 buffer_append_slash(p
->tmp_buf
);
833 buffer_append_string_buffer(p
->tmp_buf
, p
->conf
.show_readme
);
835 http_list_directory_include_file(out
, p
->tmp_buf
, "readme", p
->conf
.encode_readme
);
838 if(p
->conf
.auto_layout
) {
840 buffer_append_string_len(out
, CONST_STR_LEN(
841 "<div class=\"foot\">"
844 if (!buffer_string_is_empty(p
->conf
.set_footer
)) {
845 buffer_append_string_buffer(out
, p
->conf
.set_footer
);
847 buffer_append_string_buffer(out
, con
->conf
.server_tag
);
850 buffer_append_string_len(out
, CONST_STR_LEN(
854 if (!buffer_string_is_empty(p
->conf
.external_js
)) {
855 buffer_append_string_len(out
, CONST_STR_LEN("<script type=\"text/javascript\" src=\""));
856 buffer_append_string_buffer(out
, p
->conf
.external_js
);
857 buffer_append_string_len(out
, CONST_STR_LEN("\"></script>\n"));
858 } else if (buffer_is_empty(p
->conf
.external_js
)) {
859 http_dirlist_append_js_table_resort(out
, con
);
862 buffer_append_string_len(out
, CONST_STR_LEN(
869 static int http_list_directory(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*dir
) {
874 char *path
, *path_file
;
876 int hide_dotfiles
= p
->conf
.hide_dot_files
;
877 dirls_list_t dirs
, files
, *list
;
879 char sizebuf
[sizeof("999.9K")];
880 char datebuf
[sizeof("2005-Jan-01 22:23:24")];
882 const char *content_type
;
884 #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
888 #ifdef HAVE_LOCALTIME_R
892 if (buffer_string_is_empty(dir
)) return -1;
894 i
= buffer_string_length(dir
);
897 if (0 >= (name_max
= pathconf(dir
->ptr
, _PC_NAME_MAX
))) {
898 /* some broken fs (fuse) return 0 instead of -1 */
902 name_max
= 255; /* stupid default */
905 #elif defined __WIN32
906 name_max
= FILENAME_MAX
;
911 path
= malloc(i
+ name_max
+ 1);
912 force_assert(NULL
!= path
);
913 memcpy(path
, dir
->ptr
, i
+1);
914 path_file
= path
+ i
;
916 if (NULL
== (dp
= opendir(path
))) {
917 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
918 "opendir failed:", dir
, strerror(errno
));
924 dirs
.ent
= (dirls_entry_t
**) malloc(sizeof(dirls_entry_t
*) * DIRLIST_BLOB_SIZE
);
925 force_assert(dirs
.ent
);
926 dirs
.size
= DIRLIST_BLOB_SIZE
;
928 files
.ent
= (dirls_entry_t
**) malloc(sizeof(dirls_entry_t
*) * DIRLIST_BLOB_SIZE
);
929 force_assert(files
.ent
);
930 files
.size
= DIRLIST_BLOB_SIZE
;
933 while ((dent
= readdir(dp
)) != NULL
) {
934 unsigned short exclude_match
= 0;
936 if (dent
->d_name
[0] == '.') {
939 if (dent
->d_name
[1] == '\0')
941 if (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0')
945 if (p
->conf
.hide_readme_file
&& !buffer_string_is_empty(p
->conf
.show_readme
)) {
946 if (strcmp(dent
->d_name
, p
->conf
.show_readme
->ptr
) == 0)
949 if (p
->conf
.hide_header_file
&& !buffer_string_is_empty(p
->conf
.show_header
)) {
950 if (strcmp(dent
->d_name
, p
->conf
.show_header
->ptr
) == 0)
954 /* compare d_name against excludes array
955 * elements, skipping any that match.
958 for(i
= 0; i
< p
->conf
.excludes
->used
; i
++) {
962 pcre
*regex
= p
->conf
.excludes
->ptr
[i
]->regex
;
964 if ((n
= pcre_exec(regex
, NULL
, dent
->d_name
,
965 strlen(dent
->d_name
), 0, 0, ovec
, 3 * N
)) < 0) {
966 if (n
!= PCRE_ERROR_NOMATCH
) {
967 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
968 "execution error while matching:", n
);
970 /* aborting would require a lot of manual cleanup here.
971 * skip instead (to not leak names that break pcre matching)
988 i
= strlen(dent
->d_name
);
990 /* NOTE: the manual says, d_name is never more than NAME_MAX
991 * so this should actually not be a buffer-overflow-risk
993 if (i
> (size_t)name_max
) continue;
995 memcpy(path_file
, dent
->d_name
, i
+ 1);
996 if (stat(path
, &st
) != 0)
1000 if (S_ISDIR(st
.st_mode
))
1003 if (list
->used
== list
->size
) {
1004 list
->size
+= DIRLIST_BLOB_SIZE
;
1005 list
->ent
= (dirls_entry_t
**) realloc(list
->ent
, sizeof(dirls_entry_t
*) * list
->size
);
1006 force_assert(list
->ent
);
1009 tmp
= (dirls_entry_t
*) malloc(sizeof(dirls_entry_t
) + 1 + i
);
1010 tmp
->mtime
= st
.st_mtime
;
1011 tmp
->size
= st
.st_size
;
1013 memcpy(DIRLIST_ENT_NAME(tmp
), dent
->d_name
, i
+ 1);
1015 list
->ent
[list
->used
++] = tmp
;
1019 if (dirs
.used
) http_dirls_sort(dirs
.ent
, dirs
.used
);
1021 if (files
.used
) http_dirls_sort(files
.ent
, files
.used
);
1023 out
= buffer_init();
1024 http_list_directory_header(srv
, con
, p
, out
);
1027 for (i
= 0; i
< dirs
.used
; i
++) {
1030 #ifdef HAVE_LOCALTIME_R
1031 localtime_r(&(tmp
->mtime
), &tm
);
1032 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", &tm
);
1034 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", localtime(&(tmp
->mtime
)));
1037 buffer_append_string_len(out
, CONST_STR_LEN("<tr class=\"d\"><td class=\"n\"><a href=\""));
1038 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_REL_URI_PART
);
1039 buffer_append_string_len(out
, CONST_STR_LEN("/\">"));
1040 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_MINIMAL_XML
);
1041 buffer_append_string_len(out
, CONST_STR_LEN("</a>/</td><td class=\"m\">"));
1042 buffer_append_string_len(out
, datebuf
, sizeof(datebuf
) - 1);
1043 buffer_append_string_len(out
, CONST_STR_LEN("</td><td class=\"s\">- </td><td class=\"t\">Directory</td></tr>\n"));
1049 for (i
= 0; i
< files
.used
; i
++) {
1052 content_type
= NULL
;
1053 #if defined(HAVE_XATTR)
1054 if (con
->conf
.use_xattr
) {
1055 memcpy(path_file
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
+ 1);
1056 attrlen
= sizeof(attrval
) - 1;
1057 if (attr_get(path
, srv
->srvconf
.xattr_name
->ptr
, attrval
, &attrlen
, 0) == 0) {
1058 attrval
[attrlen
] = '\0';
1059 content_type
= attrval
;
1062 #elif defined(HAVE_EXTATTR)
1063 if (con
->conf
.use_xattr
) {
1064 memcpy(path_file
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
+ 1);
1065 if(-1 != (attrlen
= extattr_get_file(path
, EXTATTR_NAMESPACE_USER
, srv
->srvconf
.xattr_name
->ptr
, attrval
, sizeof(attrval
)-1))) {
1066 attrval
[attrlen
] = '\0';
1067 content_type
= attrval
;
1072 if (content_type
== NULL
) {
1073 content_type
= "application/octet-stream";
1074 for (k
= 0; k
< con
->conf
.mimetypes
->used
; k
++) {
1075 data_string
*ds
= (data_string
*)con
->conf
.mimetypes
->data
[k
];
1078 if (buffer_is_empty(ds
->key
))
1081 ct_len
= buffer_string_length(ds
->key
);
1082 if (tmp
->namelen
< ct_len
)
1085 if (0 == strncasecmp(DIRLIST_ENT_NAME(tmp
) + tmp
->namelen
- ct_len
, ds
->key
->ptr
, ct_len
)) {
1086 content_type
= ds
->value
->ptr
;
1092 #ifdef HAVE_LOCALTIME_R
1093 localtime_r(&(tmp
->mtime
), &tm
);
1094 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", &tm
);
1096 strftime(datebuf
, sizeof(datebuf
), "%Y-%b-%d %H:%M:%S", localtime(&(tmp
->mtime
)));
1098 http_list_directory_sizefmt(sizebuf
, sizeof(sizebuf
), tmp
->size
);
1100 buffer_append_string_len(out
, CONST_STR_LEN("<tr><td class=\"n\"><a href=\""));
1101 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_REL_URI_PART
);
1102 buffer_append_string_len(out
, CONST_STR_LEN("\">"));
1103 buffer_append_string_encoded(out
, DIRLIST_ENT_NAME(tmp
), tmp
->namelen
, ENCODING_MINIMAL_XML
);
1104 buffer_append_string_len(out
, CONST_STR_LEN("</a></td><td class=\"m\">"));
1105 buffer_append_string_len(out
, datebuf
, sizeof(datebuf
) - 1);
1106 buffer_append_string_len(out
, CONST_STR_LEN("</td><td class=\"s\">"));
1107 buffer_append_string(out
, sizebuf
);
1108 buffer_append_string_len(out
, CONST_STR_LEN("</td><td class=\"t\">"));
1109 buffer_append_string(out
, content_type
);
1110 buffer_append_string_len(out
, CONST_STR_LEN("</td></tr>\n"));
1119 http_list_directory_footer(srv
, con
, p
, out
);
1121 /* Insert possible charset to Content-Type */
1122 if (buffer_string_is_empty(p
->conf
.encoding
)) {
1123 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
1125 buffer_copy_string_len(p
->content_charset
, CONST_STR_LEN("text/html; charset="));
1126 buffer_append_string_buffer(p
->content_charset
, p
->conf
.encoding
);
1127 response_header_overwrite(srv
, con
, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(p
->content_charset
));
1130 con
->file_finished
= 1;
1131 chunkqueue_append_buffer(con
->write_queue
, out
);
1139 URIHANDLER_FUNC(mod_dirlisting_subrequest
) {
1140 plugin_data
*p
= p_d
;
1141 stat_cache_entry
*sce
= NULL
;
1145 /* we only handle GET and HEAD */
1146 switch(con
->request
.http_method
) {
1147 case HTTP_METHOD_GET
:
1148 case HTTP_METHOD_HEAD
:
1151 return HANDLER_GO_ON
;
1154 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
1156 if (buffer_is_empty(con
->physical
.path
)) return HANDLER_GO_ON
;
1157 if (buffer_is_empty(con
->uri
.path
)) return HANDLER_GO_ON
;
1158 if (con
->uri
.path
->ptr
[buffer_string_length(con
->uri
.path
) - 1] != '/') return HANDLER_GO_ON
;
1160 mod_dirlisting_patch_connection(srv
, con
, p
);
1162 if (!p
->conf
.dir_listing
) return HANDLER_GO_ON
;
1164 if (con
->conf
.log_request_handling
) {
1165 log_error_write(srv
, __FILE__
, __LINE__
, "s", "-- handling the request as Dir-Listing");
1166 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "URI :", con
->uri
.path
);
1169 if (HANDLER_ERROR
== stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) {
1170 log_error_write(srv
, __FILE__
, __LINE__
, "SB", "stat_cache_get_entry failed: ", con
->physical
.path
);
1174 if (!S_ISDIR(sce
->st
.st_mode
)) return HANDLER_GO_ON
;
1176 if (http_list_directory(srv
, con
, p
, con
->physical
.path
)) {
1177 /* dirlisting failed */
1178 con
->http_status
= 403;
1181 buffer_reset(con
->physical
.path
);
1184 return HANDLER_FINISHED
;
1187 /* this function is called at dlopen() time and inits the callbacks */
1189 int mod_dirlisting_plugin_init(plugin
*p
);
1190 int mod_dirlisting_plugin_init(plugin
*p
) {
1191 p
->version
= LIGHTTPD_VERSION_ID
;
1192 p
->name
= buffer_init_string("dirlisting");
1194 p
->init
= mod_dirlisting_init
;
1195 p
->handle_subrequest_start
= mod_dirlisting_subrequest
;
1196 p
->set_defaults
= mod_dirlisting_set_defaults
;
1197 p
->cleanup
= mod_dirlisting_free
;