diff --git a/http/server.lua b/http/server.lua index 92b0424..c5e7322 100644 --- a/http/server.lua +++ b/http/server.lua @@ -2,7 +2,7 @@ local lib = require('http.lib') -local io = io +local fio = require('fio') local require = require local package = package local mime_types = require('http.mime_types') @@ -349,8 +349,17 @@ local function load_template(self, r, format) local tpl = catfile(self.options.app_dir, 'templates', file) - local fh = io.input(tpl) - local template = fh:read('*a') + local fh, err = fio.open(tpl) + if err ~= nil then + errorf("Can not load template for '%s': '%s'", r.path, err) + end + + local template + template, err = fh:read() + if err ~= nil then + errorf("Can not load template for '%s': '%s'", r.path, err) + end + fh:close() if self.options.cache_templates then @@ -540,14 +549,18 @@ local function static_file(self, request, format) } end - local s, fh = pcall(io.input, file) + local fh, err = fio.open(file, {'O_RDONLY'}) + if err ~= nil then + return { status = 404 } + end - if not s then - return { status = 404 } - end + local body + body, err = fh:read() + if err ~= nil then + errorf("Can not return static file for '%s': '%s'", request:path(), err) + end - local body = fh:read('*a') - io.close(fh) + fh:close() if self.options.cache_static then self.cache.static[ file ] = body