<commit_before>package cn.lechange.happor;

import java.util.Map;
import org.apache.log4j.Logger;

import cn.lechange.happor.controller.HttpController;
import cn.lechange.happor.utils.UriParser;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
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.timeout.IdleStateEvent;

public class HttpRootController extends ChannelInboundHandlerAdapter {
	
	private static Logger logger = Logger.getLogger(HttpRootController.class);

	private HapporWebserver server;
	
	public HttpRootController(HapporWebserver server) {
		this.server = server;
	}
	
	private FullHttpRequest request;
	private FullHttpResponse response;
	
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		// TODO Auto-generated method stub
		if (msg instanceof FullHttpRequest) {
			request = (FullHttpRequest) msg;
			response = new DefaultFullHttpResponse(
					request.getProtocolVersion(), HttpResponseStatus.OK);
			
			HttpController lastController = null;
			
			Map<String, HttpController> controllers = server.getContext().getControllers();
			for (Map.Entry<String, HttpController> entry : controllers.entrySet()) {
				String name = entry.getKey();
				HttpController hc = entry.getValue();
				String method = hc.getMethod();
				String uriPattern = hc.getUriPattern();
				UriParser uriParser = new UriParser(request.getUri());
				
				if ((method == null || method.isEmpty() || request.getMethod().name().equals(method))
						&& uriParser.matches(uriPattern)) {
					HttpController controller = server.getContext().getController(name);
					controller.setPrev(lastController);
					controller.setServer(server);
					controller.setUriParser(uriParser);
					boolean isEnd = controller.input(ctx, request, response);
					if (isEnd) {
						break;
					}
					lastController = controller;
				}
			}
		}
	}

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx,
			Object evt) throws Exception {
		if (evt instanceof IdleStateEvent) {
			if (request == null) {
				logger.warn("connection[" + ctx + "] timeout.");<commit_msg>optimize code for higher performance<commit_after>package cn.lechange.happor;

import java.util.Map;
import org.apache.log4j.Logger;

import cn.lechange.happor.controller.HttpController;
import cn.lechange.happor.utils.UriParser;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
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.timeout.IdleStateEvent;

public class HttpRootController extends ChannelInboundHandlerAdapter {
	
	private static Logger logger = Logger.getLogger(HttpRootController.class);

	private HapporWebserver server;
	
	public HttpRootController(HapporWebserver server) {
		this.server = server;
	}
	
	private FullHttpRequest request;
	private FullHttpResponse response;
	
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		// TODO Auto-generated method stub
		if (msg instanceof FullHttpRequest) {
			request = (FullHttpRequest) msg;
			response = new DefaultFullHttpResponse(
					request.getProtocolVersion(), HttpResponseStatus.OK);
			
			HttpController lastController = null;
			UriParser uriParser = new UriParser(request.getUri());
			
			Map<String, HttpController> controllers = server.getContext().getControllers();
			for (Map.Entry<String, HttpController> entry : controllers.entrySet()) {
				String name = entry.getKey();
				HttpController hc = entry.getValue();
				String method = hc.getMethod();
				String uriPattern = hc.getUriPattern();
				
				if ((method == null || method.isEmpty() || request.getMethod().name().equals(method))
						&& uriParser.matches(uriPattern)) {
					HttpController controller = server.getContext().getController(name);
					controller.setPrev(lastController);
					controller.setServer(server);
					controller.setUriParser(uriParser);
					boolean isEnd = controller.input(ctx, request, response);
					if (isEnd) {
						break;
					}
					lastController = controller;
				}
			}
		}
	}

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx,
			Object evt) throws Exception {
		if (evt instanceof IdleStateEvent) {
			if (request == null) {
				logger.warn("connection[" + ctx + "] timeout.");
{'fraction_non_alphanumeric': 0.10281923714759536, 'fraction_numerical': 0.00041459369817578774, 'mean_word_length': 4.663145539906103, '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': '6644044', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1349, 'n_words': 291}