changeset 1167:7e6f28c769a1

better handlers
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 08 Feb 2018 19:06:31 -0700
parents 7ef40e1923b7
children 312e4cadd508
files src/luan/modules/http/impl/HttpServicer.java src/luan/modules/http/impl/LuanHandler.java src/luan/modules/http/impl/NotFound.java src/luan/modules/http/impl/Server.luan src/luan/webserver/examples/Example.java src/luan/webserver/handlers/DirHandler.java src/luan/webserver/handlers/FileHandler.java src/luan/webserver/handlers/IndexHandler.java
diffstat 8 files changed, 98 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/http/impl/HttpServicer.java	Thu Feb 08 02:22:51 2018 -0700
+++ b/src/luan/modules/http/impl/HttpServicer.java	Thu Feb 08 19:06:31 2018 -0700
@@ -15,14 +15,11 @@
 public final class HttpServicer {
 	private static final Logger logger = LoggerFactory.getLogger(HttpServicer.class);
 
-	public static Response service(LuanState luan,Request request)
+	public static Response service(LuanState luan,Request request,String modName)
 		throws LuanException
 	{
-		if( request.path.endsWith("/") )
-			return null;
 		LuanFunction fn;
 		synchronized(luan) {
-			String modName = "site:" + request.path +".luan";
 			PackageLuan.enableLoad(luan,"luan:http/Http.luan",modName);
 			LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan");
 			Object mod = PackageLuan.load(luan,modName);
--- a/src/luan/modules/http/impl/LuanHandler.java	Thu Feb 08 02:22:51 2018 -0700
+++ b/src/luan/modules/http/impl/LuanHandler.java	Thu Feb 08 19:06:31 2018 -0700
@@ -57,12 +57,19 @@
 	}
 
 	@Override public Response handle(Request request) {
+		if( request.path.endsWith("/") )
+			return null;
+		String modName = "site:" + request.path +".luan";
+		return handle(request,modName);
+	}
+
+	Response handle(Request request,String modName) {
 		Thread thread = Thread.currentThread();
 		String oldName = thread.getName();
 		thread.setName(request.headers.get("host")+request.path);
 		lock.readLock().lock();
 		try {
-			Response response = HttpServicer.service(luan,request);
+			Response response = HttpServicer.service(luan,request,modName);
 			return response;
 		} catch(LuanException e) {
 //e.printStackTrace();
--- a/src/luan/modules/http/impl/NotFound.java	Thu Feb 08 02:22:51 2018 -0700
+++ b/src/luan/modules/http/impl/NotFound.java	Thu Feb 08 19:06:31 2018 -0700
@@ -6,24 +6,14 @@
 
 
 public class NotFound implements Handler {
-	private final Handler handler;
+	private final LuanHandler luanHandler;
 
-	public NotFound(Handler handler) {
-		this.handler = handler;
+	public NotFound(LuanHandler luanHandler) {
+		this.luanHandler = luanHandler;
 	}
 
 	@Override public Response handle(Request request) {
-		Response response = handler.handle(request);
-		if( response == null ) {
-			String path = request.path;
-			try {
-				request.path = "/not_found";
-				response = handler.handle(request);
-			} finally {
-				request.path = path;
-			}
-		}
-		return response;
+		return luanHandler.handle(request,"site:/not_found.luan");
 	}
 
 }
--- a/src/luan/modules/http/impl/Server.luan	Thu Feb 08 02:22:51 2018 -0700
+++ b/src/luan/modules/http/impl/Server.luan	Thu Feb 08 19:06:31 2018 -0700
@@ -8,7 +8,7 @@
 local Package = require "luan:Package.luan"
 local Rpc = require "luan:Rpc.luan"
 local Thread = require "luan:Thread.luan"
---local Http = require "luan:http/Http.luan"
+local Http = require "luan:http/Http.luan"
 require "luan:logging/init.luan"  -- initialize logging
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "http/Server"
@@ -16,6 +16,7 @@
 java()
 local JavaServer = require "java:luan.webserver.Server"
 local FileHandler = require "java:luan.webserver.handlers.FileHandler"
+local DirHandler = require "java:luan.webserver.handlers.DirHandler"
 local IndexHandler = require "java:luan.webserver.handlers.IndexHandler"
 local ContentTypeHandler = require "java:luan.webserver.handlers.ContentTypeHandler"
 local SafeHandler = require "java:luan.webserver.handlers.SafeHandler"
@@ -39,19 +40,20 @@
 		dir_uri = "file:"..dir
 	end
 	dir_uri = gsub(dir_uri,"/$","")  -- remove trailing '/' if any
---	Http.dir = dir_uri
+	Http.dir = dir_uri
 	function Io.schemes.site(path)
 		return Io.uri( dir_uri..path )
 	end
-	local handler = FileHandler.new(dir_path)
+	local file_handler = FileHandler.new(dir_path)
+	local dir_handler = DirHandler.new(dir_path)
 	local luan_handler = LuanHandler.new()
-	handler = ListHandler.new( luan_handler, handler )
+	local not_found_hander = NotFound.new(luan_handler)
+	local handler = ListHandler.new( luan_handler, file_handler )
 	handler = IndexHandler.new(handler)
-	handler = NotFound.new(handler)
+	handler = ListHandler.new( handler, dir_handler, not_found_hander )
 	handler = ContentTypeHandler.new(handler)
 	handler = SafeHandler.new(handler)
 	handler = LogHandler.new(handler)
---	Server.handlers.addHandler(NotFound.new(Server.luan_handler))
 	Server.server = JavaServer.new(Server.port,handler)
 end
 
--- a/src/luan/webserver/examples/Example.java	Thu Feb 08 02:22:51 2018 -0700
+++ b/src/luan/webserver/examples/Example.java	Thu Feb 08 19:06:31 2018 -0700
@@ -17,6 +17,7 @@
 import luan.webserver.handlers.SafeHandler;
 import luan.webserver.handlers.LogHandler;
 import luan.webserver.handlers.FileHandler;
+import luan.webserver.handlers.DirHandler;
 import luan.webserver.handlers.ListHandler;
 import luan.webserver.handlers.ContentTypeHandler;
 
@@ -48,7 +49,7 @@
 		map.put( "/params", new Params() );
 		map.put( "/cookies", new Cookies() );
 		Handler handler = new MapHandler(map);
-		handler = new ListHandler( handler, new FileHandler() );
+		handler = new ListHandler( handler, new FileHandler(), new DirHandler() );
 		handler = new ContentTypeHandler(handler);
 		handler = new SafeHandler(handler);
 		handler = new LogHandler(handler);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/webserver/handlers/DirHandler.java	Thu Feb 08 19:06:31 2018 -0700
@@ -0,0 +1,60 @@
+package luan.webserver.handlers;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.Writer;
+import java.io.OutputStreamWriter;
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import luan.webserver.Handler;
+import luan.webserver.Request;
+import luan.webserver.Response;
+import luan.webserver.ResponseOutputStream;
+
+
+public final class DirHandler extends FileHandler {
+
+	public DirHandler() {
+		super();
+	}
+
+	public DirHandler(String pathname) {
+		super(pathname);
+	}
+
+	public DirHandler(File dir) {
+		super(dir);
+	}
+
+	Response handle(Request request,File file) throws IOException {
+		if( request.path.endsWith("/") && file.isDirectory() ) {
+			DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
+			Response response = new Response();
+			response.headers.put( "content-type", "text/html; charset=utf-8" );
+			Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
+			writer.write( "<!doctype html><html>" );
+			writer.write( "<head><style>td{padding: 2px 8px}</style></head>" );
+			writer.write( "<body>" );
+			writer.write( "<h1>Directory: "+request.path+"</h1>" );
+			writer.write( "<table border=0>" );
+			for( File child : file.listFiles() ) {
+				String name = child.getName();
+				if( child.isDirectory() )
+					name += '/';
+				writer.write( "<tr>" );
+				writer.write( "<td><a href='"+name+"'>"+name+"</a></td>" );
+				writer.write( "<td>"+child.length()+" bytes</td>" );
+				writer.write( "<td>"+fmt.format(new Date(child.lastModified()))+"</td>" );
+				writer.write( "</tr>" );
+			}
+			writer.write( "</table>" );
+			writer.write( "</body>" );
+			writer.write( "</html>" );
+			writer.close();
+			return response;
+		}
+		return null;
+	}
+}
--- a/src/luan/webserver/handlers/FileHandler.java	Thu Feb 08 02:22:51 2018 -0700
+++ b/src/luan/webserver/handlers/FileHandler.java	Thu Feb 08 19:06:31 2018 -0700
@@ -14,9 +14,8 @@
 import luan.webserver.ResponseOutputStream;
 
 
-public final class FileHandler implements Handler {
-	public boolean showDirs = true;
-	private final File dir;
+public class FileHandler implements Handler {
+	final File dir;
 
 	public FileHandler() {
 		this(".");
@@ -35,40 +34,18 @@
 	public Response handle(Request request) {
 		try {
 			File file = new File(dir,request.path);
-			if( file.isFile() ) {
-				Response response = new Response();
-				response.body = new Response.Body( file.length(), new FileInputStream(file) );
-				return response;
-			}
-			if( request.path.endsWith("/") && file.isDirectory() && showDirs ) {
-				DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
-				Response response = new Response();
-				response.headers.put( "content-type", "text/html; charset=utf-8" );
-				Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
-				writer.write( "<!doctype html><html>" );
-				writer.write( "<head><style>td{padding: 2px 8px}</style></head>" );
-				writer.write( "<body>" );
-				writer.write( "<h1>Directory: "+request.path+"</h1>" );
-				writer.write( "<table border=0>" );
-				for( File child : file.listFiles() ) {
-					String name = child.getName();
-					if( child.isDirectory() )
-						name += '/';
-					writer.write( "<tr>" );
-					writer.write( "<td><a href='"+name+"'>"+name+"</a></td>" );
-					writer.write( "<td>"+child.length()+" bytes</td>" );
-					writer.write( "<td>"+fmt.format(new Date(child.lastModified()))+"</td>" );
-					writer.write( "</tr>" );
-				}
-				writer.write( "</table>" );
-				writer.write( "</body>" );
-				writer.write( "</html>" );
-				writer.close();
-				return response;
-			}
-			return null;
+			return handle(request,file);
 		} catch(IOException e) {
 			throw new RuntimeException(e);
 		}
 	}
+
+	Response handle(Request request,File file) throws IOException {
+		if( file.isFile() ) {
+			Response response = new Response();
+			response.body = new Response.Body( file.length(), new FileInputStream(file) );
+			return response;
+		}
+		return null;
+	}
 }
--- a/src/luan/webserver/handlers/IndexHandler.java	Thu Feb 08 02:22:51 2018 -0700
+++ b/src/luan/webserver/handlers/IndexHandler.java	Thu Feb 08 19:06:31 2018 -0700
@@ -23,13 +23,11 @@
 			String path = request.path;
 			try {
 				request.path += indexName;
-				Response response = handler.handle(request);
-				if( response != null )
-					return response;
+				return handler.handle(request);
 			} finally {
 				request.path = path;
 			}
-		}
-		return handler.handle(request);
+		} else
+			return handler.handle(request);
 	}
 }