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