Mercurial > prosody-hg
comparison plugins/mod_http_files.lua @ 5253:ad45612199e0
mod_http_files: Avoid a bunch of table lookups
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 21 Dec 2012 08:14:33 +0100 |
| parents | 6209b9a0244b |
| children | df3552822054 |
comparison
equal
deleted
inserted
replaced
| 5252:6209b9a0244b | 5253:ad45612199e0 |
|---|---|
| 55 local attr = stat(full_path); | 55 local attr = stat(full_path); |
| 56 if not attr then | 56 if not attr then |
| 57 return 404; | 57 return 404; |
| 58 end | 58 end |
| 59 | 59 |
| 60 local request_headers, response_headers = request.headers, response.headers; | |
| 61 | |
| 60 local last_modified = os_date('!%a, %d %b %Y %H:%M:%S GMT', attr.modification); | 62 local last_modified = os_date('!%a, %d %b %Y %H:%M:%S GMT', attr.modification); |
| 61 response.headers.last_modified = last_modified; | 63 response_headers.last_modified = last_modified; |
| 62 | 64 |
| 63 local etag = ("%02x-%x-%x-%x"):format(attr.dev or 0, attr.ino or 0, attr.size or 0, attr.modification or 0); | 65 local etag = ("%02x-%x-%x-%x"):format(attr.dev or 0, attr.ino or 0, attr.size or 0, attr.modification or 0); |
| 64 response.headers.etag = etag; | 66 response_headers.etag = etag; |
| 65 | 67 |
| 66 if etag == request.headers.if_none_match | 68 local if_none_match = request_headers.if_none_match |
| 67 or last_modified == request.headers.if_modified_since then | 69 local if_modified_since = request_headers.if_modified_since; |
| 70 if etag == if_none_match | |
| 71 or last_modified == if_modified_since then | |
| 68 return 304; | 72 return 304; |
| 69 end | 73 end |
| 70 | 74 |
| 71 local data = cache[path]; | 75 local data = cache[path]; |
| 72 if data then | 76 if data then |
| 73 response.headers.content_type = data.content_type; | 77 response_headers.content_type = data.content_type; |
| 74 data = data.data; | 78 data = data.data; |
| 75 elseif attr.mode == "directory" then | 79 elseif attr.mode == "directory" then |
| 76 if full_path:sub(-1) ~= "/" then | 80 if full_path:sub(-1) ~= "/" then |
| 77 response.headers.location = orig_path.."/"; | 81 response_headers.location = orig_path.."/"; |
| 78 return 301; | 82 return 301; |
| 79 end | 83 end |
| 80 for i=1,#dir_indices do | 84 for i=1,#dir_indices do |
| 81 if stat(full_path..dir_indices[i], "mode") == "file" then | 85 if stat(full_path..dir_indices[i], "mode") == "file" then |
| 82 return serve_file(event, path..dir_indices[i]); | 86 return serve_file(event, path..dir_indices[i]); |
| 100 :up():up(); | 104 :up():up(); |
| 101 end | 105 end |
| 102 end | 106 end |
| 103 data = "<!DOCTYPE html>\n"..tostring(html); | 107 data = "<!DOCTYPE html>\n"..tostring(html); |
| 104 cache[path] = { data = data, content_type = mime_map.html; hits = 0 }; | 108 cache[path] = { data = data, content_type = mime_map.html; hits = 0 }; |
| 105 response.headers.content_type = mime_map.html; | 109 response_headers.content_type = mime_map.html; |
| 106 end | 110 end |
| 107 | 111 |
| 108 else | 112 else |
| 109 local f, err = open(full_path, "rb"); | 113 local f, err = open(full_path, "rb"); |
| 110 if f then | 114 if f then |
| 115 return 403; | 119 return 403; |
| 116 end | 120 end |
| 117 local ext = path:match("%.([^.]*)$"); | 121 local ext = path:match("%.([^.]*)$"); |
| 118 local content_type = mime_map[ext]; | 122 local content_type = mime_map[ext]; |
| 119 cache[path] = { data = data; content_type = content_type; }; | 123 cache[path] = { data = data; content_type = content_type; }; |
| 120 response.headers.content_type = content_type; | 124 response_headers.content_type = content_type; |
| 121 end | 125 end |
| 122 | 126 |
| 123 return response:send(data); | 127 return response:send(data); |
| 124 end | 128 end |
| 125 | 129 |
