2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 module:depends("http");
10 local lfs = require "lfs";
12 local os_date = os.date;
14 local stat = lfs.attributes;
16 local http_base = module:get_option_string("http_files_dir", module:get_option_string("http_path", "www_files"));
17 local dir_indices = module:get_option("http_files_index", { "index.html", "index.htm" });
18 local show_file_list = module:get_option_boolean("http_files_show_list");
20 local mime_map = module:shared("mime").types;
23 html = "text/html", htm = "text/html",
24 xml = "application/xml",
27 js = "application/javascript",
30 jpeg = "image/jpeg", jpg = "image/jpeg",
31 svg = "image/svg+xml",
33 module:shared("mime").types = mime_map;
35 local mime_types, err = open(module:get_option_string("mime_types_file", "/etc/mime.types"),"r");
37 local mime_data = mime_types:read("*a");
39 setmetatable(mime_map, {
40 __index = function(t, ext)
41 local typ = mime_data:match("\n(%S+)[^\n]*%s"..(ext:lower()).."%s") or "application/octet-stream";
49 local cache = setmetatable({}, { __mode = "kv" }); -- Let the garbage collector have it if it wants to.
51 function serve_file(event, path)
52 local request, response = event.request, event.response;
53 local orig_path = request.path;
54 local full_path = http_base.."/"..path;
55 local attr = stat(full_path);
60 response.headers.last_modified = os_date('!%a, %d %b %Y %H:%M:%S GMT', attr.modification);
62 local tag = ("%02x-%x-%x-%x"):format(attr.dev or 0, attr.ino or 0, attr.size or 0, attr.modification or 0);
63 response.headers.etag = tag;
64 if tag == request.headers.if_none_match then
68 local data = cache[path];
70 response.headers.content_type = data.content_type;
72 elseif attr.mode == "directory" then
73 if full_path:sub(-1) ~= "/" then
74 response.headers.location = orig_path.."/";
77 for i=1,#dir_indices do
78 if stat(full_path..dir_indices[i], "mode") == "file" then
79 return serve_file(event, path..dir_indices[i]);
83 if not show_file_list then
86 local html = require"util.stanza".stanza("html")
87 :tag("head"):tag("title"):text(path):up()
88 :tag("meta", { charset="utf-8" }):up()
90 :tag("body"):tag("h1"):text(path):up()
92 for file in lfs.dir(full_path) do
93 if file:sub(1,1) ~= "." then
94 local attr = stat(full_path..file) or {};
95 html:tag("li", { class = attr.mode })
96 :tag("a", { href = file }):text(file)
100 data = "<!DOCTYPE html>\n"..tostring(html);
101 cache[path] = { data = html, content_type = mime_map.html; hits = 0 };
102 response.headers.content_type = mime_map.html;
106 local f = open(full_path, "rb");
107 data = f and f:read("*a");
112 local ext = path:match("%.([^.]*)$");
113 local content_type = mime_map[ext];
114 cache[path] = { data = data; content_type = content_type; };
115 response.headers.content_type = content_type;
118 return response:send(data);
121 module:provides("http", {
123 ["GET /*"] = serve_file;