Mercurial Hosting > luan
comparison http/src/luan/modules/http/HttpServicer.java @ 505:7bc63886d4f2
web page modules now return a function
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 May 2015 01:45:49 -0600 |
parents | dbdf4b8193a8 |
children | 7d1afc73f835 |
comparison
equal
deleted
inserted
replaced
504:dbdf4b8193a8 | 505:7bc63886d4f2 |
---|---|
50 LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http"); | 50 LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http"); |
51 LuanTable per_session_pages = (LuanTable)module.rawGet("per_session_pages"); | 51 LuanTable per_session_pages = (LuanTable)module.rawGet("per_session_pages"); |
52 Object mod = PackageLuan.load(luan,modName); | 52 Object mod = PackageLuan.load(luan,modName); |
53 if( mod==null ) | 53 if( mod==null ) |
54 return false; | 54 return false; |
55 if( !(mod instanceof LuanTable) ) | 55 if( !(mod instanceof LuanFunction) ) |
56 throw luan.exception( "module '"+modName+"' must return a table" ); | 56 throw luan.exception( "module '"+modName+"' must return a function" ); |
57 LuanTable tbl = (LuanTable)mod; | |
58 if( Boolean.TRUE.equals(per_session_pages.rawGet(mod)) ) { | 57 if( Boolean.TRUE.equals(per_session_pages.rawGet(mod)) ) { |
59 HttpSession session = request.getSession(); | 58 HttpSession session = request.getSession(); |
60 LuanState sessionLuan = (LuanState)session.getValue("luan"); | 59 LuanState sessionLuan = (LuanState)session.getValue("luan"); |
61 if( sessionLuan!=null ) { | 60 if( sessionLuan!=null ) { |
62 luan = sessionLuan; | 61 luan = sessionLuan; |
63 } else { | 62 } else { |
64 DeepCloner cloner = new DeepCloner(); | 63 DeepCloner cloner = new DeepCloner(); |
65 luan = (LuanState)cloner.deepClone(luan); | 64 luan = (LuanState)cloner.deepClone(luan); |
66 session.putValue("luan",luan); | 65 session.putValue("luan",luan); |
67 } | 66 } |
68 tbl = (LuanTable)PackageLuan.require(luan,modName); | 67 fn = (LuanFunction)PackageLuan.require(luan,modName); |
69 fn = getService(luan,tbl); | |
70 } else { | 68 } else { |
71 fn = getService(luan,tbl); | |
72 DeepCloner cloner = new DeepCloner(); | 69 DeepCloner cloner = new DeepCloner(); |
73 luan = (LuanState)cloner.deepClone(luan); | 70 luan = (LuanState)cloner.deepClone(luan); |
74 fn = (LuanFunction)cloner.get(fn); | 71 fn = (LuanFunction)cloner.get(mod); |
75 } | 72 } |
76 } | 73 } |
77 | 74 |
78 LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http"); | 75 LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http"); |
79 | 76 |
162 luan.call(fn,"<http>"); | 159 luan.call(fn,"<http>"); |
163 setResponse(module,response); | 160 setResponse(module,response); |
164 return true; | 161 return true; |
165 } | 162 } |
166 | 163 |
167 private static LuanFunction getService(LuanState luan,LuanTable tbl) | |
168 throws LuanException | |
169 { | |
170 Object respond = tbl.get(luan,"respond"); | |
171 if( respond == null ) | |
172 throw luan.exception( "function 'respond' is not defined" ); | |
173 if( !(respond instanceof LuanFunction) ) | |
174 throw luan.exception( "'respond' must be a function but is a " + Luan.type(respond) ); | |
175 return (LuanFunction)respond; | |
176 } | |
177 | |
178 private static void setResponse(LuanTable module,HttpServletResponse response) throws LuanException { | 164 private static void setResponse(LuanTable module,HttpServletResponse response) throws LuanException { |
179 LuanTable responseTbl = (LuanTable)module.rawGet("response"); | 165 LuanTable responseTbl = (LuanTable)module.rawGet("response"); |
180 int status = Luan.asInteger(responseTbl.rawGet("status")); | 166 int status = Luan.asInteger(responseTbl.rawGet("status")); |
181 response.setStatus(status); | 167 response.setStatus(status); |
182 LuanTable responseHeaders = (LuanTable)responseTbl.rawGet("headers"); | 168 LuanTable responseHeaders = (LuanTable)responseTbl.rawGet("headers"); |