int invocation = 1;
public void handle (HttpExchange t)
throws IOException
{
InputStream is = t.getRequestBody();
Headers map = t.getRequestHeaders();
Headers rmap = t.getResponseHeaders();
URI uri = t.getRequestURI();
String path = uri.getPath();
while (is.read () != -1) ;
is.close();
File f = new File (docroot, path);
if (!f.exists()) {
notfound (t, path);
return;
}
String fixedrequest = map.getFirst ("XFixed");
String method = t.getRequestMethod();
if (method.equals ("HEAD")) {
rmap.set ("Content-Length", Long.toString (f.length()));
t.sendResponseHeaders (200, -1);
t.close();
} else if (!method.equals("GET")) {
t.sendResponseHeaders (405, -1);
t.close();
return;
}
if (path.endsWith (".html") || path.endsWith (".htm")) {
rmap.set ("Content-Type", "text/html");
} else {
rmap.set ("Content-Type", "text/plain");
}
if (f.isDirectory()) {
if (!path.endsWith ("/")) {
moved (t);
return;
}
rmap.set ("Content-Type", "text/html");
t.sendResponseHeaders (200, 0);
String[] list = f.list();
OutputStream os = t.getResponseBody();
PrintStream p = new PrintStream (os);
p.println ("<h2>Directory listing for: " + path+ "</h2>");