changeset 1150:0842b9b570f8

change http headers interface
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 04 Feb 2018 18:03:37 -0700
parents 1b7c20e20ca7
children dbb3cb906482
files src/luan/modules/http/Http.luan src/luan/modules/http/jetty/HttpServicer.java src/luan/modules/http/tools/Dump_mod.luan src/luan/modules/http/tools/java_threads.luan src/luan/modules/http/tools/luan_threads.luan src/luan/modules/http/tools/run.luan src/luan/modules/url/LuanUrl.java
diffstat 7 files changed, 38 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
diff -r 1b7c20e20ca7 -r 0842b9b570f8 src/luan/modules/http/Http.luan
--- a/src/luan/modules/http/Http.luan	Sun Feb 04 17:23:20 2018 -0700
+++ b/src/luan/modules/http/Http.luan	Sun Feb 04 18:03:37 2018 -0700
@@ -52,8 +52,6 @@
 local function new_common(this)
 	this = this or {}
 	this.headers = {}
-	this.header = {__plural=this.headers}
-	set_metatable(this.header,singular_metatable)
 	return this
 end
 
@@ -86,7 +84,7 @@
 	end
 
 	function this.url()
-		local url = this.scheme.."://"..this.header.host..this.path
+		local url = this.scheme.."://"..this.headers["host"]..this.path
 		if this.method ~= "POST" then
 			local query = this.query_string()
 			if query ~= nil then
diff -r 1b7c20e20ca7 -r 0842b9b570f8 src/luan/modules/http/jetty/HttpServicer.java
--- a/src/luan/modules/http/jetty/HttpServicer.java	Sun Feb 04 17:23:20 2018 -0700
+++ b/src/luan/modules/http/jetty/HttpServicer.java	Sun Feb 04 18:03:37 2018 -0700
@@ -87,12 +87,15 @@
 		LuanTable headersTbl = (LuanTable)requestTbl.rawGet("headers");
 		for( Enumeration<String> enKeys = request.getHeaderNames(); enKeys.hasMoreElements(); ) {
 			String key = enKeys.nextElement();
-			LuanTable values = new LuanTable();
+			List<String> values = new ArrayList<String>();
 			for( Enumeration<String> en = request.getHeaders(key); en.hasMoreElements(); ) {
-				values.rawPut(values.rawLength()+1,en.nextElement());
+				values.add(en.nextElement());
 			}
-			key = toLuanHeaderName(key);
-			headersTbl.rawPut(key,values);
+			int size = values.size();
+			if(size==0) throw new RuntimeException();
+			key = key.toLowerCase();
+			Object value = size==1 ? values.get(0) : new LuanTable(values);
+			headersTbl.rawPut(key,value);
 		}
 
 		LuanTable parametersTbl = (LuanTable)requestTbl.rawGet("parameters");
