Add support for 'http-reponse-headers' attribute to add custom response headers

This commit is contained in:
Mike Primm 2011-08-29 21:33:41 -05:00
parent 7e37817b86
commit 5088adb7eb
4 changed files with 33 additions and 0 deletions

View File

@ -303,6 +303,19 @@ public class DynmapPlugin extends JavaPlugin {
boolean checkbannedips = configuration.getBoolean("check-banned-ips", true);
int maxconnections = configuration.getInteger("max-sessions", 30);
if(maxconnections < 2) maxconnections = 2;
/* Load customized response headers, if any */
ConfigurationNode custhttp = configuration.getNode("http-response-headers");
HashMap<String, String> custhdrs = new HashMap<String,String>();
if(custhttp != null) {
for(String k : custhttp.keySet()) {
String v = custhttp.getString(k);
if(v != null) {
custhdrs.put(k, v);
}
}
}
HttpServer.setCustomHeaders(custhdrs);
if(allow_symlinks)
Log.verboseinfo("Web server is permitting symbolic links");
else

View File

@ -10,8 +10,10 @@ import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Logger;
@ -34,6 +36,7 @@ public class HttpServer extends Thread {
private Object lock = new Object();
private HashSet<HttpServerConnection> active_connections = new HashSet<HttpServerConnection>();
private HashSet<HttpServerConnection> keepalive_connections = new HashSet<HttpServerConnection>();
private static Map<String, String> headers = new HashMap<String,String>();
public HttpServer(InetAddress bindAddress, int port, boolean check_banned_ips, int max_sessions) {
this.bindAddress = bindAddress;
@ -189,4 +192,10 @@ public class HttpServer extends Thread {
}
return false;
}
public static Map<String,String> getCustomHeaders() {
return headers;
}
public static void setCustomHeaders(Map<String,String> hdrs) {
headers = hdrs;
}
}

View File

@ -111,6 +111,12 @@ public class HttpServerConnection extends Thread {
out.append(field.getValue());
out.append("\r\n");
}
for(Entry<String, String> custom : HttpServer.getCustomHeaders().entrySet()) {
out.append(custom.getKey());
out.append(": ");
out.append(custom.getValue());
out.append("\r\n");
}
out.append("\r\n");
out.flush();
}

View File

@ -263,6 +263,11 @@ showplayerfacesinmenu: true
# Set sidebaropened: 'true' to pin menu sidebar opened permanently, 'pinned' to default the sidebar to pinned, but allow it to unpin
#sidebaropened: true
# Customized HTTP response headers - add 'id: value' pairs to all HTTP response headers (internal web server only)
#http-response-headers:
# Access-Control-Allow-Origin: "my-domain.com"
# X-Custom-Header-Of-Mine: "MyHeaderValue"
joinmessage: "%playername% joined"
quitmessage: "%playername% quit"
spammessage: "You may only chat once every %interval% seconds."