Mercurial Hosting > luan
changeset 138:06159094b802
replace WebRun.java with web_run.luan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@139 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 12 Jun 2014 08:20:48 +0000 |
parents | 573ce091ae00 |
children | 3b384dc5ca91 |
files | src/luan/lib/HttpLib.java src/luan/lib/PackageLib.java src/luan/lib/StringLib.java src/luan/tools/WebRun.java src/luan/tools/web_run.luan |
diffstat | 5 files changed, 51 insertions(+), 96 deletions(-) [+] |
line wrap: on
line diff
diff -r 573ce091ae00 -r 06159094b802 src/luan/lib/HttpLib.java --- a/src/luan/lib/HttpLib.java Thu Jun 12 03:54:52 2014 +0000 +++ b/src/luan/lib/HttpLib.java Thu Jun 12 08:20:48 2014 +0000 @@ -33,16 +33,15 @@ public static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName) throws LuanException { - LuanState newLuan; - LuanFunction newFn; + LuanFunction fn; synchronized(luan) { Object mod = PackageLib.require(luan,modName); if( !(mod instanceof LuanFunction) ) throw luan.exception( "module '"+modName+"' must return a function" ); - LuanFunction fn = (LuanFunction)mod; + fn = (LuanFunction)mod; DeepCloner cloner = new DeepCloner(); - newLuan = cloner.deepClone(luan); - newFn = cloner.get(fn); + luan = cloner.deepClone(luan); + fn = cloner.get(fn); } LuanTable module = (LuanTable)luan.loaded().get(NAME); @@ -52,21 +51,23 @@ try { module.put( "request", lib.requestTable() ); module.put( "response", lib.responseTable() ); +/* module.put( "write", new LuanJavaFunction( HttpLib.class.getMethod( "text_write", LuanState.class, new Object[0].getClass() ), lib ) ); +*/ } catch(NoSuchMethodException e) { throw new RuntimeException(e); } - newLuan.call(newFn,"<http>"); + luan.call(fn,"<http>"); } private final HttpServletRequest request; private final HttpServletResponse response; - private PrintWriter writer = null; +// private PrintWriter writer = null; // private ServletOutputStream sos = null; private HttpLib(HttpServletRequest request,HttpServletResponse response) { @@ -98,13 +99,15 @@ resp.put( "set_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("setHeader",String.class,String.class),response) ); add( resp, "set_cookie", String.class, String.class, Boolean.TYPE, String.class ); add( resp, "remove_cookie", String.class, String.class ); + resp.put( "set_content_type", new LuanJavaFunction(HttpServletResponse.class.getMethod("setContentType",String.class),response) ); + add( resp, "text_writer" ); return resp; } private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { t.put( method, new LuanJavaFunction(HttpLib.class.getMethod(method,parameterTypes),this) ); } - +/* public void text_write(LuanState luan,Object... args) throws LuanException, IOException { if( writer == null ) writer = response.getWriter(); @@ -112,6 +115,10 @@ writer.print( luan.toString(obj) ); } } +*/ + public LuanTable text_writer() throws IOException { + return IoLib.textWriter(response.getWriter()); + } public String get_cookie_value(String name) { return getCookieValue(request, name);
diff -r 573ce091ae00 -r 06159094b802 src/luan/lib/PackageLib.java --- a/src/luan/lib/PackageLib.java Thu Jun 12 03:54:52 2014 +0000 +++ b/src/luan/lib/PackageLib.java Thu Jun 12 08:20:48 2014 +0000 @@ -131,9 +131,10 @@ public static final LuanFunction javaFileLoader = new LuanFunction() { @Override public Object call(LuanState luan,Object[] args) throws LuanException { String urlStr = (String)args[1]; + String path = (String)args[2]; try { String src = new IoLib.LuanUrl(urlStr).read_text(); - LuanFunction fn = BasicLib.load(luan,src,urlStr,false,false); + LuanFunction fn = BasicLib.load(luan,src,path,false,false); return fn.call(luan,args); } catch(IOException e) { throw luan.exception(e); @@ -146,7 +147,7 @@ String path = (String)args[0]; String url = IoLib.java_resource_to_url(path); if( url != null ) { - return new Object[]{javaFileLoader,url}; + return new Object[]{javaFileLoader,url,path}; } return LuanFunction.NOTHING; }
diff -r 573ce091ae00 -r 06159094b802 src/luan/lib/StringLib.java --- a/src/luan/lib/StringLib.java Thu Jun 12 03:54:52 2014 +0000 +++ b/src/luan/lib/StringLib.java Thu Jun 12 08:20:48 2014 +0000 @@ -189,7 +189,7 @@ return m.group(); String[] rtn = new String[n]; for( int i=0; i<n; i++ ) { - rtn[i] = m.group(i); + rtn[i] = m.group(i+1); } return rtn; }
diff -r 573ce091ae00 -r 06159094b802 src/luan/tools/WebRun.java --- a/src/luan/tools/WebRun.java Thu Jun 12 03:54:52 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +0,0 @@ -package luan.tools; - -import java.io.IOException; -import java.io.PrintStream; -import javax.servlet.ServletException; -import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import luan.LuanState; -import luan.LuanTable; -import luan.LuanException; -import luan.lib.HtmlLib; -import luan.lib.IoLib; - - -public class WebRun extends HttpServlet { - private static final Logger logger = LoggerFactory.getLogger(WebRun.class); - - protected LuanState newLuanState() throws LuanException { - return LuanState.newStandard(); - } - - protected void service(HttpServletRequest request,HttpServletResponse response) - throws ServletException, IOException - { - ServletOutputStream sout = response.getOutputStream(); - PrintStream out = new PrintStream(sout); - String contentType = request.getParameter("content_type"); - if( contentType != null ) - response.setContentType("text/plain"); - String code = request.getParameter("code"); - try { - LuanState luan = newLuanState(); - luan.set( "Io.stdout", IoLib.textWriter(out) ); - LuanTable env = luan.global(); - env.put("request",request); - env.put("response",response); - luan.eval(code); - } catch(LuanException e) { - logger.error(null,e); - response.reset(); - response.setHeader("Content-Type","text/html"); - out.println( "<html>" ); - out.println( "<body>" ); - out.println( "<pre>" ); - out.println( e ); - out.println(); - out.println( addLineNumbers(HtmlLib.encode(code)) ); - out.println( "</pre>" ); - out.println( "</body>" ); - out.println( "</html>" ); - } - } - - public static String addLineNumbers(String s) { - StringBuilder buf = new StringBuilder(); - int line = 1; - int i = 0; - while(true) { - buf.append( fmt(line++,3) ); - buf.append(" "); - int i2 = s.indexOf('\n',i); - if( i2 == -1 ) { - buf.append( s.substring(i) ); - break; - } - buf.append( s.substring(i,i2+1) ); - i = i2 + 1; - } - return buf.toString(); - } - - private static String fmt(int i,int w) { - StringBuilder buf = new StringBuilder(); - buf.append(i); - while( buf.length() < w ) { - buf.insert(0,' '); - } - return buf.toString(); - } - -}
diff -r 573ce091ae00 -r 06159094b802 src/luan/tools/web_run.luan --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/tools/web_run.luan Thu Jun 12 08:20:48 2014 +0000 @@ -0,0 +1,32 @@ +import "Http" + +local function lines(s) + local matcher = String.gmatch(s,"([^\n]*)\n|([^\n])+$") + return function() + local m1, m2 = matcher() + return m1 or m2 + end +end + +local function print_with_line_numbers(s) + i = 1 + for line in lines(s) do + print(i,line) + i = i + 1 + end +end + +return function() + Http.response.set_content_type "text/plain" + Io.stdout = Http.response.text_writer() + local code = Http.request.get_parameter "code" + try + local fn = load(code,"<web_run>") + fn() + catch e do + print(e) + print() + print() + print_with_line_numbers(code) + end +end