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