HTTP parser for Lua -
i'm trying parse http post requests in lua. implementation works, eats hell lot of cpu load. critical, hence on embedded platform.
i've looked on other implementations, can't fit, because image hardly fit in memory, wouldn't use library. rolled own parser, uses of system resource. question how optimize have lower cpu load.
this openwrt based system, have lua 5.1. core function looks boundary (in str variable). reads input block block, , seeks it.
the other solution use luci libraries heavy lifting, don't want code integrated in luci.
--look pattern (str) , copy input until found output. local function writeuntil(in_fp, str, out_fp) local buff = "" local ret = false local bs = 4096 --block size. amount of data read @ once local c = in_fp:read(bs) local strstartpos = 1 while c local blocklen = string.len(c) --not sure whole block read, size of actual block. local found = string.find(c, str, 1, true) --try locate str, don't have work. if (found ~= nil) if found > 2 out_fp:write(string.sub(c, 1, found - 1)) end ret = true break --we done else --try mach str till end of block local strpos = string.find(c, string.sub(str, strstartpos, strstartpos), 1, true) --try locate first character if strpos --there starting character in block if (strpos > 1) out_fp:write(string.sub(c, 1, strpos - 1)) end = strpos, blocklen --iterate through block local ch = string.sub(c, i, i) if ch == string.sub(str, strstartpos, strstartpos) buff = buff .. ch if string.len(buff) == string.len(str) ret = true break --we're done end strstartpos = strstartpos + 1 else --lost track. output. if string.len(buff) > 0 out_fp:write(buff) buff = "" end out_fp:write(ch) strstartpos = 1 end end else out_fp:write(c) end end if ret break end c = in_fp:read(bs) --read next block end return ret end
egor, right, ended solution. uses less cpu. not perfect hence scp faster (although implemented in c).
--look pattern (str) , copy input until found output. local function writeuntil(in_fp, str, out_fp) local buff = "" local ret = false local bs = 4096 --block size. amount of data read @ once local c = in_fp:read(bs) local strstartpos = 1 local laststrpos = 1 local needdata = true while c local blocklen = string.len(c) --not sure whole block read, size of actual block. local found = string.find(c, str, 1, true) --try locate str, don't have work. if (found ~= nil) if found > 1 if #buff > 0 out_fp:write(buff) end out_fp:write(string.sub(c, 1, found - 1)) end ret = true break --we done else --try mach str till end of block local strpos = string.find(c, string.sub(str, strstartpos, strstartpos), laststrpos, true) --try locate first character if strpos --there starting character in block out_fp:write(string.sub(c, laststrpos, strpos - 1)) = strpos, blocklen --iterate through block local ch = string.sub(c, i, i) if ch == string.sub(str, strstartpos, strstartpos) buff = buff .. ch if string.len(buff) == string.len(str) ret = true break --we're done end strstartpos = strstartpos + 1 laststrpos = + 1 else --lost track. output. if string.len(buff) > 0 out_fp:write(buff) buff = "" end out_fp:write(ch) strstartpos = 1 if == blocklen needdata = true else laststrpos = + 1 needdata = false end break end end else if ret == false if string.len(buff) > 0 out_fp:write(buff) buff = "" end out_fp:write(string.sub(c, laststrpos)) laststrpos = 1 needdata = true else break end end end if ret break end if needdata c = in_fp:read(bs) --read next block laststrpos = 1 end end return ret end
Comments
Post a Comment