Mercurial Hosting > luan
annotate src/luan/host/WebHandler.java @ 1598:c78d6a4e2d9a
web logging
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 04 Apr 2021 15:09:51 -0600 |
parents | c922446f53aa |
children | f2a663a4ba9e |
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); | |
68 if( luanLogging ) | |
69 handler = new LogHandler(handler,LogHandler.dirLogger(new File(logDir+"2"))); | |
70 | |
71 return new MyHandler(handler,luanHandler); | |
1230 | 72 } |
1315 | 73 }; |
1230 | 74 |
1330 | 75 public static String securityPassword = "password"; // change for security |
1598 | 76 public static boolean luanLogging = false; |
1315 | 77 private static final DomainHandler domainHandler = new DomainHandler(factory); |
1202 | 78 private static String sitesDir = null; |
1135 | 79 |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
80 public WebHandler(String dir) { |
1135 | 81 if( sitesDir != null ) |
82 throw new RuntimeException("already set"); | |
83 if( !new File(dir).exists() ) | |
84 throw new RuntimeException(); | |
1230 | 85 sitesDir = dir; |
1454 | 86 LuanLogger.initThreadLogging(); |
1135 | 87 } |
88 | |
1315 | 89 @Override public Response handle(Request request) { |
90 return domainHandler.handle(request); | |
1135 | 91 } |
92 | |
1578 | 93 public static Object callSite(String domain,String fnName,Object... args) throws LuanException { |
1598 | 94 MyHandler handler = (MyHandler)domainHandler.getHandler(domain); |
95 return handler.luanHandler.call_rpc(fnName,args); | |
1135 | 96 } |
97 | |
1392 | 98 private static void initLuan(Luan luan,String dir,String domain) { |
1454 | 99 LuanLogger.startThreadLogging(luan); |
1135 | 100 try { |
1424 | 101 LuanFunction fn = Luan.loadClasspath(luan,"luan/host/init.luan"); |
1563
8fbcc4747091
remove LuanFunction.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
102 fn.call(luan,dir,domain); |
1135 | 103 } catch(LuanException e) { |
1330 | 104 throw new LuanRuntimeException(e); |
1454 | 105 } finally { |
106 LuanLogger.endThreadLogging(); | |
1135 | 107 } |
1425 | 108 security(luan,dir); |
1135 | 109 } |
110 | |
1315 | 111 public static void removeHandler(String domain) { |
112 domainHandler.removeHandler(domain); | |
1135 | 113 } |
114 | |
1330 | 115 public static void loadHandler(String domain) throws LuanException { |
116 try { | |
117 domainHandler.getHandler(domain); | |
118 } catch(LuanRuntimeException e) { | |
1335 | 119 throw (LuanException)e.getCause(); |
1330 | 120 } |
1135 | 121 } |
122 | |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
123 private static final void security(Luan luan,String dir) { |
1174
bdf27aa2a65c
fix luanhost security bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1136
diff
changeset
|
124 final String siteUri = "file:" + dir + "/site"; |
1330 | 125 Luan.Security security = new Luan.Security() { |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
126 public void check(Luan luan,LuanClosure closure,String op,Object... args) |
1330 | 127 throws LuanException |
128 { | |
129 if( op.equals("uri") ) { | |
130 String name = (String)args[0]; | |
131 if( name.startsWith("file:") ) { | |
132 if( name.contains("..") ) | |
133 throw new LuanException("Security violation - '"+name+"' contains '..'"); | |
134 if( !(name.equals(siteUri) || name.startsWith(siteUri+"/")) ) | |
135 throw new LuanException("Security violation - '"+name+"' outside of site dir"); | |
136 } | |
137 else if( name.startsWith("classpath:luan/host/") ) { | |
138 throw new LuanException("Security violation"); | |
139 } | |
140 else if( name.startsWith("os:") || name.startsWith("bash:") ) { | |
141 throw new LuanException("Security violation"); | |
142 } | |
143 } else { | |
144 String name = closure.sourceName; | |
145 if( !( | |
146 name.startsWith("luan:") | |
147 || name.startsWith("classpath:") | |
148 || name.matches("^file:[^/]+$") | |
149 ) ) | |
150 throw new LuanException("Security violation - only luan:* modules can load Java"); | |
151 if( name.equals("luan:logging/Logging") ) | |
152 throw new LuanException("Security violation - cannot reload Logging"); | |
1135 | 153 } |
154 } | |
155 }; | |
1330 | 156 Luan.setSecurity(luan,security); |
1135 | 157 } |
158 | |
159 } |