<commit_before>var http = require("http");
var fs = require("fs");
var util = require(CONFIG.root + "/core/util.js");
var httpProxy = require('http-proxy');
var spawn = require("child_process").spawn;
var pause = require("pause");

// imported functions
var send      = require(CONFIG.root + "/core/send.js").send;
var orient    = require(CONFIG.root + "/core/db/orient.js");
var model     = require(CONFIG.root + "/core/model/orient.js");

var runningApplications = {};

var Server = exports.Server = function () {};

var host = "127.0.0.1";

// get the right host adress, if no external proxy is available
if (!CONFIG.app && !CONFIG.proxy) {
    
    host = util.ip();
    
    if (!host) {
        
        throw new Error("Missing IP Address");
    }
}

Server.prototype.start = function() {

    // establish the database connection
    orient.connect(CONFIG.orient, function(err, db) {

        if (err) {
            throw new Error(JSON.stringify(err));
        }

        // start http proxy server
        var proxyServer = httpProxy.createServer(proxyHandler);
        listenToHttpProxyEvents(proxyServer);
        proxyServer.listen(CONFIG.port, host);
    });
};

function proxyHandler(req, res, proxy) {

    var link = {
        req: req,
        res: res,
        resume: util.pause(req)
    }

    if (!req.headers.host) {
        send.badrequest(link, "No host in request headers.");
        return;
    }
    
    // TODO is a domain with port a diffrent host?
    var host = req.headers.host.split(":")[0];

    // the application is running so we can forward to request
    if (runningApplications[host]) {
        return proxyAndResume(link, proxy, runningApplications[host].port);
    }

    // the application is being started
    if (runningApplications[host] === 0) {
        send.serviceunavailable({ req: req, res: res }, "This application is starting...\nTry again in a few seconds.");
        return;
    }

    // mark this application as starting (0)
    runningApplications[host] = 0;

    // find the application for this domain (without the routing table)
    model.getDomainApplication(host, false, function(err, application) {

        if (err) {
            send.internalservererror({ req: req, res: res }, err);
            return;
        }

        var forwardRequest = function(err, application) {

            if (err) {
                send.internalservererror({ req: req, res: res }, err);
                return;
            }

            // cache the app
            runningApplications[host] = application;

            // forward the requst
            proxyAndResume(link, proxy, runningApplications[host].port);
        };

        // if the application managed to publish its portnow try to start this application
        if (application.port) {
            forwardRequest(null, application);
        } else {
            startApplication(application.appId, host, forwardRequest);
        }
    });
}

function listenToHttpProxyEvents(server) {

    server.proxy.on("proxyError", function(error, req, res) {

        if (!req.headers.host) {
            send.badrequest(link, "No host in request headers.");
            return;
        }

        // TODO: is a domain with port a diffrent host?
        var host = req.headers.host.split(":")[0];
        var application = runningApplications[host];

        if (runningApplications[host] === 0) {
            send.badrequest(link, "App starting...");
            return;
        }

        runningApplications[host] = 0;

        // TODO check if the application is still using the port and remove it from the
        // database in order not to screw future admin statistics
    
        var logOperationUrl = "/@/core/getLog";
        var logFilePath = CONFIG.APPLICATION_ROOT + application.appId + "/log.txt";
        var restartMessage = "In the meanwhile we are hardly working to revive it.";
    <commit_msg>Fixed undefined link reference (copy - paste)<commit_after>var http = require("http");
var fs = require("fs");
var util = require(CONFIG.root + "/core/util.js");
var httpProxy = require('http-proxy');
var spawn = require("child_process").spawn;
var pause = require("pause");

// imported functions
var send      = require(CONFIG.root + "/core/send.js").send;
var orient    = require(CONFIG.root + "/core/db/orient.js");
var model     = require(CONFIG.root + "/core/model/orient.js");

var runningApplications = {};

var Server = exports.Server = function () {};

var host = "127.0.0.1";

// get the right host adress, if no external proxy is available
if (!CONFIG.app && !CONFIG.proxy) {
    
    host = util.ip();
    
    if (!host) {
        
        throw new Error("Missing IP Address");
    }
}

Server.prototype.start = function() {

    // establish the database connection
    orient.connect(CONFIG.orient, function(err, db) {

        if (err) {
            throw new Error(JSON.stringify(err));
        }

        // start http proxy server
        var proxyServer = httpProxy.createServer(proxyHandler);
        listenToHttpProxyEvents(proxyServer);
        proxyServer.listen(CONFIG.port, host);
    });
};

function proxyHandler(req, res, proxy) {

    var link = {
        req: req,
        res: res,
        resume: util.pause(req)
    }

    if (!req.headers.host) {
        send.badrequest(link, "No host in request headers.");
        return;
    }
    
    // TODO is a domain with port a diffrent host?
    var host = req.headers.host.split(":")[0];

    // the application is running so we can forward to request
    if (runningApplications[host]) {
        return proxyAndResume(link, proxy, runningApplications[host].port);
    }

    // the application is being started
    if (runningApplications[host] === 0) {
        send.serviceunavailable({ req: req, res: res }, "This application is starting...\nTry again in a few seconds.");
        return;
    }

    // mark this application as starting (0)
    runningApplications[host] = 0;

    // find the application for this domain (without the routing table)
    model.getDomainApplication(host, false, function(err, application) {

        if (err) {
            send.internalservererror({ req: req, res: res }, err);
            return;
        }

        var forwardRequest = function(err, application) {

            if (err) {
                send.internalservererror({ req: req, res: res }, err);
                return;
            }

            // cache the app
            runningApplications[host] = application;

            // forward the requst
            proxyAndResume(link, proxy, runningApplications[host].port);
        };

        // if the application managed to publish its portnow try to start this application
        if (application.port) {
            forwardRequest(null, application);
        } else {
            startApplication(application.appId, host, forwardRequest);
        }
    });
}

function listenToHttpProxyEvents(server) {

    server.proxy.on("proxyError", function(error, req, res) {

        if (!req.headers.host) {
            send.badrequest(link, "No host in request headers.");
            return;
        }

        // TODO: is a domain with port a diffrent host?
        var host = req.headers.host.split(":")[0];
        var application = runningApplications[host];

        if (runningApplications[host] === 0) {
            send.badrequest({ req: req, res: res }, "App starting...");
            return;
        }

        runningApplications[host] = 0;

        // TODO check if the application is still using the port and remove it from the
        // database in order not to screw future admin statistics
    
        var logOperationUrl = "/@/core/getLog";
        var logFilePath = CONFIG.APPLICATION_ROOT + application.appId + "/log.txt";
        var restartMessage = "In the meanwhile we are hardly working to revive it.";
    
{'fraction_non_alphanumeric': 0.11948249619482496, 'fraction_numerical': 0.003297818366311517, 'mean_word_length': 2.286786160900375, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5788754', 'n_tokens_mistral': 2183, 'n_tokens_neox': 2103, 'n_words': 649}