add autoindex to example.FileServer
This commit is contained in:
parent
80c7e0961a
commit
971f7e75e1
2 changed files with 54 additions and 25 deletions
|
@ -1,5 +1,6 @@
|
||||||
package de.tudbut.tryumph.example;
|
package de.tudbut.tryumph.example;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
@ -58,28 +59,28 @@ public class FileServer implements IRequestCatcher, RequestHandler.Listener {
|
||||||
|
|
||||||
private String fileContent(Request request, String s) {
|
private String fileContent(Request request, String s) {
|
||||||
String r;
|
String r;
|
||||||
if(!s.endsWith(".html") && !s.endsWith(".htm")) {
|
if(new File(s + "/index.html").exists())
|
||||||
r = request.context.file(s);
|
s += "/index.html";
|
||||||
|
if(!s.endsWith(".html") && !s.endsWith(".htm") && !(new File(s).isDirectory())) {
|
||||||
|
r = request.context.file(s, data.getBoolean("autoindex") ? request.realPath : null);
|
||||||
r = "<pre>" + r.replace("<", "<").replace(">", ">") + "</pre>";
|
r = "<pre>" + r.replace("<", "<").replace(">", ">") + "</pre>";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
r = request.context.fileUTF(s);
|
r = request.context.fileUTF(s, data.getBoolean("autoindex") ? request.realPath : null);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path(".*/")
|
@Path("/.*")
|
||||||
public void onIndex(Request request, Callback<Response> res, Callback<Throwable> rej) {
|
|
||||||
res.call(new Response(request, fileContent(request, data.getString("dir") + request.realPath.replace("..", "") + "/index.html"), 200, "OK"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
|
||||||
@Path("/.+")
|
|
||||||
public void onFile(Request request, Callback<Response> res, Callback<Throwable> rej) {
|
public void onFile(Request request, Callback<Response> res, Callback<Throwable> rej) {
|
||||||
if(request.realPath.equals("/style.css")) {
|
if(request.realPath.equals("/style.css")) {
|
||||||
res.call(new Response(request, request.context.file("style.css"), 200, "OK", "text/css"));
|
res.call(new Response(request, request.context.file("style.css"), 200, "OK", "text/css"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if(request.realPath.matches("/.$|/./|/..$|/../")) {
|
||||||
|
res.call(new Response(request, "<h1>" + request.realPath + " is an illegal path</h1>", 400, "Bad request"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
res.call(new Response(request, fileContent(request, data.getString("dir") + request.realPath.replace("..", "")), 200, "OK"));
|
res.call(new Response(request, fileContent(request, data.getString("dir") + request.realPath.replace("..", "")), 200, "OK"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ package de.tudbut.tryumph.server;
|
||||||
import static de.tudbut.async.Async.*;
|
import static de.tudbut.async.Async.*;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
@ -124,20 +125,36 @@ public class BrowserContext {
|
||||||
|
|
||||||
private static final HashMap<String, String> cache = new HashMap<>();
|
private static final HashMap<String, String> cache = new HashMap<>();
|
||||||
public String file(String file) {
|
public String file(String file) {
|
||||||
|
return file(file, null);
|
||||||
|
}
|
||||||
|
public String fileUTF(String file) {
|
||||||
|
return fileUTF(file, null);
|
||||||
|
}
|
||||||
|
public String file(String file, String path) {
|
||||||
if(cache.containsKey(file))
|
if(cache.containsKey(file))
|
||||||
return cache.get(file);
|
return cache.get(file);
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
try {
|
try {
|
||||||
InputStream stream = requestCatcher.getClass().getClassLoader().getResourceAsStream(file);
|
InputStream stream = requestCatcher.getClass().getClassLoader().getResourceAsStream(file);
|
||||||
if(stream == null)
|
if(stream == null) {
|
||||||
|
if(new File(file).isDirectory() && path != null) {
|
||||||
|
File[] files = new File(file).listFiles();
|
||||||
|
builder.append("<ul>");
|
||||||
|
for(int i = 0; i < files.length; i++) {
|
||||||
|
builder.append("<li><a href=\"" + path + "/" + files[i].getName() + "\">" + files[i].getName() + "</a></li>");
|
||||||
|
}
|
||||||
|
builder.append("</ul>");
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
stream = new FileInputStream(file);
|
stream = new FileInputStream(file);
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while((i = stream.read()) != -1) {
|
while((i = stream.read()) != -1) {
|
||||||
builder.append((char) i);
|
builder.append((char) i);
|
||||||
}
|
}
|
||||||
|
|
||||||
stream.close();
|
stream.close();
|
||||||
|
}
|
||||||
|
|
||||||
if(!TryConfig.nocache)
|
if(!TryConfig.nocache)
|
||||||
cache.put(file, builder.toString());
|
cache.put(file, builder.toString());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -146,13 +163,23 @@ public class BrowserContext {
|
||||||
}
|
}
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
public String fileUTF(String file) {
|
public String fileUTF(String file, String path) {
|
||||||
if(cache.containsKey(file))
|
if(cache.containsKey(file))
|
||||||
return cache.get(file);
|
return cache.get(file);
|
||||||
String st = null;
|
String st = null;
|
||||||
try {
|
try {
|
||||||
InputStream stream = requestCatcher.getClass().getClassLoader().getResourceAsStream(file);
|
InputStream stream = requestCatcher.getClass().getClassLoader().getResourceAsStream(file);
|
||||||
if(stream == null)
|
if(stream == null) {
|
||||||
|
if(new File(file).isDirectory() && path != null) {
|
||||||
|
File[] files = new File(file).listFiles();
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append("<ul>");
|
||||||
|
for(int i = 0; i < files.length; i++) {
|
||||||
|
builder.append("<li><a href=\"" + path + "/" + files[i].getName() + "\">" + files[i].getName() + "</a></li>");
|
||||||
|
}
|
||||||
|
builder.append("</ul>");
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
stream = new FileInputStream(file);
|
stream = new FileInputStream(file);
|
||||||
ByteArrayOutputStream s = new ByteArrayOutputStream();
|
ByteArrayOutputStream s = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
@ -164,6 +191,7 @@ public class BrowserContext {
|
||||||
stream.close();
|
stream.close();
|
||||||
s.close();
|
s.close();
|
||||||
st = new String(s.toByteArray());
|
st = new String(s.toByteArray());
|
||||||
|
}
|
||||||
if(!TryConfig.nocache)
|
if(!TryConfig.nocache)
|
||||||
cache.put(file, st);
|
cache.put(file, st);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue