changeset 1165:668f29bc52ea

clean up content-type
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 07 Feb 2018 23:16:12 -0700
parents 1f9d34a6f308
children 7ef40e1923b7
files src/luan/modules/http/impl/Http.luan src/luan/modules/http/tools/Dump_mod.luan src/luan/modules/http/tools/run.luan src/luan/modules/url/LuanUrl.java src/luan/modules/url/MultipartClient.java src/luan/modules/url/UrlCall.java src/luan/webserver/Response.java src/luan/webserver/examples/Cookies.java src/luan/webserver/examples/Example.java src/luan/webserver/examples/Headers.java src/luan/webserver/examples/Params.java src/luan/webserver/handlers/ContentTypeHandler.java src/luan/webserver/handlers/FileHandler.java src/luan/webserver/handlers/SafeHandler.java
diffstat 14 files changed, 20 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/modules/http/impl/Http.luan
--- a/src/luan/modules/http/impl/Http.luan	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/modules/http/impl/Http.luan	Wed Feb 07 23:16:12 2018 -0700
@@ -86,7 +86,7 @@
 		this.reset()
 		this.status = status
 		if msg ~= nil then
-			this.headers["content-type"] = "text/plain"
+			this.headers["content-type"] = "text/plain; charset=utf-8"
 			local writer = this.text_writer()
 			writer.write(msg)
 		end
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/modules/http/tools/Dump_mod.luan
--- a/src/luan/modules/http/tools/Dump_mod.luan	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/modules/http/tools/Dump_mod.luan	Wed Feb 07 23:16:12 2018 -0700
@@ -9,7 +9,7 @@
 local Dump_mod = {}
 
 function Dump_mod.respond()
-	Http.response.headers["content-type"] = "text/plain"
+	Http.response.headers["content-type"] = "text/plain; charset=utf-8"
 	Io.stdout = Http.response.text_writer()
 
 	local method = Http.request.method
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/modules/http/tools/run.luan
--- a/src/luan/modules/http/tools/run.luan	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/modules/http/tools/run.luan	Wed Feb 07 23:16:12 2018 -0700
@@ -61,7 +61,7 @@
 	<body>
 		<h2>Run Luan Code</h2>
 		<form name="form0" method="post">
-			<input type="hidden" name="content_type" value="text/plain" />
+			<input type="hidden" name="content_type" value="text/plain; charset=utf-8" />
 			<div>
 				<textarea name="code" rows="20" cols="90" wrap="off" autofocus></textarea>
 			</div>
@@ -91,7 +91,7 @@
 		end;
 		catch = function(e)
 			Http.response.reset()
-			Http.response.headers["content-type"] = "text/plain"
+			Http.response.headers["content-type"] = "text/plain; charset=utf-8"
 			Io.stdout = Http.response.text_writer()
 			print(e)
 			print""
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/modules/url/LuanUrl.java
--- a/src/luan/modules/url/LuanUrl.java	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/modules/url/LuanUrl.java	Wed Feb 07 23:16:12 2018 -0700
@@ -229,7 +229,7 @@
 
 		// POST
 
-//		httpCon.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
+//		httpCon.setRequestProperty("content-type","application/x-www-form-urlencoded");
 		httpCon.setDoOutput(true);
 		httpCon.setRequestMethod("POST");
 
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/modules/url/MultipartClient.java
--- a/src/luan/modules/url/MultipartClient.java	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/modules/url/MultipartClient.java	Wed Feb 07 23:16:12 2018 -0700
@@ -46,7 +46,7 @@
 		String boundary = "luan" + System.identityHashCode(this) + Long.toString(System.currentTimeMillis(),36);
 		byte[] boundaryBytes = boundary.getBytes(__ISO_8859_1);
 
