Mercurial Hosting > luan
comparison src/luan/modules/http/HttpServicer.java @ 775:1a68fc55a80c
simplify dir structure
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 26 Aug 2016 14:36:40 -0600 |
parents | http/src/luan/modules/http/HttpServicer.java@9f58b398a892 |
children | fbbdd369a13a |
comparison
equal
deleted
inserted
replaced
774:3e30cf310e56 | 775:1a68fc55a80c |
---|---|
1 package luan.modules.http; | |
2 | |
3 import java.io.InputStream; | |
4 import java.io.BufferedInputStream; | |
5 import java.io.PrintWriter; | |
6 import java.io.IOException; | |
7 import java.util.Map; | |
8 import java.util.Set; | |
9 import java.util.List; | |
10 import java.util.ArrayList; | |
11 import java.util.Arrays; | |
12 import java.util.Iterator; | |
13 import java.util.Enumeration; | |
14 import java.util.concurrent.Executor; | |
15 import java.util.concurrent.Executors; | |
16 import javax.servlet.ServletOutputStream; | |
17 import javax.servlet.ServletException; | |
18 import javax.servlet.http.Cookie; | |
19 import javax.servlet.http.HttpServletRequest; | |
20 import javax.servlet.http.HttpServletResponse; | |
21 import javax.servlet.http.HttpSession; | |
22 import javax.servlet.http.Part; | |
23 import org.slf4j.Logger; | |
24 import org.slf4j.LoggerFactory; | |
25 import org.eclipse.jetty.util.MultiPartInputStream; | |
26 import luan.Luan; | |
27 import luan.LuanState; | |
28 import luan.LuanFunction; | |
29 import luan.LuanException; | |
30 import luan.LuanTable; | |
31 import luan.LuanMeta; | |
32 import luan.LuanPropertyMeta; | |
33 import luan.DeepCloner; | |
34 import luan.modules.PackageLuan; | |
35 import luan.modules.IoLuan; | |
36 import luan.modules.TableLuan; | |
37 import luan.modules.Utils; | |
38 import luan.modules.url.LuanUrl; | |
39 | |
40 | |
41 public final class HttpServicer { | |
42 private static final Logger logger = LoggerFactory.getLogger(HttpServicer.class); | |
43 | |
44 public static boolean service(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName) | |
45 throws LuanException | |
46 { | |
47 LuanFunction fn; | |
48 synchronized(luan) { | |
49 LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan"); | |
50 LuanTable per_session_pages = (LuanTable)module.rawGet("per_session_pages"); | |
51 Object mod = PackageLuan.load(luan,modName); | |
52 if( mod==null ) | |
53 return false; | |
54 if( !(mod instanceof LuanFunction) ) | |
55 throw new LuanException( "module '"+modName+"' must return a function" ); | |
56 if( Boolean.TRUE.equals(per_session_pages.rawGet(mod)) ) { | |
57 HttpSession session = request.getSession(); | |
58 LuanState sessionLuan = (LuanState)session.getAttribute("luan"); | |
59 if( sessionLuan!=null ) { | |
60 luan = sessionLuan; | |
61 } else { | |
62 DeepCloner cloner = new DeepCloner(); | |
63 luan = (LuanState)cloner.deepClone(luan); | |
64 session.setAttribute("luan",luan); | |
65 } | |
66 fn = (LuanFunction)PackageLuan.require(luan,modName); | |
67 } else { | |
68 DeepCloner cloner = new DeepCloner(); | |
69 luan = (LuanState)cloner.deepClone(luan); | |
70 fn = (LuanFunction)cloner.get(mod); | |
71 } | |
72 } | |
73 | |
74 LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan"); | |
75 | |
76 // request | |
77 LuanFunction newRequestFn = (LuanFunction)module.rawGet("new_request"); | |
78 LuanTable requestTbl = (LuanTable)newRequestFn.call(luan); | |
79 module.rawPut("request",requestTbl); | |
80 requestTbl.rawPut("java",request); | |
81 requestTbl.rawPut("method",request.getMethod()); | |
82 requestTbl.rawPut("path",request.getRequestURI()); | |
83 requestTbl.rawPut("protocol",request.getProtocol()); | |
84 requestTbl.rawPut("scheme",request.getScheme()); | |
85 requestTbl.rawPut("port",request.getServerPort()); | |
86 | |
87 LuanTable headersTbl = (LuanTable)requestTbl.rawGet("headers"); | |
88 for( Enumeration<String> enKeys = request.getHeaderNames(); enKeys.hasMoreElements(); ) { | |
89 String key = enKeys.nextElement(); | |
90 LuanTable values = new LuanTable(); | |
91 for( Enumeration<String> en = request.getHeaders(key); en.hasMoreElements(); ) { | |
92 values.rawPut(values.rawLength()+1,en.nextElement()); | |
93 } | |
94 key = toLuanHeaderName(key); | |
95 headersTbl.rawPut(key,values); | |
96 } | |
97 | |
98 LuanTable parametersTbl = (LuanTable)requestTbl.rawGet("parameters"); | |
99 String contentType = request.getContentType(); | |
100 if( contentType==null || !contentType.startsWith("multipart/form-data") ) { | |
101 for( Map.Entry<String,String[]> entry : request.getParameterMap().entrySet() ) { | |
102 parametersTbl.rawPut(entry.getKey(),new LuanTable(Arrays.asList(entry.getValue()))); | |
103 } | |
104 } else { // multipart | |
105 try { | |
106 InputStream in = new BufferedInputStream(request.getInputStream()); | |
107 final MultiPartInputStream mpis = new MultiPartInputStream(in,contentType,null,null); | |
108 mpis.setDeleteOnExit(true); | |
109 for( Part p : mpis.getParts() ) { | |
110 final MultiPartInputStream.MultiPart part = (MultiPartInputStream.MultiPart)p; | |
111 String name = part.getName(); | |
112 /* | |
113 System.out.println("name = "+name); | |
114 System.out.println("getContentType = "+part.getContentType()); | |
115 System.out.println("getHeaderNames = "+part.getHeaderNames()); | |
116 System.out.println("content-disposition = "+part.getHeader("content-disposition")); | |
117 System.out.println(); | |
118 */ | |
119 Object value; | |
120 String filename = part.getContentDispositionFilename(); | |
121 if( filename == null ) { | |
122 value = new String(part.getBytes()); | |
123 } else { | |
124 LuanTable partTbl = LuanPropertyMeta.INSTANCE.newTable(); | |
125 partTbl.rawPut("filename",filename); | |
126 partTbl.rawPut("content_type",part.getContentType()); | |
127 LuanPropertyMeta.INSTANCE.getters(partTbl).rawPut( "content", new LuanFunction() { | |
128 @Override public Object call(LuanState luan,Object[] args) throws LuanException { | |
129 try { | |
130 InputStream in = part.getInputStream(); | |
131 byte[] content = Utils.readAll(in); | |
132 in.close(); | |
133 return content; | |
134 } catch(IOException e) { | |
135 throw new RuntimeException(e); | |
136 } | |
137 } | |
138 } ); | |
139 value = partTbl; | |
140 } | |
141 LuanTable list = (LuanTable)parametersTbl.rawGet(name); | |
142 if( list == null ) { | |
143 list = new LuanTable(); | |
144 parametersTbl.rawPut(name,list); | |
145 } | |
146 list.rawPut(parametersTbl.rawLength()+1,value); | |
147 } | |
148 } catch(IOException e) { | |
149 throw new RuntimeException(e); | |
150 } catch(ServletException e) { | |
151 throw new RuntimeException(e); | |
152 } | |
153 } | |
154 | |
155 LuanTable cookieTbl = (LuanTable)requestTbl.rawGet("cookie"); | |
156 for( Cookie cookie : request.getCookies() ) { | |
157 cookieTbl.rawPut( cookie.getName(), unescape(cookie.getValue()) ); | |
158 } | |
159 | |
160 | |
161 // response | |
162 LuanTable responseTbl = new LuanTable(); | |
163 responseTbl.rawPut("java",response); | |
164 LuanFunction newResponseFn = (LuanFunction)module.rawGet("new_response"); | |
165 newResponseFn.call( luan, new Object[]{responseTbl} ); | |
166 module.rawPut("response",responseTbl); | |
167 | |
168 fn.call(luan); | |
169 handle_run_later(luan); | |
170 return true; | |
171 } | |
172 | |
173 public static void setResponse(LuanTable responseTbl,HttpServletResponse response) throws LuanException { | |
174 int status = Luan.asInteger(responseTbl.rawGet("status")); | |
175 response.setStatus(status); | |
176 LuanTable responseHeaders = (LuanTable)responseTbl.rawGet("headers"); | |
177 for( Map.Entry<Object,Object> entry : responseHeaders.rawIterable() ) { | |
178 String name = (String)entry.getKey(); | |
179 name = toHttpHeaderName(name); | |
180 LuanTable values = (LuanTable)entry.getValue(); | |
181 for( Object value : values.asList() ) { | |
182 if( value instanceof String ) { | |
183 response.setHeader(name,(String)value); | |
184 continue; | |
185 } | |
186 Integer i = Luan.asInteger(value); | |
187 if( i != null ) { | |
188 response.setIntHeader(name,i); | |
189 continue; | |
190 } | |
191 throw new IllegalArgumentException("value must be string or integer for headers table"); | |
192 } | |
193 } | |
194 } | |
195 | |
196 | |
197 | |
198 // static utils | |
199 | |
200 public static String toLuanHeaderName(String httpName) { | |
201 return httpName.toLowerCase().replace('-','_'); | |
202 } | |
203 | |
204 public static String toHttpHeaderName(String luanName) { | |
205 /* | |
206 StringBuilder buf = new StringBuilder(); | |
207 boolean capitalize = true; | |
208 char[] a = luanName.toCharArray(); | |
209 for( int i=0; i<a.length; i++ ) { | |
210 char c = a[i]; | |
211 if( c == '_' ) { | |
212 a[i] = '-'; | |
213 capitalize = true; | |
214 } else if( capitalize ) { | |
215 a[i] = Character.toUpperCase(c); | |
216 capitalize = false; | |
217 } | |
218 } | |
219 return String.valueOf(a); | |
220 */ | |
221 return LuanUrl.toHttpHeaderName(luanName); | |
222 } | |
223 | |
224 private static String escape(String value) { | |
225 return value.replaceAll(";", "%3B"); | |
226 } | |
227 | |
228 private static String unescape(String value) { | |
229 return value.replaceAll("%3B", ";"); | |
230 } | |
231 | |
232 private static Cookie getCookie(HttpServletRequest request,String name) { | |
233 Cookie[] cookies = request.getCookies(); | |
234 if( cookies == null ) | |
235 return null; | |
236 for (Cookie cookie : cookies) { | |
237 if (cookie.getName().equals(name)) | |
238 return cookie; | |
239 } | |
240 return null; | |
241 } | |
242 | |
243 public static void setCookie(HttpServletRequest request,HttpServletResponse response,String name,String value,boolean isPersistent, String domain) { | |
244 Cookie cookie = getCookie(request,name); | |
245 if( cookie==null || !cookie.getValue().equals(value) ) { | |
246 cookie = new Cookie(name, escape(value)); | |
247 cookie.setPath("/"); | |
248 if (domain != null && domain.length() > 0) | |
249 cookie.setDomain(domain); | |
250 if( isPersistent ) | |
251 cookie.setMaxAge(10000000); | |
252 response.addCookie(cookie); | |
253 } | |
254 } | |
255 | |
256 public static void removeCookie(HttpServletRequest request, | |
257 HttpServletResponse response, | |
258 String name, | |
259 String domain | |
260 ) { | |
261 Cookie cookie = getCookie(request, name); | |
262 if(cookie != null) { | |
263 Cookie delCookie = new Cookie(name, "delete"); | |
264 delCookie.setPath("/"); | |
265 delCookie.setMaxAge(0); | |
266 if (domain != null && domain.length() > 0) | |
267 delCookie.setDomain(domain); | |
268 response.addCookie(delCookie); | |
269 } | |
270 } | |
271 | |
272 | |
273 | |
274 private static String RUN_LATER_KEY = "Http.run_later"; | |
275 private static final Executor exec = Executors.newSingleThreadExecutor(); | |
276 | |
277 public static void run_later(final LuanState luan,final LuanFunction fn,final Object... args) { | |
278 List list = (List)luan.registry().get(RUN_LATER_KEY); | |
279 if( list == null ) { | |
280 list = new ArrayList(); | |
281 luan.registry().put(RUN_LATER_KEY,list); | |
282 } | |
283 list.add( | |
284 new Runnable(){public void run() { | |
285 try { | |
286 fn.call(luan,args); | |
287 } catch(LuanException e) { | |
288 e.printStackTrace(); | |
289 } | |
290 }} | |
291 ); | |
292 } | |
293 | |
294 private static void handle_run_later(LuanState luan) { | |
295 List list = (List)luan.registry().get(RUN_LATER_KEY); | |
296 if( list==null ) | |
297 return; | |
298 for( Object obj : list ) { | |
299 exec.execute((Runnable)obj); | |
300 } | |
301 } | |
302 } |