<commit_before>part of polymorphic.api;

class HttpRouter {
  final List<HttpRoute> routes = [];
  final HttpServer server;
  
  HttpRouter(this.server) {
    server.listen((request) {
      handleRequest(request);
    });
  }
  
  void addRoute(String pattern, HttpRequestHandler handler, {String method}) {
    var route = new HttpRoute(pattern, handler, method);
    routes.add(route);
  }
  
  HttpRequestHandler _defaultHandler = (HttpRequest request) {
    var response = request.response;
    response.statusCode = 404;
    response.writeln("ERROR: Not Found.");
    response.close();
  };
  
  void defaultRoute(HttpRequestHandler handler) {
    _defaultHandler = handler;
  }
  
  void handleRequest(HttpRequest request) {
    var path = request.uri.path;
    
    for (var route in routes) {
      var glob = new Glob(route.pattern);
      if (glob.hasMatch(path) && (route.method != null ? route.method == request.method : true)) {
        route.handler(request);
        return;
      }
    }
    
    _defaultHandler(request);
  }
}

class HttpRoute {
  final String pattern;
  final HttpRequestHandler handler;
  final String method;
  
  HttpRoute(this.pattern, this.handler, this.method);
}

typedef void HttpRequestHandler(HttpRequest request);<commit_msg>Better HTTP System<commit_after>part of polymorphic.api;

class HttpRouter {
  final List<HttpRoute> routes = [];
  final HttpServer server;
  
  HttpRouter(this.server) {
    server.listen((request) {
      handleRequest(request);
    });
  }
  
  void addRoute(dynamic pattern, HttpRequestHandler handler, {String method, bool matchSubPaths: false}) {
    if (pattern is! String && pattern is! RegExp) {
      throw new Exception("pattern must be a string or regexp");
    }
    
    var route = new HttpRoute(pattern, handler, method, matchSubPaths);
    routes.add(route);
  }
  
  HttpRequestHandler _defaultHandler = (HttpRequest request) {
    var response = request.response;
    response.statusCode = 404;
    response.writeln("ERROR: Not Found.");
    response.close();
  };
  
  void defaultRoute(HttpRequestHandler handler) {
    _defaultHandler = handler;
  }
  
  void handleRequest(HttpRequest request) {
    var path = request.uri.path;
    
    for (var route in routes) {
      var pattern = route.pattern;
      
      var matches = false;
      if (pattern is String) {
        matches = pattern == path;        
      } else if (pattern is RegExp) {
        matches = pattern.hasMatch(path);
      } else {
        throw new Exception("Invalid Pattern Type");
      }
      
      if (route.pattern.allMatches(path).isNotEmpty && (route.method != null ? route.method == request.method : true)) {
        route.handler(request);
        return;
      }
    }
    
    _defaultHandler(request);
  }
}

class HttpRoute {
  final dynamic pattern;
  final HttpRequestHandler handler;
  final String method;
  final bool matchSubPaths;
  
  HttpRoute(this.pattern, this.handler, this.method, this.matchSubPaths);
}

typedef void HttpRequestHandler(HttpRequest request);
{'fraction_non_alphanumeric': 0.10565914295060516, 'fraction_numerical': 0.001962708537782139, 'mean_word_length': 3.0396301188903565, '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': '196925', 'n_tokens_mistral': 895, 'n_tokens_neox': 879, 'n_words': 225}