diff 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
line wrap: on
line diff
--- a/src/luan/modules/http/tools/Shell_mod.luan	Mon Feb 05 10:04:07 2018 -0700
+++ b/src/luan/modules/http/tools/Shell_mod.luan	Mon Feb 05 12:37:59 2018 -0700
@@ -2,50 +2,90 @@
 local error = Luan.error
 local ipairs = Luan.ipairs or error()
 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 Time = require "luan:Time.luan"
+local Thread = require "luan:Thread.luan"
 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_mod"
 
 
 local Shell_mod = {}
 
-local history = {}
-Shell_mod.env = {}
+local forever = Time.period{days=1000000}
+local count = 0
+local new_session = Thread.global_callable("shell.new_session",forever,{next=function()
+	count = count + 1
+	return to_string(count)
+end}).next
+
+local history = ""
+local 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 {
+		function()
+			local line
+			try {
+				function()
+					line = load("return "..cmd,"<web_shell>",env)
+				end
+				catch = function(e)
+					line = load(cmd,"<web_shell>",env)
+				end
+			}
+			print( line() )
+		end
+		catch = function(e)
+			Io.print_to(Io.stderr,e)
+			print(e)
+		end
+	}
+end
+
+local timeout = Time.period{hours=10}
+
+local function get_session(session_id)
+	return Thread.global_callable("shell.session"..session_id,timeout,fns)
+--	return fns
+end
+
+local function remove_session(session_id)
+	return Thread.remove_global_callable("shell.session"..session_id)
+end
 
 function Shell_mod.respond()
+	local session_id = Http.request.cookies.session
+	if session_id == nil then
+		session_id = new_session()
+		Http.response.set_cookie("session",session_id)
+	end
+	local session = get_session(session_id)
+
 	if Http.request.parameters.clear ~= nil then
-		Http.clear_session()
+		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
-			Io.stdout = {}
-			function Io.stdout.write(...)
-				for v in Luan.values(...) do
-					history[#history+1] = v
-				end
-			end
-			print( "% "..cmd )
-			try {
-				function()
-					local line
-					try {
-						function()
-							line = load("return "..cmd,"<web_shell>",Shell_mod.env)
-						end
-						catch = function(e)
-							line = load(cmd,"<web_shell>",Shell_mod.env)
-						end
-					}
-					print( line() )
-				end
-				catch = function(e)
-					Io.print_to(Io.stderr,e)
-					print(e)
-				end
-			}
+			session.run(cmd)
 		end
 	end
 
@@ -88,11 +128,7 @@
 	<body>
 		<h2>Luan Shell</h2>
 		<p>This is a command shell.  Enter commands below.</p>
-		<pre><%
-		for _,v in ipairs(history) do
-			Io.stdout.write(v)
-		end
-		%></pre>
+		<pre><%= session.history() %></pre>
 		<form name='form0' method='post'>
 			% <input type="text" name='cmd' size="80" autofocus>
 			<input type="submit" value="run">
@@ -103,6 +139,4 @@
 <%
 end
 
-Http.per_session(Shell_mod.respond)
-
 return Shell_mod