changeset 1383:a3d0d1c2ce89

add NotFound to luanhost
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 01 Aug 2019 16:49:47 -0600
parents 9604579c1c9b
children f5368cd8c056
files src/luan/host/run.luan src/luan/modules/http/Http.luan src/luan/modules/http/LuanHandler.java src/luan/modules/http/NotFound.java
diffstat 4 files changed, 19 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
diff -r 9604579c1c9b -r a3d0d1c2ce89 src/luan/host/run.luan
--- a/src/luan/host/run.luan	Mon Jul 29 09:50:14 2019 -0600
+++ b/src/luan/host/run.luan	Thu Aug 01 16:49:47 2019 -0600
@@ -7,6 +7,8 @@
 local Hosting = require "luan:host/Hosting.luan"
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "run"
+local NotFound = require "java:luan.modules.http.NotFound"
+local ListHandler = require "java:luan.lib.webserver.handlers.ListHandler"
 local WebHandler = require "java:luan.host.WebHandler"
 Hosting.WebHandler = WebHandler
 
@@ -24,9 +26,10 @@
 local ContentTypeHandler = require "java:luan.lib.webserver.handlers.ContentTypeHandler"
 local SafeHandler = require "java:luan.lib.webserver.handlers.SafeHandler"
 
-local webHandler = WebHandler.new(Hosting.sites_dir)
-local handler = webHandler
+local handler = WebHandler.new(Hosting.sites_dir)
+local not_found_hander = NotFound.new(handler)
 handler = IndexHandler.new(handler)
+handler = ListHandler.new( handler, not_found_hander )
 handler = ContentTypeHandler.new(handler)
 handler = SafeHandler.new(handler)
 local server = Server.ForAddress.new("127.0.0.1",8080,handler)
diff -r 9604579c1c9b -r a3d0d1c2ce89 src/luan/modules/http/Http.luan
--- a/src/luan/modules/http/Http.luan	Mon Jul 29 09:50:14 2019 -0600
+++ b/src/luan/modules/http/Http.luan	Thu Aug 01 16:49:47 2019 -0600
@@ -75,6 +75,7 @@
 	OK = 200
 	MOVED_PERMANENTLY = 301
 	FOUND = 302
+	NOT_FOUND = 404
 	-- add more as needed
 }
 Http.STATUS = STATUS
diff -r 9604579c1c9b -r a3d0d1c2ce89 src/luan/modules/http/LuanHandler.java
--- a/src/luan/modules/http/LuanHandler.java	Mon Jul 29 09:50:14 2019 -0600
+++ b/src/luan/modules/http/LuanHandler.java	Thu Aug 01 16:49:47 2019 -0600
@@ -88,15 +88,17 @@
 		return luan;
 	}
 */
+	static final String NOT_FOUND = "luan-not-found";
+
 	@Override public Response handle(Request request) {
 		if( isDisabled )
 			return null;
 		if( request.path.endsWith("/") )
 			return null;
-		return handle(request,false);
+		return handle( request, request.headers.containsKey(NOT_FOUND) );
 	}
 
-	Response handle(Request request,boolean notFound) {
+	private Response handle(Request request,boolean notFound) {
 		Thread thread = Thread.currentThread();
 		String oldName = thread.getName();
 		thread.setName(request.headers.get("host")+request.path);
diff -r 9604579c1c9b -r a3d0d1c2ce89 src/luan/modules/http/NotFound.java
--- a/src/luan/modules/http/NotFound.java	Mon Jul 29 09:50:14 2019 -0600
+++ b/src/luan/modules/http/NotFound.java	Thu Aug 01 16:49:47 2019 -0600
@@ -6,14 +6,19 @@
 
 
 public class NotFound implements Handler {
-	private final LuanHandler luanHandler;
+	private final Handler handler;
 
-	public NotFound(LuanHandler luanHandler) {
-		this.luanHandler = luanHandler;
+	public NotFound(Handler handler) {
+		this.handler = handler;
 	}
 
 	@Override public Response handle(Request request) {
-		return luanHandler.handle(request,true);
+		request.headers.put(LuanHandler.NOT_FOUND,"whatever");
+		try {
+			return handler.handle(request);
+		} finally {
+			request.headers.remove(LuanHandler.NOT_FOUND);
+		}
 	}
 
 }