res.setValue("Server", "TUCServer/"+StartMain.version+" (theuchain)");
res.setDate("Date", time);
res.setDate("Last-Modified", time);
Address a = req.getAddress();
// notes: domain is null
String[] seg = a.getPath().getSegments();
if (seg[0].equalsIgnoreCase("res") || seg[0].equalsIgnoreCase("html")) {
File rf = new File("../" + a.getPath().getPath(0));
if (! rf.exists()) {
res.setCode(404);
res.setValue("Content-Type", "text/html");
body.println("<h2>404</h2><br>The resource was not available. This is not intended to be user-browsed. ");
body.close();
return;
}
String ext = a.getPath().getExtension();
if (ext.equalsIgnoreCase("png")) {
res.setValue("Content-Type", "image/png");
} else if (ext.equalsIgnoreCase("gif")) {
res.setValue("Content-Type", "image/gif");
} else if (ext.equalsIgnoreCase("jml")) {
res.setValue("Content-Type", "text/html");
} else {
res.setCode(500);
res.setValue("Content-Type", "text/html");
body.println("<h2>500 Internal TUC Server Error</h2><br>The resource does not have a known MIME-Type, cannot transfer properly. Extension: " + ext);
body.close();
return;
}
// a resource was called!
// the resources are usually binary files, so we can't copy them
// as text
// but we will do this with text files anyway, for simplicity
// Get a Stream from the file
FileInputStream reader = new FileInputStream(rf);
byte[] buffer = new byte[153600];
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) > 0)
{
// copy the bytes from the filein stream to the httpout stream
body.write(buffer, 0, bytesRead);
buffer = new byte[153600];
}
body.close(); // Close Stream
reader.close(); // Close Stream
return; // Exit from HTTP Processing
}
String addr = a.toString();
if (addr.equalsIgnoreCase("/ser")) {
Player p = new Player("slm", "AUser", 1);
p.save();
body.println("Serializing player");
stdOut.println("Stopping the server...");