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 data the value of the entry.
--- 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
--- - 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
--- 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)
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 name, ctype
while true do
@ -99,7 +101,8 @@ _m.parse_form_entry = function(entry)
while entry:sub(cursor, cursor) ~= "\r" do cursor = cursor + 1 end
cursor = cursor + 2 --[[ CRLF ]]
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
return name, ctype, entry:sub(cursor, -1)
end

15
yqform.etlua

@ -6,9 +6,18 @@
<body>
<h1>Yokoka’s Quest-style Profile generator</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">
<h2>General Information</h2>
<label for="name">Name:</label>

7
yqtemplate.lua

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

Loading…
Cancel
Save