diff src/luan/modules/http/LuanHandler.java @ 1358:1d31c1f3ea30

better not_found_handler
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 19 Apr 2019 00:47:58 -0600
parents 21b153b4bcc4
children 9721d4709bfb
line wrap: on
line diff
--- a/src/luan/modules/http/LuanHandler.java	Thu Apr 18 00:55:21 2019 -0600
+++ b/src/luan/modules/http/LuanHandler.java	Fri Apr 19 00:47:58 2019 -0600
@@ -93,17 +93,16 @@
 			return null;
 		if( request.path.endsWith("/") )
 			return null;
-		String modName = "site:" + request.path +".luan";
-		return handle(request,modName);
+		return handle(request,false);
 	}
 
-	Response handle(Request request,String modName) {
+	Response handle(Request request,boolean notFound) {
 		Thread thread = Thread.currentThread();
 		String oldName = thread.getName();
 		thread.setName(request.headers.get("host")+request.path);
 		lock.readLock().lock();
 		try {
-			return service(request,modName);
+			return service(request,notFound);
 		} catch(LuanException e) {
 			String err = e.getLuanStackTraceString();
 			logger.error(err+"\n"+request.rawHead.trim()+"\n");
@@ -209,11 +208,14 @@
 
 	// from HttpServicer
 
-	private Response service(Request request,String modName)
+	private Response service(Request request,boolean notFound)
 		throws LuanException
 	{
 		try {
-			return serviceLuan(request,modName);
+			if( !notFound )
+				return serviceLuan(request);
+			else
+				return serviceNotFound(request);
 		} catch(LuanException e) {
 			return handleError(request,e);
 		}
@@ -232,9 +234,10 @@
 		return (Response)module.fn("handle_error").call( request, e.table(luan) );
 	}
 
-	private Response serviceLuan(Request request,String modName)
+	private Response serviceLuan(Request request)
 		throws LuanException
 	{
+		String modName = "site:" + request.path +".luan";
 		LuanFunction fn;
 		Luan luan;
 		synchronized(luanInit) {
@@ -249,14 +252,34 @@
 			luan = (Luan)cloner.clone(currentLuan);
 			fn = (LuanFunction)cloner.get(mod);
 		}
-
 		LuanTable module = (LuanTable)luan.require("luan:http/Http.luan");
 		module.fn("new_request").call(request);
 		module.fn("new_response").call();
+		fn.call();
+		return (Response)module.fn("finish").call();
+	}
 
-		fn.call();
-
-		return (Response)module.fn("finish").call();
+	private Response serviceNotFound(Request request)
+		throws LuanException
+	{
+		Luan luan;
+		synchronized(luanInit) {
+			enableLoad("luan:http/Http.luan");
+ 			PackageLuan.require(currentLuan,"luan:http/Http.luan");
+			LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL);
+			luan = (Luan)cloner.clone(currentLuan);
+		}
+		LuanTable module = (LuanTable)luan.require("luan:http/Http.luan");
+		LuanFunction fn = module.fn("not_found_handler");
+		if( fn == null )
+			return null;
+		module.fn("new_request").call(request);
+		module.fn("new_response").call();
+		Object obj = Luan.first(fn.call());
+		if( !(obj instanceof Boolean) )
+			throw new LuanException("not_found_handler must return boolean");
+		boolean handled = (Boolean)obj;
+		return handled ? (Response)module.fn("finish").call() : null;
 	}
 
 	private void enableLoad(String... mods) throws LuanException {