changeset 500:ab9c2afefb47

add response.binary_writer
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 18 May 2015 20:59:30 -0600
parents fa4af530697f
children f26485a3692c
files http/src/luan/modules/http/Http.luan http/src/luan/modules/http/HttpServicer.java http/src/luan/modules/http/dump.luan
diffstat 3 files changed, 34 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/http/src/luan/modules/http/Http.luan	Mon May 18 14:24:50 2015 -0600
+++ b/http/src/luan/modules/http/Http.luan	Mon May 18 20:59:30 2015 -0600
@@ -10,9 +10,6 @@
 local IoLuan = require "java:luan.modules.IoLuan"
 
 
-to_header_name = HttpServicer.toHeaderName
-
-
 local singular_metatable = {}
 
 function singular_metatable.__index(table,key)
@@ -21,7 +18,7 @@
 end
 
 function singular_metatable.__new_index(table,key,value)
-	table.__plural[key] = {value}
+	table.__plural[key] = value and {value}
 end
 
 function singular_metatable.__pairs(table)
@@ -65,7 +62,7 @@
 		end
 		out.close()
 		local s = string_uri.read_text()
-		return s == "" and nil or s
+		return s ~= "" and s or nil
 	end
 
 	function this.url()
@@ -105,6 +102,10 @@
 		function this.text_writer()
 			return IoLuan.textWriter(this.java.getWriter())
 		end
+
+		function this.binary_writer()
+			return IoLuan.binaryWriter(this.java.getOutputStream())
+		end
 	end
 	return this
 end
@@ -127,7 +128,7 @@
 		local old_out = Io.stdout
 		local mod = require("site:"..path)
 		mod.respond()
-		text_writer.close()
+		test.text_writer.close()
 		Io.stdout = old_out
 		return result.read_text()
 	end
@@ -137,12 +138,12 @@
 	request = new_request{}
 	request.cookies = test.cookies
 
-	response = {
+	response = new_response{
 
 		text_writer = function()
 			result = Io.uri "string:"
-			text_writer = result.text_writer()
-			return text_writer
+			test.text_writer = result.text_writer()
+			return test.text_writer
 		end;
 
 		set_cookie = function(name,value)
@@ -157,8 +158,6 @@
 			response.redirect = url
 		end;
 
-		headers = {};
-
 	}
 
 end
--- a/http/src/luan/modules/http/HttpServicer.java	Mon May 18 14:24:50 2015 -0600
+++ b/http/src/luan/modules/http/HttpServicer.java	Mon May 18 20:59:30 2015 -0600
@@ -91,7 +91,7 @@
 			for( Enumeration<String> en = request.getHeaders(key); en.hasMoreElements(); ) {
 				values.rawPut(values.rawLength()+1,en.nextElement());
 			}
-			key = key.toLowerCase().replace('-','_');
+			key = toLuanHeaderName(key);
 			headersTbl.rawPut(key,values);
 		}
 
@@ -180,7 +180,7 @@
 		LuanTable responseHeaders = (LuanTable)responseTbl.rawGet("headers");
 		for( Map.Entry<Object,Object> entry : responseHeaders.rawIterable() ) {
 			String name = (String)entry.getKey();
-			name = toHeaderName(name);
+			name = toHttpHeaderName(name);
 			LuanTable values = (LuanTable)entry.getValue();
 			for( Object value : values.asList() ) {
 				if( value instanceof String ) {
@@ -204,7 +204,11 @@
 
 	// static utils
 
-	public static String toHeaderName(String luanName) {
+	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();
--- a/http/src/luan/modules/http/dump.luan	Mon May 18 14:24:50 2015 -0600
+++ b/http/src/luan/modules/http/dump.luan	Mon May 18 20:59:30 2015 -0600
@@ -3,8 +3,12 @@
 local ipairs = Luan.ipairs
 local Io = require "luan:Io"
 local Http = require "luan:http/Http"
+java()
+local HttpServicer = require "java:luan.modules.http.HttpServicer"
 
 
+to_http_header_name = HttpServicer.toHttpHeaderName
+
 function respond()
 	Http.response.header.content_type = "text/plain"
 	Io.stdout = Http.response.text_writer()
@@ -18,14 +22,7 @@
 %>
 <%=method%> <%=path%> <%=Http.request.protocol%> 
 <%
-	for name, values in pairs(Http.request.headers) do
-		local header_name = Http.to_header_name(name)
-		for _, value in ipairs(values) do
-%>
-<%=header_name%>: <%=value%>
-<%
-		end
-	end
+	dump_headers(Http.request.headers)
 %>
 
 <%
@@ -35,3 +32,15 @@
 <%
 	end
 end
+
+
+function 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%>
+<%
+		end
+	end
+end