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 server = require"net.http.server";
11 local lfs = require "lfs";
13 local os_date = os.date;
15 local stat = lfs.attributes;
16 local build_path = require"socket.url".build_path;
18 local base_path = module:get_option_string("http_files_dir", module:get_option_string("http_path"));
19 local dir_indices = module:get_option("http_index_files", { "index.html", "index.htm" });
20 local directory_index = module:get_option_boolean("http_dir_listing");
22 local mime_map = module:shared("/*/http_files/mime").types;
25 html = "text/html", htm = "text/html",
26 xml = "application/xml",
29 js = "application/javascript",
32 jpeg = "image/jpeg", jpg = "image/jpeg",
33 svg = "image/svg+xml",
35 module:shared("/*/http_files/mime").types = mime_map;
37 local mime_types, err = open(module:get_option_string("mime_types_file", "/etc/mime.types"),"r");
39 local mime_data = mime_types:read("*a");
41 setmetatable(mime_map, {
42 __index = function(t, ext)
43 local typ = mime_data:match("\n(%S+)[^\n]*%s"..(ext:lower()).."%s") or "application/octet-stream";
51 local cache = setmetatable({}, { __mode = "kv" }); -- Let the garbage collector have it if it wants to.
54 if type(opts) ~= "table" then -- assume path string
55 opts = { path = opts };
57 local base_path = opts.path;
58 local dir_indices = opts.index_files or dir_indices;
59 local directory_index = opts.directory_index;
60 local function serve_file(event, path)
61 local request, response = event.request, event.response;
62 local orig_path = request.path;
63 local full_path = base_path .. (path and "/"..path or "");
64 local attr = stat(full_path);
69 local request_headers, response_headers = request.headers, response.headers;
71 local last_modified = os_date('!%a, %d %b %Y %H:%M:%S GMT', attr.modification);
72 response_headers.last_modified = last_modified;
74 local etag = ("%02x-%x-%x-%x"):format(attr.dev or 0, attr.ino or 0, attr.size or 0, attr.modification or 0);
75 response_headers.etag = etag;
77 local if_none_match = request_headers.if_none_match
78 local if_modified_since = request_headers.if_modified_since;
79 if etag == if_none_match
80 or (not if_none_match and last_modified == if_modified_since) then
84 local data = cache[orig_path];
85 if data and data.etag == etag then
86 response_headers.content_type = data.content_type;
88 elseif attr.mode == "directory" and path then
89 if full_path:sub(-1) ~= "/" then
90 local path = { is_absolute = true, is_directory = true };
91 for dir in orig_path:gmatch("[^/]+") do path[#path+1]=dir; end
92 response_headers.location = build_path(path);
95 for i=1,#dir_indices do
96 if stat(full_path..dir_indices[i], "mode") == "file" then
97 return serve_file(event, path..dir_indices[i]);
101 if directory_index then
102 data = server._events.fire_event("directory-index", { path = request.path, full_path = full_path });
107 cache[orig_path] = { data = data, content_type = mime_map.html; etag = etag; };
108 response_headers.content_type = mime_map.html;
111 local f, err = open(full_path, "rb");
113 data, err = f:read("*a");
117 module:log("debug", "Could not open or read %s. Error was %s", full_path, err);
120 local ext = full_path:match("%.([^./]+)$");
121 local content_type = ext and mime_map[ext];
122 cache[orig_path] = { data = data; content_type = content_type; etag = etag };
123 response_headers.content_type = content_type;
126 return response:send(data);
132 function wrap_route(routes)
133 for route,handler in pairs(routes) do
134 if type(handler) ~= "function" then
135 routes[route] = serve(handler);
142 module:provides("http", {
146 directory_index = directory_index;
151 module:log("debug", "http_files_dir not set, assuming use by some other module");