comparison plugins/mod_http_files.lua @ 4797:e239668aa6d2

Merge 0.9->trunk
author Matthew Wild <mwild1@gmail.com>
date Sun, 29 Apr 2012 02:10:55 +0100
parents 1138fd3d5846
children 550f0a5e85c5
comparison
equal deleted inserted replaced
4796:04a34287dc12 4797:e239668aa6d2
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 module:depends("http");
10 local lfs = require "lfs";
11
12 local open = io.open;
13 local stat = lfs.attributes;
14
15 local http_base = module:get_option_string("http_files_dir", module:get_option_string("http_path", "www_files"));
16
17 -- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?)
18 local mime_map = {
19 html = "text/html";
20 htm = "text/html";
21 xml = "text/xml";
22 xsl = "text/xml";
23 txt = "text/plain; charset=utf-8";
24 js = "text/javascript";
25 css = "text/css";
26 };
27
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");
34 end
35 return 403;
36 end
37 local f, err = open(full_path, "rb");
38 if not f then
39 module:log("warn", "Failed to open file: %s", err);
40 return 404;
41 end
42 local data = f:read("*a");
43 f:close();
44 if not data then
45 return 403;
46 end
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);
50 end
51
52 module:provides("http", {
53 route = {
54 ["GET /*"] = serve_file;
55 };
56 });
57