webmcp
diff framework/env/request/handler.lua @ 270:aedd13009ddc
Proper handling of 404's and mime types for static file delivery
author | jbe |
---|---|
date | Fri Mar 20 16:38:37 2015 +0100 (2015-03-20) |
parents | 56d237b81c18 |
children | ee7fcdade91d |
line diff
1.1 --- a/framework/env/request/handler.lua Fri Mar 20 15:47:51 2015 +0100 1.2 +++ b/framework/env/request/handler.lua Fri Mar 20 16:38:37 2015 +0100 1.3 @@ -1,6 +1,6 @@ 1.4 --[[-- 1.5 request.handler( 1.6 - request, -- HTTP request object 1.7 + http_request, -- HTTP request object 1.8 close -- boolean indicating whether the server should announce to close the connection 1.9 ) 1.10 1.11 @@ -18,7 +18,6 @@ 1.12 end 1.13 end 1.14 1.15 --- TODO: function incomplete yet 1.16 function request.handler(http_request, close) 1.17 _G.app = {} -- may be overwritten or modified by request initializers 1.18 do 1.19 @@ -49,11 +48,21 @@ 1.20 function() 1.21 1.22 if request._route.static then 1.23 - local f = assert(io.open(WEBMCP_BASE_PATH .. "static/" .. request._route.static, "r")) 1.24 - local d = f:read("*a") 1.25 + local f, errmsg = io.open(WEBMCP_BASE_PATH .. "static/" .. request._route.static, "r") 1.26 + if not f then 1.27 + if request.get_404_route() then 1.28 + request.set_status("404 Not Found") 1.29 + request.forward(request.get_404_route()) 1.30 + return 1.31 + else 1.32 + error('Could not open static file "' .. request._route.static .. '": ' .. errmsg) 1.33 + end 1.34 + end 1.35 + local d = assert(f:read("*a")) 1.36 f:close() 1.37 slot.put_into("data", d) 1.38 - slot.set_layout(nil, "application/octet-stream") -- TODO 1.39 + local filename_extension = string.match(request._route.static, "%.([^.]+)$") 1.40 + slot.set_layout(nil, request._mime_types[filename_extension] or "application/octet-stream") 1.41 return 1.42 end 1.43