-- redbean lua server page demo
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('
\r\n')
Write('\r\n')
Write('redbean lua server page demo\r\n')
Write('
\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')
-- GetUrl() is the resolved Request-URI (TODO: Maybe change API to return a URL object?)
Write('
Thank you for visiting ')
Write(GetUrl()) -- redbean encoded this value so it doesn't need html entity escaping
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(VisualizeControlCodes(params[i][1])))
Write('\r\n')
if params[i][2] then
Write('
')
Write(EscapeHtml(VisualizeControlCodes(params[i][2])))
Write('\r\n')
end
end
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(VisualizeControlCodes(argv[i])))
Write('\r\n')
end
Write('
\r\n')
else
Write('
none\r\n')
end
Write([[
post request html form demo
]])
Write([[
xmlhttprequest request demo
]])
-- fast redbean apis for accessing already parsed request data
Write('
extra information
\r\n')
Write('
\r\n')
Write('
GetMethod()\r\n')
Write('
')
Write(EscapeHtml(GetMethod())) -- & and ' are legal in http methods
Write('\r\n')
if GetUser() then
Write('
GetUser()\r\n')
Write('
')
Write(EscapeHtml(VisualizeControlCodes(GetUser())))
Write('\r\n')
end
if GetScheme() then
Write('
GetScheme()\r\n')
Write('
')
Write(GetScheme())
Write('\r\n')
end
if GetPass() then
Write('
GetPass()\r\n')
Write('
')
Write(EscapeHtml(VisualizeControlCodes(GetPass())))
Write('\r\n')
end
Write('
GetHost() (from HTTP Request-URI or Host header or X-Forwarded-Host header or Berkeley Sockets)\r\n')
Write('
GetEffectivePath() (actual path used internally to load the lua asset: routed depending on host, request path, and rewrites)\r\n')
Write('
')
Write(EscapeHtml(VisualizeControlCodes(GetEffectivePath())))
Write('\r\n')
if GetFragment() then
Write('
GetFragment()\r\n')
Write('
')
Write(EscapeHtml(VisualizeControlCodes(GetFragment())))
Write('\r\n')
end
Write('
GetRemoteAddr() (from Berkeley Sockets or X-Forwarded-For header)\r\n')
Write('
')
ip, port = GetRemoteAddr()
Write('%s, %d' % {FormatIp(ip), port})
if CategorizeIp(ip) then
Write(' \r\n')
Write(CategorizeIp(ip))
end
Write('\r\n')
Write('
GetClientAddr()\r\n')
Write('
')
ip, port = GetClientAddr()
Write('%s, %d' % {FormatIp(ip), port})
if CategorizeIp(ip) then
Write(' \r\n')
Write(CategorizeIp(ip))
end
Write('\r\n')
Write('
GetServerIp()\r\n')
Write('
')
ip, port = GetServerAddr()
Write('%s, %d' % {FormatIp(ip), port})
if CategorizeIp(ip) then
Write(' \r\n')
Write(CategorizeIp(ip))
end
Write('\r\n')
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(url.scheme)
end
if url.user then
Write('
user\r\n')
Write('
\r\n')
Write(EscapeHtml(VisualizeControlCodes(url.user)))
end
if url.pass then
Write('
pass\r\n')
Write('
\r\n')
Write(EscapeHtml(VisualizeControlCodes(url.pass)))
end
if url.host then
Write('
host\r\n')
Write('
\r\n')
Write(EscapeHtml(VisualizeControlCodes(url.host)))
end
if url.port then
Write('
port\r\n')
Write('
\r\n')
Write(EscapeHtml(VisualizeControlCodes(url.port)))
end
if url.path then
Write('
path\r\n')
Write('
\r\n')
Write(EscapeHtml(VisualizeControlCodes(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(VisualizeControlCodes(url.params[i][1])))
Write('\r\n')
if url.params[i][2] then
Write('
')
Write(EscapeHtml(VisualizeControlCodes(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(VisualizeControlCodes(url.fragment)))
end
Write('
\r\n')
end
Write('
posix extended regular expressions
\r\n')
s = 'my ' .. FormatIp(GetRemoteAddr()) .. ' ip'
-- traditional regular expressions
m,a,b,c,d = re.search([[\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)]], s, re.BASIC)
-- easy api (~10x slower because compile is O(2ⁿ) and search is O(n))
m,a,b,c,d = re.search([[([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})]], s)
-- proper api (especially if you put the re.compile() line in /.init.lua)
pat = re.compile([[([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})]])
m,a,b,c,d = pat:search(s) -- m and rest are nil if match not found
Write('
\r\n')
paths = GetZipPaths()
if #paths > 0 then
Write('
\r\n')
for i = 1,#paths do
Write('
\r\n')
Write('')
Write(EscapeHtml(VisualizeControlCodes(paths[i])))
Write('')
if IsHiddenPath(paths[i]) then
Write(' [HIDDEN]')
end
if not IsAcceptablePath(paths[i]) then
Write(' [BLOCKED]')
end
if not IsAssetCompressed(paths[i]) then
Write(' [UNCOMPRESSED]')
end
if (GetAssetMode(paths[i]) & 0xF000) == 0x4000 then
Write(' [DIRECTORY]')
end
Write(' \r\n')
Write('Modified: ')
Write(FormatHttpDateTime(GetAssetLastModifiedTime(paths[i])))
Write(' \r\n')
Write('Mode: ')
Write("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(VisualizeControlCodes(GetComment(paths[i]))))
Write(' \r\n')
end
Write('\r\n')
end
Write('