Mercurial Hosting > luan
annotate src/luan/modules/http/tools/Shell.luan @ 1578:c922446f53aa
immutable threading
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 08 Feb 2021 14:16:19 -0700 |
parents | d3e61cd2aca0 |
children |
rev | line source |
---|---|
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
609
diff
changeset
|
1 local Luan = require "luan:Luan.luan" |
554
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
506
diff
changeset
|
2 local error = Luan.error |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
506
diff
changeset
|
3 local load = Luan.load or error() |
1159
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
4 local to_string = Luan.to_string or error() |
1295 | 5 local range = Luan.range or error() |
1277 | 6 local Table = require "luan:Table.luan" |
1295 | 7 local concat = Table.concat or error() |
1277 | 8 local pack = Table.pack or error() |
9 local unpack = Table.unpack or error() | |
1578 | 10 local copy = Table.copy or error() |
1159
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
11 local Time = require "luan:Time.luan" |
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
12 local Thread = require "luan:Thread.luan" |
1295 | 13 local Html = require "luan:Html.luan" |
14 local html_encode = Html.encode or error() | |
15 local javascript_encode = Html.javascript_encode or error() | |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
609
diff
changeset
|
16 local Io = require "luan:Io.luan" |
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
609
diff
changeset
|
17 local Http = require "luan:http/Http.luan" |
1159
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
18 local Logging = require "luan:logging/Logging.luan" |
1218 | 19 local logger = Logging.logger "Shell" |
560
2f39468680be
remove SimplyHTML from shell
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
20 |
139
3b384dc5ca91
replace WebShell.java with web_shell.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
21 |
1218 | 22 local Shell = {} |
503 | 23 |
1578 | 24 local function counter() |
25 local fns = {} | |
26 local count = 0 | |
27 function fns.next() | |
28 count = count + 1 | |
29 return to_string(count) | |
30 end | |
31 return fns | |
32 end | |
33 local new_session = Thread.global_callable("shell.new_session",counter).next | |
1159
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
34 |
1578 | 35 Shell.env = {} |
1159
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
36 |
1578 | 37 local function init_env() |
38 local env = copy(Shell.env) | |
39 local fns = {} | |
40 function fns.run(cmd) | |
1520 | 41 try |
1578 | 42 local line |
43 try | |
44 line = load("return "..cmd,"<web_shell>",env) | |
45 catch e | |
46 line = load(cmd,"<web_shell>",env) | |
47 end | |
48 return line() | |
1520 | 49 catch e |
1578 | 50 --Io.print_to(Io.stderr,e) |
51 return to_string(e) | |
1159
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
52 end |
1520 | 53 end |
1578 | 54 return fns |
1159
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
55 end |
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
56 |
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
57 local timeout = Time.period{hours=10} |
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
58 |
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
59 local function get_session(session_id) |
1578 | 60 return Thread.global_callable("shell.session"..session_id,init_env,timeout) |
1159
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
61 end |
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
62 |
1218 | 63 function Shell.respond() |
1295 | 64 local cmd = Http.request.parameters.cmd |
65 if cmd ~= nil then | |
66 Http.response.headers["content-type"] = "application/javascript" | |
1352 | 67 Io.stdout = Http.response.text_writer() |
1295 | 68 local session_id = Http.request.parameters.session or error() |
69 local session = get_session(session_id) | |
70 local rtn = pack( session.run(cmd) ) | |
71 %> | |
72 var pre = document.querySelector('pre'); | |
73 pre.innerHTML += '\n> <%=javascript_encode(html_encode(cmd))%>'; | |
74 <% | |
75 if rtn.n > 0 then | |
76 local t = {} | |
77 for i in range(1,rtn.n) do | |
78 t[#t+1] = javascript_encode(html_encode(to_string(rtn[i]))) | |
79 end | |
80 %> | |
81 pre.innerHTML += '\n<%=concat(t,"\t")%>'; | |
82 <% | |
83 end | |
1300 | 84 %> |
85 var input = document.querySelector('input'); | |
86 input.scrollIntoView(); | |
87 input.blur(); input.focus(); // because Chrome is retarded | |
88 <% | |
1295 | 89 return |
1159
3ef883468fd0
remove Http.per_session_pages
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
90 end |
1352 | 91 Io.stdout = Http.response.text_writer() |
391
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents:
347
diff
changeset
|
92 %> |
1216 | 93 <!doctype html> |
391
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents:
347
diff
changeset
|
94 <html> |
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents:
347
diff
changeset
|
95 <head> |
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents:
347
diff
changeset
|
96 <title>Luan Shell</title> |
1295 | 97 <script> |
98 | |
99 function ajax(url) { | |
100 var request = new XMLHttpRequest(); | |
101 request.open( 'GET', url ); | |
102 request.onload = function() { | |
103 if( request.status !== 200 ) { | |
104 console.log( 'ajax failed: ' + request.status ); | |
105 return; | |
106 } | |
107 //console.log( request.responseText ); | |
108 eval( request.responseText ); | |
109 }; | |
110 request.send(); | |
111 } | |
112 | |
113 function submitted() { | |
114 var input = document.querySelector('input'); | |
115 ajax( '?session=<%=new_session()%>&cmd=' + encodeURIComponent(input.value) ); | |
116 input.value = ''; | |
117 } | |
118 | |
119 </script> | |
560
2f39468680be
remove SimplyHTML from shell
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
120 <style> |
2f39468680be
remove SimplyHTML from shell
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
121 body { |
1293 | 122 font-family: sans-serif; |
560
2f39468680be
remove SimplyHTML from shell
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
123 } |
2f39468680be
remove SimplyHTML from shell
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
124 pre { |
2f39468680be
remove SimplyHTML from shell
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
125 font: inherit; |
1295 | 126 margin-bottom: 0; |
560
2f39468680be
remove SimplyHTML from shell
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
127 } |
1295 | 128 table { |
129 width: 100%; | |
130 border-collapse: collapse; | |
560
2f39468680be
remove SimplyHTML from shell
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
131 } |
1295 | 132 td { |
133 padding: 0; | |
134 } | |
135 td:last-child { | |
136 width: 100%; | |
137 } | |
138 input { | |
139 width: 100%; | |
560
2f39468680be
remove SimplyHTML from shell
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
140 font: inherit; |
1295 | 141 outline: none; |
560
2f39468680be
remove SimplyHTML from shell
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
142 } |
2f39468680be
remove SimplyHTML from shell
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
143 </style> |
391
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents:
347
diff
changeset
|
144 </head> |
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents:
347
diff
changeset
|
145 <body> |
1295 | 146 <pre>Luan <%=Luan.VERSION%></pre> |
1300 | 147 <form onsubmit="submitted(); return false"> |
1295 | 148 <table> |
149 <tr> | |
150 <td>> </td> | |
151 <td><input name=cmd autofocus></td> | |
152 </tr> | |
153 </table> | |
560
2f39468680be
remove SimplyHTML from shell
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
154 </form> |
391
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents:
347
diff
changeset
|
155 </body> |
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents:
347
diff
changeset
|
156 </html> |
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents:
347
diff
changeset
|
157 <% |
139
3b384dc5ca91
replace WebShell.java with web_shell.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
158 end |
503 | 159 |
1218 | 160 return Shell |