changeset 1161:6baccd0c85a7

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 05 Feb 2018 22:59:52 -0700
parents 4beabb087be6
children e2d2354807f3
files src/luan/modules/http/impl/Http.luan src/luan/modules/http/impl/LuanHandler.java src/luan/webserver/Response.java src/luan/webserver/handlers/SafeHandler.java
diffstat 4 files changed, 24 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
diff -r 4beabb087be6 -r 6baccd0c85a7 src/luan/modules/http/impl/Http.luan
--- a/src/luan/modules/http/impl/Http.luan	Mon Feb 05 22:33:59 2018 -0700
+++ b/src/luan/modules/http/impl/Http.luan	Mon Feb 05 22:59:52 2018 -0700
@@ -77,6 +77,15 @@
 		this.headers["location"] = location
 	end
 
+	function this.send_error(status,msg)
+		this.status = status
+		if msg ~= nil then
+			this.headers["content-type"] = "text/plain"
+			local writer = this.text_writer()
+			writer.write(msg)
+		end
+	end
+
 	function this.set_cookie(name,value,attributes)
 		HttpServicer.setCookie(this.java,name,value,attributes)
 	end
@@ -94,14 +103,12 @@
 	end
 
 	function this.text_writer()
-		this.writer and error "writer already set"
 		this.writer = ResponseOutputStream.new(this.java)
 		this.writer = OutputStreamWriter.new(this.writer)
 		return IoLuan.textWriter(this.writer)
 	end
 
 	function this.binary_writer()
-		this.writer and error "writer already set"
 		this.writer = ResponseOutputStream.new(this.java)
 		return IoLuan.binaryWriter(this.writer)
 	end
diff -r 4beabb087be6 -r 6baccd0c85a7 src/luan/modules/http/impl/LuanHandler.java
--- a/src/luan/modules/http/impl/LuanHandler.java	Mon Feb 05 22:33:59 2018 -0700
+++ b/src/luan/modules/http/impl/LuanHandler.java	Mon Feb 05 22:59:52 2018 -0700
@@ -68,14 +68,8 @@
 //e.printStackTrace();
 			String err = e.getLuanStackTraceString();
 			logger.error(err);
-			Response response = new Response();
-			response.status = Status.INTERNAL_SERVER_ERROR;
-			response.headers.put( "content-type", "text/plain; charset=UTF-8" );
-			PrintWriter writer = new PrintWriter( new ResponseOutputStream(response) );
-			writer.write( "Internel Server Error\n\n" );
-			writer.write( err );
-			writer.close();
-			return response;
+			String msg = "Internel Server Error\n\n" + err;
+			return Response.errorResponse( Status.INTERNAL_SERVER_ERROR, msg );
 		} finally {
 			lock.readLock().unlock();
 			thread.setName(oldName);
diff -r 4beabb087be6 -r 6baccd0c85a7 src/luan/webserver/Response.java
--- a/src/luan/webserver/Response.java	Mon Feb 05 22:33:59 2018 -0700
+++ b/src/luan/webserver/Response.java	Mon Feb 05 22:59:52 2018 -0700
@@ -1,6 +1,7 @@
 package luan.webserver;
 
 import java.io.InputStream;
+import java.io.PrintWriter;
 import java.util.Map;
 import java.util.LinkedHashMap;
 import java.util.Collections;
@@ -70,4 +71,15 @@
 		sb.append( "\r\n" );
 		return sb.toString();
 	}
+
+
+	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" );
+		PrintWriter writer = new PrintWriter( new ResponseOutputStream(response) );
+		writer.write( text );
+		writer.close();
+		return response;
+	}
 }
diff -r 4beabb087be6 -r 6baccd0c85a7 src/luan/webserver/handlers/SafeHandler.java
--- a/src/luan/webserver/handlers/SafeHandler.java	Mon Feb 05 22:33:59 2018 -0700
+++ b/src/luan/webserver/handlers/SafeHandler.java	Mon Feb 05 22:59:52 2018 -0700
@@ -38,18 +38,7 @@
 			writer.close();
 			return response;
 		}
-
-		Response response = new Response();
-		response.status = Status.NOT_FOUND;
-		response.headers.put( "content-type", "text/plain; charset=UTF-8" );
-		try {
-			Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
-			writer.write( request.path+" not found\n" );
-			writer.close();
-		} catch(IOException e) {
-			throw new RuntimeException(e);
-		}
-		return response;
+		return Response.errorResponse( Status.NOT_FOUND, request.path+" not found\n" );
 	}
 
 }