Mercurial Hosting > luan
comparison src/luan/modules/http/tools/Shell.luan @ 1218:a50803fde972
http/tools cleanup
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 20 Mar 2018 16:24:59 -0600 |
parents | src/luan/modules/http/tools/Shell_mod.luan@5dbb552075ff |
children | 2de84f128be3 |
comparison
equal
deleted
inserted
replaced
1217:4c2972f4d862 | 1218:a50803fde972 |
---|---|
1 local Luan = require "luan:Luan.luan" | |
2 local error = Luan.error | |
3 local ipairs = Luan.ipairs or error() | |
4 local load = Luan.load or error() | |
5 local to_string = Luan.to_string or error() | |
6 local try = Luan.try or error() | |
7 local String = require "luan:String.luan" | |
8 local concat = String.concat or error() | |
9 local Time = require "luan:Time.luan" | |
10 local Thread = require "luan:Thread.luan" | |
11 local Io = require "luan:Io.luan" | |
12 local print = Io.print or error() | |
13 local Http = require "luan:http/Http.luan" | |
14 local Logging = require "luan:logging/Logging.luan" | |
15 local logger = Logging.logger "Shell" | |
16 | |
17 | |
18 local Shell = {} | |
19 | |
20 local forever = Time.period{days=1000000} | |
21 local count = 0 | |
22 local new_session = Thread.global_callable("shell.new_session",forever,{next=function() | |
23 count = count + 1 | |
24 return to_string(count) | |
25 end}).next | |
26 | |
27 local history = "" | |
28 local env = {} | |
29 Shell.env = env | |
30 | |
31 local fns = {} | |
32 | |
33 function fns.history() | |
34 return history | |
35 end | |
36 | |
37 function fns.run(cmd) | |
38 Io.stdout = {} | |
39 Io.stdout.write = function(...) | |
40 history = concat(history,...) | |
41 end | |
42 print( "% "..cmd ) | |
43 try { | |
44 function() | |
45 local line | |
46 try { | |
47 function() | |
48 line = load("return "..cmd,"<web_shell>",env) | |
49 end | |
50 catch = function(e) | |
51 line = load(cmd,"<web_shell>",env) | |
52 end | |
53 } | |
54 print( line() ) | |
55 end | |
56 catch = function(e) | |
57 Io.print_to(Io.stderr,e) | |
58 print(e) | |
59 end | |
60 } | |
61 end | |
62 | |
63 local timeout = Time.period{hours=10} | |
64 | |
65 local function get_session(session_id) | |
66 return Thread.global_callable("shell.session"..session_id,timeout,fns) | |
67 end | |
68 | |
69 local function remove_session(session_id) | |
70 return Thread.remove_global_callable("shell.session"..session_id) | |
71 end | |
72 | |
73 function Shell.respond() | |
74 local session_id = Http.request.cookies.session | |
75 if session_id == nil then | |
76 session_id = new_session() | |
77 Http.response.set_cookie("session",session_id) | |
78 end | |
79 local session = get_session(session_id) | |
80 | |
81 if Http.request.parameters.clear ~= nil then | |
82 remove_session(session_id) | |
83 Http.response.send_redirect(Http.request.path) -- reload page | |
84 return | |
85 else | |
86 local cmd = Http.request.parameters.cmd | |
87 if cmd ~= nil then | |
88 session.run(cmd) | |
89 end | |
90 end | |
91 | |
92 Io.stdout = Http.response.text_writer() | |
93 %> | |
94 <!doctype html> | |
95 <html> | |
96 <head> | |
97 <title>Luan Shell</title> | |
98 <style> | |
99 body { | |
100 font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; | |
101 margin: 2em 5% 0 5%; | |
102 } | |
103 pre { | |
104 font: inherit; | |
105 } | |
106 input[type="text"] { | |
107 font: inherit; | |
108 padding: .5em .8em; | |
109 border-radius: 8px; | |
110 border-style: groove; | |
111 } | |
112 input[type="text"]:focus { | |
113 border-color: #66afe9; | |
114 outline: none; | |
115 } | |
116 input[type="submit"] { | |
117 color: white; | |
118 background: #337ab7; | |
119 border-color: #337ab7; | |
120 font: inherit; | |
121 padding: .5em; | |
122 border-radius: 4px; | |
123 } | |
124 input[type="submit"]:hover { | |
125 background: #236aa7 !important; | |
126 } | |
127 </style> | |
128 </head> | |
129 <body> | |
130 <h2>Luan Shell</h2> | |
131 <p>This is a command shell. Enter commands below.</p> | |
132 <pre><%= session.history() %></pre> | |
133 <form name='form0' method='post'> | |
134 % <input type="text" name='cmd' size="80" autofocus> | |
135 <input type="submit" value="run"> | |
136 <input type="submit" name="clear" value="clear"> | |
137 </form> | |
138 </body> | |
139 </html> | |
140 <% | |
141 end | |
142 | |
143 return Shell |