Skip to content

Commit f90251e

Browse files
committed
Fixed some special characters in URLs getting double-percent-encoded on standard redirection pages.
Updated some function argument checking. Moved things around.
1 parent 40c6299 commit f90251e

File tree

5 files changed

+169
-125
lines changed

5 files changed

+169
-125
lines changed

build/meta.lua

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
--=
1010
--==============================================================
1111
12+
ARGS
1213
convertTextFileEncoding, addBomToUft16File
1314
copyFile, copyFilesInDirectory, copyDirectoryRecursive
1415
execute, executeRequired
@@ -380,4 +381,52 @@ function XPCALL(lua)
380381
end
381382

382383

384+
do
385+
local function outputArgumentChecks(errLevel, argsStr)
386+
local optionalPos = argsStr:find("?", 1, true) or #argsStr
387+
local n = 1
388+
389+
for pos, argNames, types in argsStr:gmatch"()([%w_,]+):([%w_,*]+)" do
390+
if types == "*" then
391+
n = n + #argNames:gsub("[^,]+", "") + 1
392+
393+
else
394+
if pos > optionalPos then types = types..",nil" end
395+
396+
local multipleTypes = types:find(",", 1, true) ~= nil
397+
local ifFormat = multipleTypes and "not isAny(type(%s), %s)" or 'type(%s) ~= "%s"'
398+
local typesCode = multipleTypes and types:gsub("[%w_]+", '"%0"') or types
399+
local typesText = multipleTypes and types:gsub(",", " or ") or types
400+
401+
for argName in argNames:gmatch"[%w_]+" do
402+
__LUA(F(
403+
"if %s then errorf(%d, \"Bad argument #%d '%s'. (Expected %s, got %%s)\", type(%s)) end\n",
404+
ifFormat:format(argName, typesCode),
405+
errLevel + 1,
406+
n,
407+
argName,
408+
typesText,
409+
argName
410+
))
411+
n = n + 1
412+
end
413+
end
414+
end
415+
end
416+
417+
-- ARGS [ (errorLevel=1) ] "arg1:arg1Type1,arg1Type2 arg2,arg3:arg2And3Type ? optionalArg4:optionalArg4Type ..."
418+
function _G.ARGS(errLevelOrArgsStr)
419+
if type(errLevelOrArgsStr) == "string" then
420+
local argsStr = errLevelOrArgsStr
421+
outputArgumentChecks(1, argsStr)
422+
else
423+
local errLevel = errLevelOrArgsStr
424+
return function(argsStr)
425+
outputArgumentChecks(errLevel, argsStr)
426+
end
427+
end
428+
end
429+
end
430+
431+
383432

