aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorheav-4 <heavpoot@gmail.com>2022-02-07 03:15:20 +0000
committerheav-4 <heavpoot@gmail.com>2022-02-07 03:15:20 +0000
commitc705afac8659e74fffd263a5468336d4fab87961 (patch)
tree4087bf747af8f6e01c962e7e0f36be6d5b29d97f
parent4b5f42535bb5fde5b346497e70960a2769839502 (diff)
Add #set directive and flags table. Hi citrons
-rwxr-xr-xzzcxz.cgi43
1 files 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 ""