From c705afac8659e74fffd263a5468336d4fab87961 Mon Sep 17 00:00:00 2001 From: heav-4 Date: Mon, 7 Feb 2022 03:15:20 +0000 Subject: Add #set directive and flags table. Hi citrons --- zzcxz.cgi | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/zzcxz.cgi b/zzcxz.cgi index d5a62b6..52314ba 100755 --- a/zzcxz.cgi +++ b/zzcxz.cgi @@ -33,7 +33,8 @@ local function html_encode(x) return escaped end -local function parse_qs(str) +local function parse_qs(str,sep) + sep = sep or "&" local function decode(str, path) local str = str if not path then @@ -45,7 +46,7 @@ local function parse_qs(str) end local values = {} - for key,val in str:gmatch(string.format('([^%q=]+)(=*[^%q=]*)', '&', '&')) do + for key,val in str:gmatch(string.format('([^%q=]+)(=*[^%q=]*)', sep, sep)) do local key = decode(key) local keys = {} key = key:gsub('%[([^%]]*)%]', function(v) @@ -91,6 +92,14 @@ local function parse_qs(str) return values end +local cookies = parse_qs(env "HTTP_COOKIE", "; ") +local flags = {} +for k, v in pairs(cookies) do + if k:sub(1,5) == "flag_" then + flags[k:sub(6)] = v + end +end + local function redirect(to) return "", { status = '303 see other', @@ -141,6 +150,7 @@ local not_found = function() end local function parse_directive(line, directives) + flags = flags or {} local directive, args = line:match "^#([A-Za-z]+)%s*(.-)\n?$" directive = directive and directive:lower() if not directive then @@ -153,6 +163,16 @@ local function parse_directive(line, directives) local redirect = args:match "^%s*(%w%w%w%w%w)%s*$" if not redirect then return end directives.redirect = redirect + elseif directive == "set" then + if args:match "^%s*$" then return end + local flagname = args:match "^%s*(%w+)%s*$" + 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 + end + if utf8.len(flagname) > 150 then return end + directives.flags[flagname] = text else return end @@ -163,6 +183,7 @@ local load_page local function convert_markup(m) local result = {} local directives = {} + directives.flags = {} local code_block = false for line in (m..'\n'):gmatch "(.-)\n" do if not code_block then @@ -170,7 +191,7 @@ local function convert_markup(m) goto continue end if line:sub(1,1) == '#' and - parse_directive(line, directives) then + parse_directive(line, directives, flags) then goto continue end @@ -301,7 +322,10 @@ 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,"flag_"..k.."="..url_encode(v).."; Secure") + end if env "REQUEST_METHOD" ~= "POST" then local actions = {} for _,a in ipairs(page.actions) do @@ -325,7 +349,7 @@ map["^/g/(%w%w%w%w%w)$"] = function(p) content = convert_markup(page.content), actions = table.concat(actions), }, - } + }, {headers = {["set-cookie"] = cookies, bees = "3.14"}} else if directives.deadend then return base { @@ -471,10 +495,17 @@ resp.content_type = resp.content_type or 'text/html' resp.status = resp.status or '200 OK' resp.headers = resp.headers or {} resp.headers['content-type'] = resp.content_type +resp.headers['apioforms'] = math.random(0,104942)/2 print("status: "..resp.status) for k,v in pairs(resp.headers) do - print(("%s: %s"):format(k, v)) + if type(v) == "table" then + for _, v2 in ipairs(v) do + print(("%s: %s"):format(k, v2)) + end + else + print(("%s: %s"):format(k, v)) + end end print "" -- cgit v1.2.3 From 08fdf170d51768125fc104ff964bb3bad29f3891 Mon Sep 17 00:00:00 2001 From: heav-4 Date: Mon, 7 Feb 2022 03:48:44 +0000 Subject: Added #add directive and flags table. Hi citrons --- zzcxz.cgi | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/zzcxz.cgi b/zzcxz.cgi index d5a62b6..74dbfc5 100755 --- a/zzcxz.cgi +++ b/zzcxz.cgi @@ -33,7 +33,8 @@ local function html_encode(x) return escaped end -local function parse_qs(str) +local function parse_qs(str,sep) + sep = sep or "&" local function decode(str, path) local str = str if not path then @@ -45,7 +46,7 @@ local function parse_qs(str) end local values = {} - for key,val in str:gmatch(string.format('([^%q=]+)(=*[^%q=]*)', '&', '&')) do + for key,val in str:gmatch(string.format('([^%q=]+)(=*[^%q=]*)', sep, sep)) do local key = decode(key) local keys = {} key = key:gsub('%[([^%]]*)%]', function(v) @@ -91,6 +92,14 @@ local function parse_qs(str) return values end +local cookies = parse_qs(env "HTTP_COOKIE","; ") +local flags = {} +for k,v in pairs(cookies) do + if k:sub(1,5) == "flag_" then + flags[k:sub(6)] = v + end +end + local function redirect(to) return "", { status = '303 see other', @@ -153,6 +162,16 @@ local function parse_directive(line, directives) local redirect = args:match "^%s*(%w%w%w%w%w)%s*$" if not redirect then return end directives.redirect = redirect + elseif directive == "set" then + if args:match "^%s*$" then return end + local flagname = args:match "^%s*(%w+)%s*$" + 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 + end + if utf8.len(flagname) > 150 then return end + directives.flags["flag_"..flagname] = url_encode(text) else return end @@ -162,7 +181,7 @@ end local load_page local function convert_markup(m) local result = {} - local directives = {} + local directives = {flags={}} local code_block = false for line in (m..'\n'):gmatch "(.-)\n" do if not code_block then @@ -301,7 +320,10 @@ 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 @@ -324,8 +346,8 @@ map["^/g/(%w%w%w%w%w)$"] = function(p) title = html_encode(page.title), content = convert_markup(page.content), actions = table.concat(actions), - }, - } + } + }, {headers = {["set-cookie"] = cookies, bee = "apio form"}} else if directives.deadend then return base { @@ -471,10 +493,16 @@ resp.content_type = resp.content_type or 'text/html' resp.status = resp.status or '200 OK' resp.headers = resp.headers or {} resp.headers['content-type'] = resp.content_type - +resp.headers.apioform = 3.5 print("status: "..resp.status) for k,v in pairs(resp.headers) do - print(("%s: %s"):format(k, v)) + if type(v) == "table" then + for _,v2 in ipairs(v) do + print(("%s: %s"):format(k, v2)) + end + else + print(("%s: %s"):format(k, v)) + end end print "" -- cgit v1.2.3 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 From 1dd9fb662e14723fd171806915abf5198fc3983f Mon Sep 17 00:00:00 2001 From: the lemons Date: Mon, 7 Feb 2022 14:53:56 -0600 Subject: add #unset --- zzcxz.cgi | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/zzcxz.cgi b/zzcxz.cgi index 7fa53ee..c6c8de4 100755 --- a/zzcxz.cgi +++ b/zzcxz.cgi @@ -176,6 +176,11 @@ local function parse_directive(line, directives) if utf8.len(flagname) > 150 then return end directives.flags_updated[flagname] = text flags[flagname] = text + elseif directive == "unset" then + local flagname = args:match "^%s*(%w+)%s*$" + if not flagname then return end + directives.flags_updated[flagname] = false + flags[flagname] = nil else return end @@ -343,9 +348,16 @@ map["^/g/(%w%w%w%w%w)$"] = function(p) 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))) + if text then + table.insert(cookies, + ('flag_%s=%s; secure; max-age=999999999999') + :format(flagname, url_encode(text))) + else + -- clear the cookie by expiring it in the past + table.insert(cookies, + ('flag_%s=; expires=Thu, 01 Jan 1970 00:00:00 GMT;') + :format(flagname)) + end end return base { -- cgit v1.2.3 From 658139771dffb486ae4887038a76ad691ca1b153 Mon Sep 17 00:00:00 2001 From: the lemons Date: Mon, 7 Feb 2022 15:02:40 -0600 Subject: permit a wider range of values with #set --- zzcxz.cgi | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/zzcxz.cgi b/zzcxz.cgi index c6c8de4..c7ec545 100755 --- a/zzcxz.cgi +++ b/zzcxz.cgi @@ -165,15 +165,12 @@ local function parse_directive(line, directives) directives.redirect = redirect elseif directive == "set" then if args:match "^%s*$" then return end - local flagname = args:match "^%s*(%w+)%s*$" - 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 + local flagname, text = args:match "^%s*(%w+)%s*(.-)%s*$" + if not flagname then return end + text = text or "" + if utf8.len(flagname) > 150 or utf8.len(text) > 1000 then + return end - if utf8.len(flagname) > 150 then return end directives.flags_updated[flagname] = text flags[flagname] = text elseif directive == "unset" then @@ -350,7 +347,7 @@ map["^/g/(%w%w%w%w%w)$"] = function(p) for flagname, text in pairs(directives.flags_updated) do if text then table.insert(cookies, - ('flag_%s=%s; secure; max-age=999999999999') + ('flag_%s=%s; max-age=999999999999') :format(flagname, url_encode(text))) else -- clear the cookie by expiring it in the past -- cgit v1.2.3 From ee8b4e7d4bb2140d5a19bc1dc95a12fa642a786c Mon Sep 17 00:00:00 2001 From: the lemons Date: Mon, 7 Feb 2022 16:08:37 -0600 Subject: add "secure" thing to cookies --- zzcxz.cgi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zzcxz.cgi b/zzcxz.cgi index c7ec545..f722b1b 100755 --- a/zzcxz.cgi +++ b/zzcxz.cgi @@ -347,7 +347,7 @@ map["^/g/(%w%w%w%w%w)$"] = function(p) for flagname, text in pairs(directives.flags_updated) do if text then table.insert(cookies, - ('flag_%s=%s; max-age=999999999999') + ('flag_%s=%s; secure; max-age=999999999999') :format(flagname, url_encode(text))) else -- clear the cookie by expiring it in the past -- cgit v1.2.3 From c55e55971372a4af82f1e3c44d376e157a8861fe Mon Sep 17 00:00:00 2001 From: heav-4 Date: Fri, 25 Feb 2022 23:58:21 +0000 Subject: fix merge conflict (real) --- zzcxz.cgi | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/zzcxz.cgi b/zzcxz.cgi index a650e98..48ecba8 100755 --- a/zzcxz.cgi +++ b/zzcxz.cgi @@ -196,12 +196,7 @@ end local load_page local function convert_markup(m) local result = {} -<<<<<<< HEAD local directives = {flags_updated = {}} -======= - local directives = {} - directives.flags = {} ->>>>>>> master local code_block = false for line in (m..'\n'):gmatch "(.-)\n" do if not code_block then @@ -341,13 +336,10 @@ 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) -<<<<<<< HEAD -======= local cookies = {} for k,v in pairs(directives.flags) do table.insert(cookies,"flag_"..k.."="..url_encode(v).."; Secure") end ->>>>>>> master if env "REQUEST_METHOD" ~= "POST" then local actions = {} for _,a in ipairs(page.actions) do @@ -384,13 +376,8 @@ map["^/g/(%w%w%w%w%w)$"] = function(p) title = html_encode(page.title), content = convert_markup(page.content), actions = table.concat(actions), -<<<<<<< HEAD - } - }, {headers = {["set-cookie"] = cookies, bee = "apio form"}} -======= }, }, {headers = {["set-cookie"] = cookies, bees = "3.14"}} ->>>>>>> master else if directives.deadend then return base { @@ -536,20 +523,12 @@ resp.content_type = resp.content_type or 'text/html' resp.status = resp.status or '200 OK' resp.headers = resp.headers or {} resp.headers['content-type'] = resp.content_type -<<<<<<< HEAD -resp.headers.apioform = 3.5 -print("status: "..resp.status) -for k,v in pairs(resp.headers) do - if type(v) == "table" then - for _,v2 in ipairs(v) do -======= resp.headers['apioforms'] = math.random(0,104942)/2 print("status: "..resp.status) for k,v in pairs(resp.headers) do if type(v) == "table" then for _, v2 in ipairs(v) do ->>>>>>> master print(("%s: %s"):format(k, v2)) end else -- cgit v1.2.3 From 48a7e49e29c2abb2a86b7f684c74776710fd61f4 Mon Sep 17 00:00:00 2001 From: heav-4 Date: Sat, 26 Feb 2022 00:02:48 +0000 Subject: fix merge conflict (realer) --- zzcxz.cgi | 8 -------- 1 file changed, 8 deletions(-) diff --git a/zzcxz.cgi b/zzcxz.cgi index 48ecba8..00f9ab4 100755 --- a/zzcxz.cgi +++ b/zzcxz.cgi @@ -92,20 +92,12 @@ local function parse_qs(str,sep) return values end -<<<<<<< HEAD local cookies = env "HTTP_COOKIE" and parse_qs(env "HTTP_COOKIE","; ") or {} local flags = {} for k,v in pairs(cookies) do local flag = k:match "flag_(.+)" if flag then flags[flag] = v -======= -local cookies = parse_qs(env "HTTP_COOKIE", "; ") -local flags = {} -for k, v in pairs(cookies) do - if k:sub(1,5) == "flag_" then - flags[k:sub(6)] = v ->>>>>>> master end end -- cgit v1.2.3 From e2fb3fe657b594007be1e53037688adb8baa20eb Mon Sep 17 00:00:00 2001 From: heav-4 Date: Sat, 26 Feb 2022 01:14:13 +0000 Subject: add #REQUIRE --- zzcxz.cgi | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/zzcxz.cgi b/zzcxz.cgi index 00f9ab4..ce4811d 100755 --- a/zzcxz.cgi +++ b/zzcxz.cgi @@ -48,6 +48,7 @@ local function parse_qs(str,sep) local values = {} for key,val in str:gmatch(string.format('([^%q=]+)(=*[^%q=]*)', sep, sep)) do local key = decode(key) + local keys = {} key = key:gsub('%[([^%]]*)%]', function(v) -- extract keys between balanced brackets @@ -92,7 +93,7 @@ local function parse_qs(str,sep) return values end -local cookies = env "HTTP_COOKIE" and parse_qs(env "HTTP_COOKIE","; ") or {} +local cookies = env "HTTP_COOKIE" and parse_qs(env "HTTP_COOKIE",";%s") or {} local flags = {} for k,v in pairs(cookies) do local flag = k:match "flag_(.+)" @@ -150,7 +151,7 @@ local not_found = function() }, { status = '404 not found' } end -local function parse_directive(line, directives) +local function parse_directive(line, directives, tmp_flags) flags = flags or {} local directive, args = line:match "^#([A-Za-z]+)%s*(.-)\n?$" directive = directive and directive:lower() @@ -173,12 +174,16 @@ local function parse_directive(line, directives) return end directives.flags_updated[flagname] = text - flags[flagname] = text + tmp_flags[flagname] = text elseif directive == "unset" then local flagname = args:match "^%s*(%w+)%s*$" if not flagname then return end directives.flags_updated[flagname] = false - flags[flagname] = nil + tmp_flags[flagname] = nil + elseif directive == "require" then + local flagname = args:match "^%s*(%w+)%s*$" + if not flagname then return end + directives.require[flagname] = true else return end @@ -188,7 +193,11 @@ end local load_page local function convert_markup(m) local result = {} - local directives = {flags_updated = {}} + local tmp_flags = {} + for name, value in pairs(flags) do + tmp_flags[name] = value + end + local directives = {flags_updated = {}, require = {}} local code_block = false for line in (m..'\n'):gmatch "(.-)\n" do if not code_block then @@ -196,7 +205,7 @@ local function convert_markup(m) goto continue end if line:sub(1,1) == '#' and - parse_directive(line, directives, flags) then + parse_directive(line, directives, tmp_flags) then goto continue end @@ -238,7 +247,15 @@ local function convert_markup(m) return table.concat(result), directives end - +local function target_requirements(p) + local page = load_page(p, false, true) + if not page then return true end + local _,directives = convert_markup(page.content) + for k,v in pairs(directives.require) do + if not flags[k] then return false end + end + return true +end local function parse_page(s) local page = {} page.title = s:match "^(.-)\n" @@ -328,16 +345,14 @@ 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,"flag_"..k.."="..url_encode(v).."; Secure") - end if env "REQUEST_METHOD" ~= "POST" then local actions = {} for _,a in ipairs(page.actions) do - table.insert(actions, - ('
  • %s
  • '):format( - html_encode(a.target), html_encode(a.action))) + if target_requirements(a.target) then + table.insert(actions, + ('
  • %s
  • '):format( + html_encode(a.target), html_encode(a.action))) + end end if not directives.deadend then table.insert(actions, -- cgit v1.2.3