src/app.lua2p

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ _G.scriptEnvironmentGlobals = {
605605
end,
606606

607607
toDatetime = function(time)
608-
assertArg(1, time, "number")
608+
!ARGS "time:number"
609609
return getDatetime(time)
610610
end,
611611

@@ -688,13 +688,13 @@ _G.scriptEnvironmentGlobals = {
688688

689689
echof = function(s, ...)
690690
assertContext("template", "echof")
691-
assertArg(1, s, "string")
691+
!ARGS "s:string"
692692
scriptEnvironmentGlobals.echo(s:format(...))
693693
end,
694694

695695
echofRaw = function(s, ...)
696696
assertContext("template", "echofRaw")
697-
assertArg(1, s, "string")
697+
!ARGS "s:string"
698698
scriptEnvironmentGlobals.echoRaw(s:format(...))
699699
end,
700700

@@ -716,9 +716,7 @@ _G.scriptEnvironmentGlobals = {
716716

717717
generateFromTemplate = function(sitePathRel, template, paramsOrInit)
718718
assertContext("config", "generateFromTemplate")
719-
assertArg(1, sitePathRel, "string")
720-
assertArg(2, template, "string")
721-
assertArg(3, paramsOrInit, "table","function","nil")
719+
!ARGS "sitePathRel,template:string ? paramsOrInit:table,function"
722720

723721
local pathRel = sitePathToPath(sitePathRel)
724722
local page = newPage(pathRel, false)
@@ -739,7 +737,7 @@ _G.scriptEnvironmentGlobals = {
739737

740738
outputRaw = function(sitePathRel, contents)
741739
assertContext("config", "outputRaw")
742-
assertArg(2, contents, "string")
740+
!ARGS "sitePathRel,contents:string"
743741

744742
local pathRel = sitePathToPath(sitePathRel)
745743
writeOutputFile("raw", pathRel, sitePathRel, contents, nil, "")

src/functions.lua2p

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
--=
1111
--==============================================================
1212

13-
assert, assertArg
13+
assert
1414
attributeWith, attributeWithAny
1515
cleanupPath
1616
countStrings, countSubStrings
@@ -1465,8 +1465,7 @@ end
14651465

14661466

14671467
function _G.getProtectionWrapper(obj, objName)
1468-
assertArg(1, obj, "table")
1469-
assertArg(2, objName, "string")
1468+
!ARGS "obj:table objName:string"
14701469

14711470
local wrapper = protectionWrappers[obj]
14721471
if wrapper then return wrapper end
@@ -1630,8 +1629,7 @@ function _G.generateFromTemplateFile(page)
16301629
end
16311630

16321631
function _G.generateRedirection(url, targetUrl, sourcePath)
1633-
assertArg(1, url, "string")
1634-
assertArg(2, targetUrl, "string")
1632+
!ARGS "url,targetUrl,sourcePath:string"
16351633

16361634
if not url:find"^/" then
16371635
errorLine("Redirection URLs must begin with '/'. (%s)", url)
@@ -1699,8 +1697,8 @@ function _G.generateRedirection(url, targetUrl, sourcePath)
16991697
</body>
17001698
</html>
17011699
]=], {
1702-
url = encodeHtmlEntities( toUrlAbsolute(targetUrl) ),
1703-
urlAbsPercent = encodeHtmlEntities(toUrlAbsolute(toUrlAbsolute(targetUrl))),
1700+
url = encodeHtmlEntities( targetUrl ),
1701+
urlAbsPercent = encodeHtmlEntities(toUrlAbsolute(targetUrl)),
17041702
}
17051703
)
17061704

@@ -1722,39 +1720,6 @@ function _G.assert(v, ...)
17221720
_error(message, 2)
17231721
end
17241722

1725-
-- value = assertArg( [ functionName=auto, ] argumentNumber, value, expectedValueType1, ... [, depth=2 ] )
1726-
do
1727-
local function _assertArg(fName, n, v, ...)
1728-
local vType = type(v)
1729-
local varargCount = select("#", ...)
1730-
local lastArg = select(varargCount, ...)
1731-
local hasDepthArg = (type(lastArg) == "number")
1732-
local typeCount = varargCount+(hasDepthArg and -1 or 0)
1733-
1734-
for i = 1, typeCount do
1735-
if vType == select(i, ...) then return v end
1736-
end
1737-
1738-
local depth = 2 + (hasDepthArg and lastArg or 2)
1739-
1740-
if not fName then
1741-
fName = debug.traceback("", depth-1):match": in function '(.-)'" or "?"
1742-
end
1743-
1744-
local expects = table.concat({...}, " or ", 1, typeCount)
1745-
1746-
errorf(depth, "bad argument #%d to '%s' (%s expected, got %s)", n, fName, expects, vType)
1747-
end
1748-
1749-
function _G.assertArg(fNameOrArgNum, ...)
1750-
if type(fNameOrArgNum) == "string" then
1751-
return (_assertArg(fNameOrArgNum, ...))
1752-
else
1753-
return (_assertArg(nil, fNameOrArgNum, ...))
1754-
end
1755-
end
1756-
end
1757-
17581723

17591724

17601725
function _G.indexOf(t, v)
@@ -1846,7 +1811,7 @@ end
18461811

18471812

18481813
function _G.datetimeToTime(datetime)
1849-
assertArg(1, datetime, "string")
1814+
!ARGS "datetime:string"
18501815

18511816
local date = dateLib(datetime)
18521817
local time = (date-dateLib.epoch()):spanseconds()
@@ -1856,7 +1821,7 @@ end
18561821

18571822
-- datetime = getDatetime( [ time=now ] )
18581823
function _G.getDatetime(time)
1859-
assertArg(1, time, "number","nil")
1824+
!ARGS "? time:number"
18601825

18611826
local date = dateLib(time or os.time()):tolocal()
18621827
local datetime = date:fmt"${iso}%z" :gsub("..$", ":%0") :gsub("%+00:00$", "Z")
@@ -2056,8 +2021,7 @@ do
20562021
end
20572022

20582023
function _G.newPage(pathRel, isRedirection)
2059-
assertArg(1, pathRel, "string")
2060-
assertArg(2, isRedirection, "boolean")
2024+
!ARGS "pathRel:string isRedirection:boolean"
20612025

20622026
local filename = getFilename(pathRel)
20632027
local ext = getExtension(filename)
@@ -2145,7 +2109,7 @@ do
21452109
g = function(field) return field.v ~= "" and field.v or page.date.v end,
21462110
s = function(field, datetime)
21472111
assertContext("template", "publishDate", 3)
2148-
assertArg(1, datetime, "string")
2112+
!ARGS "datetime:string"
21492113
field.v = datetime
21502114
end,
21512115
},
@@ -2443,8 +2407,7 @@ do
24432407
end
24442408

24452409
function _G.makeError(message, level)
2446-
assertArg(1, message, "string")
2447-
assertArg(2, level, "number","nil")
2410+
!ARGS "message:string ? level:number"
24482411

24492412
level = (level == 0) and 0 or (level or 1)
24502413
local stack = {}
@@ -2523,7 +2486,7 @@ end
25232486

25242487

25252488
function _G.ipairsr(t)
2526-
assertArg(1, t, "table")
2489+
!ARGS "t:table"
25272490
return iprev, t, #t+1
25282491
end
25292492

0 commit comments

Comments
 (0)