Tabs and spaces.
This commit is contained in:
parent
b01e0c8cdc
commit
05f3ced64f
33 changed files with 306 additions and 306 deletions
|
|
@ -8,12 +8,12 @@ public class BoundInputStream extends InputStream {
|
|||
protected static final Logger log = Logger.getLogger("Minecraft");
|
||||
private InputStream base;
|
||||
private long bound;
|
||||
|
||||
|
||||
public BoundInputStream(InputStream base, long bound) {
|
||||
this.base = base;
|
||||
this.bound = bound;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
if (bound <= 0) return -1;
|
||||
|
|
@ -22,17 +22,17 @@ public class BoundInputStream extends InputStream {
|
|||
bound--;
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int available() throws IOException {
|
||||
return (int)Math.min(base.available(), bound);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean markSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int read(byte[] b, int off, int len) throws IOException {
|
||||
if (bound <= 0) return -1;
|
||||
|
|
@ -41,19 +41,19 @@ public class BoundInputStream extends InputStream {
|
|||
bound -= r;
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int read(byte[] b) throws IOException {
|
||||
return read(b, 0, b.length);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public long skip(long n) throws IOException {
|
||||
long r = base.skip(Math.min(bound, n));
|
||||
bound -= r;
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
base.close();
|
||||
|
|
|
|||
|
|
@ -9,5 +9,5 @@ public class HttpRequest {
|
|||
public String path;
|
||||
public String version;
|
||||
public Map<String, String> fields = new HashMap<String, String>();
|
||||
public InputStream body;
|
||||
public InputStream body;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ public class HttpResponse {
|
|||
public String version = "1.1";
|
||||
public HttpStatus status = null;
|
||||
public Map<String, String> fields = new HashMap<String, String>();
|
||||
|
||||
|
||||
private OutputStream body;
|
||||
public OutputStream getBody() throws IOException {
|
||||
if (body != null) {
|
||||
|
|
@ -21,7 +21,7 @@ public class HttpResponse {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public HttpResponse(HttpServerConnection connection, OutputStream body) {
|
||||
this.connection = connection;
|
||||
this.body = body;
|
||||
|
|
|
|||
|
|
@ -21,10 +21,10 @@ public class HttpServerConnection extends Thread {
|
|||
|
||||
private static Pattern requestHeaderLine = Pattern.compile("^(\\S+)\\s+(\\S+)\\s+HTTP/(.+)$");
|
||||
private static Pattern requestHeaderField = Pattern.compile("^([^:]+):\\s*(.+)$");
|
||||
|
||||
|
||||
private Socket socket;
|
||||
private HttpServer server;
|
||||
|
||||
|
||||
private PrintStream printOut;
|
||||
private StringWriter sw = new StringWriter();
|
||||
private Matcher requestHeaderLineMatcher;
|
||||
|
|
@ -45,7 +45,7 @@ public class HttpServerConnection extends Thread {
|
|||
sw.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private final String readLine(InputStream in) throws IOException {
|
||||
readLine(in, sw);
|
||||
String r = sw.toString();
|
||||
|
|
@ -55,16 +55,16 @@ public class HttpServerConnection extends Thread {
|
|||
|
||||
private final boolean readRequestHeader(InputStream in, HttpRequest request) throws IOException {
|
||||
String statusLine = readLine(in);
|
||||
|
||||
|
||||
if (statusLine == null)
|
||||
return false;
|
||||
|
||||
|
||||
if (requestHeaderLineMatcher == null) {
|
||||
requestHeaderLineMatcher = requestHeaderLine.matcher(statusLine);
|
||||
} else {
|
||||
requestHeaderLineMatcher.reset(statusLine);
|
||||
}
|
||||
|
||||
|
||||
Matcher m = requestHeaderLineMatcher;
|
||||
if (!m.matches())
|
||||
return false;
|
||||
|
|
@ -79,7 +79,7 @@ public class HttpServerConnection extends Thread {
|
|||
} else {
|
||||
requestHeaderFieldMatcher.reset(line);
|
||||
}
|
||||
|
||||
|
||||
m = requestHeaderFieldMatcher;
|
||||
// Warning: unknown lines are ignored.
|
||||
if (m.matches()) {
|
||||
|
|
@ -109,7 +109,7 @@ public class HttpServerConnection extends Thread {
|
|||
out.append("\r\n");
|
||||
out.flush();
|
||||
}
|
||||
|
||||
|
||||
public final void writeResponseHeader(HttpResponse response) throws IOException {
|
||||
writeResponseHeader(printOut, response);
|
||||
}
|
||||
|
|
@ -121,16 +121,16 @@ public class HttpServerConnection extends Thread {
|
|||
socket.setSoTimeout(5000);
|
||||
InputStream in = socket.getInputStream();
|
||||
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream(), 40960);
|
||||
|
||||
|
||||
printOut = new PrintStream(out, false);
|
||||
while (true) {
|
||||
HttpRequest request = new HttpRequest();
|
||||
|
||||
|
||||
if (!readRequestHeader(in, request)) {
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
long bound = -1;
|
||||
BoundInputStream boundBody = null;
|
||||
{
|
||||
|
|
@ -166,9 +166,9 @@ public class HttpServerConnection extends Thread {
|
|||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
HttpResponse response = new HttpResponse(this, out);
|
||||
|
||||
|
||||
try {
|
||||
handler.handle(relativePath, request, response);
|
||||
} catch (IOException e) {
|
||||
|
|
@ -187,9 +187,9 @@ public class HttpServerConnection extends Thread {
|
|||
//socket.close();
|
||||
//return;
|
||||
}
|
||||
|
||||
|
||||
boolean isKeepalive = !"close".equals(request.fields.get(HttpField.Connection)) && !"close".equals(response.fields.get(HttpField.Connection));
|
||||
|
||||
|
||||
String contentLength = response.fields.get("Content-Length");
|
||||
if (isKeepalive && contentLength == null) {
|
||||
// A handler has been a bad boy, but we're here to fix it.
|
||||
|
|
|
|||
|
|
@ -3,20 +3,20 @@ package org.dynmap.web;
|
|||
public final class HttpStatus {
|
||||
private int code;
|
||||
private String text;
|
||||
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
|
||||
public HttpStatus(int code, String text) {
|
||||
this.code = code;
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
|
||||
// Reference: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
|
||||
public static final HttpStatus Continue = new HttpStatus(100, "Continue");
|
||||
public static final HttpStatus SwitchingProtocols = new HttpStatus(101, "Switching Protocols");
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ public class Json {
|
|||
appendJson(o, sb);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
public static void appendJson(Object o, StringBuilder s) {
|
||||
if (o == null) {
|
||||
s.append("null");
|
||||
|
|
@ -59,7 +59,7 @@ public class Json {
|
|||
} else if (o instanceof Object) /* TODO: Always true, maybe interface? */ {
|
||||
s.append("{");
|
||||
boolean first = true;
|
||||
|
||||
|
||||
Class<?> c = o.getClass();
|
||||
for(Field field : c.getFields()) {
|
||||
if (!Modifier.isPublic(field.getModifiers()))
|
||||
|
|
@ -73,7 +73,7 @@ public class Json {
|
|||
} catch (IllegalAccessException e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
|
|
|
|||
|
|
@ -20,18 +20,18 @@ public class ClientConfigurationHandler implements HttpHandler {
|
|||
public void handle(String path, HttpRequest request, HttpResponse response) throws Exception {
|
||||
if (cachedConfiguration == null) {
|
||||
String s = Json.stringifyJson(configuration);
|
||||
|
||||
|
||||
cachedConfiguration = s.getBytes();
|
||||
}
|
||||
String dateStr = new Date().toString();
|
||||
|
||||
|
||||
response.fields.put("Date", dateStr);
|
||||
response.fields.put("Content-Type", "text/plain");
|
||||
response.fields.put("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");
|
||||
response.fields.put("Last-modified", dateStr);
|
||||
response.fields.put("Content-Length", Integer.toString(cachedConfiguration.length));
|
||||
response.status = HttpStatus.OK;
|
||||
|
||||
|
||||
BufferedOutputStream out = null;
|
||||
out = new BufferedOutputStream(response.getBody());
|
||||
out.write(cachedConfiguration);
|
||||
|
|
|
|||
|
|
@ -31,26 +31,26 @@ public class ClientUpdateHandler implements HttpHandler {
|
|||
}
|
||||
|
||||
Pattern updatePathPattern = Pattern.compile("world/([^/]+)/([0-9]*)");
|
||||
private static final HttpStatus WorldNotFound = new HttpStatus(HttpStatus.NotFound.getCode(), "World Not Found");
|
||||
private static final HttpStatus WorldNotFound = new HttpStatus(HttpStatus.NotFound.getCode(), "World Not Found");
|
||||
@Override
|
||||
public void handle(String path, HttpRequest request, HttpResponse response) throws Exception {
|
||||
|
||||
|
||||
Matcher match = updatePathPattern.matcher(path);
|
||||
|
||||
|
||||
if (!match.matches()) {
|
||||
response.status = HttpStatus.Forbidden;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
String worldName = match.group(1);
|
||||
String timeKey = match.group(2);
|
||||
|
||||
|
||||
World world = server.getWorld(worldName);
|
||||
if (world == null) {
|
||||
response.status = WorldNotFound;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
long current = System.currentTimeMillis();
|
||||
long since = 0;
|
||||
|
||||
|
|
@ -60,14 +60,14 @@ public class ClientUpdateHandler implements HttpHandler {
|
|||
} catch (NumberFormatException e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Client.Update update = new Client.Update();
|
||||
update.timestamp = current;
|
||||
update.servertime = world.getTime() % 24000;
|
||||
update.hasStorm = world.hasStorm();
|
||||
update.isThundering = world.isThundering();
|
||||
|
||||
|
||||
|
||||
|
||||
Player[] players = playerList.getVisiblePlayers();
|
||||
update.players = new Client.Player[players.length];
|
||||
for(int i=0;i<players.length;i++) {
|
||||
|
|
@ -75,10 +75,10 @@ public class ClientUpdateHandler implements HttpHandler {
|
|||
Location pl = p.getLocation();
|
||||
update.players[i] = new Client.Player(p.getDisplayName(), pl.getWorld().getName(), pl.getX(), pl.getY(), pl.getZ());
|
||||
}
|
||||
|
||||
|
||||
update.updates = mapManager.getWorldUpdates(worldName, since);
|
||||
|
||||
|
||||
|
||||
|
||||
byte[] bytes = Json.stringifyJson(update).getBytes();
|
||||
|
||||
String dateStr = new Date().toString();
|
||||
|
|
@ -88,10 +88,10 @@ public class ClientUpdateHandler implements HttpHandler {
|
|||
response.fields.put(HttpField.LastModified, dateStr);
|
||||
response.fields.put(HttpField.ContentLength, Integer.toString(bytes.length));
|
||||
response.status = HttpStatus.OK;
|
||||
|
||||
|
||||
BufferedOutputStream out = null;
|
||||
out = new BufferedOutputStream(response.getBody());
|
||||
out.write(bytes);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,16 +33,16 @@ public abstract class FileHandler implements HttpHandler {
|
|||
return m;
|
||||
return "application/octet-steam";
|
||||
}
|
||||
|
||||
|
||||
protected abstract InputStream getFileInput(String path, HttpRequest request, HttpResponse response);
|
||||
|
||||
|
||||
protected String getExtension(String path) {
|
||||
int dotindex = path.lastIndexOf('.');
|
||||
if (dotindex > 0)
|
||||
return path.substring(dotindex);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
protected final String formatPath(String path) {
|
||||
int qmark = path.indexOf('?');
|
||||
if (qmark >= 0)
|
||||
|
|
@ -54,11 +54,11 @@ public abstract class FileHandler implements HttpHandler {
|
|||
path = getDefaultFilename(path);
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
protected String getDefaultFilename(String path) {
|
||||
return path + "index.html";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void handle(String path, HttpRequest request, HttpResponse response) throws Exception {
|
||||
InputStream fileInput = null;
|
||||
|
|
@ -69,10 +69,10 @@ public abstract class FileHandler implements HttpHandler {
|
|||
response.status = HttpStatus.NotFound;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
String extension = getExtension(path);
|
||||
String mimeType = getMimeTypeFromExtension(extension);
|
||||
|
||||
|
||||
response.fields.put(HttpField.ContentType, mimeType);
|
||||
response.status = HttpStatus.OK;
|
||||
OutputStream out = response.getBody();
|
||||
|
|
|
|||
|
|
@ -32,4 +32,4 @@ public class FilesystemHandler extends FileHandler {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,4 +16,4 @@ public class JarFileHandler extends FileHandler {
|
|||
protected InputStream getFileInput(String path, HttpRequest request, HttpResponse response) {
|
||||
return this.getClass().getResourceAsStream(root + "/" + path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,16 +17,16 @@ import org.json.simple.parser.JSONParser;
|
|||
|
||||
public class SendMessageHandler implements HttpHandler {
|
||||
protected static final Logger log = Logger.getLogger("Minecraft");
|
||||
|
||||
|
||||
private static final JSONParser parser = new JSONParser();
|
||||
public Event<Message> onMessageReceived = new Event<SendMessageHandler.Message>();
|
||||
|
||||
|
||||
public int maximumMessageInterval = 1000;
|
||||
public String spamMessage = "\"You may only chat once every %interval% seconds.\"";
|
||||
private HashMap<String, WebUser> disallowedUsers = new HashMap<String, WebUser>();
|
||||
private LinkedList<WebUser> disallowedUserQueue = new LinkedList<WebUser>();
|
||||
private Object disallowedUsersLock = new Object();
|
||||
|
||||
|
||||
@Override
|
||||
public void handle(String path, HttpRequest request, HttpResponse response) throws Exception {
|
||||
if (!request.method.equals(HttpMethod.Post)) {
|
||||
|
|
@ -36,14 +36,14 @@ public class SendMessageHandler implements HttpHandler {
|
|||
}
|
||||
|
||||
InputStreamReader reader = new InputStreamReader(request.body);
|
||||
|
||||
|
||||
JSONObject o = (JSONObject)parser.parse(reader);
|
||||
final Message message = new Message();
|
||||
message.name = String.valueOf(o.get("name"));
|
||||
message.message = String.valueOf(o.get("message"));
|
||||
|
||||
final long now = System.currentTimeMillis();
|
||||
|
||||
|
||||
synchronized(disallowedUsersLock) {
|
||||
// Allow users that user that are now allowed to send messages.
|
||||
while (!disallowedUserQueue.isEmpty()) {
|
||||
|
|
@ -55,7 +55,7 @@ public class SendMessageHandler implements HttpHandler {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
WebUser user = disallowedUsers.get(message.name);
|
||||
if (user == null) {
|
||||
user = new WebUser() {{
|
||||
|
|
@ -71,14 +71,14 @@ public class SendMessageHandler implements HttpHandler {
|
|||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
onMessageReceived.trigger(message);
|
||||
|
||||
|
||||
response.fields.put(HttpField.ContentLength, "0");
|
||||
response.status = HttpStatus.OK;
|
||||
response.getBody();
|
||||
}
|
||||
|
||||
|
||||
public static class Message {
|
||||
public String name;
|
||||
public String message;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue