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");
12 local fileserver
= require
"net.http.files";
14 local base_path
= module
:get_option_path("http_files_dir", module
:get_option_path("http_path"));
15 local cache_size
= module
:get_option_number("http_files_cache_size", 128);
16 local cache_max_file_size
= module
:get_option_number("http_files_cache_max_file_size", 4096);
17 local dir_indices
= module
:get_option_array("http_index_files", { "index.html", "index.htm" });
18 local directory_index
= module
:get_option_boolean("http_dir_listing");
20 local mime_map
= module
:shared("/*/http_files/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("/*/http_files/mime").types
= mime_map
;
35 local mime_types
, err
= open(module
:get_option_path("mime_types_file", "/etc/mime.types", "config"), "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 function get_calling_module()
50 local info
= debug
.getinfo(3, "S");
51 if not info
then return "An unknown module"; end
52 return info
.source
:match
"mod_[^/\\.]+" or info
.short_src
;
55 -- COMPAT -- TODO deprecate
57 if type(opts
) ~= "table" then -- assume path string
58 opts
= { path
= opts
};
60 if opts
.directory_index
== nil then
61 opts
.directory_index
= directory_index
;
63 if opts
.mime_map
== nil then
64 opts
.mime_map
= mime_map
;
66 if opts
.cache_size
== nil then
67 opts
.cache_size
= cache_size
;
69 if opts
.cache_max_file_size
== nil then
70 opts
.cache_max_file_size
= cache_max_file_size
;
72 if opts
.index_files
== nil then
73 opts
.index_files
= dir_indices
;
75 -- TODO Crank up to warning
76 module
:log("debug", "%s should be updated to use 'net.http.files' insead of mod_http_files", get_calling_module());
77 return fileserver
.serve(opts
);
80 function wrap_route(routes
)
81 module
:log("debug", "%s should be updated to use 'net.http.files' insead of mod_http_files", get_calling_module());
82 for route
,handler
in pairs(routes
) do
83 if type(handler
) ~= "function" then
84 routes
[route
] = fileserver
.serve(handler
);
90 module
:provides("http", {
92 ["GET /*"] = fileserver
.serve({
94 directory_index
= directory_index
;
96 cache_size
= cache_size
;
97 cache_max_file_size
= cache_max_file_size
;
98 index_files
= dir_indices
;