changeset 1138:4189027691b7

webserver - add LogHandler
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 29 Jan 2018 19:48:21 -0700
parents c123ee15f99b
children 8126370ea8c0
files src/luan/webserver/Connection.java src/luan/webserver/Server.java src/luan/webserver/examples/Example.java src/luan/webserver/handlers/LogHandler.java src/luan/webserver/handlers/SafeHandler.java
diffstat 5 files changed, 38 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
diff -r c123ee15f99b -r 4189027691b7 src/luan/webserver/Connection.java
--- a/src/luan/webserver/Connection.java	Mon Jan 29 18:49:59 2018 -0700
+++ b/src/luan/webserver/Connection.java	Mon Jan 29 19:48:21 2018 -0700
@@ -30,8 +30,13 @@
 			int left = a.length;
 			outer: while(true) {
 				int n = in.read(a,size,left);
-				if( n == -1 )
+				if( n == -1 ) {
+					if( size == 0 ) {
+						socket.close();
+						return;
+					}
 					throw new IOException("unexpected end of input at "+size);
+				}
 				size += n;
 				for( int i=0; i<=size-4; i++ ) {
 					if( a[i]=='\r' && a[i+1]=='\n' && a[i+2]=='\r' && a[i+3]=='\n' ) {
diff -r c123ee15f99b -r 4189027691b7 src/luan/webserver/Server.java
--- a/src/luan/webserver/Server.java	Mon Jan 29 18:49:59 2018 -0700
+++ b/src/luan/webserver/Server.java	Mon Jan 29 19:48:21 2018 -0700
@@ -27,7 +27,7 @@
 	public synchronized void start() throws IOException {
 		isRunning = true;
 		final ServerSocket ss = newServerSocket();
-		thread = new Thread("threebody.http.Server") {
+		thread = new Thread("luan.webserver") {
 			public void run() {
 				try {
 					while(isRunning) {
diff -r c123ee15f99b -r 4189027691b7 src/luan/webserver/examples/Example.java
--- a/src/luan/webserver/examples/Example.java	Mon Jan 29 18:49:59 2018 -0700
+++ b/src/luan/webserver/examples/Example.java	Mon Jan 29 19:48:21 2018 -0700
@@ -13,6 +13,7 @@
 import luan.webserver.Server;
 import luan.webserver.handlers.MapHandler;
 import luan.webserver.handlers.SafeHandler;
+import luan.webserver.handlers.LogHandler;
 
 
 public class Example implements Handler {
@@ -41,6 +42,7 @@
 		map.put("/hello",handler);
 		handler = new MapHandler(map);
 		handler = new SafeHandler(handler);
+		handler = new LogHandler(handler);
 		new Server(8080,handler).start();
 	}
 
diff -r c123ee15f99b -r 4189027691b7 src/luan/webserver/handlers/LogHandler.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/webserver/handlers/LogHandler.java	Mon Jan 29 19:48:21 2018 -0700
@@ -0,0 +1,24 @@
+package luan.webserver.handlers;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import luan.webserver.Handler;
+import luan.webserver.Request;
+import luan.webserver.Response;
+
+
+public final class LogHandler implements Handler {
+	private static final Logger logger = LoggerFactory.getLogger("HTTP");
+
+	private final Handler handler;
+
+	public LogHandler(Handler handler) {
+		this.handler = handler;
+	}
+
+	public Response handle(Request request) {
+		Response response = handler.handle(request);
+		logger.info( request.method + " " + request.path + " " + response.status.code + " " + response.body.length );
+		return response;
+	}
+}
diff -r c123ee15f99b -r 4189027691b7 src/luan/webserver/handlers/SafeHandler.java
--- a/src/luan/webserver/handlers/SafeHandler.java	Mon Jan 29 18:49:59 2018 -0700
+++ b/src/luan/webserver/handlers/SafeHandler.java	Mon Jan 29 19:48:21 2018 -0700
@@ -4,6 +4,8 @@
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.IOException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import luan.webserver.Handler;
 import luan.webserver.Request;
 import luan.webserver.Response;
@@ -12,6 +14,8 @@
 
 
 public final class SafeHandler implements Handler {
+	private static final Logger logger = LoggerFactory.getLogger(SafeHandler.class);
+
 	private final Handler handler;
 
 	public SafeHandler(Handler handler) {
@@ -20,13 +24,11 @@
 
 	public Response handle(Request request) {
 		try {
-
 			Response response = handler.handle(request);
 			if( response != null )
 				return response;
-
 		} catch(RuntimeException e) {
-
+			logger.error("",e);
 			Response response = new Response();
 			response.status = Status.INTERNAL_SERVER_ERROR;
 			response.headers.put( "Content-Type", "text/plain; charset=UTF-8" );
@@ -35,7 +37,6 @@
 			e.printStackTrace(writer);
 			writer.close();
 			return response;
-
 		}
 
 		Response response = new Response();