package io.vertx.core.http.impl;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.compression.ZlibCodecFactory;
import io.netty.handler.codec.http.websocketx.extensions.WebSocketServerExtensionHandler;
import io.netty.handler.codec.http.websocketx.extensions.WebSocketServerExtensionHandshaker;
import io.netty.handler.codec.http.websocketx.extensions.compression.DeflateFrameServerExtensionHandshaker;
import io.netty.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateServerExtensionHandshaker;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpConnection;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.impl.ContextInternal;
import java.util.ArrayList;
public class HttpServerConnectionHandler implements Handler<HttpServerConnection> {
final HttpServerImpl server;
final Handler<HttpServerRequest> requestHandler;
final Handler<ServerWebSocket> wsHandler;
final Handler<HttpConnection> connectionHandler;
final Handler<Throwable> exceptionHandler;
public HttpServerConnectionHandler(
HttpServerImpl server,
Handler<HttpServerRequest> requestHandler,
Handler<ServerWebSocket> wsHandler,
Handler<HttpConnection> connectionHandler,
Handler<Throwable> exceptionHandler) {
this.server = server;
this.requestHandler = requestHandler;
this.wsHandler = wsHandler;
this.connectionHandler = connectionHandler;
this.exceptionHandler = exceptionHandler;
}
@Override
public void handle(HttpServerConnection conn) {
Handler<HttpServerRequest> requestHandler = this.requestHandler;
if (HttpServerImpl.DISABLE_WEBSOCKETS) {
} else {
if (conn instanceof Http1xServerConnection) {
requestHandler = new Http1xServerRequestHandler(this);
Http1xServerConnection c = (Http1xServerConnection) conn;
initializeWebSocketExtensions(c.channelHandlerContext().pipeline());
}
}
conn.exceptionHandler(exceptionHandler);
conn.handler(requestHandler);
if (connectionHandler != null) {
ContextInternal ctx = conn.getContext();
ContextInternal prev = ctx.beginDispatch();
try {
connectionHandler.handle(conn);
} catch (Exception e) {
ctx.reportException(e);
} finally {
ctx.endDispatch(prev);
}
}
}
private void initializeWebSocketExtensions(ChannelPipeline pipeline) {
ArrayList<WebSocketServerExtensionHandshaker> extensionHandshakers = new ArrayList<>();
if (server.options.getPerFrameWebSocketCompressionSupported()) {
extensionHandshakers.add(new DeflateFrameServerExtensionHandshaker(server.options.getWebSocketCompressionLevel()));
}
if (server.options.getPerMessageWebSocketCompressionSupported()) {
extensionHandshakers.add(new PerMessageDeflateServerExtensionHandshaker(server.options.getWebSocketCompressionLevel(),
ZlibCodecFactory.isSupportingWindowSizeAndMemLevel(), PerMessageDeflateServerExtensionHandshaker.MAX_WINDOW_SIZE,
server.options.getWebSocketAllowServerNoContext(), server.options.getWebSocketPreferredClientNoContext()));
}
if (!extensionHandshakers.isEmpty()) {
WebSocketServerExtensionHandler extensionHandler = new WebSocketServerExtensionHandler(
extensionHandshakers.toArray(new WebSocketServerExtensionHandshaker[0]));
pipeline.addBefore("handler", "webSocketExtensionHandler", extensionHandler);
}
}
}