Mercurial Hosting > luan
diff src/luan/modules/http/tools/Shell.luan @ 1295:9dca1e912658
improve web shell
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 10 Jan 2019 21:42:11 -0700 |
parents | 007ceb8dcf89 |
children | 590ad449ac7f |
line wrap: on
line diff
--- a/src/luan/modules/http/tools/Shell.luan Thu Jan 10 18:22:52 2019 -0700 +++ b/src/luan/modules/http/tools/Shell.luan Thu Jan 10 21:42:11 2019 -0700 @@ -3,15 +3,17 @@ local load = Luan.load or error() local to_string = Luan.to_string or error() local try = Luan.try or error() -local String = require "luan:String.luan" -local concat = String.concat or error() +local range = Luan.range or error() local Table = require "luan:Table.luan" +local concat = Table.concat or error() local pack = Table.pack or error() local unpack = Table.unpack or error() local Time = require "luan:Time.luan" local Thread = require "luan:Thread.luan" +local Html = require "luan:Html.luan" +local html_encode = Html.encode or error() +local javascript_encode = Html.javascript_encode or error() local Io = require "luan:Io.luan" -local print = Io.print or error() local Http = require "luan:http/Http.luan" local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "Shell" @@ -26,23 +28,13 @@ return to_string(count) end}).next -local history = "" local env = {} Shell.env = env local fns = {} -function fns.history() - return history -end - function fns.run(cmd) - Io.stdout = {} - Io.stdout.write = function(...) - history = concat(history,...) - end - print( "% "..cmd ) - try { + return try { function() local line try { @@ -53,14 +45,11 @@ line = load(cmd,"<web_shell>",env) end } - local rtn = pack( line() ) - if rtn.n > 0 then - print( unpack(rtn) ) - end + return line() end catch = function(e) - Io.print_to(Io.stderr,e) - print(e) +-- Io.print_to(Io.stderr,e) + return to_string(e) end } end @@ -71,74 +60,91 @@ return Thread.global_callable("shell.session"..session_id,timeout,fns) end -local function remove_session(session_id) - return Thread.remove_global_callable("shell.session"..session_id) -end - function Shell.respond() - local session_id = Http.request.cookies["session"] - if session_id == nil then - session_id = new_session() - Http.response.set_cookie("session",session_id) + Io.stdout = Http.response.text_writer() + local cmd = Http.request.parameters.cmd + if cmd ~= nil then + Http.response.headers["content-type"] = "application/javascript" + local session_id = Http.request.parameters.session or error() + local session = get_session(session_id) + local rtn = pack( session.run(cmd) ) +%> + var pre = document.querySelector('pre'); + pre.innerHTML += '\n> <%=javascript_encode(html_encode(cmd))%>'; +<% + if rtn.n > 0 then + local t = {} + for i in range(1,rtn.n) do + t[#t+1] = javascript_encode(html_encode(to_string(rtn[i]))) + end +%> + pre.innerHTML += '\n<%=concat(t,"\t")%>'; +<% + end + return end - local session = get_session(session_id) - - if Http.request.parameters.clear ~= nil then - remove_session(session_id) - Http.response.send_redirect(Http.request.path) -- reload page - return - else - local cmd = Http.request.parameters.cmd - if cmd ~= nil then - session.run(cmd) - end - end - - Io.stdout = Http.response.text_writer() %> <!doctype html> <html> <head> <title>Luan Shell</title> + <script> + + function ajax(url) { + var request = new XMLHttpRequest(); + request.open( 'GET', url ); + request.onload = function() { + if( request.status !== 200 ) { + console.log( 'ajax failed: ' + request.status ); + return; + } + //console.log( request.responseText ); + eval( request.responseText ); + }; + request.send(); + } + + function submitted() { + var input = document.querySelector('input'); + ajax( '?session=<%=new_session()%>&cmd=' + encodeURIComponent(input.value) ); + input.value = ''; + } + + </script> <style> body { font-family: sans-serif; - margin: 2em 5% 0 5%; } pre { font: inherit; + margin-bottom: 0; } - input[type="text"] { - font: inherit; - padding: .5em .8em; - border-radius: 8px; - border-style: groove; - } - input[type="text"]:focus { - border-color: #66afe9; - outline: none; + table { + width: 100%; + border-collapse: collapse; } - input[type="submit"] { - color: white; - background: #337ab7; - border-color: #337ab7; + td { + padding: 0; + } + td:last-child { + width: 100%; + } + input { + width: 100%; font: inherit; - padding: .5em; - border-radius: 4px; - } - input[type="submit"]:hover { - background: #236aa7 !important; + outline: none; } </style> </head> <body> - <h2>Luan Shell</h2> - <p>This is a command shell. Enter commands below.</p> - <pre><%= session.history() %></pre> - <form method='post'> - % <input type="text" name='cmd' size="80" autofocus> - <input type="submit" value="run"> - <input type="submit" name="clear" value="clear"> + <pre>Luan <%=Luan.VERSION%></pre> + <form autocomplete=off onsubmit="submitted(); return false"> + <table> + <tr> + <td>> </td> + <td><input name=cmd autofocus></td> + </tr> + </table> </form> </body> </html>