@@ -199,51 +202,35 @@
 		LuanTable responseHeaders = (LuanTable)responseTbl.rawGet("headers");
 		for( Map.Entry<Object,Object> entry : responseHeaders.rawIterable() ) {
 			String name = (String)entry.getKey();
-			name = toHttpHeaderName(name);
-			LuanTable values = (LuanTable)entry.getValue();
-			for( Object value : values.asList() ) {
-				if( value instanceof String ) {
-					response.setHeader(name,(String)value);
-					continue;
+			Object val = entry.getValue();
+			if( val instanceof LuanTable ) {
+				LuanTable values = (LuanTable)val;
+				for( Object value : values.asList() ) {
+					setResponse(response,name,value);
 				}
-				Integer i = Luan.asInteger(value);
-				if( i != null ) {
-					response.setIntHeader(name,i);
-					continue;
-				}
-				throw new IllegalArgumentException("value must be string or integer for headers table");
+			} else {
+				setResponse(response,name,val);
 			}
 		}
 	}
 
+	private static void setResponse(HttpServletResponse response,String name,Object value) throws LuanException {
+		if( value instanceof String ) {
+			response.setHeader(name,(String)value);
+			return;
+		}
+		Integer i = Luan.asInteger(value);
+		if( i != null ) {
+			response.setIntHeader(name,i);
+			return;
+		}
+		throw new IllegalArgumentException("value must be string or integer for headers table");
+	}
+
 
 
 	// static utils
 
-	public static String toLuanHeaderName(String httpName) {
-		return httpName.toLowerCase().replace('-','_');
-	}
-
-	public static String toHttpHeaderName(String luanName) {
-/*
-		StringBuilder buf = new StringBuilder();
-		boolean capitalize = true;
-		char[] a = luanName.toCharArray();
-		for( int i=0; i<a.length; i++ ) {
-			char c = a[i];
-			if( c == '_' ) {
-				a[i] = '-';
-				capitalize = true;
-			} else if( capitalize ) {
-				a[i] = Character.toUpperCase(c);
-				capitalize = false;
-			}
-		}
-		return String.valueOf(a);
-*/
-		return LuanUrl.toHttpHeaderName(luanName);
-	}
-
 	private static String escape(String value) {
 		return value.replaceAll(";", "%3B");
 	}
diff -r 1b7c20e20ca7 -r 0842b9b570f8 src/luan/modules/http/tools/Dump_mod.luan
--- a/src/luan/modules/http/tools/Dump_mod.luan	Sun Feb 04 17:23:20 2018 -0700
+++ b/src/luan/modules/http/tools/Dump_mod.luan	Sun Feb 04 18:03:37 2018 -0700
@@ -4,17 +4,12 @@
 local ipairs = Luan.ipairs or error()
 local Io = require "luan:Io.luan"
 local Http = require "luan:http/Http.luan"
-java()
-local Implementation = require "luan:http/Implementation.luan"
-local HttpServicer = require(Implementation.java.."HttpServicer")
+
 
 local Dump_mod = {}
 
-local to_http_header_name = HttpServicer.toHttpHeaderName
-Dump_mod.to_http_header_name = to_http_header_name
-
 function Dump_mod.respond()
-	Http.response.header.content_type = "text/plain"
+	Http.response.headers["content-type"] = "text/plain"
 	Io.stdout = Http.response.text_writer()
 
 	local method = Http.request.method
@@ -40,10 +35,9 @@
 
 function Dump_mod.dump_headers(headers)
 	for name, values in pairs(headers) do
-		local header_name = to_http_header_name(name)
 		for _, value in ipairs(values) do
 %>
-<%=header_name%>: <%=value%>
+<%=name%>: <%=value%>
 <%
 		end
 	end
diff -r 1b7c20e20ca7 -r 0842b9b570f8 src/luan/modules/http/tools/java_threads.luan
--- a/src/luan/modules/http/tools/java_threads.luan	Sun Feb 04 17:23:20 2018 -0700
+++ b/src/luan/modules/http/tools/java_threads.luan	Sun Feb 04 18:03:37 2018 -0700
@@ -23,7 +23,7 @@
 <html>
 	<body>
 		<h1>Java Threads</h1>
-		<p><%=Http.request.header.host%> - <%=Time.format(Time.now())%></p>
+		<p><%=Http.request.headers["host"]%> - <%=Time.format(Time.now())%></p>
 		<%
 		local count = 0
 		for _, thread in Luan.ipairs(threads) do
diff -r 1b7c20e20ca7 -r 0842b9b570f8 src/luan/modules/http/tools/luan_threads.luan
--- a/src/luan/modules/http/tools/luan_threads.luan	Sun Feb 04 17:23:20 2018 -0700
+++ b/src/luan/modules/http/tools/luan_threads.luan	Sun Feb 04 18:03:37 2018 -0700
@@ -25,7 +25,7 @@
 <html>
 	<body>
 		<h1>Luan Threads</h1>
-		<p><%=Http.request.header.host%> - <%=Time.format(Time.now())%></p>
+		<p><%=Http.request.headers["host"]%> - <%=Time.format(Time.now())%></p>
 		<%
 		local count = 0
 		for _, thread in Luan.ipairs(threads) do
diff -r 1b7c20e20ca7 -r 0842b9b570f8 src/luan/modules/http/tools/run.luan
--- a/src/luan/modules/http/tools/run.luan	Sun Feb 04 17:23:20 2018 -0700
+++ b/src/luan/modules/http/tools/run.luan	Sun Feb 04 18:03:37 2018 -0700
@@ -76,7 +76,7 @@
 return function()
 	local content_type = Http.request.parameter.content_type
 	if content_type ~= nil then
-		Http.response.header.content_type = content_type
+		Http.response.headers["content-type"] = content_type
 	end
 	Io.stdout = Http.response.text_writer()
 	local code = Http.request.parameter.code
@@ -91,7 +91,7 @@
 		end;
 		catch = function(e)
 			Http.response.reset()
-			Http.response.header.content_type = "text/plain"
+			Http.response.headers["content-type"] = "text/plain"
 			Io.stdout = Http.response.text_writer()
 			print(e)
 			print""
diff -r 1b7c20e20ca7 -r 0842b9b570f8 src/luan/modules/url/LuanUrl.java
--- a/src/luan/modules/url/LuanUrl.java	Sun Feb 04 17:23:20 2018 -0700
+++ b/src/luan/modules/url/LuanUrl.java	Sun Feb 04 18:03:37 2018 -0700
@@ -52,17 +52,16 @@
 				headers = new HashMap();
 				for( Object hack : headerMap.entrySet() ) {
 					Map.Entry entry = (Map.Entry)hack;
-					String key = (String)entry.getKey();
+					String name = (String)entry.getKey();
 					Object val = entry.getValue();
-					String name = toHttpHeaderName(key);
 					if( val instanceof String ) {
 						headers.put(name,val);
 					} else {
 						if( !(val instanceof LuanTable) )
-							throw new LuanException( "header '"+key+"' must be string or table" );
+							throw new LuanException( "header '"+name+"' must be string or table" );
 						LuanTable t = (LuanTable)val;
 						if( !t.isList() )
-							throw new LuanException( "header '"+key+"' table must be list" );
+							throw new LuanException( "header '"+name+"' table must be list" );
 						headers.put(name,t.asList());
 					}
 				}
@@ -148,24 +147,6 @@
 		}
 	}
 
-	public static String toHttpHeaderName(String luanName) {
-		luanName = luanName.toLowerCase();
-		StringBuilder buf = new StringBuilder();
-		boolean capitalize = true;
-		char[] a = luanName.toCharArray();
-		for( int i=0; i<a.length; i++ ) {
-			char c = a[i];
-			if( c == '_'  || c == '-' ) {
-				a[i] = '-';
-				capitalize = true;
-			} else if( capitalize ) {
-				a[i] = Character.toUpperCase(c);
-				capitalize = false;
-			}
-		}
-		return String.valueOf(a);
-	}
-
 	private static void and(StringBuilder sb) {
 		if( sb.length() > 0 )
 			sb.append('&');