package io.netty.handler.codec.http.websocketx;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.ssl.SslHandler;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

/* JADX INFO: loaded from: classes.dex */
public class WebSocketServerProtocolHandshakeHandler extends ChannelInboundHandlerAdapter {
    private final boolean allowExtensions;
    private final boolean allowMaskMismatch;
    private final boolean checkStartsWith;
    private final int maxFramePayloadSize;
    private final String subprotocols;
    private final String websocketPath;

    public WebSocketServerProtocolHandshakeHandler(String str, String str2, boolean z2, int i2, boolean z3) {
        this(str, str2, z2, i2, z3, false);
    }

    public WebSocketServerProtocolHandshakeHandler(String str, String str2, boolean z2, int i2, boolean z3, boolean z4) {
        this.websocketPath = str;
        this.subprotocols = str2;
        this.allowExtensions = z2;
        this.maxFramePayloadSize = i2;
        this.allowMaskMismatch = z3;
        this.checkStartsWith = z4;
    }

    private static String getWebSocketLocation(ChannelPipeline channelPipeline, HttpRequest httpRequest, String str) {
        return (channelPipeline.get(SslHandler.class) != null ? "wss" : "ws") + "://" + httpRequest.headers().get(HttpHeaderNames.HOST) + str;
    }

    private boolean isNotWebSocketPath(FullHttpRequest fullHttpRequest) {
        boolean z2 = this.checkStartsWith;
        String strUri = fullHttpRequest.uri();
        if (z2) {
            if (!strUri.startsWith(this.websocketPath)) {
                return true;
            }
        } else if (!strUri.equals(this.websocketPath)) {
            return true;
        }
        return false;
    }

    private static void sendHttpResponse(ChannelHandlerContext channelHandlerContext, HttpRequest httpRequest, HttpResponse httpResponse) {
        ChannelFuture channelFutureWriteAndFlush = channelHandlerContext.channel().writeAndFlush(httpResponse);
        if (HttpUtil.isKeepAlive(httpRequest) && httpResponse.status().code() == 200) {
            return;
        }
        channelFutureWriteAndFlush.addListener((GenericFutureListener<? extends Future<? super Void>>) ChannelFutureListener.CLOSE);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelRead(final ChannelHandlerContext channelHandlerContext, Object obj) {
        final FullHttpRequest fullHttpRequest = (FullHttpRequest) obj;
        if (isNotWebSocketPath(fullHttpRequest)) {
            channelHandlerContext.fireChannelRead(obj);
            return;
        }
        try {
            if (fullHttpRequest.method() != HttpMethod.GET) {
                sendHttpResponse(channelHandlerContext, fullHttpRequest, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN));
                return;
            }
            final WebSocketServerHandshaker webSocketServerHandshakerNewHandshaker = new WebSocketServerHandshakerFactory(getWebSocketLocation(channelHandlerContext.pipeline(), fullHttpRequest, this.websocketPath), this.subprotocols, this.allowExtensions, this.maxFramePayloadSize, this.allowMaskMismatch).newHandshaker(fullHttpRequest);
            if (webSocketServerHandshakerNewHandshaker == null) {
                WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(channelHandlerContext.channel());
            } else {
                webSocketServerHandshakerNewHandshaker.handshake(channelHandlerContext.channel(), fullHttpRequest).addListener((GenericFutureListener<? extends Future<? super Void>>) new ChannelFutureListener() { // from class: io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandshakeHandler.1
                    @Override // io.netty.util.concurrent.GenericFutureListener
                    public void operationComplete(ChannelFuture channelFuture) {
                        if (!channelFuture.isSuccess()) {
                            channelHandlerContext.fireExceptionCaught(channelFuture.cause());
                        } else {
                            channelHandlerContext.fireUserEventTriggered((Object) WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE);
                            channelHandlerContext.fireUserEventTriggered((Object) new WebSocketServerProtocolHandler.HandshakeComplete(fullHttpRequest.uri(), fullHttpRequest.headers(), webSocketServerHandshakerNewHandshaker.selectedSubprotocol()));
                        }
                    }
                });
                WebSocketServerProtocolHandler.setHandshaker(channelHandlerContext.channel(), webSocketServerHandshakerNewHandshaker);
                channelHandlerContext.pipeline().replace(this, "WS403Responder", WebSocketServerProtocolHandler.forbiddenHttpRequestResponder());
            }
        } finally {
            fullHttpRequest.release();
        }
    }
}
