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

import g.a.a.a.a;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestEncoder;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpScheme;
import io.netty.util.NetUtil;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import java.net.URI;
import java.nio.channels.ClosedChannelException;
import java.util.Locale;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public abstract class WebSocketClientHandshaker {
    private volatile String actualSubprotocol;
    public final HttpHeaders customHeaders;
    private final String expectedSubprotocol;
    private volatile boolean handshakeComplete;
    private final int maxFramePayloadLength;
    private final URI uri;
    private final WebSocketVersion version;
    private static final ClosedChannelException CLOSED_CHANNEL_EXCEPTION = (ClosedChannelException) a.N(WebSocketClientHandshaker.class, "processHandshake(...)");
    private static final String HTTP_SCHEME_PREFIX = HttpScheme.HTTP + "://";
    private static final String HTTPS_SCHEME_PREFIX = HttpScheme.HTTPS + "://";

    public WebSocketClientHandshaker(URI uri, WebSocketVersion webSocketVersion, String str, HttpHeaders httpHeaders, int i2) {
        this.uri = uri;
        this.version = webSocketVersion;
        this.expectedSubprotocol = str;
        this.customHeaders = httpHeaders;
        this.maxFramePayloadLength = i2;
    }

    public static String rawPath(URI uri) {
        String rawPath = uri.getRawPath();
        String rawQuery = uri.getRawQuery();
        if (rawQuery != null && !rawQuery.isEmpty()) {
            rawPath = a.k(rawPath, '?', rawQuery);
        }
        return (rawPath == null || rawPath.isEmpty()) ? "/" : rawPath;
    }

    private void setActualSubprotocol(String str) {
        this.actualSubprotocol = str;
    }

    private void setHandshakeComplete() {
        this.handshakeComplete = true;
    }

    public static CharSequence websocketHostValue(URI uri) {
        int port = uri.getPort();
        if (port == -1) {
            return uri.getHost();
        }
        String host = uri.getHost();
        HttpScheme httpScheme = HttpScheme.HTTP;
        if (port == httpScheme.port()) {
            return (httpScheme.name().contentEquals(uri.getScheme()) || WebSocketScheme.WS.name().contentEquals(uri.getScheme())) ? host : NetUtil.toSocketAddressString(host, port);
        }
        HttpScheme httpScheme2 = HttpScheme.HTTPS;
        return port == httpScheme2.port() ? (httpScheme2.name().contentEquals(uri.getScheme()) || WebSocketScheme.WSS.name().contentEquals(uri.getScheme())) ? host : NetUtil.toSocketAddressString(host, port) : NetUtil.toSocketAddressString(host, port);
    }

    public static CharSequence websocketOriginValue(URI uri) {
        String str;
        String scheme = uri.getScheme();
        int port = uri.getPort();
        WebSocketScheme webSocketScheme = WebSocketScheme.WSS;
        if (webSocketScheme.name().contentEquals(scheme) || HttpScheme.HTTPS.name().contentEquals(scheme) || (scheme == null && port == webSocketScheme.port())) {
            str = HTTPS_SCHEME_PREFIX;
        } else {
            str = HTTP_SCHEME_PREFIX;
            webSocketScheme = WebSocketScheme.WS;
        }
        int iPort = webSocketScheme.port();
        String lowerCase = uri.getHost().toLowerCase(Locale.US);
        if (port == iPort || port == -1) {
            return a.t(str, lowerCase);
        }
        StringBuilder sbF = a.F(str);
        sbF.append(NetUtil.toSocketAddressString(lowerCase, port));
        return sbF.toString();
    }

    public String actualSubprotocol() {
        return this.actualSubprotocol;
    }

    public ChannelFuture close(Channel channel, CloseWebSocketFrame closeWebSocketFrame) {
        Objects.requireNonNull(channel, "channel");
        return close(channel, closeWebSocketFrame, channel.newPromise());
    }

    public ChannelFuture close(Channel channel, CloseWebSocketFrame closeWebSocketFrame, ChannelPromise channelPromise) {
        Objects.requireNonNull(channel, "channel");
        return channel.writeAndFlush(closeWebSocketFrame, channelPromise);
    }

    public String expectedSubprotocol() {
        return this.expectedSubprotocol;
    }

    /* JADX WARN: Removed duplicated region for block: B:28:0x005d  */
    /* JADX WARN: Removed duplicated region for block: B:47:0x00d7  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void finishHandshake(io.netty.channel.Channel r8, io.netty.handler.codec.http.FullHttpResponse r9) {
        /*
            Method dump skipped, instruction units count: 236
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker.finishHandshake(io.netty.channel.Channel, io.netty.handler.codec.http.FullHttpResponse):void");
    }

    public ChannelFuture handshake(Channel channel) {
        Objects.requireNonNull(channel, "channel");
        return handshake(channel, channel.newPromise());
    }

    public final ChannelFuture handshake(Channel channel, final ChannelPromise channelPromise) {
        FullHttpRequest fullHttpRequestNewHandshakeRequest = newHandshakeRequest();
        if (((HttpResponseDecoder) channel.pipeline().get(HttpResponseDecoder.class)) == null && ((HttpClientCodec) channel.pipeline().get(HttpClientCodec.class)) == null) {
            channelPromise.setFailure((Throwable) new IllegalStateException("ChannelPipeline does not contain a HttpResponseDecoder or HttpClientCodec"));
            return channelPromise;
        }
        channel.writeAndFlush(fullHttpRequestNewHandshakeRequest).addListener((GenericFutureListener<? extends Future<? super Void>>) new ChannelFutureListener() { // from class: io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker.1
            @Override // io.netty.util.concurrent.GenericFutureListener
            public void operationComplete(ChannelFuture channelFuture) {
                if (!channelFuture.isSuccess()) {
                    channelPromise.setFailure(channelFuture.cause());
                    return;
                }
                ChannelPipeline channelPipelinePipeline = channelFuture.channel().pipeline();
                ChannelHandlerContext channelHandlerContextContext = channelPipelinePipeline.context(HttpRequestEncoder.class);
                if (channelHandlerContextContext == null) {
                    channelHandlerContextContext = channelPipelinePipeline.context(HttpClientCodec.class);
                }
                if (channelHandlerContextContext == null) {
                    channelPromise.setFailure((Throwable) new IllegalStateException("ChannelPipeline does not contain a HttpRequestEncoder or HttpClientCodec"));
                } else {
                    channelPipelinePipeline.addAfter(channelHandlerContextContext.name(), "ws-encoder", WebSocketClientHandshaker.this.newWebSocketEncoder());
                    channelPromise.setSuccess();
                }
            }
        });
        return channelPromise;
    }

    public boolean isHandshakeComplete() {
        return this.handshakeComplete;
    }

    public int maxFramePayloadLength() {
        return this.maxFramePayloadLength;
    }

    public abstract FullHttpRequest newHandshakeRequest();

    public abstract WebSocketFrameEncoder newWebSocketEncoder();

    public abstract WebSocketFrameDecoder newWebsocketDecoder();

    public final ChannelFuture processHandshake(Channel channel, HttpResponse httpResponse) {
        return processHandshake(channel, httpResponse, channel.newPromise());
    }

    public final ChannelFuture processHandshake(final Channel channel, HttpResponse httpResponse, final ChannelPromise channelPromise) {
        try {
            if (httpResponse instanceof FullHttpResponse) {
                finishHandshake(channel, (FullHttpResponse) httpResponse);
                channelPromise.setSuccess();
            } else {
                ChannelPipeline channelPipelinePipeline = channel.pipeline();
                ChannelHandlerContext channelHandlerContextContext = channelPipelinePipeline.context(HttpResponseDecoder.class);
                if (channelHandlerContextContext == null && (channelHandlerContextContext = channelPipelinePipeline.context(HttpClientCodec.class)) == null) {
                    return channelPromise.setFailure((Throwable) new IllegalStateException("ChannelPipeline does not contain a HttpResponseDecoder or HttpClientCodec"));
                }
                channelPipelinePipeline.addAfter(channelHandlerContextContext.name(), "httpAggregator", new HttpObjectAggregator(8192));
                channelPipelinePipeline.addAfter("httpAggregator", "handshaker", new SimpleChannelInboundHandler<FullHttpResponse>() { // from class: io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker.4
                    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
                    public void channelInactive(ChannelHandlerContext channelHandlerContext) {
                        channelPromise.tryFailure(WebSocketClientHandshaker.CLOSED_CHANNEL_EXCEPTION);
                        channelHandlerContext.fireChannelInactive();
                    }

                    @Override // io.netty.channel.SimpleChannelInboundHandler
                    public void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpResponse fullHttpResponse) {
                        channelHandlerContext.pipeline().remove(this);
                        try {
                            WebSocketClientHandshaker.this.finishHandshake(channel, fullHttpResponse);
                            channelPromise.setSuccess();
                        } catch (Throwable th) {
                            channelPromise.setFailure(th);
                        }
                    }

                    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler
                    public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable th) {
                        channelHandlerContext.pipeline().remove(this);
                        channelPromise.setFailure(th);
                    }
                });
                channelHandlerContextContext.fireChannelRead(ReferenceCountUtil.retain(httpResponse));
            }
        } catch (Throwable th) {
            channelPromise.setFailure(th);
        }
        return channelPromise;
    }

    public URI uri() {
        return this.uri;
    }

    public abstract void verify(FullHttpResponse fullHttpResponse);

    public WebSocketVersion version() {
        return this.version;
    }
}
