mirror of
https://github.com/dongdigua/demo.lua.git
synced 2025-02-22 03:59:46 +08:00
add original implementation for reference
This commit is contained in:
parent
acbfcc8bdc
commit
60345463c8
23
orig/README
Normal file
23
orig/README
Normal file
@ -0,0 +1,23 @@
|
||||
This is the source code for the Lua live demo that is available at
|
||||
http://www.lua.org/cgi-bin/demo
|
||||
|
||||
It has three parts:
|
||||
demo the CGI script
|
||||
demo.html the HTML page
|
||||
demo.lua the Lua program that runs user programs
|
||||
|
||||
The CGI script is a sh script that handles both GET and POST requests.
|
||||
Empty GET requests return demo.html, which is what you see when you
|
||||
visit the URL above. Nonempty GET requests fill the text box with one of
|
||||
the demo programs listed in demo.html. Lua programs in the text box are
|
||||
run in reply to POST requests via a stock Lua interpreter.
|
||||
|
||||
An important concern of the CGI script is security. Pains are taken to avoid
|
||||
executing scripts outside the cgi-bin directory and to avoid consuming too
|
||||
much time or memory when runnning user programs.
|
||||
|
||||
The Lua program reads and parses the POST input and runs user programs in
|
||||
a constrained environment, again for security.
|
||||
|
||||
This code is hereby placed in the public domain.
|
||||
Please send comments, suggestions, and bug reports to lhf@tecgraf.puc-rio.br .
|
46
orig/demo
Executable file
46
orig/demo
Executable file
@ -0,0 +1,46 @@
|
||||
#!/bin/sh
|
||||
|
||||
LUA=/home/lhf/websites/lua.org/www/bin/lua
|
||||
LOG=log
|
||||
ECHO="/bin/echo -E"
|
||||
|
||||
cat <<EOF
|
||||
Content-type: text/html
|
||||
|
||||
EOF
|
||||
|
||||
date "+%n@ %c $REMOTE_HOST $REQUEST_METHOD $HTTP_REFERER" >> $LOG
|
||||
if [ "$REQUEST_METHOD" = "GET" ]
|
||||
then
|
||||
if [ "$QUERY_STRING" = "" ]
|
||||
then
|
||||
exec cat demo.html
|
||||
else
|
||||
$ECHO "$QUERY_STRING" >> $LOG
|
||||
F=`basename -- "$QUERY_STRING"`.lua
|
||||
sed '/TEXTAREA/q' demo.html
|
||||
$ECHO "-- $F"
|
||||
(cat -- "$F" 2>&1)
|
||||
sed '1,/TEXTAREA/d' demo.html
|
||||
fi
|
||||
else
|
||||
sed '/TEXTAREA/q' demo.html
|
||||
tee -a $LOG | (ulimit -t 1 ; $LUA demo.lua 2>&1 | head -c 8k)
|
||||
cat <<EOF
|
||||
|
||||
</TEXTAREA><P><IMG SRC="images/alert.png" ALIGN="absbottom">
|
||||
Your program was aborted.<!-- -->
|
||||
<P>
|
||||
|
||||
<HR>
|
||||
<SMALL>
|
||||
EOF
|
||||
$LUA -v 2>&1
|
||||
cat <<EOF
|
||||
</SMALL>
|
||||
|
||||
</BODY>
|
||||
</HTML>
|
||||
EOF
|
||||
echo '' >> $LOG
|
||||
fi
|
54
orig/demo.html
Normal file
54
orig/demo.html
Normal file
@ -0,0 +1,54 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||
<HTML>
|
||||
<HEAD>
|
||||
<TITLE>Lua: demo</TITLE>
|
||||
<BASE HREF="http://www.lua.org/">
|
||||
<LINK REL="stylesheet" TYPE="text/css" HREF="lua.css">
|
||||
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1">
|
||||
<STYLE TYPE="text/css">
|
||||
textarea {
|
||||
font-family: monospace ;
|
||||
}
|
||||
</STYLE>
|
||||
</HEAD>
|
||||
|
||||
<BODY>
|
||||
|
||||
<HR>
|
||||
<H1>
|
||||
<A HREF="home.html"><IMG SRC="images/logo.gif" ALT="Lua" BORDER=0></A>
|
||||
Demo
|
||||
</H1>
|
||||
|
||||
Try Lua before
|
||||
<A HREF="download.html">downloading</A> it.
|
||||
Enter your Lua program
|
||||
or
|
||||
choose one of the demo programs below.
|
||||
<P>
|
||||
<A HREF="cgi-bin/demo?hello">hello</A>
|
||||
· <A HREF="cgi-bin/demo?globals">globals</A>
|
||||
· <A HREF="cgi-bin/demo?bisect">bisect</A>
|
||||
· <A HREF="cgi-bin/demo?sieve">sieve</A>
|
||||
· <A HREF="cgi-bin/demo?account">account</A>
|
||||
<FORM ACTION="cgi-bin/demo" METHOD="POST">
|
||||
<TEXTAREA ROWS="8" COLS="80" NAME="input">
|
||||
</TEXTAREA>
|
||||
<P>
|
||||
<INPUT TYPE="submit" VALUE="Run">
|
||||
<INPUT TYPE="button" VALUE="Clear" onclick="this.form.elements['input'].value=''">
|
||||
<INPUT TYPE="reset" VALUE="Original">
|
||||
</FORM>
|
||||
<P>
|
||||
|
||||
<HR>
|
||||
<SMALL>
|
||||
Last update:
|
||||
Wed Mar 19 17:01:42 GMT 2008
|
||||
</SMALL>
|
||||
<!--
|
||||
Last change: minor edit
|
||||
-->
|
||||
|
||||
</BODY>
|
||||
</HTML>
|
56
orig/demo.lua
Normal file
56
orig/demo.lua
Normal file
@ -0,0 +1,56 @@
|
||||
local T,E,I
|
||||
T=io.read"*a"
|
||||
T=string.match(T,"=(.-)$") or ""
|
||||
T=string.gsub(T,"+"," ")
|
||||
T=string.gsub(T,"%%(%x%x)",function (x) return string.char(tonumber(x,16)) end)
|
||||
T=string.gsub(T,"^%s*=%s*","return ")
|
||||
|
||||
local write=io.write
|
||||
|
||||
write(T)
|
||||
write[[</TEXTAREA>
|
||||
<P>
|
||||
<INPUT TYPE="submit" VALUE="Run">
|
||||
<INPUT TYPE="button" VALUE="Clear" onclick="this.form.elements['input'].value=''">
|
||||
<INPUT TYPE="reset" VALUE="Original">
|
||||
</FORM>
|
||||
<P>
|
||||
|
||||
<H2>Output</H2>
|
||||
<TEXTAREA ROWS="8" COLS="80">
|
||||
]]
|
||||
io.flush()
|
||||
|
||||
arg=nil
|
||||
debug.debug=nil
|
||||
debug.getfenv=getfenv
|
||||
debug.getregistry=nil
|
||||
dofile=nil
|
||||
io={write=io.write}
|
||||
loadfile=nil
|
||||
os.execute=nil
|
||||
os.getenv=nil
|
||||
os.remove=nil
|
||||
os.rename=nil
|
||||
os.tmpname=nil
|
||||
package.loaded.io=io
|
||||
package.loaded.package=nil
|
||||
package=nil
|
||||
require=nil
|
||||
|
||||
T,E=loadstring(T,"=input")
|
||||
if not T then
|
||||
write(E) E="failed to compile" I="alert"
|
||||
else
|
||||
T=(function (...) return {select('#',...),...} end)(pcall(T))
|
||||
if not T[2] then
|
||||
write(T[3]) E="failed to run" I="alert"
|
||||
else
|
||||
E="ran successfully" I="ok"
|
||||
for i=3,T[1]+1 do write(tostring(T[i]),"\t") end
|
||||
end
|
||||
end
|
||||
write[[</TEXTAREA><P>]]
|
||||
write('<IMG SRC="images/',I,'.png" ALIGN="absbottom">\n')
|
||||
write("Your program ",E,".")
|
||||
write("<!-- ")
|
Loading…
Reference in New Issue
Block a user