Mercurial Hosting > luan
annotate src/luan/host/WebHandler.java @ 1392:002152af497a
hosted postgres
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 06 Sep 2019 00:19:47 -0600 |
parents | 5c3702f60200 |
children | 221eedb0f54e |
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; | |
1337 | 4 import luan.lib.logging.Logger; |
5 import luan.lib.logging.LoggerFactory; | |
1347
643cf1c37723
move webserver to lib and bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents:
1337
diff
changeset
|
6 import luan.lib.webserver.Handler; |
643cf1c37723
move webserver to lib and bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents:
1337
diff
changeset
|
7 import luan.lib.webserver.Request; |
643cf1c37723
move webserver to lib and bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents:
1337
diff
changeset
|
8 import luan.lib.webserver.Response; |
643cf1c37723
move webserver to lib and bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents:
1337
diff
changeset
|
9 import luan.lib.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(); |
1336 | 35 Log4j.newLoggerRepository(luan); |
1392 | 36 initLuan(luan,dirStr,domain); |
1321
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
37 return new LuanHandler(luan); |
1230 | 38 } |
1315 | 39 }; |
1230 | 40 |
1330 | 41 public static String securityPassword = "password"; // change for security |
1315 | 42 private static final DomainHandler domainHandler = new DomainHandler(factory); |
1202 | 43 private static String sitesDir = null; |
1135 | 44 |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
45 public WebHandler(String dir) { |
1135 | 46 if( sitesDir != null ) |
47 throw new RuntimeException("already set"); | |
48 if( !new File(dir).exists() ) | |
49 throw new RuntimeException(); | |
1230 | 50 sitesDir = dir; |
1135 | 51 } |
52 | |
1315 | 53 @Override public Response handle(Request request) { |
54 return domainHandler.handle(request); | |
1135 | 55 } |
56 | |
57 public static Object callSite(String domain,String fnName,Object... args) throws LuanException { | |
1315 | 58 LuanHandler luanHandler = (LuanHandler)domainHandler.getHandler(domain); |
59 return luanHandler.call_rpc(fnName,args); | |
1135 | 60 } |
61 | |
62 /* | |
63 private static boolean recover(File dir) { | |
64 File backups = new File(dir.getParentFile().getParentFile(),"backups"); | |
65 if( !backups.exists() ) | |
66 return false; | |
67 String name = dir.getName(); | |
68 File from = null; | |
69 for( File backup : backups.listFiles() ) { | |
70 File d = new File(backup,"current/"+name); | |
71 if( d.exists() && (from==null || from.lastModified() < d.lastModified()) ) | |
72 from = d; | |
73 } | |
74 if( from == null ) | |
75 return false; | |
76 if( !from.renameTo(dir) ) | |
77 throw new RuntimeException("couldn't rename "+from+" to "+dir); | |
78 logger.info("recovered "+name+" from "+from); | |
79 return true; | |
80 } | |
81 */ | |
1392 | 82 private static void initLuan(Luan luan,String dir,String domain) { |
1330 | 83 security(luan,dir); |
1135 | 84 try { |
1330 | 85 LuanFunction fn = BasicLuan.load_file(luan,"classpath:luan/host/init.luan"); |
1392 | 86 fn.call(dir,domain); |
1135 | 87 } catch(LuanException e) { |
1330 | 88 throw new LuanRuntimeException(e); |
1135 | 89 } |
90 } | |
91 | |
1315 | 92 public static void removeHandler(String domain) { |
93 domainHandler.removeHandler(domain); | |
1135 | 94 } |
95 | |
1330 | 96 public static void loadHandler(String domain) throws LuanException { |
97 try { | |
98 domainHandler.getHandler(domain); | |
99 } catch(LuanRuntimeException e) { | |
1335 | 100 throw (LuanException)e.getCause(); |
1330 | 101 } |
1135 | 102 } |
103 | |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
104 private static final void security(Luan luan,String dir) { |
1174
bdf27aa2a65c
fix luanhost security bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1136
diff
changeset
|
105 final String siteUri = "file:" + dir + "/site"; |
1330 | 106 Luan.Security security = new Luan.Security() { |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1332
diff
changeset
|
107 public void check(Luan luan,LuanClosure closure,String op,Object... args) |
1330 | 108 throws LuanException |
109 { | |
110 if( op.equals("uri") ) { | |
111 String name = (String)args[0]; | |
112 if( name.startsWith("file:") ) { | |
113 if( name.contains("..") ) | |
114 throw new LuanException("Security violation - '"+name+"' contains '..'"); | |
115 if( !(name.equals(siteUri) || name.startsWith(siteUri+"/")) ) | |
116 throw new LuanException("Security violation - '"+name+"' outside of site dir"); | |
117 } | |
118 else if( name.startsWith("classpath:luan/host/") ) { | |
119 throw new LuanException("Security violation"); | |
120 } | |
121 else if( name.startsWith("os:") || name.startsWith("bash:") ) { | |
122 throw new LuanException("Security violation"); | |
123 } | |
124 } else { | |
125 String name = closure.sourceName; | |
126 if( !( | |
127 name.startsWith("luan:") | |
128 || name.startsWith("classpath:") | |
129 || name.matches("^file:[^/]+$") | |
130 ) ) | |
131 throw new LuanException("Security violation - only luan:* modules can load Java"); | |
132 if( name.equals("luan:logging/Logging") ) | |
133 throw new LuanException("Security violation - cannot reload Logging"); | |
1135 | 134 } |
135 } | |
136 }; | |
1330 | 137 Luan.setSecurity(luan,security); |
1135 | 138 } |
139 | |
140 } |