Mercurial Hosting > luan
comparison src/luan/modules/http/HttpServicer.java @ 1333:25746915a241
merge Luan and LuanState
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 12 Feb 2019 22:33:40 -0700 |
parents | 503bde9a7c80 |
children |
comparison
equal
deleted
inserted
replaced
1332:11b7e11f9ed5 | 1333:25746915a241 |
---|---|
2 | 2 |
3 import org.slf4j.Logger; | 3 import org.slf4j.Logger; |
4 import org.slf4j.LoggerFactory; | 4 import org.slf4j.LoggerFactory; |
5 import luan.webserver.Request; | 5 import luan.webserver.Request; |
6 import luan.webserver.Response; | 6 import luan.webserver.Response; |
7 import luan.LuanState; | 7 import luan.Luan; |
8 import luan.LuanFunction; | 8 import luan.LuanFunction; |
9 import luan.LuanException; | 9 import luan.LuanException; |
10 import luan.LuanTable; | 10 import luan.LuanTable; |
11 import luan.LuanCloner; | 11 import luan.LuanCloner; |
12 import luan.modules.PackageLuan; | 12 import luan.modules.PackageLuan; |
13 | 13 |
14 | 14 |
15 public final class HttpServicer { | 15 public final class HttpServicer { |
16 private static final Logger logger = LoggerFactory.getLogger(HttpServicer.class); | 16 private static final Logger logger = LoggerFactory.getLogger(HttpServicer.class); |
17 | 17 |
18 public static Response service(LuanState luan,Request request,String modName) | 18 public static Response service(Luan luan,Request request,String modName) |
19 throws LuanException | 19 throws LuanException |
20 { | 20 { |
21 try { | 21 try { |
22 return serviceLuan(luan,request,modName); | 22 return serviceLuan(luan,request,modName); |
23 } catch(LuanException e) { | 23 } catch(LuanException e) { |
24 return handleError(luan,request,e); | 24 return handleError(luan,request,e); |
25 } | 25 } |
26 } | 26 } |
27 | 27 |
28 private static Response handleError(LuanState luan,Request request,LuanException e) | 28 private static Response handleError(Luan luan,Request request,LuanException e) |
29 throws LuanException | 29 throws LuanException |
30 { | 30 { |
31 //e.printStackTrace(); | 31 //e.printStackTrace(); |
32 synchronized(luan) { | 32 synchronized(luan) { |
33 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); | 33 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); |
34 luan = (LuanState)cloner.clone(luan); | 34 luan = (Luan)cloner.clone(luan); |
35 } | 35 } |
36 LuanTable module = (LuanTable)luan.require("luan:http/Http.luan"); | 36 LuanTable module = (LuanTable)luan.require("luan:http/Http.luan"); |
37 return (Response)module.call( "handle_error", request, e.table(luan) ); | 37 return (Response)module.call( "handle_error", request, e.table(luan) ); |
38 } | 38 } |
39 | 39 |
40 private static Response serviceLuan(LuanState luan,Request request,String modName) | 40 private static Response serviceLuan(Luan luan,Request request,String modName) |
41 throws LuanException | 41 throws LuanException |
42 { | 42 { |
43 LuanFunction fn; | 43 LuanFunction fn; |
44 synchronized(luan) { | 44 synchronized(luan) { |
45 PackageLuan.enableLoad(luan,"luan:http/Http.luan",modName); | 45 PackageLuan.enableLoad(luan,"luan:http/Http.luan",modName); |
48 if( mod.equals(Boolean.FALSE) ) | 48 if( mod.equals(Boolean.FALSE) ) |
49 return null; | 49 return null; |
50 if( !(mod instanceof LuanFunction) ) | 50 if( !(mod instanceof LuanFunction) ) |
51 throw new LuanException( "module '"+modName+"' must return a function" ); | 51 throw new LuanException( "module '"+modName+"' must return a function" ); |
52 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); | 52 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); |
53 luan = (LuanState)cloner.clone(luan); | 53 luan = (Luan)cloner.clone(luan); |
54 fn = (LuanFunction)cloner.get(mod); | 54 fn = (LuanFunction)cloner.get(mod); |
55 } | 55 } |
56 | 56 |
57 LuanTable module = (LuanTable)luan.require("luan:http/Http.luan"); | 57 LuanTable module = (LuanTable)luan.require("luan:http/Http.luan"); |
58 module.call( "new_request", request ); | 58 module.call( "new_request", request ); |