Mercurial Hosting > luan
comparison src/luan/modules/http/tools/Shell_mod.luan @ 1159:3ef883468fd0
remove Http.per_session_pages
fix clone closure bug
replace Thread.global with Thread.global_callable()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 05 Feb 2018 12:37:59 -0700 |
parents | 21d157b153fe |
children | 26533dd4cd09 |
comparison
equal
deleted
inserted
replaced
1158:267fdf5e9fbd | 1159:3ef883468fd0 |
---|---|
1 local Luan = require "luan:Luan.luan" | 1 local Luan = require "luan:Luan.luan" |
2 local error = Luan.error | 2 local error = Luan.error |
3 local ipairs = Luan.ipairs or error() | 3 local ipairs = Luan.ipairs or error() |
4 local load = Luan.load or error() | 4 local load = Luan.load or error() |
5 local to_string = Luan.to_string or error() | |
5 local try = Luan.try 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" | |
6 local Io = require "luan:Io.luan" | 11 local Io = require "luan:Io.luan" |
7 local print = Io.print or error() | 12 local print = Io.print or error() |
8 local Http = require "luan:http/Http.luan" | 13 local Http = require "luan:http/Http.luan" |
14 local Logging = require "luan:logging/Logging.luan" | |
15 local logger = Logging.logger "Shell_mod" | |
9 | 16 |
10 | 17 |
11 local Shell_mod = {} | 18 local Shell_mod = {} |
12 | 19 |
13 local history = {} | 20 local forever = Time.period{days=1000000} |
14 Shell_mod.env = {} | 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 | |
30 local fns = {} | |
31 | |
32 function fns.history() | |
33 return history | |
34 end | |
35 | |
36 function fns.run(cmd) | |
37 Io.stdout = {} | |
38 Io.stdout.write = function(...) | |
39 history = concat(history,...) | |
40 end | |
41 print( "% "..cmd ) | |
42 try { | |
43 function() | |
44 local line | |
45 try { | |
46 function() | |
47 line = load("return "..cmd,"<web_shell>",env) | |
48 end | |
49 catch = function(e) | |
50 line = load(cmd,"<web_shell>",env) | |
51 end | |
52 } | |
53 print( line() ) | |
54 end | |
55 catch = function(e) | |
56 Io.print_to(Io.stderr,e) | |
57 print(e) | |
58 end | |
59 } | |
60 end | |
61 | |
62 local timeout = Time.period{hours=10} | |
63 | |
64 local function get_session(session_id) | |
65 return Thread.global_callable("shell.session"..session_id,timeout,fns) | |
66 -- return fns | |
67 end | |
68 | |
69 local function remove_session(session_id) | |
70 return Thread.remove_global_callable("shell.session"..session_id) | |
71 end | |
15 | 72 |
16 function Shell_mod.respond() | 73 function Shell_mod.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 | |
17 if Http.request.parameters.clear ~= nil then | 81 if Http.request.parameters.clear ~= nil then |
18 Http.clear_session() | 82 remove_session(session_id) |
19 Http.response.send_redirect(Http.request.path) -- reload page | 83 Http.response.send_redirect(Http.request.path) -- reload page |
20 return | 84 return |
21 else | 85 else |
22 local cmd = Http.request.parameters.cmd | 86 local cmd = Http.request.parameters.cmd |
23 if cmd ~= nil then | 87 if cmd ~= nil then |
24 Io.stdout = {} | 88 session.run(cmd) |
25 function Io.stdout.write(...) | |
26 for v in Luan.values(...) do | |
27 history[#history+1] = v | |
28 end | |
29 end | |
30 print( "% "..cmd ) | |
31 try { | |
32 function() | |
33 local line | |
34 try { | |
35 function() | |
36 line = load("return "..cmd,"<web_shell>",Shell_mod.env) | |
37 end | |
38 catch = function(e) | |
39 line = load(cmd,"<web_shell>",Shell_mod.env) | |
40 end | |
41 } | |
42 print( line() ) | |
43 end | |
44 catch = function(e) | |
45 Io.print_to(Io.stderr,e) | |
46 print(e) | |
47 end | |
48 } | |
49 end | 89 end |
50 end | 90 end |
51 | 91 |
52 Io.stdout = Http.response.text_writer() | 92 Io.stdout = Http.response.text_writer() |
53 %> | 93 %> |
86 </style> | 126 </style> |
87 </head> | 127 </head> |
88 <body> | 128 <body> |
89 <h2>Luan Shell</h2> | 129 <h2>Luan Shell</h2> |
90 <p>This is a command shell. Enter commands below.</p> | 130 <p>This is a command shell. Enter commands below.</p> |
91 <pre><% | 131 <pre><%= session.history() %></pre> |
92 for _,v in ipairs(history) do | |
93 Io.stdout.write(v) | |
94 end | |
95 %></pre> | |
96 <form name='form0' method='post'> | 132 <form name='form0' method='post'> |
97 % <input type="text" name='cmd' size="80" autofocus> | 133 % <input type="text" name='cmd' size="80" autofocus> |
98 <input type="submit" value="run"> | 134 <input type="submit" value="run"> |
99 <input type="submit" name="clear" value="clear"> | 135 <input type="submit" name="clear" value="clear"> |
100 </form> | 136 </form> |
101 </body> | 137 </body> |
102 </html> | 138 </html> |
103 <% | 139 <% |
104 end | 140 end |
105 | 141 |
106 Http.per_session(Shell_mod.respond) | |
107 | |
108 return Shell_mod | 142 return Shell_mod |