<commit_before>package org.async.rmi.http;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import org.async.rmi.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import static io.netty.handler.codec.http.HttpHeaders.Names.*;
import static io.netty.handler.codec.http.HttpMethod.GET;
import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

/**
 * Created by <NAME>
 * 11/14/14.
 */
public class ClassLoaderHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    @SuppressWarnings("UnusedDeclaration")
    private static final Logger logger = LoggerFactory.getLogger(ClassLoaderHandler.class);
    private final ClassLoader cl;

    public ClassLoaderHandler(ClassLoader cl) {
        this.cl = cl;
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) {
        if (!request.decoderResult().isSuccess()) {
            sendError(ctx, BAD_REQUEST);
            return;
        }
        if (request.method() != GET) {
            sendError(ctx, METHOD_NOT_ALLOWED);
            return;
        }

        final String uri = request.uri();
        final String className = sanitizeUri(uri);
        if (className == null) {
            sendError(ctx, FORBIDDEN);
            return;
        }
        logger.debug("serving http request for class {}", className);
        try {
            byte[] bytes = getClassBytes(className);

            boolean keepAlive = HttpHeaderUtil.isKeepAlive(request);
            FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(bytes));
            response.headers().set(CONTENT_TYPE, "application/x-java-class");
            response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
            if (!keepAlive) {
                ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
            } else {
                response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
                ctx.writeAndFlush(response);
            }
        } catch (Exception e) {
            logger.warn("exception while serving request {}, for class {}", request, className, e);
            sendError(ctx, INTERNAL_SERVER_ERROR);
        }
    }

    private byte[] getClassBytes(String className) throws IOException {
        String classAsPath = className.replace('.', '/') + ".class";<commit_msg>Fix travis !<commit_after>package org.async.rmi.http;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import org.async.rmi.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import static io.netty.handler.codec.http.HttpHeaders.Names.*;
import static io.netty.handler.codec.http.HttpMethod.GET;
import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

/**
 * Created by <NAME>
 * 11/14/14.
 */
public class ClassLoaderHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    @SuppressWarnings("UnusedDeclaration")
    private static final Logger logger = LoggerFactory.getLogger(ClassLoaderHandler.class);
    private final ClassLoader cl;

    public ClassLoaderHandler(ClassLoader cl) {
        this.cl = cl;
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) {
        if (!request.decoderResult().isSuccess()) {
            sendError(ctx, BAD_REQUEST);
            return;
        }
        if (request.method() != GET) {
            sendError(ctx, METHOD_NOT_ALLOWED);
            return;
        }

        final String uri = request.uri();
        final String className = sanitizeUri(uri);
        if (className == null) {
            sendError(ctx, FORBIDDEN);
            return;
        }
        logger.debug("serving http request for class {}", className);
        try {
            byte[] bytes = getClassBytes(className);

            boolean keepAlive = HttpHeaderUtil.isKeepAlive(request);
            FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(bytes));
            response.headers().set(CONTENT_TYPE, "application/x-java-class");
            response.headers().set(CONTENT_LENGTH, String.valueOf(response.content().readableBytes()));
            if (!keepAlive) {
                ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
            } else {
                response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
                ctx.writeAndFlush(response);
            }
        } catch (Exception e) {
            logger.warn("exception while serving request {}, for class {}", request, className, e);
            sendError(ctx, INTERNAL_SERVER_ERROR);
        }
    }

    private byte[] getClassBytes(String className) throws IOException {
        String classAsPath = className.replace('.', '/') + ".class";
{'fraction_non_alphanumeric': 0.11041629444345184, 'fraction_numerical': 0.004288011434697159, 'mean_word_length': 3.0506512301013027, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2010948', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1539, 'n_words': 303}