Mercurial Hosting > luan
annotate src/luan/host/WebHandler.java @ 1202:d3a3ca116e42
gc site instances
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 05 Mar 2018 19:30:51 -0700 |
parents | 83c8a5a47f70 |
children | 7e6d132904fd |
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 java.io.IOException; | |
1202 | 5 import java.lang.ref.Reference; |
6 import java.lang.ref.SoftReference; | |
7 //import java.lang.ref.WeakReference; | |
1135 | 8 import java.util.Map; |
9 import java.util.HashMap; | |
10 import org.slf4j.Logger; | |
11 import org.slf4j.LoggerFactory; | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
12 import luan.webserver.Handler; |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
13 import luan.webserver.Server; |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
14 import luan.webserver.Request; |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
15 import luan.webserver.Response; |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
16 import luan.webserver.handlers.IndexHandler; |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
17 import luan.webserver.handlers.ListHandler; |
1135 | 18 import luan.Luan; |
19 import luan.LuanState; | |
20 import luan.LuanException; | |
21 import luan.LuanTable; | |
22 import luan.LuanFunction; | |
23 import luan.modules.IoLuan; | |
24 import luan.modules.JavaLuan; | |
25 import luan.modules.PackageLuan; | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
26 import luan.modules.http.LuanHandler; |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
27 import luan.modules.http.NotFound; |
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); |
32 | |
1202 | 33 public static String allowJavaFileName = "allow_java"; // change for security |
34 private static final Map<String,Reference<LuanState>> siteMap = new HashMap<String,Reference<LuanState>>(); | |
35 private static String sitesDir = null; | |
1135 | 36 |
1202 | 37 private static final String HANDLER = "WebHandler.handler"; |
38 private static final String LUAN_HANDLER = "WebHandler.luanHandler"; | |
1135 | 39 |
40 public static boolean isServing() { | |
41 return sitesDir != null; | |
42 } | |
43 | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
44 public WebHandler(String dir) { |
1135 | 45 if( sitesDir != null ) |
46 throw new RuntimeException("already set"); | |
47 if( !new File(dir).exists() ) | |
48 throw new RuntimeException(); | |
49 this.sitesDir = dir; | |
50 } | |
51 | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
52 public Response handle(Request request) { |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
53 String host = (String)request.headers.get("host"); |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
54 int i = host.indexOf(':'); |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
55 String domain = i == -1 ? host : host.substring(0,i); |
1135 | 56 // System.out.println("handle "+domain); |
1202 | 57 LuanState luan = getSite(domain); |
58 if( luan == null ) | |
59 return null; | |
60 Handler handler = (Handler)luan.registry().get(HANDLER); | |
61 return handler.handle(request); | |
1135 | 62 } |
63 | |
64 public static Object runLuan(String domain,String sourceText,String sourceName) throws LuanException { | |
1202 | 65 LuanState luan = getSite(domain); |
66 LuanHandler luanHandler = (LuanHandler)luan.registry().get(LUAN_HANDLER); | |
67 return luanHandler.runLuan(sourceText,sourceName); | |
1135 | 68 } |
69 | |
70 public static Object callSite(String domain,String fnName,Object... args) throws LuanException { | |
1202 | 71 LuanState luan = getSite(domain); |
72 LuanHandler luanHandler = (LuanHandler)luan.registry().get(LUAN_HANDLER); | |
73 return luanHandler.call_rpc(fnName,args); | |
1135 | 74 } |
75 | |
1202 | 76 private static LuanState getSite(String domain) { |
1135 | 77 synchronized(siteMap) { |
1202 | 78 Reference<LuanState> ref = siteMap.get(domain); |
79 LuanState luan = ref==null ? null : ref.get(); | |
80 if( luan == null ) { | |
81 if(ref!=null) logger.info("gc "+domain); | |
1135 | 82 if( sitesDir==null ) |
83 throw new NullPointerException("sitesDir"); | |
84 File dir = new File(sitesDir,domain); | |
85 if( !dir.exists() /* && !recover(dir) */ ) | |
86 return null; | |
1202 | 87 luan = newSite(dir.toString(),domain); |
88 siteMap.put(domain,new SoftReference<LuanState>(luan)); | |
1135 | 89 } |
1202 | 90 return luan; |
1135 | 91 } |
92 } | |
93 /* | |
94 private static boolean recover(File dir) { | |
95 File backups = new File(dir.getParentFile().getParentFile(),"backups"); | |
96 if( !backups.exists() ) | |
97 return false; | |
98 String name = dir.getName(); | |
99 File from = null; | |
100 for( File backup : backups.listFiles() ) { | |
101 File d = new File(backup,"current/"+name); | |
102 if( d.exists() && (from==null || from.lastModified() < d.lastModified()) ) | |
103 from = d; | |
104 } | |
105 if( from == null ) | |
106 return false; | |
107 if( !from.renameTo(dir) ) | |
108 throw new RuntimeException("couldn't rename "+from+" to "+dir); | |
109 logger.info("recovered "+name+" from "+from); | |
110 return true; | |
111 } | |
112 */ | |
113 static LuanTable initLuan(LuanState luan,String dir,String domain) { | |
114 LuanTable init; | |
115 try { | |
116 init = (LuanTable)luan.eval( | |
117 "local Luan = require 'luan:Luan.luan'\n" | |
118 +"local f = Luan.load_file 'classpath:luan/host/Init.luan'\n" | |
119 +"return f('"+dir+"','"+domain+"')\n" | |
120 ); | |
121 } catch(LuanException e) { | |
122 throw new RuntimeException(e); | |
123 } | |
124 File allowJavaFile = new File(dir,"site/private/"+allowJavaFileName); | |
125 if( !allowJavaFile.exists() ) { | |
126 JavaLuan.setSecurity( luan, javaSecurity ); | |
127 IoLuan.setSecurity( luan, ioSecurity(dir) ); | |
128 } | |
129 return init; | |
130 } | |
131 | |
1202 | 132 private static LuanState newSite(String dir,String domain) { |
1135 | 133 LuanState luan = new LuanState(); |
134 LuanTable init = initLuan(luan,dir,domain); | |
135 | |
136 String loggerRoot = (String)init.rawGet("logger_root"); | |
137 LuanHandler luanHandler = new LuanHandler(luan,loggerRoot); | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
138 NotFound notFoundHandler = new NotFound(luanHandler); |
1135 | 139 |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
140 Handler handler = luanHandler; |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
141 handler = new IndexHandler(handler); |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
142 handler = new ListHandler( handler, notFoundHandler ); |
1135 | 143 |
1202 | 144 String logDir = dir + "/site/private/local/logs/web"; |
1135 | 145 new File(logDir).mkdirs(); |
146 | |
1202 | 147 luan.registry().put(HANDLER,handler); |
148 luan.registry().put(LUAN_HANDLER,luanHandler); | |
149 | |
150 return luan; | |
1135 | 151 } |
152 | |
153 public static void removeHandler(String domain) throws Exception { | |
154 synchronized(siteMap) { | |
1202 | 155 Reference<LuanState> ref = siteMap.remove(domain); |
156 LuanState luan = ref==null ? null : ref.get(); | |
157 if( luan != null ) { | |
158 LuanHandler luanHandler = (LuanHandler)luan.registry().get(LUAN_HANDLER); | |
159 luanHandler.close(); | |
1135 | 160 } |
161 } | |
162 } | |
163 | |
164 public static void loadHandler(String domain) { | |
165 getSite(domain); | |
166 } | |
167 | |
168 private static final IoLuan.Security ioSecurity(String dir) { | |
1174
bdf27aa2a65c
fix luanhost security bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1136
diff
changeset
|
169 final String siteUri = "file:" + dir + "/site"; |
1135 | 170 return new IoLuan.Security() { |
171 public void check(LuanState luan,String name) throws LuanException { | |
172 if( name.startsWith("file:") ) { | |
173 if( name.contains("..") ) | |
174 throw new LuanException("Security violation - '"+name+"' contains '..'"); | |
1174
bdf27aa2a65c
fix luanhost security bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1136
diff
changeset
|
175 if( !(name.equals(siteUri) || name.startsWith(siteUri+"/")) ) |
1135 | 176 throw new LuanException("Security violation - '"+name+"' outside of site dir"); |
177 } | |
178 else if( name.startsWith("classpath:luan/host/") ) { | |
179 throw new LuanException("Security violation"); | |
180 } | |
181 else if( name.startsWith("os:") || name.startsWith("bash:") ) { | |
182 throw new LuanException("Security violation"); | |
183 } | |
184 } | |
185 }; | |
186 } | |
187 | |
188 private static final JavaLuan.Security javaSecurity = new JavaLuan.Security() { | |
189 public void check(LuanState luan,String name) throws LuanException { | |
190 if( !name.startsWith("luan:") ) | |
191 throw new LuanException("Security violation - only luan:* modules can load Java"); | |
192 if( name.equals("luan:logging/Logging") ) | |
193 throw new LuanException("Security violation - cannot reload Logging"); | |
194 } | |
195 }; | |
196 } |