changeset 1256:c147e2e877e3

allow subclassing of HttpServicer
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 19 Sep 2018 20:15:16 -0600
parents 4f571a974132
children e38f5869e9df
files src/luan/host/WebHandler.java src/luan/modules/http/Http.luan src/luan/modules/http/HttpServicer.java src/luan/modules/http/LuanHandler.java src/luan/modules/http/Server.luan src/luan/webserver/RequestParser.java
diffstat 6 files changed, 41 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
diff -r 4f571a974132 -r c147e2e877e3 src/luan/host/WebHandler.java
--- a/src/luan/host/WebHandler.java	Mon Sep 03 06:13:55 2018 -0600
+++ b/src/luan/host/WebHandler.java	Wed Sep 19 20:15:16 2018 -0600
@@ -165,7 +165,7 @@
 		LuanTable init = initLuan(luan,dir,domain);
 
 		String loggerRoot = (String)init.rawGet("logger_root");
-		LuanHandler luanHandler = new LuanHandler(luan,loggerRoot);
+		LuanHandler luanHandler = new LuanHandler(luan,loggerRoot,null);
 		NotFound notFoundHandler = new NotFound(luanHandler);
 
 		Handler handler = luanHandler;
diff -r 4f571a974132 -r c147e2e877e3 src/luan/modules/http/Http.luan
--- a/src/luan/modules/http/Http.luan	Mon Sep 03 06:13:55 2018 -0600
+++ b/src/luan/modules/http/Http.luan	Wed Sep 19 20:15:16 2018 -0600
@@ -12,7 +12,6 @@
 local String = require "luan:String.luan"
 local lower = String.lower or error()
 local matches = String.matches or error()
-local HttpServicer = require "java:luan.modules.http.HttpServicer"
 local IoLuan = require "java:luan.modules.IoLuan"
 local JavaLuan = require "java:luan.Luan"
 local Response = require "java:luan.webserver.Response"
diff -r 4f571a974132 -r c147e2e877e3 src/luan/modules/http/HttpServicer.java
--- a/src/luan/modules/http/HttpServicer.java	Mon Sep 03 06:13:55 2018 -0600
+++ b/src/luan/modules/http/HttpServicer.java	Wed Sep 19 20:15:16 2018 -0600
@@ -4,6 +4,7 @@
 import org.slf4j.LoggerFactory;
 import luan.webserver.Request;
 import luan.webserver.Response;
+import luan.webserver.Status;
 import luan.LuanState;
 import luan.LuanFunction;
 import luan.LuanException;
@@ -12,14 +13,31 @@
 import luan.modules.PackageLuan;
 
 
