Mercurial Hosting > luan
annotate src/luan/host/WebHandler.java @ 1454:219f2b937f2b
remove log4j
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 08 Mar 2020 14:11:30 -0600 |
parents | b6030fa18839 |
children | e66e3d50b289 |
rev | line source |
---|---|
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
1 package luan.host; |
1135 | 2 |
3 import java.io.File; | |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1400
diff
changeset
|
4 import goodjava.logging.Logger; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1400
diff
changeset
|
5 import goodjava.logging.LoggerFactory; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1400
diff
changeset
|
6 import goodjava.webserver.Handler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1400
diff
changeset
|
7 import goodjava.webserver.Request; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1400
diff
changeset
|
8 import goodjava.webserver.Response; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1400
diff
changeset
|
9 import goodjava.webserver.handlers.DomainHandler; |
1330 | 10 import luan.Luan; |
1135 | 11 import luan.LuanException; |
12 import luan.LuanTable; | |
1330 | 13 import luan.LuanFunction; |
14 import luan.LuanClosure; | |
1335 | 15 import luan.LuanRuntimeException; |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
16 import luan.modules.http.LuanHandler; |
1454 | 17 import luan.modules.logging.LuanLogger; |
1135 | 18 |
19 | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
20 public class WebHandler implements Handler { |
1135 | 21 private static final Logger logger = LoggerFactory.getLogger(WebHandler.class); |
22 | |
1315 | 23 private static final DomainHandler.Factory factory = new DomainHandler.Factory() { |
24 public Handler newHandler(String domain) { | |
25 File dir = new File(sitesDir,domain); | |
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1392
diff
changeset
|
26 if( !dir.exists() ) |
1315 | 27 return null; |
28 String dirStr = dir.toString(); | |
1230 | 29 |
1315 | 30 String logDir = dirStr + "/site/private/local/logs/web"; |
31 new File(logDir).mkdirs(); | |
1230 | 32 |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
33 Luan luan = new Luan(); |
1392 | 34 initLuan(luan,dirStr,domain); |
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1392
diff
changeset
|
35 return new LuanHandler(luan,domain); |
1230 | 36 } |
1315 | 37 }; |
1230 | 38 |
1330 | 39 public static String securityPassword = "password"; // change for security |
1315 | 40 private static final DomainHandler domainHandler = new DomainHandler(factory); |
1202 | 41 private static String sitesDir = null; |
1135 | 42 |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
43 public WebHandler(String dir) { |
1135 | 44 if( sitesDir != null ) |
45 throw new RuntimeException("already set"); | |
46 if( !new File(dir).exists() ) | |
47 throw new RuntimeException(); | |
1230 | 48 sitesDir = dir; |
1454 | 49 LuanLogger.initThreadLogging(); |
1135 | 50 } |
51 | |
1315 | 52 @Override public Response handle(Request request) { |
53 return domainHandler.handle(request); | |
1135 | 54 } |
55 | |
56 public static Object callSite(String domain,String fnName,Object... args) throws LuanException { | |
1315 | 57 LuanHandler luanHandler = (LuanHandler)domainHandler.getHandler(domain); |
58 return luanHandler.call_rpc(fnName,args); | |
1135 | 59 } |
60 | |
1392 | 61 private static void initLuan(Luan luan,String dir,String domain) { |
1454 | 62 LuanLogger.startThreadLogging(luan); |
1135 | 63 try { |
1424 | 64 LuanFunction fn = Luan.loadClasspath(luan,"luan/host/init.luan"); |
1392 | 65 fn.call(dir,domain); |
1135 | 66 } catch(LuanException e) { |
1330 | 67 throw new LuanRuntimeException(e); |
1454 | 68 } finally { |
69 LuanLogger.endThreadLogging(); | |
1135 | 70 } |
1425 | 71 security(luan,dir); |
1135 | 72 } |
73 | |
1315 | 74 public static void removeHandler(String domain) { |
75 domainHandler.removeHandler(domain); | |
1135 | 76 } |
77 | |
1330 | 78 public static void loadHandler(String domain) throws LuanException { |
79 try { | |
80 domainHandler.getHandler(domain); | |
81 } catch(LuanRuntimeException e) { | |
1335 | 82 throw (LuanException)e.getCause(); |
1330 | 83 } |
1135 | 84 } |
85 | |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
86 private static final void security(Luan luan,String dir) { |
1174
bdf27aa2a65c
fix luanhost security bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1136
diff
changeset
|
87 final String siteUri = "file:" + dir + "/site"; |
1330 | 88 Luan.Security security = new Luan.Security() { |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
89 public void check(Luan luan,LuanClosure closure,String op,Object... args) |
1330 | 90 throws LuanException |
91 { | |
92 if( op.equals("uri") ) { | |
93 String name = (String)args[0]; | |
94 if( name.startsWith("file:") ) { | |
95 if( name.contains("..") ) | |
96 throw new LuanException("Security violation - '"+name+"' contains '..'"); | |
97 if( !(name.equals(siteUri) || name.startsWith(siteUri+"/")) ) | |
98 throw new LuanException("Security violation - '"+name+"' outside of site dir"); | |
99 } | |
100 else if( name.startsWith("classpath:luan/host/") ) { | |
101 throw new LuanException("Security violation"); | |
102 } | |
103 else if( name.startsWith("os:") || name.startsWith("bash:") ) { | |
104 throw new LuanException("Security violation"); | |
105 } | |
106 } else { | |
107 String name = closure.sourceName; | |
108 if( !( | |
109 name.startsWith("luan:") | |
110 || name.startsWith("classpath:") | |
111 || name.matches("^file:[^/]+$") | |
112 ) ) | |
113 throw new LuanException("Security violation - only luan:* modules can load Java"); | |
114 if( name.equals("luan:logging/Logging") ) | |
115 throw new LuanException("Security violation - cannot reload Logging"); | |
1135 | 116 } |
117 } | |
118 }; | |
1330 | 119 Luan.setSecurity(luan,security); |
1135 | 120 } |
121 | |
122 } |