Mercurial Hosting > luan
changeset 175:bdbd4740121f
finish web server
git-svn-id: https://luan-java.googlecode.com/svn/trunk@176 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Mon, 23 Jun 2014 07:48:17 +0000 |
parents | 609c5b3118db |
children | 88ad4fc4c643 |
files | core/src/luan/impl/LuanParser.java core/src/luan/modules/PackageLuan.java web/src/luan/modules/web/HttpLuan.java web/src/luan/modules/web/LuanHandler.java web/src/luan/modules/web/Web_server.luan web/src/luan/modules/web/web_run.luan web/src/luan/modules/web/web_shell.luan |
diffstat | 7 files changed, 86 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
diff -r 609c5b3118db -r bdbd4740121f core/src/luan/impl/LuanParser.java --- a/core/src/luan/impl/LuanParser.java Mon Jun 23 02:57:50 2014 +0000 +++ b/core/src/luan/impl/LuanParser.java Mon Jun 23 07:48:17 2014 +0000 @@ -349,7 +349,10 @@ String modName = StringLiteral(In.NOTHING); if( modName==null ) return parser.failure(null); - String varName = modName.substring(modName.lastIndexOf('.')+1); + int i = modName.lastIndexOf('/'); + if( i == -1 ) + i = modName.lastIndexOf('.'); + String varName = modName.substring(i+1); LuanSource.Element se = se(start); FnCall require = new FnCall( se, importExpr, new ConstExpr(modName) ); Settable settable;
diff -r 609c5b3118db -r bdbd4740121f core/src/luan/modules/PackageLuan.java --- a/core/src/luan/modules/PackageLuan.java Mon Jun 23 02:57:50 2014 +0000 +++ b/core/src/luan/modules/PackageLuan.java Mon Jun 23 07:48:17 2014 +0000 @@ -26,6 +26,7 @@ module.put("jpath",jpath); try { add( module, "require", LuanState.class, String.class ); + add( module, "load", LuanState.class, String.class ); add( module, "load_lib", String.class ); add( module, "search_path", String.class, String.class ); add( module, "search", LuanState.class, String.class ); @@ -46,12 +47,19 @@ } public static Object require(LuanState luan,String modName) throws LuanException { + Object mod = load(luan,modName); + if( mod==null ) + throw luan.exception( "module '"+modName+"' not found" ); + return mod; + } + + public static Object load(LuanState luan,String modName) throws LuanException { LuanTable loaded = luan.loaded(); Object mod = loaded.get(modName); if( mod == null ) { Object[] a = search(luan,modName); if( a == null ) - throw luan.exception( "module '"+modName+"' not found" ); + return null; LuanFunction loader = (LuanFunction)a[0]; a[0] = modName; mod = Luan.first(luan.call(loader,"<require \""+modName+"\">",a)); @@ -59,8 +67,10 @@ loaded.put(modName,mod); } else { mod = loaded.get(modName); - if( mod==null ) - loaded.put(modName,true); + if( mod==null ) { + mod = true; + loaded.put(modName,mod); + } } } return mod; @@ -91,7 +101,6 @@ }; public static String search_path(String name,String path) { - name = name.replace('.','/'); for( String s : path.split(";") ) { String file = s.replaceAll("\\?",name); if( Utils.exists(file) ) @@ -139,6 +148,7 @@ public static final LuanFunction javaSearcher = new LuanFunction() { @Override public Object[] call(LuanState luan,Object[] args) { String modName = (String)args[0]; + modName = modName.replace('/','.'); String path = (String)luan.get("Package.jpath"); if( path==null ) path = jpath;
diff -r 609c5b3118db -r bdbd4740121f web/src/luan/modules/web/HttpLuan.java --- a/web/src/luan/modules/web/HttpLuan.java Mon Jun 23 02:57:50 2014 +0000 +++ b/web/src/luan/modules/web/HttpLuan.java Mon Jun 23 07:48:17 2014 +0000 @@ -28,16 +28,32 @@ public static final LuanFunction LOADER = new LuanFunction() { @Override public Object call(LuanState luan,Object[] args) { - return new LuanTable(); // starts empty + LuanTable module = new LuanTable(); + try { + addStatic( module, "new_luan_handler", LuanState.class ); + } catch(NoSuchMethodException e) { + throw new RuntimeException(e); + } + return module; } }; - public static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName) + private static void addStatic(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { + t.put( method, new LuanJavaFunction(HttpLuan.class.getMethod(method,parameterTypes),null) ); + } + + public static LuanHandler new_luan_handler(LuanState luan) { + return new LuanHandler(luan); + } + + public static boolean service(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName) throws LuanException { LuanFunction fn; synchronized(luan) { - Object mod = PackageLuan.require(luan,modName); + Object mod = PackageLuan.load(luan,modName); + if( mod==null ) + return false; if( !(mod instanceof LuanTable) ) throw luan.exception( "module '"+modName+"' must return a table" ); LuanTable tbl = (LuanTable)mod; @@ -63,9 +79,9 @@ } } - LuanTable module = (LuanTable)luan.loaded().get("web.Http"); + LuanTable module = (LuanTable)luan.loaded().get("web/Http"); if( module == null ) - throw luan.exception( "module 'web.Http' not defined" ); + throw luan.exception( "module 'web/Http' not defined" ); HttpLuan lib = new HttpLuan(request,response); try { module.put( "request", lib.requestTable() ); @@ -80,6 +96,7 @@ } luan.call(fn,"<http>"); + return true; }
diff -r 609c5b3118db -r bdbd4740121f web/src/luan/modules/web/LuanHandler.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/src/luan/modules/web/LuanHandler.java Mon Jun 23 07:48:17 2014 +0000 @@ -0,0 +1,35 @@ +package luan.modules.web; + +import java.io.IOException; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.handler.AbstractHandler; +import luan.LuanState; +import luan.LuanException; + + +public class LuanHandler extends AbstractHandler { + private final LuanState luan; + + LuanHandler(LuanState luan) { + this.luan = luan; + } + + public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) + throws IOException + { + try { + if( !HttpLuan.service(luan,request,response,target) ) + return; + response.setStatus(HttpServletResponse.SC_OK); + baseRequest.setHandled(true); + } catch(LuanException e) { +//e.printStackTrace(); + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e.toString()); + baseRequest.setHandled(true); + } + } +}
diff -r 609c5b3118db -r bdbd4740121f web/src/luan/modules/web/Web_server.luan --- a/web/src/luan/modules/web/Web_server.luan Mon Jun 23 02:57:50 2014 +0000 +++ b/web/src/luan/modules/web/Web_server.luan Mon Jun 23 07:48:17 2014 +0000 @@ -4,6 +4,8 @@ import "org.eclipse.jetty.server.handler.DefaultHandler" import "org.eclipse.jetty.server.handler.HandlerList" import "org.eclipse.jetty.server.handler.ResourceHandler" +import "org.eclipse.jetty.server.session.SessionHandler" +import "web/Http" port = 8080 @@ -11,11 +13,17 @@ resource_handler = ResourceHandler.new() +session_handler = SessionHandler.new() + +luan_handler = Http.new_luan_handler() + handlers = HandlerList.new() -handlers.setHandlers { resource_handler, DefaultHandler.new() } +handlers.setHandlers { resource_handler, session_handler, luan_handler, DefaultHandler.new() } function serve(dir) + dir = dir.gsub("/$","") -- remove trailing '/' if any + Package.path = dir.."?.luan;java:luan/modules/?.luan" resource_handler.setResourceBase(dir) local server = Server.new(port) server.setHandler(handlers);
diff -r 609c5b3118db -r bdbd4740121f web/src/luan/modules/web/web_run.luan --- a/web/src/luan/modules/web/web_run.luan Mon Jun 23 02:57:50 2014 +0000 +++ b/web/src/luan/modules/web/web_run.luan Mon Jun 23 07:48:17 2014 +0000 @@ -1,4 +1,4 @@ -import "web.Http" +import "web/Http" local function lines(s) local matcher = String.gmatch(s,"([^\n]*)\n|([^\n])+$")
diff -r 609c5b3118db -r bdbd4740121f web/src/luan/modules/web/web_shell.luan --- a/web/src/luan/modules/web/web_shell.luan Mon Jun 23 02:57:50 2014 +0000 +++ b/web/src/luan/modules/web/web_shell.luan Mon Jun 23 07:48:17 2014 +0000 @@ -1,4 +1,4 @@ -import "web.Http" +import "web/Http" per_session = true