Add HTTPD Module_logs, resolves #238

This commit is contained in:
StevenLawson 2014-07-13 18:04:08 -04:00
parent a0d5743232
commit 173b686658
3 changed files with 70 additions and 23 deletions

View File

@ -93,7 +93,7 @@ public class Module_file extends TFM_HTTPD_Module
return newUri;
}
private Response serveFile(String uri, Map<String, String> header, File homeDir)
public Response serveFile(String uri, Map<String, String> params, File homeDir)
{
Response res = null;
@ -180,22 +180,43 @@ public class Module_file extends TFM_HTTPD_Module
// Calculate etag
String etag = Integer.toHexString((f.getAbsolutePath() + f.lastModified() + "" + f.length()).hashCode());
// Support (simple) skipping:
final long fileLen = f.length();
long startFrom = 0;
long endAt = -1;
String range = header.get("range");
final String range = params.get("range");
if (range != null)
{
if (range.startsWith("bytes="))
final String[] rangeParams = net.minecraft.util.org.apache.commons.lang3.StringUtils.split(range, "=");
if (rangeParams.length >= 2)
{
if ("bytes".equalsIgnoreCase(rangeParams[0]))
{
range = range.substring("bytes=".length());
int minus = range.indexOf('-');
try
{
int minus = rangeParams[1].indexOf('-');
if (minus > 0)
{
startFrom = Long.parseLong(range.substring(0, minus));
endAt = Long.parseLong(range.substring(minus + 1));
startFrom = Long.parseLong(rangeParams[1].substring(0, minus));
endAt = Long.parseLong(rangeParams[1].substring(minus + 1));
}
}
catch (NumberFormatException ignored)
{
}
}
else if ("tail".equalsIgnoreCase(rangeParams[0]))
{
try
{
final long tailLen = Long.parseLong(rangeParams[1]);
if (tailLen < fileLen)
{
startFrom = fileLen - tailLen - 2;
if (startFrom < 0)
{
startFrom = 0;
}
}
}
catch (NumberFormatException ignored)
@ -203,9 +224,9 @@ public class Module_file extends TFM_HTTPD_Module
}
}
}
}
// Change return code and add Content-Range header when skipping is requested
long fileLen = f.length();
if (range != null && startFrom >= 0)
{
if (startFrom >= fileLen)
@ -244,12 +265,6 @@ public class Module_file extends TFM_HTTPD_Module
}
}
else
{
if (etag.equals(header.get("if-none-match")))
{
res = new Response(Response.Status.NOT_MODIFIED, mime, "");
}
else
{
res = new Response(Response.Status.OK, mime, new FileInputStream(f));
res.addHeader("Content-Length", "" + fileLen);
@ -257,7 +272,6 @@ public class Module_file extends TFM_HTTPD_Module
}
}
}
}
catch (IOException ioe)
{
res = new Response(Response.Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "FORBIDDEN: Reading file failed.");
@ -359,6 +373,6 @@ public class Module_file extends TFM_HTTPD_Module
@Override
public Response getResponse()
{
return serveFile(uri, headers, getRootDir());
return serveFile(uri, params, getRootDir());
}
}

View File

@ -0,0 +1,25 @@
package me.StevenLawson.TotalFreedomMod.HTTPD;
import java.io.File;
import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry;
public class Module_logs extends Module_file
{
public Module_logs(NanoHTTPD.HTTPSession session)
{
super(session);
}
@Override
public NanoHTTPD.Response getResponse()
{
if (TFM_ConfigEntry.LOGS_SECRET.getString().equals(params.get("password")))
{
return serveFile("latest.log", params, new File("./logs"));
}
else
{
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "Incorrect password.");
}
}
}

View File

@ -134,6 +134,14 @@ public class TFM_HTTPD_Manager
{
return new Module_players(session).getResponse();
}
}),
LOGS(new ModuleExecutable(false, "logs")
{
@Override
public Response getResponse(HTTPSession session)
{
return new Module_logs(session).getResponse();
}
});
//
private final ModuleExecutable moduleExecutable;