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