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