Mercurial Hosting > luan
comparison src/luan/modules/http/LuanHandler.java @ 1562:b89212fd04b5
remove table.luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 08 Nov 2020 16:50:59 -0700 |
parents | 219f2b937f2b |
children | 8fbcc4747091 |
comparison
equal
deleted
inserted
replaced
1561:e1a13e707bf3 | 1562:b89212fd04b5 |
---|---|
64 this.luanInit = luanInit; | 64 this.luanInit = luanInit; |
65 this.domain = domain; | 65 this.domain = domain; |
66 try { | 66 try { |
67 Fns fns = new Fns(this); | 67 Fns fns = new Fns(this); |
68 LuanTable Http = (LuanTable)luanInit.require("luan:http/Http.luan"); | 68 LuanTable Http = (LuanTable)luanInit.require("luan:http/Http.luan"); |
69 if( Http.get("reset_luan") == null ) | 69 if( Http.get(luanInit,"reset_luan") == null ) |
70 Http.put( "reset_luan", new LuanJavaFunction(luanInit,resetLuanMethod,fns) ); | 70 Http.put( luanInit, "reset_luan", new LuanJavaFunction(luanInit,resetLuanMethod,fns) ); |
71 Http.put( "eval_in_root", new LuanJavaFunction(luanInit,evalInRootMethod,fns) ); | 71 Http.put( luanInit, "eval_in_root", new LuanJavaFunction(luanInit,evalInRootMethod,fns) ); |
72 Http.put( "disable_luan", new LuanJavaFunction(luanInit,disableLuanMethod,fns) ); | 72 Http.put( luanInit, "disable_luan", new LuanJavaFunction(luanInit,disableLuanMethod,fns) ); |
73 Http.put( "dont_gc", new LuanJavaFunction(luanInit,dontGcMethod,fns) ); | 73 Http.put( luanInit, "dont_gc", new LuanJavaFunction(luanInit,dontGcMethod,fns) ); |
74 } catch(LuanException e) { | 74 } catch(LuanException e) { |
75 throw new RuntimeException(e); | 75 throw new RuntimeException(e); |
76 } | 76 } |
77 if( domain != null ) | 77 if( domain != null ) |
78 logger.info("new "+domain); | 78 logger.info("new "+domain); |
136 public void close() { | 136 public void close() { |
137 Object obj = dontGc.remove(this); | 137 Object obj = dontGc.remove(this); |
138 //logger.info("close "+domain+" "+(obj!=null)); | 138 //logger.info("close "+domain+" "+(obj!=null)); |
139 } | 139 } |
140 | 140 |
141 public Object call_rpc(String fnName,Object... args) throws LuanException { | 141 public Object call_rpc(Luan luan,String fnName,Object... args) throws LuanException { |
142 rwLock.readLock().lock(); | 142 rwLock.readLock().lock(); |
143 LuanLogger.startThreadLogging(currentLuan); | 143 LuanLogger.startThreadLogging(currentLuan); |
144 try { | 144 try { |
145 LuanFunction fn; | 145 LuanFunction fn; |
146 synchronized(luanInit) { | 146 synchronized(luanInit) { |
147 enableLoad("luan:Rpc.luan"); | 147 enableLoad("luan:Rpc.luan"); |
148 LuanTable rpc = (LuanTable)currentLuan.require("luan:Rpc.luan"); | 148 LuanTable rpc = (LuanTable)currentLuan.require("luan:Rpc.luan"); |
149 LuanTable fns = (LuanTable)rpc.get("functions"); | 149 LuanTable fns = (LuanTable)rpc.get(luan,"functions"); |
150 fn = (LuanFunction)fns.get(fnName); | 150 fn = (LuanFunction)fns.get(luan,fnName); |
151 if( fn == null ) | 151 if( fn == null ) |
152 throw new LuanException( "function not found: " + fnName ); | 152 throw new LuanException( "function not found: " + fnName ); |
153 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); | 153 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); |
154 fn = (LuanFunction)cloner.get(fn); | 154 fn = (LuanFunction)cloner.get(fn); |
155 } | 155 } |
254 synchronized(luanInit) { | 254 synchronized(luanInit) { |
255 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); | 255 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); |
256 luan = (Luan)cloner.clone(currentLuan); | 256 luan = (Luan)cloner.clone(currentLuan); |
257 } | 257 } |
258 LuanTable module = (LuanTable)luan.require("luan:http/Http.luan"); | 258 LuanTable module = (LuanTable)luan.require("luan:http/Http.luan"); |
259 return (Response)module.fn("handle_error").call( request, e.table(luan) ); | 259 return (Response)module.fn(luan,"handle_error").call( request, e.table(luan) ); |
260 } | 260 } |
261 | 261 |
262 private Response serviceLuan(Request request) | 262 private Response serviceLuan(Request request) |
263 throws LuanException | 263 throws LuanException |
264 { | 264 { |
276 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); | 276 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); |
277 luan = (Luan)cloner.clone(currentLuan); | 277 luan = (Luan)cloner.clone(currentLuan); |
278 fn = (LuanFunction)cloner.get(mod); | 278 fn = (LuanFunction)cloner.get(mod); |
279 } | 279 } |
280 LuanTable module = (LuanTable)luan.require("luan:http/Http.luan"); | 280 LuanTable module = (LuanTable)luan.require("luan:http/Http.luan"); |
281 module.fn("new_request").call(request); | 281 module.fn(luan,"new_request").call(request); |
282 module.fn("new_response").call(); | 282 module.fn(luan,"new_response").call(); |
283 fn.call(); | 283 fn.call(); |
284 return (Response)module.fn("finish").call(); | 284 return (Response)module.fn(luan,"finish").call(); |
285 } | 285 } |
286 | 286 |
287 private Response serviceNotFound(Request request) | 287 private Response serviceNotFound(Request request) |
288 throws LuanException | 288 throws LuanException |
289 { | 289 { |
290 LuanFunction fn; | 290 LuanFunction fn; |
291 Luan luan; | 291 Luan luan; |
292 synchronized(luanInit) { | 292 synchronized(luanInit) { |
293 enableLoad("luan:http/Http.luan"); | 293 enableLoad("luan:http/Http.luan"); |
294 LuanTable module = (LuanTable)currentLuan.require("luan:http/Http.luan"); | 294 LuanTable module = (LuanTable)currentLuan.require("luan:http/Http.luan"); |
295 fn = module.fn("not_found_handler"); | 295 fn = module.fn(currentLuan,"not_found_handler"); |
296 if( fn == null ) | 296 if( fn == null ) |
297 return null; | 297 return null; |
298 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); | 298 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); |
299 luan = (Luan)cloner.clone(currentLuan); | 299 luan = (Luan)cloner.clone(currentLuan); |
300 fn = (LuanFunction)cloner.get(fn); | 300 fn = (LuanFunction)cloner.get(fn); |
301 } | 301 } |
302 LuanTable module = (LuanTable)luan.require("luan:http/Http.luan"); | 302 LuanTable module = (LuanTable)luan.require("luan:http/Http.luan"); |
303 module.fn("new_request").call(request); | 303 module.fn(luan,"new_request").call(request); |
304 module.fn("new_response").call(); | 304 module.fn(luan,"new_response").call(); |
305 Object obj = Luan.first(fn.call()); | 305 Object obj = Luan.first(fn.call()); |
306 if( !(obj instanceof Boolean) ) | 306 if( !(obj instanceof Boolean) ) |
307 throw new LuanException("not_found_handler must return boolean"); | 307 throw new LuanException("not_found_handler must return boolean"); |
308 boolean handled = (Boolean)obj; | 308 boolean handled = (Boolean)obj; |
309 return handled ? (Response)module.fn("finish").call() : null; | 309 return handled ? (Response)module.fn(luan,"finish").call() : null; |
310 } | 310 } |
311 | 311 |
312 private void enableLoad(String... mods) throws LuanException { | 312 private void enableLoad(String... mods) throws LuanException { |
313 LuanTable loaded = PackageLuan.loaded(currentLuan); | 313 LuanTable loaded = PackageLuan.loaded(currentLuan); |
314 for( String mod : mods ) { | 314 for( String mod : mods ) { |