Mercurial Hosting > luan
annotate src/luan/host/WebHandler.java @ 1333:25746915a241
merge Luan and LuanState
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 12 Feb 2019 22:33:40 -0700 |
parents | 11b7e11f9ed5 |
children | e0cf0d108a77 |
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; | |
4 import org.slf4j.Logger; | |
5 import org.slf4j.LoggerFactory; | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
6 import luan.webserver.Handler; |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
7 import luan.webserver.Request; |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
8 import luan.webserver.Response; |
1315 | 9 import luan.webserver.handlers.DomainHandler; |
1330 | 10 import luan.Luan; |
1135 | 11 import luan.LuanException; |
12 import luan.LuanTable; | |
1330 | 13 import luan.LuanFunction; |
14 import luan.LuanClosure; | |
15 import luan.modules.BasicLuan; | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
16 import luan.modules.http.LuanHandler; |
1332 | 17 import luan.modules.logging.Log4j; |
1135 | 18 |
19 | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
20 public class WebHandler implements Handler { |
1135 | 21 private static final Logger logger = LoggerFactory.getLogger(WebHandler.class); |
22 | |
1330 | 23 private static final class LuanRuntimeException extends RuntimeException { |
24 final LuanException e; | |
25 | |
26 LuanRuntimeException(LuanException e) { | |
27 this.e = e; | |
28 } | |
29 } | |
30 | |
1315 | 31 private static final DomainHandler.Factory factory = new DomainHandler.Factory() { |
32 public Handler newHandler(String domain) { | |
33 File dir = new File(sitesDir,domain); | |
34 if( !dir.exists() /* && !recover(dir) */ ) | |
35 return null; | |
36 String dirStr = dir.toString(); | |
1230 | 37 |
1315 | 38 String logDir = dirStr + "/site/private/local/logs/web"; |
39 new File(logDir).mkdirs(); | |
1230 | 40 |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
41 Luan luan = new Luan(); |
1321
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
42 try { |
1332 | 43 Log4j.newLoggerRepository(luan); |
1321
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
44 } catch(LuanException e) { |
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
45 throw new RuntimeException(e); |
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
46 } |
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
47 initLuan(luan,dirStr,domain,true); |
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
48 return new LuanHandler(luan); |
1230 | 49 } |
1315 | 50 }; |
1230 | 51 |
1330 | 52 public static String securityPassword = "password"; // change for security |
1315 | 53 private static final DomainHandler domainHandler = new DomainHandler(factory); |
1202 | 54 private static String sitesDir = null; |
1135 | 55 |
56 public static boolean isServing() { | |
57 return sitesDir != null; | |
58 } | |
59 | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
60 public WebHandler(String dir) { |
1135 | 61 if( sitesDir != null ) |
62 throw new RuntimeException("already set"); | |
63 if( !new File(dir).exists() ) | |
64 throw new RuntimeException(); | |
1230 | 65 sitesDir = dir; |
1135 | 66 } |
67 | |
1315 | 68 @Override public Response handle(Request request) { |
69 return domainHandler.handle(request); | |
1135 | 70 } |
71 | |
72 public static Object runLuan(String domain,String sourceText,String sourceName) throws LuanException { | |
1315 | 73 LuanHandler luanHandler = (LuanHandler)domainHandler.getHandler(domain); |
74 return luanHandler.runLuan(sourceText,sourceName); | |
1135 | 75 } |
76 | |
77 public static Object callSite(String domain,String fnName,Object... args) throws LuanException { | |
1315 | 78 LuanHandler luanHandler = (LuanHandler)domainHandler.getHandler(domain); |
79 return luanHandler.call_rpc(fnName,args); | |
1135 | 80 } |
81 | |
82 /* | |
83 private static boolean recover(File dir) { | |
84 File backups = new File(dir.getParentFile().getParentFile(),"backups"); | |
85 if( !backups.exists() ) | |
86 return false; | |
87 String name = dir.getName(); | |
88 File from = null; | |
89 for( File backup : backups.listFiles() ) { | |
90 File d = new File(backup,"current/"+name); | |
91 if( d.exists() && (from==null || from.lastModified() < d.lastModified()) ) | |
92 from = d; | |
93 } | |
94 if( from == null ) | |
95 return false; | |
96 if( !from.renameTo(dir) ) | |
97 throw new RuntimeException("couldn't rename "+from+" to "+dir); | |
98 logger.info("recovered "+name+" from "+from); | |
99 return true; | |
100 } | |
101 */ | |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
102 static void initLuan(Luan luan,String dir,String domain,boolean logging) { |
1330 | 103 security(luan,dir); |
1135 | 104 try { |
1330 | 105 LuanFunction fn = BasicLuan.load_file(luan,"classpath:luan/host/init.luan"); |
106 fn.call(luan,new Object[]{dir,domain,logging}); | |
1135 | 107 } catch(LuanException e) { |
1330 | 108 throw new LuanRuntimeException(e); |
1135 | 109 } |
110 } | |
111 | |
1315 | 112 public static void removeHandler(String domain) { |
113 domainHandler.removeHandler(domain); | |
1135 | 114 } |
115 | |
1330 | 116 public static void loadHandler(String domain) throws LuanException { |
117 try { | |
118 domainHandler.getHandler(domain); | |
119 } catch(LuanRuntimeException e) { | |
120 throw e.e; | |
121 } | |
1135 | 122 } |
123 | |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
124 private static final void security(Luan luan,String dir) { |
1174
bdf27aa2a65c
fix luanhost security bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1136
diff
changeset
|
125 final String siteUri = "file:" + dir + "/site"; |
1330 | 126 Luan.Security security = new Luan.Security() { |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
127 public void check(Luan luan,LuanClosure closure,String op,Object... args) |
1330 | 128 throws LuanException |
129 { | |
130 if( op.equals("uri") ) { | |
131 String name = (String)args[0]; | |
132 if( name.startsWith("file:") ) { | |
133 if( name.contains("..") ) | |
134 throw new LuanException("Security violation - '"+name+"' contains '..'"); | |
135 if( !(name.equals(siteUri) || name.startsWith(siteUri+"/")) ) | |
136 throw new LuanException("Security violation - '"+name+"' outside of site dir"); | |
137 } | |
138 else if( name.startsWith("classpath:luan/host/") ) { | |
139 throw new LuanException("Security violation"); | |
140 } | |
141 else if( name.startsWith("os:") || name.startsWith("bash:") ) { | |
142 throw new LuanException("Security violation"); | |
143 } | |
144 } else { | |
145 String name = closure.sourceName; | |
146 if( !( | |
147 name.startsWith("luan:") | |
148 || name.startsWith("classpath:") | |
149 || name.matches("^file:[^/]+$") | |
150 ) ) | |
151 throw new LuanException("Security violation - only luan:* modules can load Java"); | |
152 if( name.equals("luan:logging/Logging") ) | |
153 throw new LuanException("Security violation - cannot reload Logging"); | |
1135 | 154 } |
155 } | |
156 }; | |
1330 | 157 Luan.setSecurity(luan,security); |
1135 | 158 } |
159 | |
160 } |