Mercurial Hosting > luan
changeset 1168:312e4cadd508
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 08 Feb 2018 19:17:57 -0700 |
parents | 7e6f28c769a1 |
children | 96e7b49ce013 |
files | 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 |
diffstat | 4 files changed, 51 insertions(+), 51 deletions(-) [+] |
line wrap: on
line diff
diff -r 7e6f28c769a1 -r 312e4cadd508 src/luan/modules/http/impl/Server.luan --- a/src/luan/modules/http/impl/Server.luan Thu Feb 08 19:06:31 2018 -0700 +++ b/src/luan/modules/http/impl/Server.luan Thu Feb 08 19:17:57 2018 -0700 @@ -45,7 +45,7 @@ return Io.uri( dir_uri..path ) end local file_handler = FileHandler.new(dir_path) - local dir_handler = DirHandler.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( luan_handler, file_handler )
diff -r 7e6f28c769a1 -r 312e4cadd508 src/luan/webserver/examples/Example.java --- a/src/luan/webserver/examples/Example.java Thu Feb 08 19:06:31 2018 -0700 +++ b/src/luan/webserver/examples/Example.java Thu Feb 08 19:17:57 2018 -0700 @@ -48,8 +48,10 @@ map.put( "/headers", new Headers() ); map.put( "/params", new Params() ); map.put( "/cookies", new Cookies() ); - Handler handler = new MapHandler(map); - handler = new ListHandler( handler, new FileHandler(), new DirHandler() ); + Handler mapHandler = new MapHandler(map); + FileHandler fileHandler = new FileHandler(); + Handler dirHandler = new DirHandler(fileHandler); + Handler handler = new ListHandler( mapHandler, fileHandler, dirHandler ); handler = new ContentTypeHandler(handler); handler = new SafeHandler(handler); handler = new LogHandler(handler);
diff -r 7e6f28c769a1 -r 312e4cadd508 src/luan/webserver/handlers/DirHandler.java --- a/src/luan/webserver/handlers/DirHandler.java Thu Feb 08 19:06:31 2018 -0700 +++ b/src/luan/webserver/handlers/DirHandler.java Thu Feb 08 19:17:57 2018 -0700 @@ -14,47 +14,45 @@ import luan.webserver.ResponseOutputStream; -public final class DirHandler extends FileHandler { - - public DirHandler() { - super(); - } +public final class DirHandler implements Handler { + private final FileHandler fileHandler; - public DirHandler(String pathname) { - super(pathname); - } - - public DirHandler(File dir) { - super(dir); + public DirHandler(FileHandler fileHandler) { + this.fileHandler = fileHandler; } - 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>" ); + public Response handle(Request request) { + try { + File file = fileHandler.file(request); + 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; } - writer.write( "</table>" ); - writer.write( "</body>" ); - writer.write( "</html>" ); - writer.close(); - return response; + return null; + } catch(IOException e) { + throw new RuntimeException(e); } - return null; } }
diff -r 7e6f28c769a1 -r 312e4cadd508 src/luan/webserver/handlers/FileHandler.java --- a/src/luan/webserver/handlers/FileHandler.java Thu Feb 08 19:06:31 2018 -0700 +++ b/src/luan/webserver/handlers/FileHandler.java Thu Feb 08 19:17:57 2018 -0700 @@ -31,21 +31,21 @@ this.dir = dir; } + File file(Request request) { + return new File(dir,request.path); + } + public Response handle(Request request) { try { - File file = new File(dir,request.path); - return handle(request,file); + File file = file(request); + if( file.isFile() ) { + Response response = new Response(); + response.body = new Response.Body( file.length(), new FileInputStream(file) ); + return response; + } + return null; } 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; - } }