<commit_before>package com.github.dreamhead.moco.internal;

import com.github.dreamhead.moco.HttpRequest;
import com.github.dreamhead.moco.model.DefaultHttpRequest;
import com.github.dreamhead.moco.model.DefaultMutableHttpResponse;
import com.github.dreamhead.moco.util.ByteBufs;
import com.github.dreamhead.moco.util.Strings;
import com.github.dreamhead.moco.websocket.ActualWebSocketServer;
import com.github.dreamhead.moco.websocket.WebsocketResponse;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
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.HttpResponseStatus;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;

import static com.github.dreamhead.moco.model.DefaultMutableHttpResponse.newResponse;
import static com.google.common.net.HttpHeaders.UPGRADE;
import static io.netty.channel.ChannelHandler.Sharable;
import static io.netty.handler.codec.http.HttpUtil.isContentLengthSet;
import static io.netty.handler.codec.http.HttpUtil.isKeepAlive;
import static io.netty.handler.codec.http.HttpUtil.setContentLength;
import static io.netty.handler.codec.http.HttpUtil.setKeepAlive;

@Sharable
public final class MocoHandler extends SimpleChannelInboundHandler<Object> {
    private static final int DEFAULT_STATUS = HttpResponseStatus.OK.code();
    private final ActualWebSocketServer websocketServer;
    private final ActualHttpServer server;

    public MocoHandler(final ActualHttpServer server) {
        this.server = server;
        this.websocketServer = server.getWebsocketServer();
    }

    @Override
    public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
        if (websocketServer != null) {
            websocketServer.disconnect(ctx.channel());
        }
    }

    @Override
    protected void channelRead0(final ChannelHandlerContext ctx, final Object message) {
        if (message instanceof FullHttpRequest) {
            handleHttpRequest(ctx, (FullHttpRequest) message);
            return;
        }

        if (message instanceof WebSocketFrame) {
            handleWebsocketFrame(ctx, (WebSocketFrame) message);
        }
    }

    private void handleWebsocketFrame(final ChannelHandlerContext ctx, final WebSocketFrame message) {
        if (message instanceof PingWebSocketFrame) {
            PongWebSocketFrame pingPong = websocketServer.handlePingPong((PingWebSocketFrame) message);
            ctx.channel().writeAndFlush(pingPong);
            return;
        }
        WebsocketResponse response = websocketServer.handleRequest(ctx, message);
        ByteBuf byteBuf = ByteBufs.toByteBuf(response.getContent().getContent());
        ctx.channel().writeAndFlush(new BinaryWebSocketFrame(byteBuf));
    }

    private void handleHttpRequest(final ChannelHandlerContext ctx, final FullHttpRequest request) {
        if (!request.decoderResult().isSuccess() || !upgradeWebsocket(request)) {
            FullHttpResponse response = handleRequest(request);
            closeIfNotKeepAlive(request, ctx.write(response));
            return;
        }

        websocketServer.connectRequest(ctx, request);
    }

    private boolean upgradeWebsocket(final FullHttpRequest request) {
        String upgrade = request.headers().get(UPGRADE);
        return "websocket".equals(Strings.strip(upgrade));
    }

    @Override<commit_msg>cleaned up handle websocket frame in moco handler<commit_after>package com.github.dreamhead.moco.internal;

import com.github.dreamhead.moco.HttpRequest;
import com.github.dreamhead.moco.model.DefaultHttpRequest;
import com.github.dreamhead.moco.model.DefaultMutableHttpResponse;
import com.github.dreamhead.moco.util.ByteBufs;
import com.github.dreamhead.moco.util.Strings;
import com.github.dreamhead.moco.websocket.ActualWebSocketServer;
import com.github.dreamhead.moco.websocket.WebsocketResponse;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
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.HttpResponseStatus;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;

import static com.github.dreamhead.moco.model.DefaultMutableHttpResponse.newResponse;
import static com.google.common.net.HttpHeaders.UPGRADE;
import static io.netty.channel.ChannelHandler.Sharable;
import static io.netty.handler.codec.http.HttpUtil.isContentLengthSet;
import static io.netty.handler.codec.http.HttpUtil.isKeepAlive;
import static io.netty.handler.codec.http.HttpUtil.setContentLength;
import static io.netty.handler.codec.http.HttpUtil.setKeepAlive;

@Sharable
public final class MocoHandler extends SimpleChannelInboundHandler<Object> {
    private static final int DEFAULT_STATUS = HttpResponseStatus.OK.code();
    private final ActualWebSocketServer websocketServer;
    private final ActualHttpServer server;

    public MocoHandler(final ActualHttpServer server) {
        this.server = server;
        this.websocketServer = server.getWebsocketServer();
    }

    @Override
    public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
        if (websocketServer != null) {
            websocketServer.disconnect(ctx.channel());
        }
    }

    @Override
    protected void channelRead0(final ChannelHandlerContext ctx, final Object message) {
        if (message instanceof FullHttpRequest) {
            handleHttpRequest(ctx, (FullHttpRequest) message);
            return;
        }

        if (message instanceof WebSocketFrame) {
            handleWebsocketFrame(ctx, (WebSocketFrame) message);
        }
    }

    private void handleWebsocketFrame(final ChannelHandlerContext ctx, final WebSocketFrame message) {
        WebSocketFrame frame = getResponseFrame(ctx, message);
        ctx.channel().writeAndFlush(frame);
    }

    private WebSocketFrame getResponseFrame(final ChannelHandlerContext ctx, final WebSocketFrame message) {
        if (message instanceof PingWebSocketFrame) {
            return websocketServer.handlePingPong((PingWebSocketFrame) message);
        }

        WebsocketResponse response = websocketServer.handleRequest(ctx, message);
        ByteBuf byteBuf = ByteBufs.toByteBuf(response.getContent().getContent());
        return new BinaryWebSocketFrame(byteBuf);
    }

    private void handleHttpRequest(final ChannelHandlerContext ctx, final FullHttpRequest request) {
        if (!request.decoderResult().isSuccess() || !upgradeWebsocket(request)) {
            FullHttpResponse response = handleRequest(request);
            closeIfNotKeepAlive(request, ctx.write(response));
            return;
        }

        websocketServer.connectRequest(ctx, request);
    }

    private boolean upgradeWebsocket(final FullHttpRequest request) {
        String upgrade = request.headers().get(UPGRADE);
        return "websocket".equals(Strings.strip(upgrade));
    }

    @Override
{'fraction_non_alphanumeric': 0.08894668400520156, 'fraction_numerical': 0.0002600780234070221, 'mean_word_length': 4.278654770075498, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5014495', 'n_tokens_mistral': 2185, 'n_tokens_neox': 2123, 'n_words': 383}