public void handle(HttpRequest request) {
this.request = request;
}
@Override
public void ready(Write write) {
HttpResponse r = new HttpResponse(Http.Status.OK, Http.Message.OK);
r.getHeaders().put("Cache-Control", "private, max-age=0, no-cache");
try {
String path = request.getPath();
if (path.endsWith("/")) {
path = path.substring(0, path.length() - "/".length());
}
File d = new File(dir.getCanonicalPath() + Http.Url.decode(path));
if (d.isDirectory()) {
StringBuilder b = new StringBuilder();
b.append("<!doctype html>");
b.append("<html>");
b.append("<head>");
b.append("<meta charset=\"utf-8\">");
b.append("</head>");
b.append("<body>");
File[] files = d.listFiles();
if (files != null) {
b.append("<ul>");
if (!path.isEmpty()) {
int k = path.lastIndexOf('/');
String parent;
if (k == 0) {
parent = "/";
} else {
parent = path.substring(0, k);
}
b.append("<li>");
b.append("<a href=\"").append(parent).append("\">");
b.append("..");
b.append("</a>");
b.append("</li>");
}
for (File f : files) {
b.append("<li>");
b.append("<a href=\"").append(path + "/" + Http.Url.encode(f.getName())).append("\">");
b.append(f.getName());
b.append("</a>");
b.append("</li>");
}
b.append("</ul>");
}
b.append("</body>");
b.append("</html>");
ByteBuffer bb = ByteBuffer.wrap(b.toString().getBytes(Http.UTF8_CHARSET));
r.getHeaders().put(Http.CONTENT_LENGTH, String.valueOf(bb.remaining()));
write.write(r);
write.handle(null, bb);
write.close();
} else {
r.getHeaders().put(Http.CONTENT_LENGTH, String.valueOf(d.length()));
write.write(r);
try (InputStream in = new FileInputStream(d)) {
while (true) {
byte[] b = new byte[10 * 1024];
int l = in.read(b);
if (l < 0) {
break;
}
write.handle(null, ByteBuffer.wrap(b, 0, l));
}
}
write.close();
}
} catch (IOException ioe) {
write.write(new HttpResponse(Http.Status.INTERNAL_SERVER_ERROR, Http.Message.INTERNAL_SERVER_ERROR));
write.close();
}
}