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";
13 local stat = lfs.attributes;
15 local http_base = module:get_option_string("http_files_dir", module:get_option_string("http_path", "www_files"));
17 -- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?)
23 txt = "text/plain; charset=utf-8";
24 js = "text/javascript";
28 function serve_file(event, path)
29 local response = event.response;
30 local full_path = http_base.."/"..path;
31 if stat(full_path, "mode") == "directory" then
32 if stat(full_path.."/index.html", "mode") == "file" then
33 return serve_file(event, path.."/index.html");
37 local f, err = open(full_path, "rb");
39 module:log("warn", "Failed to open file: %s", err);
42 local data = f:read("*a");
47 local ext = path:match("%.([^.]*)$");
48 response.headers.content_type = mime_map[ext]; -- Content-Type should be nil when not known
49 return response:send(data);
52 module:provides("http", {
54 ["GET /*"] = serve_file;