Mercurial Hosting > luan
comparison http/src/luan/modules/http/HttpServicer.java @ 498:ee55be414a34
Http.response is now mostly luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 18 May 2015 00:25:35 -0600 |
parents | 55f9f74f1e55 |
children | fa4af530697f |
comparison
equal
deleted
inserted
replaced
497:55f9f74f1e55 | 498:ee55be414a34 |
---|---|
148 LuanTable cookieTbl = (LuanTable)requestTbl.rawGet("cookie"); | 148 LuanTable cookieTbl = (LuanTable)requestTbl.rawGet("cookie"); |
149 for( Cookie cookie : request.getCookies() ) { | 149 for( Cookie cookie : request.getCookies() ) { |
150 cookieTbl.rawPut( cookie.getName(), unescape(cookie.getValue()) ); | 150 cookieTbl.rawPut( cookie.getName(), unescape(cookie.getValue()) ); |
151 } | 151 } |
152 | 152 |
153 HttpServicer lib = new HttpServicer(request,response); | 153 |
154 try { | 154 // response |
155 module.put( luan, "response", lib.responseTable() ); | 155 LuanTable responseTbl = new LuanTable(); |
156 } catch(NoSuchMethodException e) { | 156 responseTbl.rawPut("java",response); |
157 throw new RuntimeException(e); | 157 luan.call( (LuanFunction)module.rawGet("new_response"), new Object[]{responseTbl} ); |
158 } | 158 module.rawPut("response",responseTbl); |
159 | 159 |
160 luan.call(fn,"<http>"); | 160 luan.call(fn,"<http>"); |
161 setResponse(module,response); | |
161 return true; | 162 return true; |
162 } | 163 } |
163 | 164 |
164 private static LuanFunction getService(LuanState luan,LuanTable tbl) | 165 private static LuanFunction getService(LuanState luan,LuanTable tbl) |
165 throws LuanException | 166 throws LuanException |
170 if( !(respond instanceof LuanFunction) ) | 171 if( !(respond instanceof LuanFunction) ) |
171 throw luan.exception( "'respond' must be a function but is a " + Luan.type(respond) ); | 172 throw luan.exception( "'respond' must be a function but is a " + Luan.type(respond) ); |
172 return (LuanFunction)respond; | 173 return (LuanFunction)respond; |
173 } | 174 } |
174 | 175 |
175 | 176 private static void setResponse(LuanTable module,HttpServletResponse response) throws LuanException { |
176 private final HttpServletRequest request; | 177 LuanTable responseTbl = (LuanTable)module.rawGet("response"); |
177 private final HttpServletResponse response; | 178 int status = Luan.asInteger(responseTbl.rawGet("status")); |
178 | 179 response.setStatus(status); |
179 private HttpServicer(HttpServletRequest request,HttpServletResponse response) { | 180 LuanTable responseHeaders = (LuanTable)responseTbl.rawGet("headers"); |
180 this.request = request; | 181 for( Map.Entry<Object,Object> entry : responseHeaders.rawIterable() ) { |
181 this.response = response; | 182 String name = (String)entry.getKey(); |
182 } | 183 name = name.replace('_','-'); |
183 | 184 LuanTable values = (LuanTable)entry.getValue(); |
184 private LuanTable responseTable() throws NoSuchMethodException { | 185 for( Object value : values.asList() ) { |
185 LuanTable tbl = LuanPropertyMeta.INSTANCE.newTable(); | 186 if( value instanceof String ) { |
186 LuanTable getters = LuanPropertyMeta.INSTANCE.getters(tbl); | 187 response.setHeader(name,(String)value); |
187 LuanTable setters = LuanPropertyMeta.INSTANCE.setters(tbl); | 188 continue; |
188 tbl.rawPut("java",response); | 189 } |
189 tbl.rawPut( "send_redirect", new LuanJavaFunction( | 190 Integer i = Luan.asInteger(value); |
190 HttpServletResponse.class.getMethod( "sendRedirect", String.class ), response | |
191 ) ); | |
192 tbl.rawPut( "send_error", new LuanJavaFunction( | |
193 HttpServletResponse.class.getMethod( "sendError", Integer.TYPE, String.class ), response | |
194 ) ); | |
195 LuanTable headers = new NameMeta() { | |
196 | |
197 @Override Object get(String name) { | |
198 return response.getHeader(name); | |
199 } | |
200 | |
201 @Override protected Iterator keys(LuanTable tbl) { | |
202 return response.getHeaderNames().iterator(); | |
203 } | |
204 | |
205 @Override public boolean canNewindex() { | |
206 return true; | |
207 } | |
208 | |
209 @Override public void __new_index(LuanState luan,LuanTable tbl,Object key,Object val) { | |
210 if( !(key instanceof String) ) | |
211 throw new IllegalArgumentException("key must be string for headers table"); | |
212 String name = (String)key; | |
213 if( val instanceof String ) { | |
214 response.setHeader(name,(String)val); | |
215 return; | |
216 } | |
217 Integer i = Luan.asInteger(val); | |
218 if( i != null ) { | 191 if( i != null ) { |
219 response.setIntHeader(name,i); | 192 response.setIntHeader(name,i); |
220 return; | 193 continue; |
221 } | 194 } |
222 throw new IllegalArgumentException("value must be string or integer for headers table"); | 195 throw new IllegalArgumentException("value must be string or integer for headers table"); |
223 } | 196 } |
224 | 197 } |
225 @Override protected String type(LuanTable tbl) { | 198 // no more headers |
226 return "response.headers"; | 199 responseTbl.rawPut("headers",null); |
227 } | 200 responseTbl.rawPut("header",null); |
228 | 201 } |
229 }.newTable(); | 202 |
230 tbl.rawPut( "headers", headers ); | |
231 getters.rawPut( "content_type", new LuanJavaFunction( | |
232 HttpServletResponse.class.getMethod( "getContentType" ), response | |
233 ) ); | |
234 setters.rawPut( "content_type", new LuanJavaFunction( | |
235 HttpServletResponse.class.getMethod( "setContentType", String.class ), response | |
236 ) ); | |
237 getters.rawPut( "character_encoding", new LuanJavaFunction( | |
238 HttpServletResponse.class.getMethod( "getCharacterEncoding" ), response | |
239 ) ); | |
240 setters.rawPut( "character_encoding", new LuanJavaFunction( | |
241 HttpServletResponse.class.getMethod( "setCharacterEncoding", String.class ), response | |
242 ) ); | |
243 add( tbl, "text_writer" ); | |
244 add( tbl, "set_cookie", String.class, String.class, Boolean.TYPE, String.class ); | |
245 add( tbl, "remove_cookie", String.class, String.class ); | |
246 try { | |
247 getters.rawPut( "status", new LuanJavaFunction( | |
248 HttpServletResponse.class.getMethod( "getStatus" ), response | |
249 ) ); | |
250 } catch(NoSuchMethodException e) { | |
251 logger.info("please upgrade jetty"); | |
252 } | |
253 setters.rawPut( "status", new LuanJavaFunction( | |
254 HttpServletResponse.class.getMethod( "setStatus", Integer.TYPE ), response | |
255 ) ); | |
256 return tbl; | |
257 } | |
258 | |
259 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | |
260 t.rawPut( method, new LuanJavaFunction(HttpServicer.class.getMethod(method,parameterTypes),this) ); | |
261 } | |
262 | |
263 public LuanTable text_writer() throws IOException { | |
264 return IoLuan.textWriter(response.getWriter()); | |
265 } | |
266 | |
267 public void set_cookie(String name,String value,boolean isPersistent, String domain) { | |
268 setCookie(request,response,name,value,isPersistent,domain); | |
269 } | |
270 | |
271 public void remove_cookie(String name, String domain) { | |
272 removeCookie(request,response,name,domain); | |
273 } | |
274 | 203 |
275 | 204 |
276 // static utils | 205 // static utils |
277 | 206 |
278 private static String escape(String value) { | 207 private static String escape(String value) { |
321 delCookie.setDomain(domain); | 250 delCookie.setDomain(domain); |
322 response.addCookie(delCookie); | 251 response.addCookie(delCookie); |
323 } | 252 } |
324 } | 253 } |
325 | 254 |
326 | |
327 | |
328 // util classes | |
329 | |
330 private static abstract class NameMeta extends LuanMeta { | |
331 abstract Object get(String name); | |
332 | |
333 @Override public Object __index(LuanState luan,LuanTable tbl,Object key) { | |
334 if( !(key instanceof String) ) | |
335 return null; | |
336 String name = (String)key; | |
337 return get(name); | |
338 } | |
339 | |
340 }; | |
341 | |
342 } | 255 } |