package io.netty.handler.codec.http.websocketx.extensions.compression;
import static io.netty.handler.codec.http.websocketx.extensions.compression.
PerMessageDeflateServerExtensionHandshaker.*;
import io.netty.handler.codec.compression.ZlibCodecFactory;
import io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtension;
import io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandshaker;
import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionData;
import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionDecoder;
import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public final class PerMessageDeflateClientExtensionHandshaker implements WebSocketClientExtensionHandshaker {
private final int compressionLevel;
private final boolean allowClientWindowSize;
private final int requestedServerWindowSize;
private final boolean allowClientNoContext;
private final boolean requestedServerNoContext;
public PerMessageDeflateClientExtensionHandshaker() {
this(6, ZlibCodecFactory.isSupportingWindowSizeAndMemLevel(), MAX_WINDOW_SIZE, false, false);
}
public PerMessageDeflateClientExtensionHandshaker(int compressionLevel,
boolean allowClientWindowSize, int requestedServerWindowSize,
boolean allowClientNoContext, boolean requestedServerNoContext) {
if (requestedServerWindowSize > MAX_WINDOW_SIZE || requestedServerWindowSize < MIN_WINDOW_SIZE) {
throw new IllegalArgumentException(
"requestedServerWindowSize: " + requestedServerWindowSize + " (expected: 8-15)");
}
if (compressionLevel < 0 || compressionLevel > 9) {
throw new IllegalArgumentException(
"compressionLevel: " + compressionLevel + " (expected: 0-9)");
}
this.compressionLevel = compressionLevel;
this.allowClientWindowSize = allowClientWindowSize;
this.requestedServerWindowSize = requestedServerWindowSize;
this.allowClientNoContext = allowClientNoContext;
this.requestedServerNoContext = requestedServerNoContext;
}
@Override
public WebSocketExtensionData newRequestData() {
HashMap<String, String> parameters = new HashMap<String, String>(4);
if (requestedServerWindowSize != MAX_WINDOW_SIZE) {
parameters.put(SERVER_NO_CONTEXT, null);
}
if (allowClientNoContext) {
parameters.put(CLIENT_NO_CONTEXT, null);
}
if (requestedServerWindowSize != MAX_WINDOW_SIZE) {
parameters.put(SERVER_MAX_WINDOW, Integer.toString(requestedServerWindowSize));
}
if (allowClientWindowSize) {
parameters.put(CLIENT_MAX_WINDOW, null);
}
return new WebSocketExtensionData(PERMESSAGE_DEFLATE_EXTENSION, parameters);
}
@Override
public WebSocketClientExtension handshakeExtension(WebSocketExtensionData extensionData) {
if (!PERMESSAGE_DEFLATE_EXTENSION.equals(extensionData.name())) {
return null;
}
boolean succeed = true;
int clientWindowSize = MAX_WINDOW_SIZE;
int serverWindowSize = MAX_WINDOW_SIZE;
boolean serverNoContext = false;
boolean clientNoContext = false;
Iterator<Entry<String, String>> parametersIterator =
extensionData.parameters().entrySet().iterator();
while (succeed && parametersIterator.hasNext()) {
Entry<String, String> parameter = parametersIterator.next();
if (CLIENT_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) {
if (allowClientWindowSize) {
clientWindowSize = Integer.parseInt(parameter.getValue());
} else {
succeed = false;
}
} else if (SERVER_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) {
serverWindowSize = Integer.parseInt(parameter.getValue());
if (clientWindowSize > MAX_WINDOW_SIZE || clientWindowSize < MIN_WINDOW_SIZE) {
succeed = false;
}
} else if (CLIENT_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) {
if (allowClientNoContext) {
clientNoContext = true;
} else {
succeed = false;
}
} else if (SERVER_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) {
if (requestedServerNoContext) {
serverNoContext = true;
} else {
succeed = false;
}
} else {
succeed = false;
}
}
if ((requestedServerNoContext && !serverNoContext) ||
requestedServerWindowSize != serverWindowSize) {
succeed = false;
}
if (succeed) {
return new PermessageDeflateExtension(serverNoContext, serverWindowSize,
clientNoContext, clientWindowSize);
} else {
return null;
}
}
private final class PermessageDeflateExtension implements WebSocketClientExtension {
private final boolean serverNoContext;
private final int serverWindowSize;
private final boolean clientNoContext;
private final int clientWindowSize;
@Override
public int rsv() {
return RSV1;
}
public PermessageDeflateExtension(boolean serverNoContext, int serverWindowSize,
boolean clientNoContext, int clientWindowSize) {
this.serverNoContext = serverNoContext;
this.serverWindowSize = serverWindowSize;
this.clientNoContext = clientNoContext;
this.clientWindowSize = clientWindowSize;
}
@Override
public WebSocketExtensionEncoder newExtensionEncoder() {
return new PerMessageDeflateEncoder(compressionLevel, serverWindowSize, serverNoContext);
}
@Override
public WebSocketExtensionDecoder newExtensionDecoder() {
return new PerMessageDeflateDecoder(clientNoContext);
}
}
}