<commit_before>/**
 * Author:  <NAME> http://github.com/0xor1
 */

library purity.host;

import 'dart:io';
import 'core.dart' as core;
import 'package:http_server/http_server.dart' as http_server;
import 'package:route/server.dart' show Router;
import 'package:logging/logging.dart' show Logger, Level, LogRecord;

final Logger _log = new Logger('Purity Host');

class Host extends core.Host{

  Host(dynamic address, int port, String staticFileDirectory, core.InitSource initSrc, core.CloseSource closeSrc, int garbageCollectionFrequency, [bool verbose = false]):
    super(initSrc, closeSrc, garbageCollectionFrequency, verbose){

    Logger.root.level = Level.ALL;
    Logger.root.onRecord.listen((LogRecord rec) {
      print('${rec.level.name}: ${rec.time}: ${rec.message}');
    });

    if(!new Directory(staticFileDirectory).existsSync()) {
      _log.severe('The specified static file directory was not found: $staticFileDirectory');
      return;
    }

    HttpServer.bind(address, port).then((server) {

        _log.info("Server is running on "
                 "'http://${server.address.address}:$port/'");

        var router = new Router(server);

        router.serve(core.PURITY_WEB_SOCKET_ROUTE_PATH).listen((HttpRequest request){
          if(WebSocketTransformer.isUpgradeRequest(request)){
            WebSocketTransformer.upgrade(request)
            .then((ws){
              var biConnection = new core.EndPointConnection(ws, ws.add, ws.close);
              createSourceEndPoint(request.connectionInfo.remoteAddress.toString(), biConnection);
            });
          }else{
            _log.warning("Purity app web socket request not valid");
            request.response.statusCode = HttpStatus.BAD_REQUEST;
            request.response.close();
          }
        });

        // Default handler serves files.
        var virDir = new http_server.VirtualDirectory(staticFileDirectory);
        virDir.jailRoot = true;
        virDir.allowDirectoryListing = true;
        virDir.directoryHandler = (dir, request) {
          // Redirect directory requests to index.html files.
          var indexUri = new Uri.file(dir.path).resolve('index.html');
          virDir.serveFile(new File(indexUri.toFilePath()), request);
        };

        // Add an error page handler.
        virDir.errorPageHandler = (HttpRequest request) {
          _log.warning("Resource not found: ${request.uri.path}");
          request.response.statusCode = HttpStatus.NOT_FOUND;
          request.response.close();
        };

        // Serve everything not routed elsewhere through the virtual directory.
        virDir.serve(router.defaultStream);
      });

  }
}
<commit_msg>fix indentation<commit_after>/**
 * Author:  <NAME> http://github.com/0xor1
 */

library purity.host;

import 'dart:io';
import 'core.dart' as core;
import 'package:http_server/http_server.dart' as http_server;
import 'package:route/server.dart' show Router;
import 'package:logging/logging.dart' show Logger, Level, LogRecord;

final Logger _log = new Logger('Purity Host');

class Host extends core.Host{

  Host(dynamic address, int port, String staticFileDirectory, core.InitSource initSrc, core.CloseSource closeSrc, int garbageCollectionFrequency, [bool verbose = false]):
    super(initSrc, closeSrc, garbageCollectionFrequency, verbose){

    Logger.root.level = Level.ALL;
    Logger.root.onRecord.listen((LogRecord rec) {
      print('${rec.level.name}: ${rec.time}: ${rec.message}');
    });

    if(!new Directory(staticFileDirectory).existsSync()) {
      _log.severe('The specified static file directory was not found: $staticFileDirectory');
      return;
    }

    HttpServer.bind(address, port).then((server) {

      _log.info("Server is running on "
               "'http://${server.address.address}:$port/'");

      var router = new Router(server);

      router.serve(core.PURITY_WEB_SOCKET_ROUTE_PATH).listen((HttpRequest request){
        if(WebSocketTransformer.isUpgradeRequest(request)){
          WebSocketTransformer.upgrade(request)
          .then((ws){
            var biConnection = new core.EndPointConnection(ws, ws.add, ws.close);
            createSourceEndPoint(request.connectionInfo.remoteAddress.toString(), biConnection);
          });
        }else{
          _log.warning("Purity app web socket request not valid");
          request.response.statusCode = HttpStatus.BAD_REQUEST;
          request.response.close();
        }
      });

      // Default handler serves files.
      var virDir = new http_server.VirtualDirectory(staticFileDirectory);
      virDir.jailRoot = true;
      virDir.allowDirectoryListing = true;
      virDir.directoryHandler = (dir, request) {
        // Redirect directory requests to index.html files.
        var indexUri = new Uri.file(dir.path).resolve('index.html');
        virDir.serveFile(new File(indexUri.toFilePath()), request);
      };

      // Add an error page handler.
      virDir.errorPageHandler = (HttpRequest request) {
        _log.warning("Resource not found: ${request.uri.path}");
        request.response.statusCode = HttpStatus.NOT_FOUND;
        request.response.close();
      };

      // Serve everything not routed elsewhere through the virtual directory.
      virDir.serve(router.defaultStream);
    });

  }
}

{'fraction_non_alphanumeric': 0.12137203166226913, 'fraction_numerical': 0.0007538635506973238, 'mean_word_length': 3.0573394495412844, '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': '2677564', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1456, 'n_words': 325}