Mercurial > prosody-hg
comparison plugins/mod_http_files.lua @ 5255:bf34f1ee08ee
mod_http_files: Only serve cached data if etag is unchanged.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 21 Dec 2012 08:25:09 +0100 |
| parents | df3552822054 |
| children | a77c6eba461e |
comparison
equal
deleted
inserted
replaced
| 5254:df3552822054 | 5255:bf34f1ee08ee |
|---|---|
| 71 or last_modified == if_modified_since then | 71 or last_modified == if_modified_since then |
| 72 return 304; | 72 return 304; |
| 73 end | 73 end |
| 74 | 74 |
| 75 local data = cache[path]; | 75 local data = cache[path]; |
| 76 if data then | 76 if data and data.etag == etag then |
| 77 response_headers.content_type = data.content_type; | 77 response_headers.content_type = data.content_type; |
| 78 data = data.data; | 78 data = data.data; |
| 79 elseif attr.mode == "directory" then | 79 elseif attr.mode == "directory" then |
| 80 if full_path:sub(-1) ~= "/" then | 80 if full_path:sub(-1) ~= "/" then |
| 81 response_headers.location = orig_path.."/"; | 81 response_headers.location = orig_path.."/"; |
| 103 :tag("a", { href = file }):text(file) | 103 :tag("a", { href = file }):text(file) |
| 104 :up():up(); | 104 :up():up(); |
| 105 end | 105 end |
| 106 end | 106 end |
| 107 data = "<!DOCTYPE html>\n"..tostring(html); | 107 data = "<!DOCTYPE html>\n"..tostring(html); |
| 108 cache[path] = { data = data, content_type = mime_map.html; hits = 0 }; | 108 cache[path] = { data = data, content_type = mime_map.html; etag = etag; }; |
| 109 response_headers.content_type = mime_map.html; | 109 response_headers.content_type = mime_map.html; |
| 110 end | 110 end |
| 111 | 111 |
| 112 else | 112 else |
| 113 local f, err = open(full_path, "rb"); | 113 local f, err = open(full_path, "rb"); |
| 118 if not data then | 118 if not data then |
| 119 return 403; | 119 return 403; |
| 120 end | 120 end |
| 121 local ext = path:match("%.([^./]+)$"); | 121 local ext = path:match("%.([^./]+)$"); |
| 122 local content_type = ext and mime_map[ext]; | 122 local content_type = ext and mime_map[ext]; |
| 123 cache[path] = { data = data; content_type = content_type; }; | 123 cache[path] = { data = data; content_type = content_type; etag = etag }; |
| 124 response_headers.content_type = content_type; | 124 response_headers.content_type = content_type; |
| 125 end | 125 end |
| 126 | 126 |
| 127 return response:send(data); | 127 return response:send(data); |
| 128 end | 128 end |
