* libs/web: Fixed Luci template cache
* libs/web: Added luci.http.urlencode, luci.http.urldecode * Minor enhancements
This commit is contained in:
parent
cdd871d834
commit
f8925eefa3
3 changed files with 24 additions and 7 deletions
|
@ -27,7 +27,6 @@ limitations under the License.
|
||||||
module("luci.fs", package.seeall)
|
module("luci.fs", package.seeall)
|
||||||
|
|
||||||
require("posix")
|
require("posix")
|
||||||
posix.umask("rwx------")
|
|
||||||
|
|
||||||
-- Glob
|
-- Glob
|
||||||
glob = posix.glob
|
glob = posix.glob
|
||||||
|
|
|
@ -44,3 +44,19 @@ function build_querystring(table)
|
||||||
|
|
||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function urldecode(str)
|
||||||
|
str = str:gsub("+", " ")
|
||||||
|
str = str:gsub("%%(%x%x)",
|
||||||
|
function(h) return string.char(tonumber(h,16)) end)
|
||||||
|
str = str:gsub("\r\n", "\n")
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
|
||||||
|
function urlencode(str)
|
||||||
|
str = str:gsub("\n", "\r\n")
|
||||||
|
str = str:gsub("([^%w ])",
|
||||||
|
function (c) return string.format ("%%%02X", string.byte(c)) end)
|
||||||
|
str = str:gsub(" ", "+")
|
||||||
|
return str
|
||||||
|
end
|
|
@ -35,6 +35,9 @@ luci.config.template = luci.config.template or {}
|
||||||
viewdir = luci.config.template.viewdir or luci.sys.libpath() .. "/view"
|
viewdir = luci.config.template.viewdir or luci.sys.libpath() .. "/view"
|
||||||
compiledir = luci.config.template.compiledir or luci.sys.libpath() .. "/view"
|
compiledir = luci.config.template.compiledir or luci.sys.libpath() .. "/view"
|
||||||
|
|
||||||
|
-- Enforce cache security
|
||||||
|
compiledir = compiledir .. "/" .. luci.sys.process.info("uid")
|
||||||
|
|
||||||
|
|
||||||
-- Compile modes:
|
-- Compile modes:
|
||||||
-- none: Never compile, only use precompiled data from files
|
-- none: Never compile, only use precompiled data from files
|
||||||
|
@ -147,13 +150,17 @@ function Template.__init__(self, name)
|
||||||
|
|
||||||
-- Compile and build
|
-- Compile and build
|
||||||
local sourcefile = viewdir .. "/" .. name .. ".htm"
|
local sourcefile = viewdir .. "/" .. name .. ".htm"
|
||||||
local compiledfile = compiledir .. "/" .. name .. ".lua"
|
local compiledfile = compiledir .. "/" .. luci.http.urlencode(name) .. ".lua"
|
||||||
local err
|
local err
|
||||||
|
|
||||||
if compiler_mode == "file" then
|
if compiler_mode == "file" then
|
||||||
local tplmt = luci.fs.mtime(sourcefile)
|
local tplmt = luci.fs.mtime(sourcefile)
|
||||||
local commt = luci.fs.mtime(compiledfile)
|
local commt = luci.fs.mtime(compiledfile)
|
||||||
|
|
||||||
|
if not luci.fs.mtime(compiledir) then
|
||||||
|
luci.fs.mkdir(compiledir, true)
|
||||||
|
end
|
||||||
|
|
||||||
-- Build if there is no compiled file or if compiled file is outdated
|
-- Build if there is no compiled file or if compiled file is outdated
|
||||||
if ((commt == nil) and not (tplmt == nil))
|
if ((commt == nil) and not (tplmt == nil))
|
||||||
or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then
|
or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then
|
||||||
|
@ -163,11 +170,6 @@ function Template.__init__(self, name)
|
||||||
if source then
|
if source then
|
||||||
local compiled, err = compile(source)
|
local compiled, err = compile(source)
|
||||||
|
|
||||||
local compiledfile_dir = luci.fs.dirname(compiledfile)
|
|
||||||
if not luci.fs.mtime(compiledfile_dir) then
|
|
||||||
luci.fs.mkdir(compiledfile_dir)
|
|
||||||
end
|
|
||||||
|
|
||||||
luci.fs.writefile(compiledfile, luci.util.dump(compiled))
|
luci.fs.writefile(compiledfile, luci.util.dump(compiled))
|
||||||
self.template = compiled
|
self.template = compiled
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue