Added socket parameter for access to remote IP.

Started on schematic module.
This commit is contained in:
StevenLawson 2013-09-03 10:28:56 -04:00
parent 88103cefc2
commit 0067e2cc65
8 changed files with 131 additions and 19 deletions

View File

@ -1,5 +1,6 @@
package me.StevenLawson.TotalFreedomMod.HTTPD;
import java.net.Socket;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
@ -7,9 +8,9 @@ import static me.StevenLawson.TotalFreedomMod.HTTPD.HTMLGenerationTools.*;
public class Module_dump extends TFM_HTTPD_Module
{
public Module_dump(String uri, NanoHTTPD.Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files)
public Module_dump(String uri, NanoHTTPD.Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files, Socket socket)
{
super(uri, method, headers, params, files);
super(uri, method, headers, params, files, socket);
}
@Override

View File

@ -5,6 +5,7 @@ import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Collections;
@ -53,9 +54,9 @@ public class Module_file extends TFM_HTTPD_Module
MIME_TYPES.put("class", "application/octet-stream");
}
public Module_file(String uri, NanoHTTPD.Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files)
public Module_file(String uri, Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files, Socket socket)
{
super(uri, method, headers, params, files);
super(uri, method, headers, params, files, socket);
}
private File getRootDir()

View File

@ -1,5 +1,6 @@
package me.StevenLawson.TotalFreedomMod.HTTPD;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -20,9 +21,9 @@ import static org.apache.commons.lang3.StringEscapeUtils.*;
public class Module_help extends TFM_HTTPD_Module
{
public Module_help(String uri, NanoHTTPD.Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files)
public Module_help(String uri, NanoHTTPD.Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files, Socket socket)
{
super(uri, method, headers, params, files);
super(uri, method, headers, params, files, socket);
}
@Override

View File

@ -1,5 +1,6 @@
package me.StevenLawson.TotalFreedomMod.HTTPD;
import java.net.Socket;
import java.util.Map;
import me.StevenLawson.TotalFreedomMod.TFM_SuperadminList;
import me.StevenLawson.TotalFreedomMod.TFM_Util;
@ -8,9 +9,9 @@ import org.bukkit.entity.Player;
public class Module_list extends TFM_HTTPD_Module
{
public Module_list(String uri, NanoHTTPD.Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files)
public Module_list(String uri, NanoHTTPD.Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files, Socket socket)
{
super(uri, method, headers, params, files);
super(uri, method, headers, params, files, socket);
}
@Override

View File

@ -0,0 +1,86 @@
package me.StevenLawson.TotalFreedomMod.HTTPD;
import java.net.Socket;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
public class Module_schematic extends TFM_HTTPD_Module
{
public Module_schematic(String uri, NanoHTTPD.Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files, Socket socket)
{
super(uri, method, headers, params, files, socket);
}
@Override
public String getTitle()
{
return "TotalFreedomMod :: Schematic Manager";
}
@Override
public String getBody()
{
final StringBuilder out = new StringBuilder();
final String[] args = StringUtils.split(uri, "/");
ModuleMode mode = ModuleMode.getMode(getArg(args, 1));
switch (mode)
{
case DOWNLOAD:
{
break;
}
case UPLOAD:
{
break;
}
default:
{
out.append(HTMLGenerationTools.paragraph("Invalid request mode."));
break;
}
}
return out.toString();
}
private static enum ModuleMode
{
UPLOAD("upload"),
DOWNLOAD("download"),
INVALID(null);
//
private final String modeName;
private ModuleMode(String modeName)
{
this.modeName = modeName;
}
@Override
public String toString()
{
return this.modeName;
}
public static ModuleMode getMode(String needle)
{
for (ModuleMode mode : values())
{
final String haystack = mode.toString();
if (haystack != null && haystack.equalsIgnoreCase(needle))
{
return mode;
}
}
return INVALID;
}
}
private String getArg(String[] args, int index)
{
String out = (args.length == index + 1 ? args[index] : null);
return (out == null ? null : (out.trim().isEmpty() ? null : out.trim()));
}
}

View File

@ -134,7 +134,7 @@ public abstract class NanoHTTPD
{
outputStream = finalAccept.getOutputStream();
TempFileManager tempFileManager = tempFileManagerFactory.create();
HTTPSession session = new HTTPSession(tempFileManager, inputStream, outputStream);
HTTPSession session = new HTTPSession(tempFileManager, inputStream, outputStream, finalAccept);
while (!finalAccept.isClosed())
{
session.execute();
@ -200,7 +200,7 @@ public abstract class NanoHTTPD
* @return HTTP response, see class Response for details
*/
public abstract Response serve(String uri, Method method, Map<String, String> headers, Map<String, String> parms,
Map<String, String> files);
Map<String, String> files, Socket socket);
/**
* Override this to customize the server.
@ -232,7 +232,8 @@ public abstract class NanoHTTPD
Method method = session.getMethod();
Map<String, String> parms = session.getParms();
Map<String, String> headers = session.getHeaders();
return serve(uri, method, headers, parms, files);
Socket socket = session.getSocket();
return serve(uri, method, headers, parms, files, socket);
}
/**
@ -742,12 +743,14 @@ public abstract class NanoHTTPD
private Method method;
private Map<String, String> parms;
private Map<String, String> headers;
private Socket socket;
public HTTPSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream)
private HTTPSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, Socket socket)
{
this.tempFileManager = tempFileManager;
this.inputStream = inputStream;
this.outputStream = outputStream;
this.socket = socket;
}
public void execute() throws IOException
@ -1281,6 +1284,11 @@ public abstract class NanoHTTPD
{
return inputStream;
}
public final Socket getSocket()
{
return socket;
}
}
private static final class ResponseException extends Exception

View File

@ -1,6 +1,7 @@
package me.StevenLawson.TotalFreedomMod.HTTPD;
import java.io.IOException;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
@ -64,7 +65,8 @@ public class TFM_HTTPD_Manager
DUMP(false, "dump"),
HELP(true, "help"),
LIST(true, "list"),
FILE(false, "file");
FILE(false, "file"),
SCHEMATIC(false, "schematic");
private final boolean runOnBukkitThread;
private final String name;
@ -110,7 +112,13 @@ public class TFM_HTTPD_Manager
}
@Override
public Response serve(final String uri, final Method method, final Map<String, String> headers, final Map<String, String> params, final Map<String, String> files)
public Response serve(
final String uri,
final Method method,
final Map<String, String> headers,
final Map<String, String> params,
final Map<String, String> files,
final Socket socket)
{
Response response = null;
@ -127,9 +135,9 @@ public class TFM_HTTPD_Manager
switch (moduleType)
{
case HELP:
return new Module_help(uri, method, headers, params, files).getResponse();
return new Module_help(uri, method, headers, params, files, socket).getResponse();
case LIST:
return new Module_list(uri, method, headers, params, files).getResponse();
return new Module_list(uri, method, headers, params, files, socket).getResponse();
default:
return null;
}
@ -150,10 +158,13 @@ public class TFM_HTTPD_Manager
switch (moduleType)
{
case DUMP:
response = new Module_dump(uri, method, headers, params, files).getResponse();
response = new Module_dump(uri, method, headers, params, files, socket).getResponse();
break;
case SCHEMATIC:
response = new Module_schematic(uri, method, headers, params, files, socket).getResponse();
break;
default:
response = new Module_file(uri, method, headers, params, files).getResponse();
response = new Module_file(uri, method, headers, params, files, socket).getResponse();
}
}

View File

@ -1,5 +1,6 @@
package me.StevenLawson.TotalFreedomMod.HTTPD;
import java.net.Socket;
import java.util.Map;
import me.StevenLawson.TotalFreedomMod.HTTPD.NanoHTTPD.*;
@ -10,14 +11,16 @@ public abstract class TFM_HTTPD_Module
protected final Map<String, String> headers;
protected final Map<String, String> params;
protected final Map<String, String> files;
protected final Socket socket;
public TFM_HTTPD_Module(String uri, Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files)
public TFM_HTTPD_Module(String uri, Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files, Socket socket)
{
this.uri = uri;
this.method = method;
this.headers = headers;
this.params = params;
this.files = files;
this.socket = socket;
}
public String getBody()