-		httpCon.setRequestProperty("Content-Type","multipart/form-data; boundary="+boundary);
+		httpCon.setRequestProperty("content-type","multipart/form-data; boundary="+boundary);
 		OutputStream out = httpCon.getOutputStream();
 		for( Object hack : params.entrySet() ) {
 			Map.Entry entry = (Map.Entry)hack;
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/modules/url/UrlCall.java
--- a/src/luan/modules/url/UrlCall.java	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/modules/url/UrlCall.java	Wed Feb 07 23:16:12 2018 -0700
@@ -40,7 +40,7 @@
 	public String post(String content,String contentType) throws IOException {
 		HttpURLConnection connection = (HttpURLConnection)this.connection;
 
-		connection.setRequestProperty("Content-type",contentType);
+		connection.setRequestProperty("content-type",contentType);
 		connection.setDoOutput(true);
 		connection.setRequestMethod("POST");
 
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/webserver/Response.java
--- a/src/luan/webserver/Response.java	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/webserver/Response.java	Wed Feb 07 23:16:12 2018 -0700
@@ -76,7 +76,7 @@
 	public static Response errorResponse(Status status,String text) {
 		Response response = new Response();
 		response.status = status;
-		response.headers.put( "content-type", "text/plain; charset=UTF-8" );
+		response.headers.put( "content-type", "text/plain; charset=utf-8" );
 		PrintWriter writer = new PrintWriter( new ResponseOutputStream(response) );
 		writer.write( text );
 		writer.close();
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/webserver/examples/Cookies.java
--- a/src/luan/webserver/examples/Cookies.java	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/webserver/examples/Cookies.java	Wed Feb 07 23:16:12 2018 -0700
@@ -26,7 +26,7 @@
 				response.setCookie(name,"delete",attributes);
 			}
 		}
-		response.headers.put( "content-type", "text/plain; charset=UTF-8" );
+		response.headers.put( "content-type", "text/plain; charset=utf-8" );
 		try {
 			Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
 			for( Map.Entry<String,String> entry : request.cookies.entrySet() ) {
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/webserver/examples/Example.java
--- a/src/luan/webserver/examples/Example.java	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/webserver/examples/Example.java	Wed Feb 07 23:16:12 2018 -0700
@@ -25,7 +25,7 @@
 
 	public Response handle(Request request) {
 		Response response = new Response();
-		response.headers.put( "content-type", "text/plain; charset=UTF-8" );
+		response.headers.put( "content-type", "text/plain; charset=utf-8" );
 		try {
 			Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
 			writer.write("Hello World\n");
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/webserver/examples/Headers.java
--- a/src/luan/webserver/examples/Headers.java	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/webserver/examples/Headers.java	Wed Feb 07 23:16:12 2018 -0700
@@ -14,7 +14,7 @@
 
 	public Response handle(Request request) {
 		Response response = new Response();
-		response.headers.put( "content-type", "text/plain; charset=UTF-8" );
+		response.headers.put( "content-type", "text/plain; charset=utf-8" );
 		try {
 			Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
 			for( Map.Entry<String,Object> entry : request.headers.entrySet() ) {
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/webserver/examples/Params.java
--- a/src/luan/webserver/examples/Params.java	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/webserver/examples/Params.java	Wed Feb 07 23:16:12 2018 -0700
@@ -14,7 +14,7 @@
 
 	public Response handle(Request request) {
 		Response response = new Response();
-		response.headers.put( "content-type", "text/plain; charset=UTF-8" );
+		response.headers.put( "content-type", "text/plain; charset=utf-8" );
 		try {
 			Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
 			for( Map.Entry<String,Object> entry : request.parameters.entrySet() ) {
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/webserver/handlers/ContentTypeHandler.java
--- a/src/luan/webserver/handlers/ContentTypeHandler.java	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/webserver/handlers/ContentTypeHandler.java	Wed Feb 07 23:16:12 2018 -0700
@@ -8,8 +8,6 @@
 
 
 public class ContentTypeHandler implements Handler {
-	public final static String CONTENT_TYPE = "content-type";
-
 	private final Handler handler;
 
 	// maps extension to content-type
@@ -20,13 +18,14 @@
 	public String contentTypeForNoExtension;
 
 	public ContentTypeHandler(Handler handler) {
-		this(handler,"UTF-8");
+		this(handler,"utf-8");
 	}
 
 	public ContentTypeHandler(Handler handler,String charset) {
 		this.handler = handler;
-		String htmlType = "text/html; charset=" + charset;
-		String textType = "text/plain; charset=" + charset;
+		String attrs = charset== null ? "" : "; charset="+charset;
+		String htmlType = "text/html" + attrs;
+		String textType = "text/plain" + attrs;
 		contentTypeForNoExtension = htmlType;
 		map.put( "html", htmlType );
 		map.put( "txt", textType );
@@ -35,7 +34,7 @@
 
 	public Response handle(Request request) {
 		Response response = handler.handle(request);
-		if( response!=null && !response.headers.containsKey(CONTENT_TYPE) ) {
+		if( response!=null && !response.headers.containsKey("content-type") ) {
 			String path = request.path;
 			int iSlash = path.lastIndexOf('/');
 			int iDot = path.lastIndexOf('.');
@@ -47,7 +46,7 @@
 				type = map.get( extension.toLowerCase() );
 			}
 			if( type != null )
-				response.headers.put(CONTENT_TYPE,type);
+				response.headers.put("content-type",type);
 		}
 		return response;
 	}
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/webserver/handlers/FileHandler.java
--- a/src/luan/webserver/handlers/FileHandler.java	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/webserver/handlers/FileHandler.java	Wed Feb 07 23:16:12 2018 -0700
@@ -43,7 +43,7 @@
 			if( request.path.endsWith("/") && file.isDirectory() && showDirs ) {
 				DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
 				Response response = new Response();
-				response.headers.put( "content-type", "text/html; charset=UTF-8" );
+				response.headers.put( "content-type", "text/html; charset=utf-8" );
 				Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
 				writer.write( "<!doctype html><html>" );
 				writer.write( "<head><style>td{padding: 2px 8px}</style></head>" );
diff -r 1f9d34a6f308 -r 668f29bc52ea src/luan/webserver/handlers/SafeHandler.java
--- a/src/luan/webserver/handlers/SafeHandler.java	Wed Feb 07 17:36:38 2018 -0700
+++ b/src/luan/webserver/handlers/SafeHandler.java	Wed Feb 07 23:16:12 2018 -0700
@@ -31,7 +31,7 @@
 			logger.error("",e);
 			Response response = new Response();
 			response.status = Status.INTERNAL_SERVER_ERROR;
-			response.headers.put( "content-type", "text/plain; charset=UTF-8" );
+			response.headers.put( "content-type", "text/plain; charset=utf-8" );
 			PrintWriter writer = new PrintWriter( new ResponseOutputStream(response) );
 			writer.write( "Internel Server Error\n\n" );
 			e.printStackTrace(writer);