Permalink
Browse files

Change the way Gzip is handled. This enables interceptors to painless…

…ly define new Gzip settings for their own responses.
  • Loading branch information...
LordFokas committed Dec 12, 2016
1 parent 060d6d2 commit fe4feb600c6c918eb304bc7d446db92b40661085
@@ -426,7 +426,7 @@ public void execute() throws IOException {
String acceptEncoding = this.headers.get("accept-encoding");
this.cookies.unloadQueue(r);
r.setRequestMethod(this.method);
r.setGzipEncoding(httpd.useGzipWhenAccepted(r) && acceptEncoding != null && acceptEncoding.contains("gzip"));
r.setGzipEncoding(r.useGzipWhenAccepted() && acceptEncoding != null && acceptEncoding.contains("gzip"));
r.setKeepAlive(keepAlive);
r.send(this.outputStream);
}
@@ -485,16 +485,6 @@ public static String decodePercent(String str) {
return decoded;
}
/**
* @return true if the gzip compression should be used if the client
* accespts it. Default this option is on for text content and off
* for everything. Override this for custom semantics.
*/
@SuppressWarnings("static-method")
protected boolean useGzipWhenAccepted(Response r) {
return r.getMimeType() != null && (r.getMimeType().toLowerCase().contains("text/") || r.getMimeType().toLowerCase().contains("/json"));
}
public final int getListeningPort() {
return this.myServerSocket == null ? -1 : this.myServerSocket.getLocalPort();
}
@@ -115,6 +115,14 @@ public String put(String key, String value) {
private boolean keepAlive;
private List<String> cookieHeaders;
private GzipUsage gzipUsage = GzipUsage.DEFAULT;
private static enum GzipUsage {
DEFAULT,
ALWAYS,
NEVER;
}
/**
* Creates a fixed length response if totalBytes>=0, otherwise chunked.
@@ -407,4 +415,18 @@ public static Response newFixedLengthResponse(IStatus status, String mimeType, S
public static Response newFixedLengthResponse(String msg) {
return newFixedLengthResponse(Status.OK, NanoHTTPD.MIME_HTML, msg);
}
public Response setUseGzip(boolean useGzip) {
gzipUsage = useGzip ? GzipUsage.ALWAYS : GzipUsage.NEVER;
return this;
}
// If a Gzip usage has been enforced, use it.
// Else decide whether or not to use Gzip.
public boolean useGzipWhenAccepted() {
if (gzipUsage == GzipUsage.DEFAULT)
return getMimeType() != null && (getMimeType().toLowerCase().contains("text/") || getMimeType().toLowerCase().contains("/json"));
else
return gzipUsage == GzipUsage.ALWAYS;
}
}
@@ -67,12 +67,7 @@ public TestServer() {
@Override
public Response serve(IHTTPSession session) {
return response;
}
@Override
protected boolean useGzipWhenAccepted(Response r) {
return true;
return response.setUseGzip(true);
}
}
@@ -186,12 +186,4 @@ public Response handleWebSocket(final IHTTPSession session) {
return null;
}
}
/**
* not all websockets implementations accept gzip compression.
*/
@Override
protected boolean useGzipWhenAccepted(Response r) {
return false;
}
}
@@ -57,6 +57,8 @@
private State state = State.UNCONNECTED;
protected boolean enforceNoGzip = true;
private final IHTTPSession handshakeRequest;
private final Response handshakeResponse = new Response(Status.SWITCH_PROTOCOL, null, (InputStream) null, 0) {
@@ -75,7 +77,8 @@ public void send(OutputStream out) {
public WebSocket(IHTTPSession handshakeRequest) {
this.handshakeRequest = handshakeRequest;
this.in = handshakeRequest.getInputStream();
if (enforceNoGzip)
handshakeResponse.setUseGzip(false);
this.handshakeResponse.addHeader(NanoWSD.HEADER_UPGRADE, NanoWSD.HEADER_UPGRADE_VALUE);
this.handshakeResponse.addHeader(NanoWSD.HEADER_CONNECTION, NanoWSD.HEADER_CONNECTION_VALUE);
}

0 comments on commit fe4feb6

Please sign in to comment.