diff http/src/luan/modules/http/HttpServicer.java @ 497:55f9f74f1e55

Http.request is now pure luan
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 17 May 2015 19:25:47 -0600
parents c65df5b25932
children ee55be414a34
line wrap: on
line diff
--- a/http/src/luan/modules/http/HttpServicer.java	Sat May 16 20:19:05 2015 -0600
+++ b/http/src/luan/modules/http/HttpServicer.java	Sun May 17 19:25:47 2015 -0600
@@ -74,145 +74,38 @@
 		}
 
 		LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http");
-		HttpServicer lib = new HttpServicer(request,response);
-		try {
-			module.put( luan, "request", lib.requestTable() );
-			module.put( luan, "response", lib.responseTable() );
-		} catch(NoSuchMethodException e) {
-			throw new RuntimeException(e);
+
+		// request
+		LuanTable requestTbl = (LuanTable)luan.call( (LuanFunction)module.rawGet("new_request") );
+		module.rawPut("request",requestTbl);
+		requestTbl.rawPut("java",request);
+		requestTbl.rawPut("method",request.getMethod());
+		requestTbl.rawPut("path",request.getRequestURI());
+		requestTbl.rawPut("protocol",request.getProtocol());
+		requestTbl.rawPut("scheme",request.getScheme());
+
+		LuanTable headersTbl = (LuanTable)requestTbl.rawGet("headers");
+		for( Enumeration<String> enKeys = request.getHeaderNames(); enKeys.hasMoreElements(); ) {
+			String key = enKeys.nextElement();
+			key = key.toLowerCase().replace('-','_');
+			LuanTable values = new LuanTable();
+			for( Enumeration<String> en = request.getHeaders(key); en.hasMoreElements(); ) {
+				values.rawPut(values.rawLength()+1,en.nextElement());
+			}
+			headersTbl.rawPut(key,values);
 		}
 
-		luan.call(fn,"<http>");
-		return true;
-	}
-
-	private static LuanFunction getService(LuanState luan,LuanTable tbl)
-		throws LuanException
-	{
-		Object respond = tbl.get(luan,"respond");
-		if( respond == null )
-			throw luan.exception( "function 'respond' is not defined" );
-		if( !(respond instanceof LuanFunction) )
-			throw luan.exception( "'respond' must be a function but is a " + Luan.type(respond) );
-		return (LuanFunction)respond;
-	}
-
-
-	private final HttpServletRequest request;
-	private final HttpServletResponse response;
-//	private PrintWriter writer = null;
-//	private ServletOutputStream sos = null;
-
-	private HttpServicer(HttpServletRequest request,HttpServletResponse response) {
-		this.request = request;
-		this.response = response;
-	}
-
-	private LuanTable requestTable() throws NoSuchMethodException {
-		LuanTable tbl = LuanPropertyMeta.INSTANCE.newTable();
-		LuanTable getters = LuanPropertyMeta.INSTANCE.getters(tbl);
-		tbl.rawPut("java",request);
-		LuanTable parameters = new NameMeta() {
-
-			@Override Object get(String name) {
-				return request.getParameter(name);
-			}
-
-			@Override protected Iterator keys(LuanTable tbl) {
-				return new EnumerationIterator(request.getParameterNames());
-			}
-
-			@Override protected String type(LuanTable tbl) {
-				return "request.parameters";
-			}
-
-		}.newTable();
-		tbl.rawPut( "parameters", parameters );
-		add( tbl, "get_parameter_values", String.class );
-		LuanTable headers = new NameMeta() {
-
-			@Override Object get(String name) {
-				return request.getHeader(name);
-			}
-
-			@Override protected Iterator keys(LuanTable tbl) {
-				return new EnumerationIterator(request.getHeaderNames());
-			}
-
-			@Override protected String type(LuanTable tbl) {
-				return "request.headers";
+		LuanTable parametersTbl = (LuanTable)requestTbl.rawGet("parameters");
+		String contentType = request.getContentType();
+		if( contentType==null || !contentType.startsWith("multipart/form-data") ) {
+			for( Map.Entry<String,String[]> entry : request.getParameterMap().entrySet() ) {
+				parametersTbl.rawPut(entry.getKey(),new LuanTable(Arrays.asList(entry.getValue())));
 			}
-
-		}.newTable();
-		tbl.rawPut( "headers", headers );
-		getters.rawPut( "method", new LuanJavaFunction(
-			HttpServletRequest.class.getMethod( "getMethod" ), request
-		) );
-		getters.rawPut( "path", new LuanJavaFunction(
-			HttpServletRequest.class.getMethod( "getRequestURI" ), request
-		) );
-		getters.rawPut( "server_name", new LuanJavaFunction(
-			HttpServletRequest.class.getMethod( "getServerName" ), request
-		) );
-		getters.rawPut( "url", new LuanJavaFunction(
-			HttpServicer.class.getMethod( "getURL" ), this
-		) );
-		getters.rawPut( "query_string", new LuanJavaFunction(
-			HttpServicer.class.getMethod( "getQueryString" ), this
-		) );
-		getters.rawPut( "remote_address", new LuanJavaFunction(
-			HttpServletRequest.class.getMethod( "getRemoteAddr" ), request
-		) );
-		getters.rawPut( "protocol", new LuanJavaFunction(
-			HttpServletRequest.class.getMethod( "getProtocol" ), request
-		) );
-		getters.rawPut( "scheme", new LuanJavaFunction(
-			HttpServletRequest.class.getMethod( "getScheme" ), request
-		) );
-		getters.rawPut( "is_secure", new LuanJavaFunction(
-			HttpServletRequest.class.getMethod( "isSecure" ), request
-		) );
-		LuanTable cookies = new LuanMeta() {
-
-			@Override public Object __index(LuanState luan,LuanTable tbl,Object key) {
-				if( !(key instanceof String) )
-					return null;
-				String name = (String)key;
-				return getCookieValue(request,name);
-			}
-
-			@Override protected Iterator<Object> keys(LuanTable tbl) {
-				return new Iterator<Object>() {
-					final Cookie[] cookies = request.getCookies();
-					int i = 0;
-	
-					@Override public boolean hasNext() {
-						return i < cookies.length;
-					}
-					@Override public Object next() {
-						return cookies[i++].getName();
-					}
-					@Override public void remove() {
-						throw new UnsupportedOperationException();
-					}
-				};
-			}
-
-			@Override protected String type(LuanTable tbl) {
-				return "request.cookies";
-			}
-
-		}.newTable();
-		tbl.rawPut( "cookies", cookies );
-
-		String contentType = request.getContentType();
-		if( contentType!=null && contentType.startsWith("multipart/form-data") ) {
+		} else {  // multipart
 			try {
 				InputStream in = new BufferedInputStream(request.getInputStream());
 				final MultiPartInputStream mpis = new MultiPartInputStream(in,contentType,null,null);
 				mpis.setDeleteOnExit(true);
-				parameters = new LuanTable();
-				final Map map = new HashMap();
 				for( Part p : mpis.getParts() ) {
 					final MultiPartInputStream.MultiPart part = (MultiPartInputStream.MultiPart)p;
 					String name = part.getName();
@@ -238,26 +131,13 @@
 						} );
 						value = partTbl;
 					}
-					parameters.rawPut(name,value);
-					Object old = map.get(name);
-					if( old == null ) {
-						map.put(name,value);
-					} else if( old instanceof Object[] ) {
-						Object[] aOld = (Object[])old;
-						Object[] aNew = new Object[aOld.length+1];
-						System.arraycopy(aOld,0,aNew,0,aOld.length);
-						aNew[aOld.length] = value;
-						map.put(name,aNew);
-					} else {
-						map.put(name,new Object[]{old,value});
+					LuanTable list = (LuanTable)parametersTbl.rawGet(name);
+					if( list == null ) {
+						list = new LuanTable();
+						parametersTbl.rawPut(name,list);
 					}
+					parametersTbl.rawPut(parametersTbl.rawLength()+1,value);
 				}
-				tbl.rawPut( "parameters", parameters );
-				tbl.rawPut( "get_parameter_values", new LuanFunction() {
-					@Override public Object call(LuanState luan,Object[] args) throws LuanException {
-						return args.length==0 ? null : map.get(args[0]);
-					}
-				} );
 			} catch(IOException e) {
 				throw new RuntimeException(e);
 			} catch(ServletException e) {
@@ -265,7 +145,40 @@
 			}
 		}
 
-		return tbl;
+		LuanTable cookieTbl = (LuanTable)requestTbl.rawGet("cookie");
+		for( Cookie cookie : request.getCookies() ) {
+			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);
+		}
+
+		luan.call(fn,"<http>");
+		return true;
+	}
+
+	private static LuanFunction getService(LuanState luan,LuanTable tbl)
+		throws LuanException
+	{
+		Object respond = tbl.get(luan,"respond");
+		if( respond == null )
+			throw luan.exception( "function 'respond' is not defined" );
+		if( !(respond instanceof LuanFunction) )
+			throw luan.exception( "'respond' must be a function but is a " + Luan.type(respond) );
+		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 {
@@ -346,23 +259,11 @@
 	private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
 		t.rawPut( method, new LuanJavaFunction(HttpServicer.class.getMethod(method,parameterTypes),this) );
 	}
-/*
-	public void text_write(LuanState luan,Object... args) throws LuanException, IOException {
-		if( writer == null )
-			writer = response.getWriter();
-		for( Object obj : args ) {
-			writer.print( luan.toString(obj) );
-		}
-	}
-*/
+
 	public LuanTable text_writer() throws IOException {
 		return IoLuan.textWriter(response.getWriter());
 	}
 
-	public Object[] get_parameter_values(String name) {
-		return request.getParameterValues(name);
-	}
-
 	public void set_cookie(String name,String value,boolean isPersistent, String domain) {
 		setCookie(request,response,name,value,isPersistent,domain);
 	}
@@ -374,60 +275,6 @@
 
 	// static utils
 
-	public String getQueryString() {
-		return getQueryString(request);
-	}
-
-	public static String getQueryString(HttpServletRequest request) {
-		return getQueryString(request,0);
-	}
-
-	public static String getQueryString(HttpServletRequest request,int maxValueLen) {
-		String method = request.getMethod();
-		if( method.equals("GET") )
-			return request.getQueryString();
-		if( !method.equals("POST") && !method.equals("HEAD") )
-			throw new RuntimeException(method);
-		Enumeration en = request.getParameterNames();
-		StringBuilder queryBuf = new StringBuilder();
-		if( !en.hasMoreElements() )
-			return null;
-		do {
-			String param = (String)en.nextElement();
-			String value = request.getParameter(param);
-			if( maxValueLen > 0 ) {
-				int len = value.length();
-				if( len > maxValueLen )
-					value = value.substring(0,maxValueLen) + "..." + (len-maxValueLen);
-			}
-			queryBuf.append(param);
-			queryBuf.append('=');
-			queryBuf.append(value);
-			queryBuf.append('&');
-		} while( en.hasMoreElements() );
-		queryBuf.deleteCharAt(queryBuf.length() - 1);
-		return queryBuf.toString();
-	}
-
-	public  String getURL() {
-		return getURL(request);
-	}
-
-	public static String getURL(HttpServletRequest request) {
-		return getURL(request,0);
-	}
-
-	public static String getURL(HttpServletRequest request,int maxValueLen) {
-//		StringBuffer buf = HttpUtils.getRequestURL(request);
-		StringBuffer buf = request.getRequestURL();
-		String qStr = getQueryString(request,maxValueLen);
-		if(qStr != null && qStr.length() > 0) {
-			buf.append('?');
-			buf.append(qStr);
-		}
-		return buf.toString();
-	}
-
 	private static String escape(String value) {
 		return value.replaceAll(";", "%3B");
 	}
@@ -447,11 +294,6 @@
 		return null;
 	}
 
-	public static String getCookieValue(HttpServletRequest request,String name) {
-		Cookie cookie = getCookie(request,name);
-		return cookie==null ? null : unescape(cookie.getValue());
-	}
-
 	public static void setCookie(HttpServletRequest request,HttpServletResponse response,String name,String value,boolean isPersistent, String domain) {
 		Cookie cookie = getCookie(request,name);
 		if( cookie==null || !cookie.getValue().equals(value) ) {
@@ -485,26 +327,6 @@
 
 	// util classes
 
-	static final class EnumerationIterator implements Iterator {
-		private final Enumeration en;
-
-		EnumerationIterator(Enumeration en) {
-			this.en = en;
-		}
-
-		@Override public boolean hasNext() {
-			return en.hasMoreElements();
-		}
-
-		@Override public Object next() {
-			return en.nextElement();
-		}
-
-		@Override public void remove() {
-			throw new UnsupportedOperationException();
-		}
-	}
-
 	private static abstract class NameMeta extends LuanMeta {
 		abstract Object get(String name);
 
@@ -517,9 +339,4 @@
 
 	};
 
-	private static String string(Object value) {
-		if( !(value instanceof String) )
-			throw new IllegalArgumentException("value must be string");
-		return (String)value;
-	}
 }