From 870ccb5124109afadb42691efea8cb727cbde326 Mon Sep 17 00:00:00 2001 From: the lemons Date: Mon, 7 Feb 2022 14:43:23 -0600 Subject: bugfixes and miscellaneous improvements --- zzcxz.cgi | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/zzcxz.cgi b/zzcxz.cgi index 74dbfc5..7fa53ee 100755 --- a/zzcxz.cgi +++ b/zzcxz.cgi @@ -92,11 +92,12 @@ local function parse_qs(str,sep) return values end -local cookies = parse_qs(env "HTTP_COOKIE","; ") +local cookies = env "HTTP_COOKIE" and parse_qs(env "HTTP_COOKIE","; ") or {} local flags = {} for k,v in pairs(cookies) do - if k:sub(1,5) == "flag_" then - flags[k:sub(6)] = v + local flag = k:match "flag_(.+)" + if flag then + flags[flag] = v end end @@ -168,10 +169,13 @@ local function parse_directive(line, directives) local text = "" if not flagname then flagname, text = args:match "^%s*(%w+)%s*(%w+)%s*$" - if not flagname or not text or utf8.len(text) > 1000 then return end + if not flagname or not text or utf8.len(text) > 1000 then + return + end end if utf8.len(flagname) > 150 then return end - directives.flags["flag_"..flagname] = url_encode(text) + directives.flags_updated[flagname] = text + flags[flagname] = text else return end @@ -181,7 +185,7 @@ end local load_page local function convert_markup(m) local result = {} - local directives = {flags={}} + local directives = {flags_updated = {}} local code_block = false for line in (m..'\n'):gmatch "(.-)\n" do if not code_block then @@ -276,7 +280,8 @@ local function new_action(page, action, result) ::generate_name:: local new_name = {} for i=1,5 do - table.insert(new_name, string.char(string.byte 'a' + math.random(0,25))) + table.insert(new_name, string.char( + string.byte 'a' + math.random(0,25))) end new_name = table.concat(new_name) @@ -320,10 +325,6 @@ map["^/g/(%w%w%w%w%w)$"] = function(p) local page = load_page(p) if not page then return not_found() end local _, directives = convert_markup(page.content) - local cookies = {} - for k,v in pairs(directives.flags) do - table.insert(cookies,k.."="..v.."; Secure; Max-Age=1000000000") - end if env "REQUEST_METHOD" ~= "POST" then local actions = {} for _,a in ipairs(page.actions) do @@ -340,6 +341,13 @@ map["^/g/(%w%w%w%w%w)$"] = function(p) ) end + local cookies = {} + for flagname, text in pairs(directives.flags_updated) do + table.insert(cookies, + ('flag_%s=%s; Secure; Max-Age=999999999') + :format(flagname, url_encode(text))) + end + return base { title = html_encode(page.title), content = page_template { -- cgit v1.2.3