Browse Source

[YQTemplate] displaying images?

main
Annwan 4 months ago
parent
commit
f830a8c2d3
  1. 17
      utils.lua
  2. 15
      yqform.etlua
  3. 7
      yqtemplate.lua

17
utils.lua

@ -68,16 +68,18 @@ end
--- @return string? content_type the content type of the attached file, or nil if entry is not a file. --- @return string? content_type the content type of the attached file, or nil if entry is not a file.
--- @return string data the value of the entry. --- @return string data the value of the entry.
--- We are doing some severe assumptions here. --- We are doing some severe assumptions here.
--- - Firstly we assume that if the first line of a header doesn't start with
--- `Content-Disposition`, it is invalid and we can ignore it.
--- - Secondly we assume that in the headers, any `CR` is always gonna be
--- - Firstly we assume that in the headers, any `CR` is always gonna be
--- followed by a `LF` thus we only check for CR and advance by 2 when found --- followed by a `LF` thus we only check for CR and advance by 2 when found
--- - Thirdly we assume that the only headers that can matter are
--- - Secondly we assume that the only headers that can matter are
--- `Content-Disposition` (for the field name) and `Content-Type` (if this is a --- `Content-Disposition` (for the field name) and `Content-Type` (if this is a
--- file upload for the type of the uploaded file. --- file upload for the type of the uploaded file.
--- - Fourthly we assume a field name can't contain a double quote
--- - Thirdly we assume a field name can't contain a double quote, even escaped
---
--- Additionaly if the entry is bogus or something goes wrong the function may
--- abort and return `"", nil, ""` instead.
_m.parse_form_entry = function(entry) _m.parse_form_entry = function(entry)
if #entry < 10 then return "", nil, "" end
-- If an entry is less than 32 bytes, it's bogus, skip
if #entry < 32 then return "", nil, "" end
local cursor = 3 local cursor = 3
local name, ctype local name, ctype
while true do while true do
@ -99,7 +101,8 @@ _m.parse_form_entry = function(entry)
while entry:sub(cursor, cursor) ~= "\r" do cursor = cursor + 1 end while entry:sub(cursor, cursor) ~= "\r" do cursor = cursor + 1 end
cursor = cursor + 2 --[[ CRLF ]] cursor = cursor + 2 --[[ CRLF ]]
end end
if cursor == oldcursor then print(entry) os.exit(1); end
-- If we didn't advance the cursor, something went very wrong, skip
if cursor == oldcursor then print(entry) return "", nil, "" end
end end
return name, ctype, entry:sub(cursor, -1) return name, ctype, entry:sub(cursor, -1)
end end

15
yqform.etlua

@ -6,9 +6,18 @@
<body> <body>
<h1>Yokoka’s Quest-style Profile generator</h1> <h1>Yokoka’s Quest-style Profile generator</h1>
<h1>THIS IS WORK IN PROGRESS, THIS IS NON FUNCTIONAL, PLEASE DO NOT USE YET</h1> <h1>THIS IS WORK IN PROGRESS, THIS IS NON FUNCTIONAL, PLEASE DO NOT USE YET</h1>
<pre>
<%=data%>
</pre>
<% if args %>
<h2>Information Gathered</h2>
<h3>General Information</h3>
<dl>
<dt>Name</dt>
<dd><%= args.name.v %></dd>
<dt>Avatar</dt>
<dd><img src="data:<%= args.avatar.t -%>;base64,<%= encoder(args.avatar.v) %>" /></dd>
<dt>Is Leader</dt>
<dd><% if avatar.isLeader and avatar.isLeader.v == 1 then %>YES<% else %>NO<% end %></dd>
</dl>
<% end %>
<form method="POST" action="/cgi/yqtemplate" enctype="multipart/form-data"> <form method="POST" action="/cgi/yqtemplate" enctype="multipart/form-data">
<h2>General Information</h2> <h2>General Information</h2>
<label for="name">Name:</label> <label for="name">Name:</label>

7
yqtemplate.lua

@ -10,12 +10,14 @@ return {
run = function(fcgi) run = function(fcgi)
fcgi.print("Content-Type: text/html; charset=utf-8\r\n\r\n") fcgi.print("Content-Type: text/html; charset=utf-8\r\n\r\n")
local data = "" local data = ""
local args = nil
if fcgi.getenv("REQUEST_METHOD") == "POST" then if fcgi.getenv("REQUEST_METHOD") == "POST" then
args = {}
local content_type = fcgi.getenv("CONTENT_TYPE") local content_type = fcgi.getenv("CONTENT_TYPE")
local boundary = content_type:match("; boundary=(.*)$") local boundary = content_type:match("; boundary=(.*)$")
local post_data = fcgi.post() local post_data = fcgi.post()
data = "Content type:" .. content_type .. "\n Boundary: " .. boundary .. "\n" data = "Content type:" .. content_type .. "\n Boundary: " .. boundary .. "\n"
local args = {}
for w in string.gmatch(post_data, "(.-)" .. boundary) do for w in string.gmatch(post_data, "(.-)" .. boundary) do
local name, dtype, value = utils.parse_form_entry(w) local name, dtype, value = utils.parse_form_entry(w)
args[name] = { t = dtype, v = value } args[name] = { t = dtype, v = value }
@ -31,7 +33,8 @@ return {
end end
fcgi.print( fcgi.print(
form { form {
data = data,
encoder = utils.tobase64,
args = args,
classes = { classes = {
"Beast", "Beast",
"Swordsman", "Swordsman",

Loading…
Cancel
Save