<commit_before>var http = require('http'),
    path = require('path'),
    os = require('os'),
    fs = require('fs'),
	url = require('url'),
    tty = require('tty'),
    colors = require('colors'),
    _ = require('underscore'),
    async = require('async'),
    extend = require('node.extend'),
    indexPath = path.resolve(path.join(__dirname, 'templates/index.mtpl'));

var contentType = {
    'html': 'text/html',
    'css': 'text/css',
    'js': 'application/javascript'
};

var defaultHeaders = {
    'Content-Type': 'text/plain',
    'Cache-Control': 'must-revalidate, no-cache'
};

var writeHead = function(req, resp, statusCode, headers) {
    var d = new Date(),
        seconds = d.getSeconds() < 10? '0' + d.getSeconds() : d.getSeconds(),
        datestring = d.getHours() + ':' + d.getMinutes() + ':' + seconds,
        line = datestring + ' [' + statusCode + ']: ' + req.url,
        colorized = line;
    if (tty.isatty(process.stdout.fd)) {
        colorized = (+statusCode) >= 500? line.red.bold :
                    (+statusCode) >= 400? line.red : line;
    }
    console.log(colorized);
    resp.writeHead(statusCode, headers);
};

var readIndex = (function() {
    var compiled, error;
    return function(req, resp, indexPath, callback) {
        if (typeof compiled === 'undefined') {
			fs.readFile(indexPath, 'utf8', function(err, file) {
				callback(error = err, compiled = _.template(file || ''));
			});
        } else {
			callback(error, compiled);
        }
    };
})();

var serveError = function(req, resp, err) {
    writeHead(req, resp, 500, defaultHeaders);
    resp.write(err + '\n');
    resp.end();
};

var serve404 = function(req, resp) {
    writeHead(req, resp, 404, defaultHeaders);
    resp.end('not found');
};

var serveFile = function(req, resp, filename) {
    fs.readFile(filename, function(err, data) {
        if (err) {
            serveError(req, resp, err);
            return;
        }
        writeHead(req, resp, 200, _.extend({}, defaultHeaders, {
            'Content-Type': contentType[path.extname(filename).slice(1)],
            'Content-Length': data.length
        }));
        resp.write(data);
        resp.end();
    });
};

var serveIndex = function(req, resp, filename, config, extra) {
    readIndex(req, resp, indexPath, function(err, compiled) {
        var content, csiPath = path.join(config.baseUrl, config.paths.csi),
			isTest = /test/i.test(req.url.split('?')[0]);

        if (err) {
            serveError(req, resp, err);
			return;
        }

		content = compiled({
			isTest: isTest,<commit_msg>fixed server bug where 404 was reported as 500<commit_after>var http = require('http'),
    path = require('path'),
    os = require('os'),
    fs = require('fs'),
	url = require('url'),
    tty = require('tty'),
    colors = require('colors'),
    _ = require('underscore'),
    async = require('async'),
    extend = require('node.extend'),
    indexPath = path.resolve(path.join(__dirname, 'templates/index.mtpl'));

var contentType = {
    'html': 'text/html',
    'css': 'text/css',
    'js': 'application/javascript'
};

var defaultHeaders = {
    'Content-Type': 'text/plain',
    'Cache-Control': 'must-revalidate, no-cache'
};

var writeHead = function(req, resp, statusCode, headers) {
    var d = new Date(),
        seconds = d.getSeconds() < 10? '0' + d.getSeconds() : d.getSeconds(),
        datestring = d.getHours() + ':' + d.getMinutes() + ':' + seconds,
        line = datestring + ' [' + statusCode + ']: ' + req.url,
        colorized = line;
    if (tty.isatty(process.stdout.fd)) {
        colorized = (+statusCode) >= 500? line.red.bold :
                    (+statusCode) >= 400? line.red : line;
    }
    console.log(colorized);
    resp.writeHead(statusCode, headers);
};

var readIndex = (function() {
    var compiled, error;
    return function(req, resp, indexPath, callback) {
        if (typeof compiled === 'undefined') {
			fs.readFile(indexPath, 'utf8', function(err, file) {
				callback(error = err, compiled = _.template(file || ''));
			});
        } else {
			callback(error, compiled);
        }
    };
})();

var serveError = function(req, resp, err) {
    writeHead(req, resp, 500, defaultHeaders);
    resp.write(err + '\n');
    resp.end();
};

var serve404 = function(req, resp) {
    writeHead(req, resp, 404, defaultHeaders);
    resp.end('not found');
};

var serveFile = function(req, resp, filename) {
    fs.readFile(filename, function(err, data) {
        if (err) {
            if (err.code === 'ENOENT') {
                serve404(req, resp);
            } else {
                serveError(req, resp, err);
            }
            return;
        }
        writeHead(req, resp, 200, _.extend({}, defaultHeaders, {
            'Content-Type': contentType[path.extname(filename).slice(1)],
            'Content-Length': data.length
        }));
        resp.write(data);
        resp.end();
    });
};

var serveIndex = function(req, resp, filename, config, extra) {
    readIndex(req, resp, indexPath, function(err, compiled) {
        var content, csiPath = path.join(config.baseUrl, config.paths.csi),
			isTest = /test/i.test(req.url.split('?')[0]);

        if (err) {
            serveError(req, resp, err);
			return;
        }

		content = compiled({
			isTest: isTest,
{'fraction_non_alphanumeric': 0.1741256111320045, 'fraction_numerical': 0.010718315156073712, 'mean_word_length': 2.6506520247083047, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6828794', 'n_tokens_mistral': 1773, 'n_tokens_neox': 1682, 'n_words': 371}