2021-03-25 09:21:13 +00:00
|
|
|
-- redbean lua server page demo
|
|
|
|
|
|
|
|
local function main()
|
2021-03-29 08:22:49 +00:00
|
|
|
-- This is the best way to print data to the console or log file.
|
|
|
|
Log(kLogWarn, "hello from \e[1mlua\e[0m!")
|
|
|
|
|
2021-03-27 14:29:55 +00:00
|
|
|
-- This check is pedantic but might be good to have.
|
2021-03-25 09:21:13 +00:00
|
|
|
if GetMethod() ~= 'GET' and GetMethod() ~= 'HEAD' then
|
|
|
|
ServeError(405)
|
|
|
|
SetHeader('Allow', 'GET, HEAD')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- These two lines are optional.
|
|
|
|
-- The default behavior is to do this if you don't.
|
|
|
|
SetStatus(200) -- Shorthand for SetStatus(200, "OK")
|
|
|
|
SetHeader('Content-Type', 'text/html; charset=utf-8')
|
|
|
|
|
|
|
|
-- Response data is buffered until the script finishes running.
|
|
|
|
-- Compression is applied automatically, based on your headers.
|
|
|
|
Write('<!doctype html>\n')
|
|
|
|
Write('<title>redbean</title>\n')
|
|
|
|
Write('<h1>redbean lua server page demo</h1>\n')
|
|
|
|
|
2021-03-29 08:22:49 +00:00
|
|
|
-- Prevent caching.
|
|
|
|
-- We need this because we're doing things like putting the client's
|
|
|
|
-- IP address in the response so we naturally don't want that cached
|
|
|
|
SetHeader('Expires', FormatHttpDateTime(GetDate()))
|
|
|
|
SetHeader('Cache-Control', 'no-cache, must-revalidate, max-age=0')
|
|
|
|
|
2021-03-25 09:21:13 +00:00
|
|
|
-- GetParams() returns an ordered list of Request-URI query params.
|
|
|
|
Write('<h3>request uri parameters</h3>\n')
|
|
|
|
params = GetParams()
|
|
|
|
if #params > 0 then
|
|
|
|
Write('<dl>\n')
|
|
|
|
for i = 1,#params do
|
|
|
|
Write('<dt>')
|
|
|
|
Write(EscapeHtml(params[i][1]))
|
|
|
|
Write('\n')
|
|
|
|
if params[i][2] then
|
|
|
|
Write('<dd>')
|
|
|
|
Write(EscapeHtml(params[i][2]))
|
|
|
|
Write('\n')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Write('</dl>\n')
|
|
|
|
else
|
|
|
|
Write('<p>\n')
|
|
|
|
Write('<em>none</em><br>\n')
|
|
|
|
Write('ProTip: Try <a href="')
|
2021-03-27 14:29:55 +00:00
|
|
|
Write(EscapeHtml(EscapePath(GetPath()) .. '?x=hi%20there&y&z&z=' .. EscapeParam('&')))
|
2021-03-25 09:21:13 +00:00
|
|
|
Write('">clicking here</a>!\n')
|
|
|
|
end
|
2021-03-27 14:29:55 +00:00
|
|
|
|
|
|
|
-- Access redbean command line arguments.
|
|
|
|
-- These are the ones that come *after* the redbean server arguments.
|
|
|
|
Write('<h3>command line arguments</h3>\n')
|
|
|
|
if #argv > 0 then
|
|
|
|
Write('<ul>\n')
|
|
|
|
for i = 1,#argv do
|
|
|
|
Write('<li>')
|
|
|
|
Write(EscapeHtml(argv[i]))
|
|
|
|
Write('\n')
|
|
|
|
end
|
|
|
|
Write('</ul>\n')
|
|
|
|
else
|
|
|
|
Write('<p><em>none</em>\n')
|
|
|
|
end
|
|
|
|
|
2021-03-28 07:10:17 +00:00
|
|
|
Write([[
|
|
|
|
<h3>post request html form demo</h3>
|
2021-04-18 18:34:59 +00:00
|
|
|
<form action="redbean-form.lua" method="post">
|
2021-03-28 07:10:17 +00:00
|
|
|
<input type="text" id="firstname" name="firstname">
|
|
|
|
<label for="firstname">first name</label>
|
|
|
|
<br>
|
|
|
|
<input type="text" id="lastname" name="lastname">
|
|
|
|
<label for="lastname">last name</label>
|
|
|
|
<br>
|
|
|
|
<input type="submit" value="Submit">
|
|
|
|
</form>
|
|
|
|
]])
|
|
|
|
|
|
|
|
Write([[
|
|
|
|
<h3>xmlhttprequest request demo</h3>
|
|
|
|
<input id="x" value="lâtìn1">
|
2021-03-28 14:54:21 +00:00
|
|
|
<label for="x">X-Custom-Header</label><br>
|
|
|
|
<button id="send">send</button><br>
|
2021-03-28 07:10:17 +00:00
|
|
|
<div id="result"></div>
|
|
|
|
<script>
|
|
|
|
function OnSend() {
|
|
|
|
var r = new XMLHttpRequest();
|
|
|
|
r.onload = function() {
|
|
|
|
document.getElementById("result").innerText = this.getResponseHeader('X-Custom-Header');
|
|
|
|
};
|
2021-04-18 18:34:59 +00:00
|
|
|
r.open('POST', 'redbean-xhr.lua');
|
2021-03-28 07:10:17 +00:00
|
|
|
r.setRequestHeader('X-Custom-Header', document.getElementById('x').value);
|
|
|
|
r.send();
|
|
|
|
}
|
|
|
|
document.getElementById('send').onclick = OnSend;
|
|
|
|
</script>
|
|
|
|
]])
|
2021-03-28 14:54:21 +00:00
|
|
|
|
|
|
|
Write('<h3>extra information</h3>\n')
|
|
|
|
Write('<dt>GetClientAddr()\n')
|
|
|
|
Write('<dd>')
|
|
|
|
Write(GetClientAddr())
|
|
|
|
Write('\n')
|
|
|
|
Write('<dt>GetServerAddr()\n')
|
|
|
|
Write('<dd>')
|
|
|
|
Write(GetServerAddr())
|
|
|
|
Write('\n')
|
|
|
|
Write('</dl>\n')
|
2021-03-25 09:21:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
main()
|