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