Skip to content

Commit

Permalink
added pay2spawn file hosting as requested by #2
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Oct 11, 2014
1 parent 165c816 commit eb7391c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/main/java/net/doubledoordev/backend/Main.java
Expand Up @@ -42,6 +42,7 @@

import net.doubledoordev.backend.server.Server;
import net.doubledoordev.backend.server.rcon.RCon;
import net.doubledoordev.backend.util.Cache;
import net.doubledoordev.backend.util.Constants;
import net.doubledoordev.backend.util.Settings;
import net.doubledoordev.backend.webserver.NanoHTTPD;
Expand Down Expand Up @@ -85,6 +86,7 @@ public static void main(String[] args) throws Exception
LOGGER.info("Starting webserver...");
Webserver.WEBSERVER.start();
LOGGER.info("Setting up caching...");
Cache.init();

LOGGER.info("+-------------------------------------------------------+");
LOGGER.info("| Loading done. Press any key to terminate the program. |");
Expand Down
Expand Up @@ -82,6 +82,7 @@ public class Constants
public static final File SERVERS = new File(ROOT, "servers");
public static final File BACKUPS = new File(ROOT, "backups");
public static final String STATIC_PATH = "/static/";
public static final String P2S_PATH = "/pay2spawn/";
public static final String FAVOTICON = "favicon.ico";
public static final String COOKIE_KEY = "user";
public static final JsonParser JSONPARSER = new JsonParser();
Expand Down
Expand Up @@ -281,7 +281,7 @@ public final boolean isAlive()
* Override this to customize the server.
* <p/>
* <p/>
* (By default, this delegates to serveFile() and allows directory listing.)
* (By default, this delegates to serveResourceFile() and allows directory listing.)
*
* @param uri Percent-decoded URI without parameters, for example "/index.cgi"
* @param method "GET", "POST" etc.
Expand All @@ -300,7 +300,7 @@ public Response serve(String uri, Method method, Map<String, String> headers, Ma
* Override this to customize the server.
* <p/>
* <p/>
* (By default, this delegates to serveFile() and allows directory listing.)
* (By default, this delegates to serveResourceFile() and allows directory listing.)
*
* @param session The HTTP session
* @return HTTP response, see class Response for details
Expand Down
Expand Up @@ -135,7 +135,7 @@ else if (tok.equals(" "))
return newUri;
}

protected Response respond(Map<String, String> headers, String uri)
protected Response respond(String uri)
{
// Remove URL arguments
uri = uri.trim().replace(File.separatorChar, '/');
Expand All @@ -155,8 +155,7 @@ protected Response respond(Map<String, String> headers, String uri)
return getNotFoundResponse();
}

String mimeTypeForFile = getMimeTypeForFile(uri);
Response response = serveFile(uri, headers, mimeTypeForFile);
Response response = serveResourceFile(uri);
return response != null ? response : getNotFoundResponse();
}

Expand All @@ -178,14 +177,14 @@ private boolean canServeUri(String uri)
/**
* Serves file from homeDir and its' subdirectories (only). Uses only URI, ignores all headers and HTTP parameters.
*/
Response serveFile(String uri, Map<String, String> header, String mime)
Response serveResourceFile(String uri)
{
Response res;
try
{
InputStream stream = getClass().getResourceAsStream(rootDir + uri);
int fileLen = stream.available();
res = createResponse(Response.Status.OK, mime, stream);
res = createResponse(Response.Status.OK, getMimeTypeForFile(uri), stream);
res.addHeader("Content-Length", "" + fileLen);
}
catch (IOException ioe)
Expand All @@ -197,7 +196,7 @@ Response serveFile(String uri, Map<String, String> header, String mime)
}

// Get MIME type from file name extension, if possible
private String getMimeTypeForFile(String uri)
public String getMimeTypeForFile(String uri)
{
int dot = uri.lastIndexOf('.');
String mime = null;
Expand All @@ -209,15 +208,15 @@ private String getMimeTypeForFile(String uri)
}

// Announce that the file server accepts partial content requests
private Response createResponse(Response.Status status, String mimeType, InputStream message)
Response createResponse(Response.Status status, String mimeType, InputStream message)
{
Response res = new Response(status, mimeType, message);
res.addHeader("Accept-Ranges", "bytes");
return res;
}

// Announce that the file server accepts partial content requests
private Response createResponse(Response.Status status, String mimeType, String message)
Response createResponse(Response.Status status, String mimeType, String message)
{
Response res = new Response(status, mimeType, message);
res.addHeader("Accept-Ranges", "bytes");
Expand Down
35 changes: 33 additions & 2 deletions src/main/java/net/doubledoordev/backend/webserver/Webserver.java
Expand Up @@ -40,13 +40,19 @@

package net.doubledoordev.backend.webserver;

import net.doubledoordev.backend.Main;
import net.doubledoordev.backend.permissions.Group;
import net.doubledoordev.backend.permissions.User;
import net.doubledoordev.backend.server.Server;
import net.doubledoordev.backend.util.Settings;
import net.doubledoordev.backend.webserver.methods.Get;
import net.doubledoordev.backend.webserver.methods.Post;
import net.doubledoordev.backend.webserver.methods.Put;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import static net.doubledoordev.backend.util.Constants.*;
Expand Down Expand Up @@ -80,11 +86,14 @@ public Response serve(IHTTPSession session)

// We want to split off static ASAP.
if (session.getUri().startsWith(STATIC_PATH))
return super.respond(session.getHeaders(), session.getUri().substring(STATIC_PATH.length()));
return super.respond(session.getUri().substring(STATIC_PATH.length()));

// stupid favicon.ico
if (session.getUri().contains(FAVOTICON))
return super.respond(session.getHeaders(), session.getUri());
return super.respond(session.getUri());

if (session.getUri().startsWith(P2S_PATH))
return servePay2SpawnFile(session.getUri().split("/"));

/**
* Populate data map with user (if logged in) and admin status.
Expand Down Expand Up @@ -121,4 +130,26 @@ public Response serve(IHTTPSession session)
return new Response(Response.Status.NO_CONTENT, MIME_PLAINTEXT, "No Content");
}
}

private Response servePay2SpawnFile(String[] uri)
{
if (uri.length != 4) return createResponse(Response.Status.BAD_REQUEST, MIME_PLAINTEXT, "Error 400, Arguments accepted are only server and name.");
Server server = Settings.getServerByName(uri[2]);
if (server == null) return createResponse(Response.Status.BAD_REQUEST, MIME_PLAINTEXT, String.format("Error 400, Server '%s' doesn't exits.", uri[2]));
File file = new File(server.getFolder(), String.format("pay2spawn/%s.html", uri[3]));
if (!file.exists()) return createResponse(Response.Status.BAD_REQUEST, MIME_PLAINTEXT, String.format("Error 400, User '%s' does not have a Pay2Spawn file on the server '%s'.", uri[3], uri[2]));
Response res;
try
{
InputStream stream = new FileInputStream(file);
int fileLen = stream.available();
res = createResponse(Response.Status.OK, "text/html", stream);
res.addHeader("Content-Length", "" + fileLen);
}
catch (IOException ioe)
{
res = getForbiddenResponse("Reading file failed.");
}
return res == null ? getNotFoundResponse() : res;
}
}

0 comments on commit eb7391c

Please sign in to comment.