-public final class HttpServicer {
+public class HttpServicer {
 	private static final Logger logger = LoggerFactory.getLogger(HttpServicer.class);
 
-	public static Response service(LuanState luan,Request request,String modName)
+	public Response service(LuanState luan,Request request,String modName) {
+		try {
+			return serviceLuan(luan,request,modName);
+		} catch(LuanException e) {
+			return handleError(request,e);
+		}
+	}
+
+	protected Response handleError(Request request,LuanException e) {
+//e.printStackTrace();
+		String err = e.getLuanStackTraceString();
+		logger.error(err+"\n"+request.rawHead.trim()+"\n");
+		String msg = "Internel Server Error\n\n" + err;
+		return Response.errorResponse( Status.INTERNAL_SERVER_ERROR, msg );
+	}
+
+	protected Response serviceLuan(LuanState luan,Request request,String modName)
 		throws LuanException
 	{
 		LuanFunction fn;
 		synchronized(luan) {
+			inSinchronized(luan,request);
 			PackageLuan.enableLoad(luan,"luan:http/Http.luan",modName);
 			PackageLuan.require(luan,"luan:http/Http.luan");
 			Object mod = PackageLuan.load(luan,modName);
@@ -49,4 +67,8 @@
 		return response;
 	}
 
+	protected void inSinchronized(LuanState luan,Request request)
+		throws LuanException
+	{}
+
 }
diff -r 4f571a974132 -r c147e2e877e3 src/luan/modules/http/LuanHandler.java
--- a/src/luan/modules/http/LuanHandler.java	Mon Sep 03 06:13:55 2018 -0600
+++ b/src/luan/modules/http/LuanHandler.java	Wed Sep 19 20:15:16 2018 -0600
@@ -14,7 +14,6 @@
 import luan.webserver.Response;
 import luan.webserver.Server;
 import luan.webserver.Handler;
-import luan.webserver.Status;
 import luan.webserver.ResponseOutputStream;
 import luan.Luan;
 import luan.LuanState;
@@ -29,6 +28,7 @@
 public class LuanHandler implements Handler, Closeable {
 	private final LuanState luanInit;
 	private final Logger logger;
+	private final HttpServicer httpServicer;
 	private final ReadWriteLock lock = new ReentrantReadWriteLock();
 	private LuanState luan;
 
@@ -41,7 +41,7 @@
 		}
 	}
 
-	public LuanHandler(LuanState luanInit,String loggerRoot) {
+	public LuanHandler(LuanState luanInit,String loggerRoot,HttpServicer httpServicer) {
 		this.luanInit = luanInit;
 		if( loggerRoot==null )
 			loggerRoot = "";
@@ -52,6 +52,7 @@
 		} catch(LuanException e) {
 			throw new RuntimeException(e);
 		}
+		this.httpServicer = httpServicer!=null ? httpServicer : new HttpServicer();
 		setLuan();
 		luanInit.onClose(this);
 	}
@@ -80,14 +81,7 @@
 		thread.setName(request.headers.get("host")+request.path);
 		lock.readLock().lock();
 		try {
-			Response response = HttpServicer.service(luan,request,modName);
-			return response;
-		} catch(LuanException e) {
-//e.printStackTrace();
-			String err = e.getLuanStackTraceString();
-			logger.error(err+"\n"+request.rawHead.trim()+"\n");
-			String msg = "Internel Server Error\n\n" + err;
-			return Response.errorResponse( Status.INTERNAL_SERVER_ERROR, msg );
+			return httpServicer.service(luan,request,modName);
 		} finally {
 			lock.readLock().unlock();
 			thread.setName(oldName);
diff -r 4f571a974132 -r c147e2e877e3 src/luan/modules/http/Server.luan
--- a/src/luan/modules/http/Server.luan	Mon Sep 03 06:13:55 2018 -0600
+++ b/src/luan/modules/http/Server.luan	Wed Sep 19 20:15:16 2018 -0600
@@ -30,20 +30,14 @@
 local Server = {}
 
 function Server.init_dir(dir)
-	local dir_uri, dir_path
-	if matches(dir,":") then
-		dir_uri = dir
-		dir_path = match(dir,"^file:(.*)$") or error "server dir must be scheme 'file:'"
-	else
-		dir_path = dir
-		dir_uri = "file:"..dir
+	if not matches(dir,":") then
+		dir = "file:"..dir
 	end
-	dir_uri = gsub(dir_uri,"/$","")  -- remove trailing '/' if any
-	Http.dir = dir_uri
+	dir = gsub(dir,"/$","")  -- remove trailing '/' if any
+	Http.dir = dir
 	function Io.schemes.site(path)
-		return Io.uri( dir_uri..path )
+		return Io.uri( dir..path )
 	end
-	return dir_path
 end
 
 function Server.start()
@@ -68,7 +62,8 @@
 
 function Server.serve(dir,port)
 	port = port or 8080
-	local dir_path = Server.init_dir(dir)
+	Server.init_dir(dir)
+	local dir_path = match(Http.dir,"^file:(.*)$") or error "server dir must be scheme 'file:'"
 	local file_handler = FileHandler.new(dir_path)
 	local dir_handler = DirHandler.new(file_handler)
 	local luan_handler = LuanHandler.new()
diff -r 4f571a974132 -r c147e2e877e3 src/luan/webserver/RequestParser.java
--- a/src/luan/webserver/RequestParser.java	Mon Sep 03 06:13:55 2018 -0600
+++ b/src/luan/webserver/RequestParser.java	Wed Sep 19 20:15:16 2018 -0600
@@ -174,7 +174,11 @@
 			if( parser.match('=') ) {
 				start = parser.currentIndex();
 				while( parser.noneOf(";") );
-				String value = urlDecode( parser.textFrom(start) );
+				String value = parser.textFrom(start);
+				int len = value.length();
+				if( value.charAt(0)=='"' && value.charAt(len-1)=='"' )
+					value = value.substring(1,len-1);
+				value = urlDecode(value);
 				request.cookies.put(name,value);
 			}
 			if( parser.endOfInput() )