Mercurial Hosting > luan
changeset 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 |
files | core/src/luan/LuanTable.java http/src/luan/modules/http/Http.luan http/src/luan/modules/http/HttpServicer.java http/src/luan/modules/http/run.luan |
diffstat | 4 files changed, 66 insertions(+), 114 deletions(-) [+] |
line wrap: on
line diff
diff -r 55f9f74f1e55 -r ee55be414a34 core/src/luan/LuanTable.java --- a/core/src/luan/LuanTable.java Sun May 17 19:25:47 2015 -0600 +++ b/core/src/luan/LuanTable.java Mon May 18 00:25:35 2015 -0600 @@ -269,6 +269,15 @@ }; } + public Iterable<Map.Entry<Object,Object>> rawIterable() throws LuanException { + final Iterator<Map.Entry<Object,Object>> iter = rawIterator(); + return new Iterable<Map.Entry<Object,Object>>() { + public Iterator<Map.Entry<Object,Object>> iterator() { + return iter; + } + }; + } + public Iterator<Map.Entry<Object,Object>> iterator(final LuanState luan) throws LuanException { if( getHandler("__pairs") == null ) return rawIterator();
diff -r 55f9f74f1e55 -r ee55be414a34 http/src/luan/modules/http/Http.luan --- a/http/src/luan/modules/http/Http.luan Sun May 17 19:25:47 2015 -0600 +++ b/http/src/luan/modules/http/Http.luan Mon May 18 00:25:35 2015 -0600 @@ -1,3 +1,4 @@ +java() local Luan = require "luan:Luan" local ipairs = Luan.ipairs local pairs = Luan.pairs @@ -5,6 +6,8 @@ local Io = require "luan:Io" local Html = require "luan:Html" local url_encode = Html.url_encode +local HttpServicer = require "java:luan.modules.http.HttpServicer" +local IoLuan = require "java:luan.modules.IoLuan" local singular_metatable = {} @@ -69,6 +72,33 @@ return this end +STATUS = { + OK = 200; + -- add more as needed +} + +function new_response(this) + this = new_common(this) + this.status = STATUS.OK + if this.java ~= nil then + this.send_redirect = this.java.sendRedirect + this.send_error = this.java.sendError + + function this.set_cookie(name,value,is_persistent,domain) + HttpServicer.setCookie(this.java,response.java,name,value,is_persistent,domain) + end + + function this.remove_cookie(name,domain) + HttpServicer.removeCookie(this.java,response.java,name,domain) + end + + function this.text_writer() + return IoLuan.textWriter(this.java.getWriter()) + end + end + return this +end + -- request = new_request{} -- filled in by HttpServicer
diff -r 55f9f74f1e55 -r ee55be414a34 http/src/luan/modules/http/HttpServicer.java --- a/http/src/luan/modules/http/HttpServicer.java Sun May 17 19:25:47 2015 -0600 +++ b/http/src/luan/modules/http/HttpServicer.java Mon May 18 00:25:35 2015 -0600 @@ -150,14 +150,15 @@ cookieTbl.rawPut( cookie.getName(), unescape(cookie.getValue()) ); } - HttpServicer lib = new HttpServicer(request,response); - try { - module.put( luan, "response", lib.responseTable() ); - } catch(NoSuchMethodException e) { - throw new RuntimeException(e); - } + + // response + LuanTable responseTbl = new LuanTable(); + responseTbl.rawPut("java",response); + luan.call( (LuanFunction)module.rawGet("new_response"), new Object[]{responseTbl} ); + module.rawPut("response",responseTbl); luan.call(fn,"<http>"); + setResponse(module,response); return true; } @@ -172,105 +173,33 @@ return (LuanFunction)respond; } - - private final HttpServletRequest request; - private final HttpServletResponse response; - - private HttpServicer(HttpServletRequest request,HttpServletResponse response) { - this.request = request; - this.response = response; - } - - private LuanTable responseTable() throws NoSuchMethodException { - LuanTable tbl = LuanPropertyMeta.INSTANCE.newTable(); - LuanTable getters = LuanPropertyMeta.INSTANCE.getters(tbl); - LuanTable setters = LuanPropertyMeta.INSTANCE.setters(tbl); - tbl.rawPut("java",response); - tbl.rawPut( "send_redirect", new LuanJavaFunction( - HttpServletResponse.class.getMethod( "sendRedirect", String.class ), response - ) ); - tbl.rawPut( "send_error", new LuanJavaFunction( - HttpServletResponse.class.getMethod( "sendError", Integer.TYPE, String.class ), response - ) ); - LuanTable headers = new NameMeta() { - - @Override Object get(String name) { - return response.getHeader(name); - } - - @Override protected Iterator keys(LuanTable tbl) { - return response.getHeaderNames().iterator(); - } - - @Override public boolean canNewindex() { - return true; - } - - @Override public void __new_index(LuanState luan,LuanTable tbl,Object key,Object val) { - if( !(key instanceof String) ) - throw new IllegalArgumentException("key must be string for headers table"); - String name = (String)key; - if( val instanceof String ) { - response.setHeader(name,(String)val); - return; + private static void setResponse(LuanTable module,HttpServletResponse response) throws LuanException { + LuanTable responseTbl = (LuanTable)module.rawGet("response"); + int status = Luan.asInteger(responseTbl.rawGet("status")); + response.setStatus(status); + LuanTable responseHeaders = (LuanTable)responseTbl.rawGet("headers"); + for( Map.Entry<Object,Object> entry : responseHeaders.rawIterable() ) { + String name = (String)entry.getKey(); + name = name.replace('_','-'); + LuanTable values = (LuanTable)entry.getValue(); + for( Object value : values.asList() ) { + if( value instanceof String ) { + response.setHeader(name,(String)value); + continue; } - Integer i = Luan.asInteger(val); + Integer i = Luan.asInteger(value); if( i != null ) { response.setIntHeader(name,i); - return; + continue; } throw new IllegalArgumentException("value must be string or integer for headers table"); } - - @Override protected String type(LuanTable tbl) { - return "response.headers"; - } - - }.newTable(); - tbl.rawPut( "headers", headers ); - getters.rawPut( "content_type", new LuanJavaFunction( - HttpServletResponse.class.getMethod( "getContentType" ), response - ) ); - setters.rawPut( "content_type", new LuanJavaFunction( - HttpServletResponse.class.getMethod( "setContentType", String.class ), response - ) ); - getters.rawPut( "character_encoding", new LuanJavaFunction( - HttpServletResponse.class.getMethod( "getCharacterEncoding" ), response - ) ); - setters.rawPut( "character_encoding", new LuanJavaFunction( - HttpServletResponse.class.getMethod( "setCharacterEncoding", String.class ), response - ) ); - add( tbl, "text_writer" ); - add( tbl, "set_cookie", String.class, String.class, Boolean.TYPE, String.class ); - add( tbl, "remove_cookie", String.class, String.class ); - try { - getters.rawPut( "status", new LuanJavaFunction( - HttpServletResponse.class.getMethod( "getStatus" ), response - ) ); - } catch(NoSuchMethodException e) { - logger.info("please upgrade jetty"); } - setters.rawPut( "status", new LuanJavaFunction( - HttpServletResponse.class.getMethod( "setStatus", Integer.TYPE ), response - ) ); - return tbl; + // no more headers + responseTbl.rawPut("headers",null); + responseTbl.rawPut("header",null); } - private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { - t.rawPut( method, new LuanJavaFunction(HttpServicer.class.getMethod(method,parameterTypes),this) ); - } - - public LuanTable text_writer() throws IOException { - return IoLuan.textWriter(response.getWriter()); - } - - public void set_cookie(String name,String value,boolean isPersistent, String domain) { - setCookie(request,response,name,value,isPersistent,domain); - } - - public void remove_cookie(String name, String domain) { - removeCookie(request,response,name,domain); - } // static utils @@ -323,20 +252,4 @@ } } - - - // util classes - - private static abstract class NameMeta extends LuanMeta { - abstract Object get(String name); - - @Override public Object __index(LuanState luan,LuanTable tbl,Object key) { - if( !(key instanceof String) ) - return null; - String name = (String)key; - return get(name); - } - - }; - }
diff -r 55f9f74f1e55 -r ee55be414a34 http/src/luan/modules/http/run.luan --- a/http/src/luan/modules/http/run.luan Sun May 17 19:25:47 2015 -0600 +++ b/http/src/luan/modules/http/run.luan Mon May 18 00:25:35 2015 -0600 @@ -56,7 +56,7 @@ end local content_type = Http.request.parameter.content_type if content_type ~= nil then - Http.response.content_type = content_type + Http.response.header.content_type = content_type end local env = { request = Http.request; @@ -68,7 +68,7 @@ run() end; catch = function(e) - Http.response.content_type = "text/plain" + Http.response.header.content_type = "text/plain" print(e) print() print()