Mercurial Hosting > luan
comparison src/luan/modules/http/LuanDomainHandler.java @ 1329:5a39b006acd1
make LuanDomainHandler flexible
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 10 Feb 2019 02:01:49 -0700 |
parents | 55b8210b4ec0 |
children | 11b7e11f9ed5 |
comparison
equal
deleted
inserted
replaced
1328:55b8210b4ec0 | 1329:5a39b006acd1 |
---|---|
1 package luan.modules.http; | 1 package luan.modules.http; |
2 | 2 |
3 import org.apache.log4j.MDC; | |
4 import luan.webserver.Request; | 3 import luan.webserver.Request; |
5 import luan.webserver.Response; | 4 import luan.webserver.Response; |
6 import luan.webserver.Handler; | 5 import luan.webserver.Handler; |
7 import luan.webserver.handlers.DomainHandler; | 6 import luan.webserver.handlers.DomainHandler; |
8 import luan.LuanState; | 7 import luan.LuanState; |
10 import luan.LuanCloner; | 9 import luan.LuanCloner; |
11 import luan.LuanException; | 10 import luan.LuanException; |
12 import luan.modules.logging.LuanLogger; | 11 import luan.modules.logging.LuanLogger; |
13 | 12 |
14 | 13 |
15 public final class LuanDomainHandler implements Handler { | 14 public class LuanDomainHandler implements Handler, DomainHandler.Factory { |
16 | 15 |
17 private final LuanState luanInit; | 16 private final LuanState luanInit; |
18 | 17 private final DomainHandler domainHandler = new DomainHandler(this); |
19 private final DomainHandler.Factory factory = new DomainHandler.Factory() { | |
20 public Handler newHandler(String domain) { | |
21 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); | |
22 LuanState luan = (LuanState)cloner.clone(luanInit); | |
23 try { | |
24 LuanLogger.newLoggerRepository(luan); | |
25 LuanTable Http = (LuanTable)luan.require("luan:http/Http.luan"); | |
26 Http.put( "domain", domain ); | |
27 } catch(LuanException e) { | |
28 throw new RuntimeException(e); | |
29 } | |
30 return new LuanHandler(luan); | |
31 } | |
32 }; | |
33 | |
34 private final DomainHandler domainHandler = new DomainHandler(factory); | |
35 | 18 |
36 public LuanDomainHandler(LuanState luanInit) { | 19 public LuanDomainHandler(LuanState luanInit) { |
37 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); | 20 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); |
38 this.luanInit = (LuanState)cloner.clone(luanInit); | 21 this.luanInit = (LuanState)cloner.clone(luanInit); |
39 } | 22 } |
40 | 23 |
24 @Override public Handler newHandler(String domain) { | |
25 LuanState luan = newLuan(domain); | |
26 return new LuanHandler(luan); | |
27 } | |
28 | |
29 protected LuanState newLuan(String domain) { | |
30 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); | |
31 LuanState luan = (LuanState)cloner.clone(luanInit); | |
32 try { | |
33 LuanLogger.newLoggerRepository(luan); | |
34 LuanTable Http = (LuanTable)luan.require("luan:http/Http.luan"); | |
35 Http.put( "domain", domain ); | |
36 } catch(LuanException e) { | |
37 throw new RuntimeException(e); | |
38 } | |
39 return luan; | |
40 } | |
41 | |
41 @Override public Response handle(Request request) { | 42 @Override public Response handle(Request request) { |
42 String host = (String)request.headers.get("host"); | 43 return domainHandler.handle(request); |
43 MDC.put("host",host); | |
44 try { | |
45 return domainHandler.handle(request); | |
46 } finally { | |
47 MDC.remove("host"); | |
48 } | |
49 } | 44 } |
50 } | 45 } |