<commit_before>// Copyright (c) 2015, RadiantBlue Technologies, Inc.
// This file may only be used under the MIT-style
// license found in the accompanying LICENSE.txt file.

import 'dart:async';
import 'dart:io';
import 'dart:convert';

import 'package:args/args.dart';


class Server {
    String server;
    int port;
    String rootDir;

    static const FILE = '/file';
    static const PROXY = '/proxy';

    final headers = {
        // "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*, ",
        "Access-Control-Allow-Methods": "POST, GET, OPTIONS",
        "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"
    };

    Server(String this.server, int this.port, String this.rootDir);

    void run() {
        log("Listening on $server:$port for directory $rootDir");

        HttpServer.bind(server, port).then((server) {
            server.listen(_handler);
        });
    }

    void log(Object o) {
        DateTime now = new DateTime.now();
        if (o is HttpRequest) {
            print("REQU  $now  ${o.method}  ${o.uri}");
        } else if (o is HttpResponse) {
            print("RESP $now  ${o.statusCode}");
        } else {
            print("INFO  $now  $o");
        }
    }

    void fail(HttpRequest request) {
        request.response.statusCode = HttpStatus.NOT_FOUND;
        request.response.write("[${HttpStatus.NOT_FOUND}] Not found.");
        request.response.close();
    }

    void _handler(HttpRequest request) {
        log(request);

        headers.forEach((k, v) => request.response.headers.set(k, v));

        _handlerInner(request);

        log(request.response);
    }

    void _handlerInner(HttpRequest request) {

        if (request.method != 'GET') {
            fail(request);
            return;
        }

        String path = request.uri.path;

        if (path.startsWith(FILE)) {
            _fileHandler(request, path.substring(FILE.length));
            return;
        }

        if (path == PROXY) {
            String query = request.uri.queryParameters["xyzzy"];
            query = Uri.decodeComponent(query);
            Uri uri = Uri.parse(query);
            _proxyHandler(request, uri);
            return;
        }

        fail(request);
    }

    void _fileHandler(HttpRequest request, String path) {

        var filename = rootDir + path;

        log("request for $filename");

        File file = null;

        try {
            file = new File(filename);
        } catch (e) {
            //log("Unable to open file: $filename");
            //log("Exception: $e");
            fail(request);
            return;
        }

        if (!file.existsSync()) {
            log("Unable to read file: $filename");
            fail(request);
            return;
        }

        Stream<List<int>> s = null;
        var bytes = new List<int>();

        request.response.statusCode = HttpStatus.OK;
        request.response.addStream(file.openRead()).then((_) {
            log("added bytes for $filename");
            request.response.flush().then((_) {
                request.response.close();
            });
        });

        /*file.openRead().listen((data) {
            log("added ${data.length} bytes for $filename");
            bytes.addAll(data);
        }, onDone: () {
            request.response.statusCode = HttpStatus.OK;
            request.response.add(bytes);
            request.response.close();
            return;
        }, onError: ((s) {
            log("error reading $filename: $s");
            fail(request);
        }));*/

        return;
    }

    void _proxyHandler(HttpRequest request, Uri uri) {
        //log("proxy request for: $uri");

        getter(uri).then((s) {
            request.response.statusCode = HttpStatus.OK;
            request.response.write(s);
            request.response.close();
        });
    }

    Future<String> getter(uri) {
        var c = new Completer<String>();

        var cli = new HttpClient();

        cli.getUrl(uri).then((HttpClientRequest request) => request.close()).then((HttpClientResponse response) {
            response.transform(UTF8.decoder).listen((contents) {
                c.complete(contents);
            });
        });

        return c.future;
    }
}<commit_msg>clean up<commit_after>// Copyright (c) 2015, RadiantBlue Technologies, Inc.
// This file may only be used under the MIT-style
// license found in the accompanying LICENSE.txt file.

import 'dart:async';
import 'dart:io';
import 'dart:convert';

import 'package:args/args.dart';


class Server {
    String server;
    int port;
    String rootDir;

    static const FILE = '/file';
    static const PROXY = '/proxy';

    final headers = {
        // "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*, ",
        "Access-Control-Allow-Methods": "POST, GET, OPTIONS",
        "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"
    };

    Server(String this.server, int this.port, String this.rootDir);

    void run() {
        log("Listening on $server:$port for directory $rootDir");

        HttpServer.bind(server, port).then((server) {
            server.listen(_handler);
        });
    }

    void log(Object o) {
        DateTime now = new DateTime.now();
        if (o is HttpRequest) {
            print("REQU  $now  ${o.method}  ${o.uri}");
        } else if (o is HttpResponse) {
            print("RESP $now  ${o.statusCode}");
        } else {
            print("INFO  $now  $o");
        }
    }

    void fail(HttpRequest request) {
        request.response.statusCode = HttpStatus.NOT_FOUND;
        request.response.write("[${HttpStatus.NOT_FOUND}] Not found.");
        request.response.close();
    }

    void _handler(HttpRequest request) {
        log(request);

        headers.forEach((k, v) => request.response.headers.set(k, v));

        _handlerInner(request);

        log(request.response);
    }

    void _handlerInner(HttpRequest request) {

        if (request.method != 'GET') {
            fail(request);
            return;
        }

        String path = request.uri.path;

        if (path.startsWith(FILE)) {
            _fileHandler(request, path.substring(FILE.length));
            return;
        }

        if (path == PROXY) {
            String query = request.uri.queryParameters["xyzzy"];
            query = Uri.decodeComponent(query);
            Uri uri = Uri.parse(query);
            _proxyHandler(request, uri);
            return;
        }

        fail(request);
    }

    void _fileHandler(HttpRequest request, String path) {

        var filename = rootDir + path;

        //log("request for $filename");

        File file = null;

        try {
            file = new File(filename);
        } catch (e) {
            //log("Unable to open file: $filename");
            //log("Exception: $e");
            fail(request);
            return;
        }

        if (!file.existsSync()) {
            //log("Unable to read file: $filename");
            fail(request);
            return;
        }

        request.response.statusCode = HttpStatus.OK;
        request.response.addStream(file.openRead()).then((_) {
            //log("added bytes for $filename");
            request.response.flush().then((_) {
                request.response.close();
            });
        });

        return;
    }

    void _proxyHandler(HttpRequest request, Uri uri) {
        //log("proxy request for: $uri");

        getter(uri).then((s) {
            request.response.statusCode = HttpStatus.OK;
            request.response.write(s);
            request.response.close();
        });
    }

    Future<String> getter(uri) {
        var c = new Completer<String>();

        var cli = new HttpClient();

        cli.getUrl(uri).then((HttpClientRequest request) => request.close()).then((HttpClientResponse response) {
            response.transform(UTF8.decoder).listen((contents) {
                c.complete(contents);
            });
        });

        return c.future;
    }
}
{'fraction_non_alphanumeric': 0.1386285150468673, 'fraction_numerical': 0.00123334977799704, 'mean_word_length': 1.8837126600284495, 'pattern_counts': {'":': 8, '<': 10, '<?xml version=': 0, '>': 14, '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': '2463615', 'n_tokens_mistral': 2328, 'n_tokens_neox': 2237, 'n_words': 500}