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