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