Software /
code /
prosody
Comparison
plugins/mod_httpserver.lua @ 2356:6d1e745a96f8
mod_httpserver: Return Content-Type header based on file extension.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Thu, 10 Dec 2009 16:22:34 +0500 |
parent | 2355:08f02de5ab9d |
child | 2357:d978e2ae7013 |
comparison
equal
deleted
inserted
replaced
2355:08f02de5ab9d | 2356:6d1e745a96f8 |
---|---|
14 | 14 |
15 local http_base = config.get("*", "core", "http_path") or "www_files"; | 15 local http_base = config.get("*", "core", "http_path") or "www_files"; |
16 | 16 |
17 local response_400 = { status = "400 Bad Request", body = "<h1>Bad Request</h1>Sorry, we didn't understand your request :(" }; | 17 local response_400 = { status = "400 Bad Request", body = "<h1>Bad Request</h1>Sorry, we didn't understand your request :(" }; |
18 local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" }; | 18 local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" }; |
19 | |
20 -- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?) | |
21 local mime_map = { | |
22 html = "text/html"; | |
23 htm = "text/html"; | |
24 xml = "text/xml"; | |
25 xsl = "text/xml"; | |
26 txt = "plain/text; charset=utf-8"; | |
27 js = "text/javascript"; | |
28 css = "text/css"; | |
29 }; | |
19 | 30 |
20 local function preprocess_path(path) | 31 local function preprocess_path(path) |
21 if path:sub(1,1) ~= "/" then | 32 if path:sub(1,1) ~= "/" then |
22 path = "/"..path; | 33 path = "/"..path; |
23 end | 34 end |
38 function serve_file(path) | 49 function serve_file(path) |
39 local f, err = open(http_base..path, "r"); | 50 local f, err = open(http_base..path, "r"); |
40 if not f then return response_404; end | 51 if not f then return response_404; end |
41 local data = f:read("*a"); | 52 local data = f:read("*a"); |
42 f:close(); | 53 f:close(); |
43 return data; | 54 local ext = path:match("%.([^.]*)$"); |
55 local mime = mime_map[ext]; | |
56 if not mime then | |
57 mime = ext and "application/octet-stream" or "text/html"; | |
58 end | |
59 module:log("warn", "ext: %s, mime: %s", ext, mime); | |
60 return { | |
61 headers = { ["Content-Type"] = mime; }; | |
62 body = data; | |
63 }; | |
44 end | 64 end |
45 | 65 |
46 local function handle_file_request(method, body, request) | 66 local function handle_file_request(method, body, request) |
47 local path = preprocess_path(request.url.path); | 67 local path = preprocess_path(request.url.path); |
48 if not path then return response_400; end | 68 if not path then return response_400; end |