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