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; 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; Response res = null;
@ -180,32 +180,53 @@ public class Module_file extends TFM_HTTPD_Module
// Calculate etag // Calculate etag
String etag = Integer.toHexString((f.getAbsolutePath() + f.lastModified() + "" + f.length()).hashCode()); String etag = Integer.toHexString((f.getAbsolutePath() + f.lastModified() + "" + f.length()).hashCode());
// Support (simple) skipping: final long fileLen = f.length();
long startFrom = 0; long startFrom = 0;
long endAt = -1; long endAt = -1;
String range = header.get("range"); final String range = params.get("range");
if (range != null) if (range != null)
{ {
if (range.startsWith("bytes=")) final String[] rangeParams = net.minecraft.util.org.apache.commons.lang3.StringUtils.split(range, "=");
if (rangeParams.length >= 2)
{ {
range = range.substring("bytes=".length()); if ("bytes".equalsIgnoreCase(rangeParams[0]))
int minus = range.indexOf('-');
try
{ {
if (minus > 0) try
{
int minus = rangeParams[1].indexOf('-');
if (minus > 0)
{
startFrom = Long.parseLong(rangeParams[1].substring(0, minus));
endAt = Long.parseLong(rangeParams[1].substring(minus + 1));
}
}
catch (NumberFormatException ignored)
{ {
startFrom = Long.parseLong(range.substring(0, minus));
endAt = Long.parseLong(range.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)
{
}
} }
} }
} }
// Change return code and add Content-Range header when skipping is requested // Change return code and add Content-Range header when skipping is requested
long fileLen = f.length();
if (range != null && startFrom >= 0) if (range != null && startFrom >= 0)
{ {
if (startFrom >= fileLen) if (startFrom >= fileLen)
@ -245,16 +266,9 @@ public class Module_file extends TFM_HTTPD_Module
} }
else else
{ {
if (etag.equals(header.get("if-none-match"))) res = new Response(Response.Status.OK, mime, new FileInputStream(f));
{ res.addHeader("Content-Length", "" + fileLen);
res = new Response(Response.Status.NOT_MODIFIED, mime, ""); res.addHeader("ETag", etag);
}
else
{
res = new Response(Response.Status.OK, mime, new FileInputStream(f));
res.addHeader("Content-Length", "" + fileLen);
res.addHeader("ETag", etag);
}
} }
} }
} }
@ -359,6 +373,6 @@ public class Module_file extends TFM_HTTPD_Module
@Override @Override
public Response getResponse() 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(); 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; private final ModuleExecutable moduleExecutable;