Lighttpd
上QQ阅读APP看书,第一时间看更新

Showing Directory Contents

If we have a download directory and want to easily serve an up-to-date listing of its contents, mod_dirlisting can do the job. It is configurable with the header and footer, custom CSS, and exclude filters (which should usually match our mod_access settings, to hide inaccessible files from the listing).

Note

mod_dirlisting versus Large Directories

Since Lighttpd is single-threaded, while the directory listing gets created, no other work is done by Lighttpd, as with all modules. Therefore, use mod_dirlisting only for small download directories—less than 100 entries should be a good rule of thumb. Otherwise, use a script through one of the CGI backends, which can run independently from Lighttpd.

Let us get straight to an example configuration of mod_dirlisting. In this example, we want to activate mod_dirlisting for the download directory, show a hidden HEADER.txt file (if there is one in the directory) before the listing, show "dotfiles" (starting with a ".") in the listing, but hide files ending with "~" or ".old", and use a custom CSS and footer:

server.modules += ("mod_dirlisting") # add mod_dirlisting to modules
$HTTP["url"] =~ "(^|/)/download/" {
dir-listing.activate = "enable" # enable dirlisting
dir-listing.hide-dotfiles = "disable"
# show files starting with "."
dir-listing.exclude = ("~$", "\.old$")
# hide files ending with "~" or ".old"
dir-listing.external-css = "/css/dir.css" # use custom CSS
dir-listing.show-header = "enable"
# show the contents of the HEADER.txt file before the dirlisting
dir-listing.hide-header-file = "enable"
# hide the HEADER.txt file from the dirlisting
dir-listing.set-footer = "Thanks for trusting <a \href=\"http://ourdomain.com\">us</a>!"
# show a custom footer (the HTML code is inserted directly)
dir-listing.encoding = "utf-8"
# oh, and show the whole thing encoded in UTF-8.
}

This shows a fair amount of configuration options for mod_dirlisting. Here is the complete table of options. The first few options enable the listing and select the files that are shown or hidden, while the remaining options change the display of the listing:

These options allow us to customize almost everything. The listing will show subdirectories first, then the files. The files will be shown with the name (which is linked to the file itself), last modification time, size, and MIME type.

Showing Directory Contents

The generated HTML code contains a number of style classes to customize the display in the external CSS. The CSS style included by default, if no external CSS is set, should provide a good starting point:

a, a:active {text-decoration: none; color: blue;}
Lighttpddirectory content, showinga:visited {color: #48468F;}
a:hover, a:focus {text-decoration: underline; color: red;}
body {background-color: #F5F5F5;}
h2 {margin-bottom: 12px;}
table {margin-left: 12px;}
th, td { font: 90% monospace; text-align: left;}
th { font-weight: bold; padding-right: 14px; padding-bottom: 3px;}
td {padding-right: 14px;}
td.s, th.s {text-align: right;}
div.list { background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom:14px;}
div.foot { font: 90% monospace; color: #787878; padding-top: 4px;}

Here is a complete list of CSS classes and their corresponding HTML elements:

Armed with this knowledge, we can make our listing beautiful, or at least colorful. But how do we protect our download area from deep linking, and separate our paying customers from the anonymous freeloaders? Lighttpd offers not one, but two modules to secure our valuable download.