author | Kim Alvefur <zash@zash.se> |
Fri, 21 Dec 2012 08:14:33 +0100 | |
changeset 5253 | ad45612199e0 |
parent 5252 | 6209b9a0244b |
child 5254 | df3552822054 |
permissions | -rw-r--r-- |
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1384
diff
changeset
|
1 |
-- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2785
diff
changeset
|
2 |
-- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2785
diff
changeset
|
3 |
-- Copyright (C) 2008-2010 Waqas Hussain |
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1384
diff
changeset
|
4 |
-- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1384
diff
changeset
|
5 |
-- This project is MIT/X11 licensed. Please see the |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1384
diff
changeset
|
6 |
-- COPYING file in the source package for more information. |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1384
diff
changeset
|
7 |
-- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1384
diff
changeset
|
8 |
|
4670
bd5e5e23942a
mod_httpserver: Adapt to use the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
3353
diff
changeset
|
9 |
module:depends("http"); |
3353
cd3cbf361f8f
mod_httpserver: Serve index.html if a request is made for a directory and it contains one (thanks Brian Cully)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
10 |
local lfs = require "lfs"; |
635
25f1117d7886
Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
|
5235
e0373e0dd048
mod_http_files: Add Last-Modified header
Kim Alvefur <zash@zash.se>
parents:
5234
diff
changeset
|
12 |
local os_date = os.date; |
635
25f1117d7886
Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
local open = io.open; |
3353
cd3cbf361f8f
mod_httpserver: Serve index.html if a request is made for a directory and it contains one (thanks Brian Cully)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
14 |
local stat = lfs.attributes; |
635
25f1117d7886
Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
|
4703
684ea42ca77a
mod_http_files: Change option name from http_path to http_files_dir
Matthew Wild <mwild1@gmail.com>
parents:
4701
diff
changeset
|
16 |
local http_base = module:get_option_string("http_files_dir", module:get_option_string("http_path", "www_files")); |
5232
c9bb5879e193
mod_http_files: Configurable number of index files to check for
Kim Alvefur <zash@zash.se>
parents:
4868
diff
changeset
|
17 |
local dir_indices = module:get_option("http_files_index", { "index.html", "index.htm" }); |
5238
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
18 |
local show_file_list = module:get_option_boolean("http_files_show_list"); |
635
25f1117d7886
Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
|
5237
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
20 |
local mime_map = module:shared("mime").types; |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
21 |
if not mime_map then |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
22 |
mime_map = { |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
23 |
html = "text/html", htm = "text/html", |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
24 |
xml = "application/xml", |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
25 |
txt = "text/plain", |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
26 |
css = "text/css", |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
27 |
js = "application/javascript", |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
28 |
png = "image/png", |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
29 |
gif = "image/gif", |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
30 |
jpeg = "image/jpeg", jpg = "image/jpeg", |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
31 |
svg = "image/svg+xml", |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
32 |
}; |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
33 |
module:shared("mime").types = mime_map; |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
34 |
|
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
35 |
local mime_types, err = open(module:get_option_string("mime_types_file", "/etc/mime.types"),"r"); |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
36 |
if mime_types then |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
37 |
local mime_data = mime_types:read("*a"); |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
38 |
mime_types:close(); |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
39 |
setmetatable(mime_map, { |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
40 |
__index = function(t, ext) |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
41 |
local typ = mime_data:match("\n(%S+)[^\n]*%s"..(ext:lower()).."%s") or "application/octet-stream"; |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
42 |
t[ext] = typ; |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
43 |
return typ; |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
44 |
end |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
45 |
}); |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
46 |
end |
b1038f449e15
mod_http_files: Have mimetypes in a shared table. Get mimetypes from /etc/mime.types if exists.
Kim Alvefur <zash@zash.se>
parents:
5236
diff
changeset
|
47 |
end |
2771
c9834f338a4e
mod_httpserver: Return Content-Type header based on file extension.
Waqas Hussain <waqas20@gmail.com>
parents:
1870
diff
changeset
|
48 |
|
5236
8d116a0cdacd
mod_http_files: Cache data read from disk in a weak table
Kim Alvefur <zash@zash.se>
parents:
5235
diff
changeset
|
49 |
local cache = setmetatable({}, { __mode = "kv" }); -- Let the garbage collector have it if it wants to. |
8d116a0cdacd
mod_http_files: Cache data read from disk in a weak table
Kim Alvefur <zash@zash.se>
parents:
5235
diff
changeset
|
50 |
|
4672
e17fe44146b0
mod_http_files: Rename argument to reflect what it actually is
Kim Alvefur <zash@zash.se>
parents:
4671
diff
changeset
|
51 |
function serve_file(event, path) |
5233
342c46e62f50
mod_http_files: Return 404 faster if file does not exist
Kim Alvefur <zash@zash.se>
parents:
5232
diff
changeset
|
52 |
local request, response = event.request, event.response; |
342c46e62f50
mod_http_files: Return 404 faster if file does not exist
Kim Alvefur <zash@zash.se>
parents:
5232
diff
changeset
|
53 |
local orig_path = request.path; |
4716
6eeb142a8073
mod_http_files, net.http.parser: Move path normalization to net.http.parser so that all modules can benefit
Matthew Wild <mwild1@gmail.com>
parents:
4703
diff
changeset
|
54 |
local full_path = http_base.."/"..path; |
5233
342c46e62f50
mod_http_files: Return 404 faster if file does not exist
Kim Alvefur <zash@zash.se>
parents:
5232
diff
changeset
|
55 |
local attr = stat(full_path); |
342c46e62f50
mod_http_files: Return 404 faster if file does not exist
Kim Alvefur <zash@zash.se>
parents:
5232
diff
changeset
|
56 |
if not attr then |
342c46e62f50
mod_http_files: Return 404 faster if file does not exist
Kim Alvefur <zash@zash.se>
parents:
5232
diff
changeset
|
57 |
return 404; |
342c46e62f50
mod_http_files: Return 404 faster if file does not exist
Kim Alvefur <zash@zash.se>
parents:
5232
diff
changeset
|
58 |
end |
342c46e62f50
mod_http_files: Return 404 faster if file does not exist
Kim Alvefur <zash@zash.se>
parents:
5232
diff
changeset
|
59 |
|
5253
ad45612199e0
mod_http_files: Avoid a bunch of table lookups
Kim Alvefur <zash@zash.se>
parents:
5252
diff
changeset
|
60 |
local request_headers, response_headers = request.headers, response.headers; |
ad45612199e0
mod_http_files: Avoid a bunch of table lookups
Kim Alvefur <zash@zash.se>
parents:
5252
diff
changeset
|
61 |
|
5248
13553f4132a8
mod_http_files: Compare If-Modified-Since to last modification date
Kim Alvefur <zash@zash.se>
parents:
5247
diff
changeset
|
62 |
local last_modified = os_date('!%a, %d %b %Y %H:%M:%S GMT', attr.modification); |
5253
ad45612199e0
mod_http_files: Avoid a bunch of table lookups
Kim Alvefur <zash@zash.se>
parents:
5252
diff
changeset
|
63 |
response_headers.last_modified = last_modified; |
5234
a9f0a1becc66
mod_http_files: Add ETag and check If-None-Match to allow client-side cache
Kim Alvefur <zash@zash.se>
parents:
5233
diff
changeset
|
64 |
|
5248
13553f4132a8
mod_http_files: Compare If-Modified-Since to last modification date
Kim Alvefur <zash@zash.se>
parents:
5247
diff
changeset
|
65 |
local etag = ("%02x-%x-%x-%x"):format(attr.dev or 0, attr.ino or 0, attr.size or 0, attr.modification or 0); |
5253
ad45612199e0
mod_http_files: Avoid a bunch of table lookups
Kim Alvefur <zash@zash.se>
parents:
5252
diff
changeset
|
66 |
response_headers.etag = etag; |
5248
13553f4132a8
mod_http_files: Compare If-Modified-Since to last modification date
Kim Alvefur <zash@zash.se>
parents:
5247
diff
changeset
|
67 |
|
5253
ad45612199e0
mod_http_files: Avoid a bunch of table lookups
Kim Alvefur <zash@zash.se>
parents:
5252
diff
changeset
|
68 |
local if_none_match = request_headers.if_none_match |
ad45612199e0
mod_http_files: Avoid a bunch of table lookups
Kim Alvefur <zash@zash.se>
parents:
5252
diff
changeset
|
69 |
local if_modified_since = request_headers.if_modified_since; |
ad45612199e0
mod_http_files: Avoid a bunch of table lookups
Kim Alvefur <zash@zash.se>
parents:
5252
diff
changeset
|
70 |
if etag == if_none_match |
ad45612199e0
mod_http_files: Avoid a bunch of table lookups
Kim Alvefur <zash@zash.se>
parents:
5252
diff
changeset
|
71 |
or last_modified == if_modified_since then |
5234
a9f0a1becc66
mod_http_files: Add ETag and check If-None-Match to allow client-side cache
Kim Alvefur <zash@zash.se>
parents:
5233
diff
changeset
|
72 |
return 304; |
a9f0a1becc66
mod_http_files: Add ETag and check If-None-Match to allow client-side cache
Kim Alvefur <zash@zash.se>
parents:
5233
diff
changeset
|
73 |
end |
a9f0a1becc66
mod_http_files: Add ETag and check If-None-Match to allow client-side cache
Kim Alvefur <zash@zash.se>
parents:
5233
diff
changeset
|
74 |
|
5236
8d116a0cdacd
mod_http_files: Cache data read from disk in a weak table
Kim Alvefur <zash@zash.se>
parents:
5235
diff
changeset
|
75 |
local data = cache[path]; |
8d116a0cdacd
mod_http_files: Cache data read from disk in a weak table
Kim Alvefur <zash@zash.se>
parents:
5235
diff
changeset
|
76 |
if data then |
5253
ad45612199e0
mod_http_files: Avoid a bunch of table lookups
Kim Alvefur <zash@zash.se>
parents:
5252
diff
changeset
|
77 |
response_headers.content_type = data.content_type; |
5236
8d116a0cdacd
mod_http_files: Cache data read from disk in a weak table
Kim Alvefur <zash@zash.se>
parents:
5235
diff
changeset
|
78 |
data = data.data; |
8d116a0cdacd
mod_http_files: Cache data read from disk in a weak table
Kim Alvefur <zash@zash.se>
parents:
5235
diff
changeset
|
79 |
elseif attr.mode == "directory" then |
4868
550f0a5e85c5
mod_http_files: Respond with a 301 redirect for directories to append a / (fixes relative links)
Kim Alvefur <zash@zash.se>
parents:
4722
diff
changeset
|
80 |
if full_path:sub(-1) ~= "/" then |
5253
ad45612199e0
mod_http_files: Avoid a bunch of table lookups
Kim Alvefur <zash@zash.se>
parents:
5252
diff
changeset
|
81 |
response_headers.location = orig_path.."/"; |
4868
550f0a5e85c5
mod_http_files: Respond with a 301 redirect for directories to append a / (fixes relative links)
Kim Alvefur <zash@zash.se>
parents:
4722
diff
changeset
|
82 |
return 301; |
3353
cd3cbf361f8f
mod_httpserver: Serve index.html if a request is made for a directory and it contains one (thanks Brian Cully)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
83 |
end |
5232
c9bb5879e193
mod_http_files: Configurable number of index files to check for
Kim Alvefur <zash@zash.se>
parents:
4868
diff
changeset
|
84 |
for i=1,#dir_indices do |
c9bb5879e193
mod_http_files: Configurable number of index files to check for
Kim Alvefur <zash@zash.se>
parents:
4868
diff
changeset
|
85 |
if stat(full_path..dir_indices[i], "mode") == "file" then |
c9bb5879e193
mod_http_files: Configurable number of index files to check for
Kim Alvefur <zash@zash.se>
parents:
4868
diff
changeset
|
86 |
return serve_file(event, path..dir_indices[i]); |
c9bb5879e193
mod_http_files: Configurable number of index files to check for
Kim Alvefur <zash@zash.se>
parents:
4868
diff
changeset
|
87 |
end |
4868
550f0a5e85c5
mod_http_files: Respond with a 301 redirect for directories to append a / (fixes relative links)
Kim Alvefur <zash@zash.se>
parents:
4722
diff
changeset
|
88 |
end |
550f0a5e85c5
mod_http_files: Respond with a 301 redirect for directories to append a / (fixes relative links)
Kim Alvefur <zash@zash.se>
parents:
4722
diff
changeset
|
89 |
|
5238
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
90 |
if not show_file_list then |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
91 |
return 403; |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
92 |
else |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
93 |
local html = require"util.stanza".stanza("html") |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
94 |
:tag("head"):tag("title"):text(path):up() |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
95 |
:tag("meta", { charset="utf-8" }):up() |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
96 |
:up() |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
97 |
:tag("body"):tag("h1"):text(path):up() |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
98 |
:tag("ul"); |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
99 |
for file in lfs.dir(full_path) do |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
100 |
if file:sub(1,1) ~= "." then |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
101 |
local attr = stat(full_path..file) or {}; |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
102 |
html:tag("li", { class = attr.mode }) |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
103 |
:tag("a", { href = file }):text(file) |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
104 |
:up():up(); |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
105 |
end |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
106 |
end |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
107 |
data = "<!DOCTYPE html>\n"..tostring(html); |
5247
9257f4c47ffa
mod_http_files: Fix caching the stringified directory index
Kim Alvefur <zash@zash.se>
parents:
5238
diff
changeset
|
108 |
cache[path] = { data = data, content_type = mime_map.html; hits = 0 }; |
5253
ad45612199e0
mod_http_files: Avoid a bunch of table lookups
Kim Alvefur <zash@zash.se>
parents:
5252
diff
changeset
|
109 |
response_headers.content_type = mime_map.html; |
5238
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
110 |
end |
0cc0359d8c39
mod_http_files: Generate simple directory index.
Kim Alvefur <zash@zash.se>
parents:
5237
diff
changeset
|
111 |
|
5236
8d116a0cdacd
mod_http_files: Cache data read from disk in a weak table
Kim Alvefur <zash@zash.se>
parents:
5235
diff
changeset
|
112 |
else |
5252
6209b9a0244b
mod_http_files: No use in closing a file handle if we couldn't open it
Kim Alvefur <zash@zash.se>
parents:
5248
diff
changeset
|
113 |
local f, err = open(full_path, "rb"); |
6209b9a0244b
mod_http_files: No use in closing a file handle if we couldn't open it
Kim Alvefur <zash@zash.se>
parents:
5248
diff
changeset
|
114 |
if f then |
6209b9a0244b
mod_http_files: No use in closing a file handle if we couldn't open it
Kim Alvefur <zash@zash.se>
parents:
5248
diff
changeset
|
115 |
data = f:read("*a"); |
6209b9a0244b
mod_http_files: No use in closing a file handle if we couldn't open it
Kim Alvefur <zash@zash.se>
parents:
5248
diff
changeset
|
116 |
f:close(); |
6209b9a0244b
mod_http_files: No use in closing a file handle if we couldn't open it
Kim Alvefur <zash@zash.se>
parents:
5248
diff
changeset
|
117 |
end |
5236
8d116a0cdacd
mod_http_files: Cache data read from disk in a weak table
Kim Alvefur <zash@zash.se>
parents:
5235
diff
changeset
|
118 |
if not data then |
8d116a0cdacd
mod_http_files: Cache data read from disk in a weak table
Kim Alvefur <zash@zash.se>
parents:
5235
diff
changeset
|
119 |
return 403; |
8d116a0cdacd
mod_http_files: Cache data read from disk in a weak table
Kim Alvefur <zash@zash.se>
parents:
5235
diff
changeset
|
120 |
end |
8d116a0cdacd
mod_http_files: Cache data read from disk in a weak table
Kim Alvefur <zash@zash.se>
parents:
5235
diff
changeset
|
121 |
local ext = path:match("%.([^.]*)$"); |
8d116a0cdacd
mod_http_files: Cache data read from disk in a weak table
Kim Alvefur <zash@zash.se>
parents:
5235
diff
changeset
|
122 |
local content_type = mime_map[ext]; |
8d116a0cdacd
mod_http_files: Cache data read from disk in a weak table
Kim Alvefur <zash@zash.se>
parents:
5235
diff
changeset
|
123 |
cache[path] = { data = data; content_type = content_type; }; |
5253
ad45612199e0
mod_http_files: Avoid a bunch of table lookups
Kim Alvefur <zash@zash.se>
parents:
5252
diff
changeset
|
124 |
response_headers.content_type = content_type; |
3353
cd3cbf361f8f
mod_httpserver: Serve index.html if a request is made for a directory and it contains one (thanks Brian Cully)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
125 |
end |
5233
342c46e62f50
mod_http_files: Return 404 faster if file does not exist
Kim Alvefur <zash@zash.se>
parents:
5232
diff
changeset
|
126 |
|
4670
bd5e5e23942a
mod_httpserver: Adapt to use the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
3353
diff
changeset
|
127 |
return response:send(data); |
1667
c7bb2264e3b8
mod_httpserver: Set default file handler (you can now request static files as /*) and restructure code a bit
Matthew Wild <mwild1@gmail.com>
parents:
1552
diff
changeset
|
128 |
end |
1770
3e17002221eb
mod_httpserver: Backport from trunk more thorough validation of URLs prior to processing
Matthew Wild <mwild1@gmail.com>
parents:
1552
diff
changeset
|
129 |
|
4670
bd5e5e23942a
mod_httpserver: Adapt to use the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
3353
diff
changeset
|
130 |
module:provides("http", { |
bd5e5e23942a
mod_httpserver: Adapt to use the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
3353
diff
changeset
|
131 |
route = { |
4722
1138fd3d5846
mod_http_files: Specify method in HTTP route
Matthew Wild <mwild1@gmail.com>
parents:
4716
diff
changeset
|
132 |
["GET /*"] = serve_file; |
4670
bd5e5e23942a
mod_httpserver: Adapt to use the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
3353
diff
changeset
|
133 |
}; |
bd5e5e23942a
mod_httpserver: Adapt to use the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
3353
diff
changeset
|
134 |
}); |
1667
c7bb2264e3b8
mod_httpserver: Set default file handler (you can now request static files as /*) and restructure code a bit
Matthew Wild <mwild1@gmail.com>
parents:
1552
diff
changeset
|
135 |