lighttpd 1.4.x https://www.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1202 lines
35 KiB

#include "first.h"
#include "base.h"
#include "log.h"
#include "buffer.h"
#include "fdevent.h"
#include "http_header.h"
#include "plugin.h"
#include "stat_cache.h"
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
fallback to lseek()/read() if mmap() fails (#fixes 2666) fallback to lseek()/read() if mmap() fails (#fixes 2666) e.g. when mmap() is used on lighttpd-controlled temporary files used POST request body (mod_cgi) and PUT file upload (mod_webdav) replace use of stream_open() on potentially untrusted files (protect against SIGBUS if a file is modified while map is read) Note: stream.[ch] may be removed in a future release For now, stream.[ch] will read entire file into memory if mmap fails and so it should only be used on trusted files, e.g. config files. http_auth basic and digest files are typically small and so buffered stdio fopen(), fgets(), fclose() will likely be approximately as fast as mmap. mod_dirlisting header and readme files are typically small and so open(), read(), close() will typically be approximately as fast as mmap mod_ssi will likely be much faster, now buffering SSI page construction rather than a potentially huge number of file open() calls, one for each tiny chunk of text between SSI directives. mod_webdav COPY and MOVE may be slower due to removal of mmap, but are now more resilient to partial writes. x-ref: &#34;handle filesystems without mmap() support&#34; https://redmine.lighttpd.net/issues/2666 &#34;WebDAV upload-&gt; mmap failed: operation not permitted&#34; https://redmine.lighttpd.net/issues/962 &#34;Lighttpd 1.4.20 Crash (SIGBUS in mod_compress)&#34; https://redmine.lighttpd.net/issues/1879 &#34;Crash SIGBUS&#34; https://redmine.lighttpd.net/issues/2391 github: closes #57
6 years ago
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#ifdef HAVE_PCRE_H
#include <pcre.h>
#endif
/**
* this is a dirlisting for a lighttpd plugin
*/
#ifdef HAVE_ATTR_ATTRIBUTES_H
#include <attr/attributes.h>
#endif
#ifdef HAVE_SYS_EXTATTR_H
#include <sys/extattr.h>
#endif
/* plugin config for all request/connections */
typedef struct {
#ifdef HAVE_PCRE_H
pcre *regex;
#endif
buffer *string;
} excludes;
typedef struct {
excludes **ptr;
size_t used;
size_t size;
} excludes_buffer;
typedef struct {
unsigned short dir_listing;
unsigned short hide_dot_files;
unsigned short hide_readme_file;
unsigned short encode_readme;
unsigned short hide_header_file;
unsigned short encode_header;
unsigned short auto_layout;
excludes_buffer *excludes;
buffer *show_readme;
buffer *show_header;
buffer *external_css;
buffer *external_js;
buffer *encoding;
buffer *set_footer;
} plugin_config;
typedef struct {
PLUGIN_DATA;
buffer *tmp_buf;
buffer *content_charset;
plugin_config **config_storage;
plugin_config conf;
} plugin_data;
static excludes_buffer *excludes_buffer_init(void) {
excludes_buffer *exb;
exb = calloc(1, sizeof(*exb));
return exb;
}
#ifdef HAVE_PCRE_H
static int excludes_buffer_append(excludes_buffer *exb, buffer *string) {
size_t i;
const char *errptr;
int erroff;
if (!string) return -1;
if (exb->used == exb->size) {
exb->size += 4;
exb->ptr = realloc(exb->ptr, exb->size * sizeof(*exb->ptr));
for(i = exb->used; i < exb->size; i++) {
exb->ptr[i] = calloc(1, sizeof(**exb->ptr));
}
}
if (NULL == (exb->ptr[exb->used]->regex = pcre_compile(string->ptr, 0,
&errptr, &erroff, NULL))) {
return -1;
}
exb->ptr[exb->used]->string = buffer_init();
fix buffer, chunk and http_chunk API * remove unused structs and functions (buffer_array, read_buffer) * change return type from int to void for many functions, as the return value (indicating error/success) was never checked, and the function would only fail on programming errors and not on invalid input; changed functions to use force_assert instead of returning an error. * all &#34;len&#34; parameters now are the real size of the memory to be read. the length of strings is given always without the terminating 0. * the &#34;buffer&#34; struct still counts the terminating 0 in -&gt;used, provide buffer_string_length() to get the length of a string in a buffer. unset config &#34;strings&#34; have used == 0, which is used in some places to distinguish unset values from &#34;&#34; (empty string) values. * most buffer usages should now use it as string container. * optimise some buffer copying by &#34;moving&#34; data to other buffers * use (u)intmax_t for generic int-to-string functions * remove unused enum values: UNUSED_CHUNK, ENCODING_UNSET * converted BUFFER_APPEND_SLASH to inline function (no macro feature needed) * refactor: create chunkqueue_steal: moving (partial) chunks into another queue * http_chunk: added separate function to terminate chunked body instead of magic handling in http_chunk_append_mem(). http_chunk_append_* now handle empty chunks, and never terminate the chunked body. From: Stefan Bühler &lt;stbuehler@web.de&gt; git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2975 152afb58-edef-0310-8abb-c4023f1b3aa9
7 years ago
buffer_copy_buffer(exb->ptr[exb->used]->string, string);
exb->used++;
return 0;
}
#endif
static void excludes_buffer_free(excludes_buffer *exb) {
#ifdef HAVE_PCRE_H
size_t i;
for (i = 0; i < exb->size; i++) {
if (exb->ptr[i]->regex) pcre_free(exb->ptr[i]->regex);
if (exb->ptr[i]->string) buffer_free(exb->ptr[i]->string);
free(exb->ptr[i]);
}
if (exb->ptr) free(exb->ptr);
#endif
free(exb);
}
/* init the plugin data */
INIT_FUNC(mod_dirlisting_init) {
plugin_data *p;
p = calloc(1, sizeof(*p));
p->tmp_buf = buffer_init();
p->content_charset = buffer_init();
return p;
}
/* detroy the plugin data */
FREE_FUNC(mod_dirlisting_free) {
plugin_data *p = p_d;
UNUSED(srv);
if (!p) return HANDLER_GO_ON;
if (p->config_storage) {
size_t i;
for (i = 0; i < srv->config_context->used; i++) {
plugin_config *s = p->config_storage[i];
if (!s) continue;
excludes_buffer_free(s->excludes);
buffer_free(s->show_readme);
buffer_free(s->show_header);
buffer_free(s->external_css);
buffer_free(s->external_js);
buffer_free(s->encoding);
buffer_free(s->set_footer);
free(s);
}
free(p->config_storage);
}
buffer_free(p->tmp_buf);
buffer_free(p->content_charset);
free(p);
return HANDLER_GO_ON;
}
/* handle plugin config and check values */
#define CONFIG_EXCLUDE "dir-listing.exclude"
#define CONFIG_ACTIVATE "dir-listing.activate"
#define CONFIG_HIDE_DOTFILES "dir-listing.hide-dotfiles"
#define CONFIG_EXTERNAL_CSS "dir-listing.external-css"
#define CONFIG_EXTERNAL_JS "dir-listing.external-js"
#define CONFIG_ENCODING "dir-listing.encoding"
#define CONFIG_SHOW_README "dir-listing.show-readme"
#define CONFIG_HIDE_README_FILE "dir-listing.hide-readme-file"
#define CONFIG_SHOW_HEADER "dir-listing.show-header"
#define CONFIG_HIDE_HEADER_FILE "dir-listing.hide-header-file"
#define CONFIG_DIR_LISTING "server.dir-listing"
#define CONFIG_SET_FOOTER "dir-listing.set-footer"
#define CONFIG_ENCODE_README "dir-listing.encode-readme"
#define CONFIG_ENCODE_HEADER "dir-listing.encode-header"
#define CONFIG_AUTO_LAYOUT "dir-listing.auto-layout"
SETDEFAULTS_FUNC(mod_dirlisting_set_defaults) {
plugin_data *p = p_d;
size_t i = 0;
config_values_t cv[] = {
{ CONFIG_EXCLUDE, NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
{ CONFIG_ACTIVATE, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
{ CONFIG_HIDE_DOTFILES, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
{ CONFIG_EXTERNAL_CSS, NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
{ CONFIG_ENCODING, NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
{ CONFIG_SHOW_README, NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
{ CONFIG_HIDE_README_FILE, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 6 */
{ CONFIG_SHOW_HEADER, NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
{ CONFIG_HIDE_HEADER_FILE, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
{ CONFIG_DIR_LISTING, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 9 */
{ CONFIG_SET_FOOTER, NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 10 */
{ CONFIG_ENCODE_README, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 11 */
{ CONFIG_ENCODE_HEADER, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
{ CONFIG_AUTO_LAYOUT, NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 13 */
{ CONFIG_EXTERNAL_JS, NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
};
if (!p) return HANDLER_ERROR;
p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *));
for (i = 0; i < srv->config_context->used; i++) {
data_config const* config = (data_config const*)srv->config_context->data[i];
plugin_config *s;
data_unset *du_excludes;
s = calloc(1, sizeof(plugin_config));
s->excludes = excludes_buffer_init();
s->dir_listing = 0;
s->show_readme = buffer_init();
s->show_header = buffer_init();
s->external_css = buffer_init();
s->external_js = buffer_init();
s->hide_dot_files = 1;
s->hide_readme_file = 0;
s->hide_header_file = 0;
s->encode_readme = 1;
s->encode_header = 1;
s->auto_layout = 1;
s->encoding = buffer_init();
s->set_footer = buffer_init();
cv[0].destination = s->excludes;
cv[1].destination = &(s->dir_listing);
cv[2].destination = &(s->hide_dot_files);
cv[3].destination = s->external_css;
cv[4].destination = s->encoding;
cv[5].destination = s->show_readme;
cv[6].destination = &(s->hide_readme_file);
cv[7].destination = s->show_header;
cv[8].destination = &(s->hide_header_file);
cv[9].destination = &(s->dir_listing); /* old name */
cv[10].destination = s->set_footer;
cv[11].destination = &(s->encode_readme);
cv[12].destination = &(s->encode_header);
cv[13].destination = &(s->auto_layout);
cv[14].destination = s->external_js;
p->config_storage[i] = s;
if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
return HANDLER_ERROR;
}
if (NULL != (du_excludes = array_get_element(config->value, CONFIG_EXCLUDE))) {
array *excludes_list;
excludes_list = ((data_array*)du_excludes)->value;
if (du_excludes->type != TYPE_ARRAY || !array_is_vlist(excludes_list)) {
log_error_write(srv, __FILE__, __LINE__, "s",
"unexpected type for " CONFIG_EXCLUDE "; expected list of \"regex\"");
return HANDLER_ERROR;
}
#ifndef HAVE_PCRE_H
if (excludes_list->used > 0) {
log_error_write(srv, __FILE__, __LINE__, "sss",
"pcre support is missing for: ", CONFIG_EXCLUDE, ", please install libpcre and the headers");
return HANDLER_ERROR;
}
#else
for (size_t j = 0; j < excludes_list->used; ++j) {
data_unset *du_exclude = excludes_list->data[j];
if (du_exclude->type != TYPE_STRING) {
log_error_write(srv, __FILE__, __LINE__, "sssbs",
"unexpected type for key: ", CONFIG_EXCLUDE, "[",
du_exclude->key, "](string)");
return HANDLER_ERROR;
}
if (0 != excludes_buffer_append(s->excludes, ((data_string*)(du_exclude))->value)) {
log_error_write(srv, __FILE__, __LINE__, "sb",
"pcre-compile failed for", ((data_string*)(du_exclude))->value);
return HANDLER_ERROR;
}
}
#endif
}
if (!buffer_string_is_empty(s->show_readme)) {
if (buffer_is_equal_string(s->show_readme, CONST_STR_LEN("enable"))) {
buffer_copy_string_len(s->show_readme, CONST_STR_LEN("README.txt"));
}
else if (buffer_is_equal_string(s->show_readme, CONST_STR_LEN("disable"))) {
buffer_clear(s->show_readme);
}
}
if (!buffer_string_is_empty(s->show_header)) {
if (buffer_is_equal_string(s->show_header, CONST_STR_LEN("enable"))) {
buffer_copy_string_len(s->show_header, CONST_STR_LEN("HEADER.txt"));
}
else if (buffer_is_equal_string(s->show_header, CONST_STR_LEN("disable"))) {
buffer_clear(s->show_header);
}
}
}
return HANDLER_GO_ON;
}
#define PATCH(x) \
p->conf.x = s->x;
static int mod_dirlisting_patch_connection(server *srv, connection *con, plugin_data *p) {
size_t i, j;
plugin_config *s = p->config_storage[0];
PATCH(dir_listing);
PATCH(external_css);
PATCH(external_js);
PATCH(hide_dot_files);
PATCH(encoding);
PATCH(show_readme);
PATCH(hide_readme_file);
PATCH(show_header);
PATCH(hide_header_file);
PATCH(excludes);
PATCH(set_footer);
PATCH(encode_readme);
PATCH(encode_header);
PATCH(auto_layout);
/* skip the first, the global context */
for (i = 1; i < srv->config_context->used; i++) {
data_config *dc = (data_config *)srv->config_context->data[i];
s = p->config_storage[i];
/* condition didn't match */
if (!config_check_cond(srv, con, dc)) continue;
/* merge config */
for (j = 0; j < dc->value->used; j++) {
data_unset *du = dc->value->data[j];
if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_ACTIVATE)) ||
buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_DIR_LISTING))) {
PATCH(dir_listing);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_HIDE_DOTFILES))) {
PATCH(hide_dot_files);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_EXTERNAL_CSS))) {
PATCH(external_css);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_EXTERNAL_JS))) {
PATCH(external_js);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_ENCODING))) {
PATCH(encoding);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_SHOW_README))) {
PATCH(show_readme);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_HIDE_README_FILE))) {
PATCH(hide_readme_file);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_SHOW_HEADER))) {
PATCH(show_header);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_HIDE_HEADER_FILE))) {
PATCH(hide_header_file);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_SET_FOOTER))) {
PATCH(set_footer);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_EXCLUDE))) {
PATCH(excludes);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_ENCODE_README))) {
PATCH(encode_readme);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_ENCODE_HEADER))) {
PATCH(encode_header);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(CONFIG_AUTO_LAYOUT))) {
PATCH(auto_layout);
}
}
}
return 0;
}
#undef PATCH
typedef struct {
size_t namelen;
time_t mtime;
off_t size;
} dirls_entry_t;
typedef struct {
dirls_entry_t **ent;
size_t used;
size_t size;
} dirls_list_t;
#define DIRLIST_ENT_NAME(ent) ((char*)(ent) + sizeof(dirls_entry_t))
#define DIRLIST_BLOB_SIZE 16
/* simple combsort algorithm */
static void http_dirls_sort(dirls_entry_t **ent, int num) {
int gap = num;
int i, j;
int swapped;
dirls_entry_t *tmp;
do {
gap = (gap * 10) / 13;
if (gap == 9 || gap == 10)
gap = 11;
if (gap < 1)
gap = 1;
swapped = 0;
for (i = 0; i < num - gap; i++) {
j = i + gap;
if (strcmp(DIRLIST_ENT_NAME(ent[i]), DIRLIST_ENT_NAME(ent[j])) > 0) {
tmp = ent[i];
ent[i] = ent[j];
ent[j] = tmp;
swapped = 1;
}
}
} while (gap > 1 || swapped);
}
/* buffer must be able to hold "999.9K"
* conversion is simple but not perfect
*/
static int http_list_directory_sizefmt(char *buf, size_t bufsz, off_t size) {
const char unit[] = " KMGTPE"; /* Kilo, Mega, Giga, Tera, Peta, Exa */
const char *u = unit; /* u will always increment at least once */
int remain;
size_t buflen;
if (size < 100)
size += 99;
if (size < 100)
size = 0;
while (1) {
remain = (int) size & 1023;
size >>= 10;
u++;
if ((size & (~0 ^ 1023)) == 0)
break;
}
remain /= 100;
if (remain > 9)
remain = 9;
if (size > 999) {
size = 0;
remain = 9;
u++;
}
li_itostrn(buf, bufsz, size);
buflen = strlen(buf);
if (buflen + 3 >= bufsz) return buflen;
buf[buflen+0] = '.';
buf[buflen+1] = remain + '0';
buf[buflen+2] = *u;
buf[buflen+3] = '\0';
return buflen + 3;
}
static void http_list_directory_include_file(buffer *out, int symlinks, buffer *path, const char *classname, int encode) {
int fd = fdevent_open_cloexec(path->ptr, symlinks, O_RDONLY, 0);
fallback to lseek()/read() if mmap() fails (#fixes 2666) fallback to lseek()/read() if mmap() fails (#fixes 2666) e.g. when mmap() is used on lighttpd-controlled temporary files used POST request body (mod_cgi) and PUT file upload (mod_webdav) replace use of stream_open() on potentially untrusted files (protect against SIGBUS if a file is modified while map is read) Note: stream.[ch] may be removed in a future release For now, stream.[ch] will read entire file into memory if mmap fails and so it should only be used on trusted files, e.g. config files. http_auth basic and digest files are typically small and so buffered stdio fopen(), fgets(), fclose() will likely be approximately as fast as mmap. mod_dirlisting header and readme files are typically small and so open(), read(), close() will typically be approximately as fast as mmap mod_ssi will likely be much faster, now buffering SSI page construction rather than a potentially huge number of file open() calls, one for each tiny chunk of text between SSI directives. mod_webdav COPY and MOVE may be slower due to removal of mmap, but are now more resilient to partial writes. x-ref: &#34;handle filesystems without mmap() support&#34; https://redmine.lighttpd.net/issues/2666 &#34;WebDAV upload-&gt; mmap failed: operation not permitted&#34; https://redmine.lighttpd.net/issues/962 &#34;Lighttpd 1.4.20 Crash (SIGBUS in mod_compress)&#34; https://redmine.lighttpd.net/issues/1879 &#34;Crash SIGBUS&#34; https://redmine.lighttpd.net/issues/2391 github: closes #57
6 years ago
ssize_t rd;
char buf[8192];
if (-1 == fd) return;
if (encode) {
buffer_append_string_len(out, CONST_STR_LEN("<pre class=\""));
buffer_append_string(out, classname);
buffer_append_string_len(out, CONST_STR_LEN("\">"));
}
while ((rd = read(fd, buf, sizeof(buf))) > 0) {
if (encode) {
buffer_append_string_encoded(out, buf, (size_t)rd, ENCODING_MINIMAL_XML);
} else {
buffer_append_string_len(out, buf, (size_t)rd);
}
}
close(fd);
fallback to lseek()/read() if mmap() fails (#fixes 2666) fallback to lseek()/read() if mmap() fails (#fixes 2666) e.g. when mmap() is used on lighttpd-controlled temporary files used POST request body (mod_cgi) and PUT file upload (mod_webdav) replace use of stream_open() on potentially untrusted files (protect against SIGBUS if a file is modified while map is read) Note: stream.[ch] may be removed in a future release For now, stream.[ch] will read entire file into memory if mmap fails and so it should only be used on trusted files, e.g. config files. http_auth basic and digest files are typically small and so buffered stdio fopen(), fgets(), fclose() will likely be approximately as fast as mmap. mod_dirlisting header and readme files are typically small and so open(), read(), close() will typically be approximately as fast as mmap mod_ssi will likely be much faster, now buffering SSI page construction rather than a potentially huge number of file open() calls, one for each tiny chunk of text between SSI directives. mod_webdav COPY and MOVE may be slower due to removal of mmap, but are now more resilient to partial writes. x-ref: &#34;handle filesystems without mmap() support&#34; https://redmine.lighttpd.net/issues/2666 &#34;WebDAV upload-&gt; mmap failed: operation not permitted&#34; https://redmine.lighttpd.net/issues/962 &#34;Lighttpd 1.4.20 Crash (SIGBUS in mod_compress)&#34; https://redmine.lighttpd.net/issues/1879 &#34;Crash SIGBUS&#34; https://redmine.lighttpd.net/issues/2391 github: closes #57
6 years ago
if (encode) {
buffer_append_string_len(out, CONST_STR_LEN("</pre>"));
}
}
/* portions copied from mod_status
* modified and specialized for stable dirlist sorting by name */
static const char js_simple_table_resort[] = \
"var click_column;\n" \
"var name_column = 0;\n" \
"var date_column = 1;\n" \
"var size_column = 2;\n" \
"var type_column = 3;\n" \
"var prev_span = null;\n" \
"\n" \
"if (typeof(String.prototype.localeCompare) === 'undefined') {\n" \
" String.prototype.localeCompare = function(str, locale, options) {\n" \
" return ((this == str) ? 0 : ((this > str) ? 1 : -1));\n" \
" };\n" \
"}\n" \
"\n" \
"if (typeof(String.prototype.toLocaleUpperCase) === 'undefined') {\n" \
" String.prototype.toLocaleUpperCase = function() {\n" \
" return this.toUpperCase();\n" \
" };\n" \
"}\n" \
"\n" \
"function get_inner_text(el) {\n" \
" if((typeof el == 'string')||(typeof el == 'undefined'))\n" \
" return el;\n" \
" if(el.innerText)\n" \
" return el.innerText;\n" \
" else {\n" \
" var str = \"\";\n" \
" var cs = el.childNodes;\n" \
" var l = cs.length;\n" \
" for (i=0;i<l;i++) {\n" \
" if (cs[i].nodeType==1) str += get_inner_text(cs[i]);\n" \
" else if (cs[i].nodeType==3) str += cs[i].nodeValue;\n" \
" }\n" \
" }\n" \
" return str;\n" \
"}\n" \
"\n" \
"function isdigit(c) {\n" \
" return (c >= '0' && c <= '9');\n" \
"}\n" \
"\n" \
"function unit_multiplier(unit) {\n" \
" return (unit=='K') ? 1000\n" \
" : (unit=='M') ? 1000000\n" \
" : (unit=='G') ? 1000000000\n" \
" : (unit=='T') ? 1000000000000\n" \
" : (unit=='P') ? 1000000000000000\n" \
" : (unit=='E') ? 1000000000000000000 : 1;\n" \
"}\n" \
"\n" \
"var li_date_regex=/(\\d{4})-(\\w{3})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})/;\n" \
"\n" \
"var li_mon = ['Jan','Feb','Mar','Apr','May','Jun',\n" \
" 'Jul','Aug','Sep','Oct','Nov','Dec'];\n" \
"\n" \
"function li_mon_num(mon) {\n" \
" var i; for (i = 0; i < 12 && mon != li_mon[i]; ++i); return i;\n" \
"}\n" \
"\n" \
"function li_date_cmp(s1, s2) {\n" \
" var dp1 = li_date_regex.exec(s1)\n" \
" var dp2 = li_date_regex.exec(s2)\n" \
" for (var i = 1; i < 7; ++i) {\n" \
" var cmp = (2 != i)\n" \
" ? parseInt(dp1[i]) - parseInt(dp2[i])\n" \
" : li_mon_num(dp1[2]) - li_mon_num(dp2[2]);\n" \
" if (0 != cmp) return cmp;\n" \
" }\n" \
" return 0;\n" \
"}\n" \
"\n" \
"function sortfn_then_by_name(a,b,sort_column) {\n" \
" if (sort_column == name_column || sort_column == type_column) {\n" \
" var ad = (a.cells[type_column].innerHTML === 'Directory');\n" \
" var bd = (b.cells[type_column].innerHTML === 'Directory');\n" \
" if (ad != bd) return (ad ? -1 : 1);\n" \
" }\n" \
" var at = get_inner_text(a.cells[sort_column]);\n" \
" var bt = get_inner_text(b.cells[sort_column]);\n" \
" var cmp;\n" \
" if (sort_column == name_column) {\n" \
" if (at == '..') return -1;\n" \
" if (bt == '..') return 1;\n" \
" }\n" \
" if (a.cells[sort_column].className == 'int') {\n" \
" cmp = parseInt(at)-parseInt(bt);\n" \
" } else if (sort_column == date_column) {\n" \
" var ad = isdigit(at.substr(0,1));\n" \
" var bd = isdigit(bt.substr(0,1));\n" \
" if (ad != bd) return (!ad ? -1 : 1);\n" \
" cmp = li_date_cmp(at,bt);\n" \
" } else if (sort_column == size_column) {\n" \
" var ai = parseInt(at, 10) * unit_multiplier(at.substr(-1,1));\n" \
" var bi = parseInt(bt, 10) * unit_multiplier(bt.substr(-1,1));\n" \
" if (at.substr(0,1) == '-') ai = -1;\n" \
" if (bt.substr(0,1) == '-') bi = -1;\n" \
" cmp = ai - bi;\n" \
" } else {\n" \
" cmp = at.toLocaleUpperCase().localeCompare(bt.toLocaleUpperCase());\n" \
" if (0 != cmp) return cmp;\n" \
" cmp = at.localeCompare(bt);\n" \
" }\n" \
" if (0 != cmp || sort_column == name_column) return cmp;\n" \
" return sortfn_then_by_name(a,b,name_column);\n" \
"}\n" \
"\n" \
"function sortfn(a,b) {\n" \
" return sortfn_then_by_name(a,b,click_column);\n" \
"}\n" \
"\n" \
"function resort(lnk) {\n" \
" var span = lnk.childNodes[1];\n" \
" var table = lnk.parentNode.parentNode.parentNode.parentNode;\n" \
" var rows = new Array();\n" \
" for (j=1;j<table.rows.length;j++)\n" \
" rows[j-1] = table.rows[j];\n" \
" click_column = lnk.parentNode.cellIndex;\n" \
" rows.sort(sortfn);\n" \
"\n" \
" if (prev_span != null) prev_span.innerHTML = '';\n" \
" if (span.getAttribute('sortdir')=='down') {\n" \
" span.innerHTML = '&uarr;';\n" \
" span.setAttribute('sortdir','up');\n" \
" rows.reverse();\n" \
" } else {\n" \
" span.innerHTML = '&darr;';\n" \
" span.setAttribute('sortdir','down');\n" \
" }\n" \
" for (i=0;i<rows.length;i++)\n" \
" table.tBodies[0].appendChild(rows[i]);\n" \
" prev_span = span;\n" \
"}\n";
/* portions copied from mod_dirlist (lighttpd2) */
static const char js_simple_table_init_sort[] = \
"\n" \
"function init_sort(init_sort_column, ascending) {\n" \
" var tables = document.getElementsByTagName(\"table\");\n" \
" for (var i = 0; i < tables.length; i++) {\n" \
" var table = tables[i];\n" \
" //var c = table.getAttribute(\"class\")\n" \
" //if (-1 != c.split(\" \").indexOf(\"sort\")) {\n" \
" var row = table.rows[0].cells;\n" \
" for (var j = 0; j < row.length; j++) {\n" \
" var n = row[j];\n" \
" if (n.childNodes.length == 1 && n.childNodes[0].nodeType == 3) {\n" \
" var link = document.createElement(\"a\");\n" \
" var title = n.childNodes[0].nodeValue.replace(/:$/, \"\");\n" \
" link.appendChild(document.createTextNode(title));\n" \
" link.setAttribute(\"href\", \"#\");\n" \
" link.setAttribute(\"class\", \"sortheader\");\n" \
" link.setAttribute(\"onclick\", \"resort(this);return false;\");\n" \
" var arrow = document.createElement(\"span\");\n" \
" arrow.setAttribute(\"class\", \"sortarrow\");\n" \
" arrow.appendChild(document.createTextNode(\":\"));\n" \
" link.appendChild(arrow)\n" \
" n.replaceChild(link, n.firstChild);\n" \
" }\n" \
" }\n" \
" var lnk = row[init_sort_column].firstChild;\n" \
" if (ascending) {\n" \
" var span = lnk.childNodes[1];\n" \
" span.setAttribute('sortdir','down');\n" \
" }\n" \
" resort(lnk);\n" \
" //}\n" \
" }\n" \
"}\n";
static void http_dirlist_append_js_table_resort (buffer *b, connection *con) {
char col = '0';
char ascending = '0';
if (!buffer_string_is_empty(con->uri.query)) {
const char *qs = con->uri.query->ptr;
do {
if (qs[0] == 'C' && qs[1] == '=') {
switch (qs[2]) {
case 'N': col = '0'; break;
case 'M': col = '1'; break;
case 'S': col = '2'; break;
case 'T':
case 'D': col = '3'; break;
default: break;
}
}
else if (qs[0] == 'O' && qs[1] == '=') {
switch (qs[2]) {
case 'A': ascending = '1'; break;
case 'D': ascending = '0'; break;
default: break;
}
}
} while ((qs = strchr(qs, '&')) && *++qs);
}
buffer_append_string_len(b, CONST_STR_LEN("\n<script type=\"text/javascript\">\n// <!--\n\n"));
buffer_append_string_len(b, js_simple_table_resort, sizeof(js_simple_table_resort)-1);
buffer_append_string_len(b, js_simple_table_init_sort, sizeof(js_simple_table_init_sort)-1);
buffer_append_string_len(b, CONST_STR_LEN("\ninit_sort("));
buffer_append_string_len(b, &col, 1);
buffer_append_string_len(b, CONST_STR_LEN(", "));
buffer_append_string_len(b, &ascending, 1);
buffer_append_string_len(b, CONST_STR_LEN(");\n\n// -->\n</script>\n\n"));
}
static void http_list_directory_header(server *srv, connection *con, plugin_data *p, buffer *out) {
UNUSED(srv);
if (p->conf.auto_layout) {
buffer_append_string_len(out, CONST_STR_LEN(
"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
));
if (!buffer_string_is_empty(p->conf.encoding)) {
buffer_append_string_len(out, CONST_STR_LEN("<meta charset=\""));
buffer_append_string_buffer(out, p->conf.encoding);
buffer_append_string_len(out, CONST_STR_LEN("\">\n"));
}
buffer_append_string_len(out, CONST_STR_LEN("<title>Index of "));
buffer_append_string_encoded(out, CONST_BUF_LEN(con->uri.path), ENCODING_MINIMAL_XML);
buffer_append_string_len(out, CONST_STR_LEN("</title>\n"));
if (!buffer_string_is_empty(p->conf.external_css)) {
buffer_append_string_len(out, CONST_STR_LEN("<meta name=\"viewport\" content=\"initial-scale=1\">"));
buffer_append_string_len(out, CONST_STR_LEN("<link rel=\"stylesheet\" type=\"text/css\" href=\""));