-- redbean lua server page demo
local function DescribeIp(ip)
Write(' [')
if IsPrivateIp(ip) then
Write('PRIVATE')
elseif IsTestIp(ip) then
Write('TESTNET')
elseif IsLocalIp(ip) then
Write('LOCALNET')
else
Write('PUBLIC')
end
Write(']')
end
local function main()
-- This is the best way to print data to the console or log file.
Log(kLogWarn, "hello from \e[1mlua\e[0m!")
-- This check is pedantic but might be good to have.
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('\r\n')
Write('
redbean\r\n')
Write('
redbean lua server page demo
\r\n')
-- 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')
-- Roundtripping information can make it safer.
Write('
Thank you for visiting ')
Write(EscapeHtml(EncodeUrl(ParseUrl(GetUrl()))))
Write('\r\n')
-- GetParam(NAME) is the fastest easiest way to get URL and FORM params
-- If you want the RequestURL query params specifically in full do this
Write('
request url parameters
\r\n')
params = ParseUrl(GetUrl()).params -- like GetParams() but w/o form body
if params and #params>0 then
Write('
\r\n')
for i = 1,#params do
Write('
')
Write(EscapeHtml(params[i][1]))
Write('\r\n')
if params[i][2] then
Write('
')
Write(EscapeHtml(params[i][2]))
Write('\r\n')
end
end
Write('
\r\n')
else
Write('
\r\n')
Write('none \r\n')
Write('ProTip: Try clicking here!\r\n')
end
-- redbean command line arguments
-- these come *after* the c getopt server arguments
Write('
command line arguments
\r\n')
if #argv > 0 then
Write('
\r\n')
for i = 1,#argv do
Write('
')
Write(EscapeHtml(argv[i]))
Write('\r\n')
end
Write('
\r\n')
-- redbean apis for generalized parsing and encoding
referer = GetHeader('Referer')
if referer then
url = ParseUrl(referer)
if url.scheme then
url.scheme = string.upper(url.scheme)
end
Write('
referer url
\r\n')
Write('
\r\n')
Write(EscapeHtml(EncodeUrl(url)))
Write('
\r\n')
if url.scheme then
Write('
scheme\r\n')
Write('
\r\n')
Write(EscapeHtml(url.scheme))
end
if url.user then
Write('
user\r\n')
Write('
\r\n')
Write(EscapeHtml(url.user))
end
if url.pass then
Write('
pass\r\n')
Write('
\r\n')
Write(EscapeHtml(url.pass))
end
if url.host then
Write('
host\r\n')
Write('
\r\n')
Write(EscapeHtml(url.host))
end
if url.port then
Write('
port\r\n')
Write('
\r\n')
Write(EscapeHtml(url.port))
end
if url.path then
Write('
path\r\n')
Write('
\r\n')
Write(EscapeHtml(url.path))
end
if url.params then
Write('
params\r\n')
Write('
\r\n')
Write('
\r\n')
for i = 1,#url.params do
Write('
')
Write(EscapeHtml(url.params[i][1]))
Write('\r\n')
if url.params[i][2] then
Write('
')
Write(EscapeHtml(url.params[i][2]))
Write('\r\n')
end
end
Write('
\r\n')
end
if url.fragment then
Write('
fragment\r\n')
Write('
\r\n')
Write(EscapeHtml(url.fragment))
end
Write('
\r\n')
end
Write('
posix extended regular expressions
\r\n')
s = 'my ' .. FormatIp(GetClientIp()) .. ' ip'
r = CompileRegex('([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})', 'e')
m,a,b,c,d = ExecuteRegex(r, s)
Write('
\r\n')
paths = GetZipPaths()
if #paths > 0 then
Write('
\r\n')
for i = 1,#paths do
Write('
\r\n')
Write('')
Write(EscapeHtml(paths[i]))
Write('')
if IsHiddenPath(paths[i]) then
Write(' [HIDDEN]')
end
if not IsAcceptablePath(paths[i]) then
Write(' [BLOCKED]')
end
if not IsCompressed(paths[i]) then
Write(' [UNCOMPRESSED]')
end
if (GetAssetMode(paths[i]) & 0xF000) == 0x4000 then
Write(' [DIRECTORY]')
end
Write(' \r\n')
Write('Modified: ')
Write(FormatHttpDateTime(GetLastModifiedTime(paths[i])))
Write(' \r\n')
Write('Mode: ')
Write(string.format("0%o", GetAssetMode(paths[i])))
Write(' \r\n')
Write('Size: ')
Write(tostring(GetAssetSize(paths[i])))
Write(' \r\n')
if GetComment(paths[i]) then
Write('Comment: ')
Write(EscapeHtml(GetComment(paths[i])))
Write(' \r\n')
end
Write('\r\n')
end
Write('