Mercurial Hosting > luan
comparison src/luan/modules/http/HttpServicer.java @ 1263:382c444a6c77
add Http.eval_in_root and Http.handle_error
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 24 Sep 2018 22:06:25 -0600 |
parents | c147e2e877e3 |
children | 3f4644246e39 |
comparison
equal
deleted
inserted
replaced
1262:81d3a01fbd09 | 1263:382c444a6c77 |
---|---|
11 import luan.LuanTable; | 11 import luan.LuanTable; |
12 import luan.LuanCloner; | 12 import luan.LuanCloner; |
13 import luan.modules.PackageLuan; | 13 import luan.modules.PackageLuan; |
14 | 14 |
15 | 15 |
16 public class HttpServicer { | 16 public final class HttpServicer { |
17 private static final Logger logger = LoggerFactory.getLogger(HttpServicer.class); | 17 private static final Logger logger = LoggerFactory.getLogger(HttpServicer.class); |
18 | 18 |
19 public Response service(LuanState luan,Request request,String modName) { | 19 public static Response service(LuanState luan,Request request,String modName) { |
20 try { | 20 try { |
21 return serviceLuan(luan,request,modName); | 21 return serviceLuan(luan,request,modName); |
22 } catch(LuanException e) { | 22 } catch(LuanException e) { |
23 return handleError(request,e); | 23 return handleError(luan,request,e); |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 protected Response handleError(Request request,LuanException e) { | 27 private static Response handleError(LuanState luan,Request request,LuanException e) { |
28 //e.printStackTrace(); | 28 //e.printStackTrace(); |
29 /* | |
29 String err = e.getLuanStackTraceString(); | 30 String err = e.getLuanStackTraceString(); |
30 logger.error(err+"\n"+request.rawHead.trim()+"\n"); | 31 logger.error(err+"\n"+request.rawHead.trim()+"\n"); |
31 String msg = "Internel Server Error\n\n" + err; | 32 String msg = "Internel Server Error\n\n" + err; |
32 return Response.errorResponse( Status.INTERNAL_SERVER_ERROR, msg ); | 33 return Response.errorResponse( Status.INTERNAL_SERVER_ERROR, msg ); |
34 */ | |
35 synchronized(luan) { | |
36 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); | |
37 luan = (LuanState)cloner.clone(luan); | |
38 } | |
39 try { | |
40 LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan"); | |
41 LuanFunction fn = (LuanFunction)module.rawGet("handle_error"); | |
42 return (Response)fn.call( luan, new Object[]{request,e.table()} ); | |
43 } catch(LuanException e2) { | |
44 throw new RuntimeException(e2); | |
45 } | |
33 } | 46 } |
34 | 47 |
35 protected Response serviceLuan(LuanState luan,Request request,String modName) | 48 private static Response serviceLuan(LuanState luan,Request request,String modName) |
36 throws LuanException | 49 throws LuanException |
37 { | 50 { |
38 LuanFunction fn; | 51 LuanFunction fn; |
39 synchronized(luan) { | 52 synchronized(luan) { |
40 inSinchronized(luan,request); | |
41 PackageLuan.enableLoad(luan,"luan:http/Http.luan",modName); | 53 PackageLuan.enableLoad(luan,"luan:http/Http.luan",modName); |
42 PackageLuan.require(luan,"luan:http/Http.luan"); | 54 PackageLuan.require(luan,"luan:http/Http.luan"); |
43 Object mod = PackageLuan.load(luan,modName); | 55 Object mod = PackageLuan.load(luan,modName); |
44 if( mod.equals(Boolean.FALSE) ) | 56 if( mod.equals(Boolean.FALSE) ) |
45 return null; | 57 return null; |
65 LuanFunction finishFn = (LuanFunction)module.rawGet("finish"); | 77 LuanFunction finishFn = (LuanFunction)module.rawGet("finish"); |
66 Response response = (Response)finishFn.call(luan); | 78 Response response = (Response)finishFn.call(luan); |
67 return response; | 79 return response; |
68 } | 80 } |
69 | 81 |
70 protected void inSinchronized(LuanState luan,Request request) | 82 private HttpServicer() {} // never |
71 throws LuanException | |
72 {} | |
73 | |
74 } | 83 } |