From 69c416c85bf92fca4b59aa78888a8aefbd0e0640 Mon Sep 17 00:00:00 2001 From: iceyrazor Date: Sat, 1 Aug 2026 01:25:42 -0500 Subject: init --- LICENSE | 21 +++++++++++++++++++ README.md | 3 +++ test.lua | 17 +++++++++++++++ www/cgi-bin/cgi.lua | 52 ++++++++++++++++++++++++++++++++++++++++++++++ www/cgi-bin/req-params.lua | 38 +++++++++++++++++++++++++++++++++ www/cgi-bin/test.lua | 43 ++++++++++++++++++++++++++++++++++++++ www/html/index.html | 5 +++++ 7 files changed, 179 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100755 test.lua create mode 100755 www/cgi-bin/cgi.lua create mode 100755 www/cgi-bin/req-params.lua create mode 100755 www/cgi-bin/test.lua create mode 100644 www/html/index.html diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e4e5954 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 iceyrazor + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9107eb5 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# lua static site generator + +generates html through simple function syntax. see www/cgi-bin/test.lua. ment to be use with cgi but not required diff --git a/test.lua b/test.lua new file mode 100755 index 0000000..7fdbaf9 --- /dev/null +++ b/test.lua @@ -0,0 +1,17 @@ +#!/usr/bin/env luajit + +local ssg = require("www/cgi-bin/cgi") +local str="" +ssg.newline=true + +ssg.render(m("html", + m("head", + m("title","ssg test") + ).. + m("body", + m("h1","Hello from Lua CGI").. + m("p", "ssg test").. + m("p",_VERSION) + ) +), +{["Content-Type"]="text/html"},200) diff --git a/www/cgi-bin/cgi.lua b/www/cgi-bin/cgi.lua new file mode 100755 index 0000000..5033984 --- /dev/null +++ b/www/cgi-bin/cgi.lua @@ -0,0 +1,52 @@ +local cgi={} + +cgi.newline=false + +function cgi.tag(key,value,attrib) + local ret_str="<"..key + + if (attrib) then + for a_key,a_val in pairs(attrib) do + ret_str=ret_str.." "..a_key..'="'..a_val..'"' + end + end + + ret_str=ret_str..">" + if cgi.newline==true then ret_str=ret_str.."\n" end + + ret_str=ret_str..value + + if cgi.newline==true then ret_str=ret_str.."\n" end + + ret_str=ret_str.."" + + if cgi.newline==true then ret_str=ret_str.."\n" end + + return ret_str +end + +local function get_phrase(code) + local cases={ + [200]="OK", + [401]="Unauthorized", + } + if(cases[code]) then return cases[code] else return "" end +end + +function cgi.header(code,headers) + print("Status: "..code.." "..get_phrase(code)) + for h_name,header in pairs(headers) do + print(h_name..": "..header) + end + print() +end + +function cgi.render(content,headers,code) + headers["Content-Length"] = tostring(content:len()+1) + cgi.header(code,headers) + print(content); +end + +_G.m = cgi.tag + +return cgi diff --git a/www/cgi-bin/req-params.lua b/www/cgi-bin/req-params.lua new file mode 100755 index 0000000..077a7e4 --- /dev/null +++ b/www/cgi-bin/req-params.lua @@ -0,0 +1,38 @@ +-- /var/www/cgi-bin/cgi.lua +-- Simple Lua CGI helper for GET and POST + +local params = {} + +-- Parse URL-encoded query string into a table +local function parse_query(qs) + local t = {} + for k, v in string.gmatch(qs or "", "([^&=]+)=([^&]*)") do + -- URL-decode + k = k:gsub("+", " "):gsub("%%(%x%x)", function(h) return string.char(tonumber(h,16)) end) + v = v:gsub("+", " "):gsub("%%(%x%x)", function(h) return string.char(tonumber(h,16)) end) + t[k] = v + end + return t +end + +-- GET parameters +params.get_params = parse_query(os.getenv("QUERY_STRING")) + +-- POST parameters +params.post_params = {} +local content_length = tonumber(os.getenv("CONTENT_LENGTH") or 0) +if content_length > 0 then + local body = io.read(content_length) + params.post_params = parse_query(body) +end + +-- Convenience functions +function params.get(key) return params.get_params[key] end +function params.post(key) return params.post_params[key] end + +-- Combine GET+POST if you want +function params.param(key) + return params.post(key) or params.get(key) +end + +return params diff --git a/www/cgi-bin/test.lua b/www/cgi-bin/test.lua new file mode 100755 index 0000000..c86b3f5 --- /dev/null +++ b/www/cgi-bin/test.lua @@ -0,0 +1,43 @@ +#!/usr/bin/env luajit + +local cgi = require("cgi") +local req_params = require("req-params") +local str="" + +if req_params.param("password") and req_params.param("password") == "401" then + cgi.render("401 Unauthorized",{["Content-Type"]="text/html"},401) + return +end + +-- Access GET parameters +if next(req_params.get_params) then + str=str..m("h2","GET parameters:") + for k,v in pairs(req_params.get_params) do + str=str..k.." = "..v.."
" + end +end + +-- Access POST parameters +if next(req_params.post_params) then + str=str..m("h2","POST parameters:") + for k,v in pairs(req_params.post_params) do + str=str..k.." = "..v.."
" + end +end + +-- Combined helper +local name = req_params.param("name") or "anonymous" +str=str..m("p","Hello, "..name.."!") + +cgi.render(m("html", + m("head", + m("title","ssg test") + ).. + m("body", + m("h1","Hello from Lua CGI").. + m("p", "ssg test").. + m("p",_VERSION).. + str + ) +), +{["Content-Type"]="text/html"},200) \ No newline at end of file diff --git a/www/html/index.html b/www/html/index.html new file mode 100644 index 0000000..34459dd --- /dev/null +++ b/www/html/index.html @@ -0,0 +1,5 @@ + + + a page + + -- cgit v1.3