changeset 1358:1d31c1f3ea30

better not_found_handler
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 19 Apr 2019 00:47:58 -0600
parents 70a7eb76ee2c
children 9721d4709bfb
files src/luan/modules/http/LuanHandler.java src/luan/modules/http/NotFound.java src/luan/modules/http/Server.luan
diffstat 3 files changed, 38 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
diff -r 70a7eb76ee2c -r 1d31c1f3ea30 src/luan/modules/http/LuanHandler.java
--- 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 {
diff -r 70a7eb76ee2c -r 1d31c1f3ea30 src/luan/modules/http/NotFound.java
--- a/src/luan/modules/http/NotFound.java	Thu Apr 18 00:55:21 2019 -0600
+++ b/src/luan/modules/http/NotFound.java	Fri Apr 19 00:47:58 2019 -0600
@@ -13,7 +13,7 @@
 	}
 
 	@Override public Response handle(Request request) {
-		return luanHandler.handle(request,"site:/not_found.luan");
+		return luanHandler.handle(request,true);
 	}
 
 }
diff -r 70a7eb76ee2c -r 1d31c1f3ea30 src/luan/modules/http/Server.luan
--- a/src/luan/modules/http/Server.luan	Thu Apr 18 00:55:21 2019 -0600
+++ b/src/luan/modules/http/Server.luan	Fri Apr 19 00:47:58 2019 -0600
@@ -25,6 +25,7 @@
 local ListHandler = require "java:luan.lib.webserver.handlers.ListHandler"
 local LuanHandler = require "java:luan.modules.http.LuanHandler"
 local System = require "java:java.lang.System"
+local NotFound = require "java:luan.modules.http.NotFound"
 
 
 local Server = {}
@@ -67,9 +68,10 @@
 	local file_handler = FileHandler.new(dir_path)
 	local dir_handler = DirHandler.new(file_handler)
 	local luan_handler = LuanHandler.new()
+	local not_found_hander = NotFound.new(luan_handler)
 	local handler = ListHandler.new( file_handler, luan_handler )
 	handler = IndexHandler.new(handler)
-	handler = ListHandler.new( handler, dir_handler )
+	handler = ListHandler.new( handler, dir_handler, not_found_hander )
 	handler = ContentTypeHandler.new(handler)
 	handler = SafeHandler.new(handler)
 	handler = LogHandler.new(handler)