<commit_before>/**
 * Base URL utilities for the Ajax.org Cloud IDE
 * 
 * Access via require("c9/urls").
 *
 * @copyright 2015, Ajax.org B.V.
 */

var plugin = module.exports = main;
var assert = require("assert");
 
plugin.consumes = ["error.logger"];
plugin.provides = ["urls"];

var errorLogger = mockErrorLogger;

function main(options, imports, register) {
    errorLogger = imports["error.logger"];
    
    register(null, {
        urls: plugin
    });
}

/**
 * Get a desired base URL, given some context.
 * 
 * @param req                             The current request object or URL
 * @param {String} [sourceUrlPattern]     The source URL pattern, e.g. https://ide.$DOMAIN
 * @param {String} targetBaseUrlPattern   The target URL pattern, e.g. https://$DOMAIN
 */
plugin.getBaseUrl = function(req, sourceBaseUrlPattern, targetBaseUrlPattern) {
    var sourceHost = req.headers && req.headers.host
        || req.host
        || req.url && req.url.replace(/^https?:\/\/([^/]*).*/, "$1")
        || req;
    assert(!sourceHost.match(/\$DOMAIN/), "Please provide a valid request object");

    var sourceHostMatcher = sourceBaseUrlPattern
        .replace(/^https?:\/\//, "")
        .replace(/\/.*/, "")
        .replace(/\./, "\\.")
        .replace("$DOMAIN", "([^/]+)");
    var hostMatch = sourceHost.match(sourceHostMatcher);
    var targetHost;
    if (hostMatch) {
        targetHost = hostMatch[1];
    }
    else {
        errorLogger.log(new Error("Could not construct URL: request host "
            + sourceHost + " should match " + sourceBaseUrlPattern));
        targetHost = "c9.io";
    }
    
    if (targetHost.match(/^(ide|vfs)./) && !targetBaseUrlPattern)
        errorLogger.log(new Error("Warning: no targetBaseUrlPattern specified, will stay at " + targetHost), { sourceBaseUrlPattern: sourceBaseUrlPattern });
    return replaceDomain(targetBaseUrlPattern || sourceBaseUrlPattern, targetHost)
        .replace(/\/$/, "");
};

plugin.replaceDomains = function(settings, domains) {
    domains = Array.isArray(domains) ? domains : domains.split(",");
    var primaryDomain = domains[0];
    settings.domains = domains;
    settings.primaryDomain = replaceDomain(settings.primaryDomain || "$DOMAIN", primaryDomain);
    settings.primaryBaseUrl = replaceDomain(settings.primaryBaseUrl || "https://$DOMAIN", primaryDomain);
    for (var s in settings) {
        if (!settings[s])
            continue;
        if (settings[s].baseUrl)
            settings[s].baseUrl = replaceDomain(settings[s].baseUrl, primaryDomain);
        if (settings[s].primaryBaseUrl)
            settings[s].primaryBaseUrl = replaceDomain(settings[s].primaryBaseUrl, primaryDomain);
        if (settings[s].baseUrls) {
            assert(settings[s].baseUrls.length === 1);
            settings[s].baseUrls = domains.map(function(d) {
                return replaceDomain(settings[s].baseUrls[0], d);
            });
        }
    }
};

function replaceDomain(url, domain) {
    return url.replace("$DOMAIN", domain);
}

function mockErrorLogger() {}
mockErrorLogger.log = function(err) {
    console.error(err.stack);
};
plugin.mockErrorLogger = mockErrorLogger;
<commit_msg>Work on multi-domain support in IDE and apps-proxy<commit_after>/**
 * Base URL utilities for the Ajax.org Cloud IDE
 * 
 * Access via require("c9/urls").
 *
 * @copyright 2015, Ajax.org B.V.
 */

var plugin = module.exports = main;
var assert = require("assert");
 
plugin.consumes = ["error.logger"];
plugin.provides = ["urls"];

var errorLogger = mockErrorLogger;

function main(options, imports, register) {
    errorLogger = imports["error.logger"];
    
    register(null, {
        urls: plugin
    });
}

/**
 * Get a desired base URL, given some context.
 * 
 * @param req                             The current request object or URL
 * @param {String} [sourceUrlPattern]     The source URL pattern, e.g. https://ide.$DOMAIN
 * @param {String} targetBaseUrlPattern   The target URL pattern, e.g. https://$DOMAIN
 */
plugin.getBaseUrl = function(req, sourceBaseUrlPattern, targetBaseUrlPattern) {
    var sourceHost = req.headers && req.headers.host
        || req.host
        || req.url && req.url.replace(/^https?:\/\/([^/]*).*/, "$1")
        || req;
    assert(!sourceHost.match(/\$DOMAIN/), "Please provide a valid request object");

    var sourceHostMatcher = sourceBaseUrlPattern
        .replace(/^https?:\/\//, "")
        .replace(/\/.*/, "")
        .replace(/\./, "\\.")
        .replace("$DOMAIN", "([^/]+)");
    var hostMatch = sourceHost.match(sourceHostMatcher);
    var targetHost;
    if (hostMatch) {
        targetHost = hostMatch[1];
    }
    else {
        errorLogger.log(new Error("Could not construct URL: request host "
            + sourceHost + " should match " + sourceBaseUrlPattern));
        targetHost = "c9.io";
    }
    
    if (targetHost.match(/^(ide|vfs)./) && !targetBaseUrlPattern)
        errorLogger.log(new Error("Warning: no targetBaseUrlPattern specified, will stay at " + targetHost), { sourceBaseUrlPattern: sourceBaseUrlPattern });
    if (targetHost.match(/^(ide|vfs)./))
        console.trace("Warning: possibly incorrect baseUrl constructed, with 'ide.' in the hostname: " + targetHost); 
    return replaceDomain(targetBaseUrlPattern || sourceBaseUrlPattern, targetHost)
        .replace(/\/$/, "");
};

plugin.replaceDomains = function(settings, domains) {
    domains = Array.isArray(domains) ? domains : domains.split(",");
    var primaryDomain = domains[0];
    settings.domains = domains;
    settings.primaryDomain = replaceDomain(settings.primaryDomain || "$DOMAIN", primaryDomain);
    settings.primaryBaseUrl = replaceDomain(settings.primaryBaseUrl || "https://$DOMAIN", primaryDomain);
    for (var s in settings) {
        if (!settings[s])
            continue;
        if (settings[s].baseUrl)
            settings[s].baseUrl = replaceDomain(settings[s].baseUrl, primaryDomain);
        if (settings[s].primaryBaseUrl)
            settings[s].primaryBaseUrl = replaceDomain(settings[s].primaryBaseUrl, primaryDomain);
        if (settings[s].baseUrls) {
            assert(settings[s].baseUrls.length === 1);
            settings[s].baseUrls = domains.map(function(d) {
                return replaceDomain(settings[s].baseUrls[0], d);
            });
        }
    }
};

function replaceDomain(url, domain) {
    return url.replace("$DOMAIN", domain);
}

function mockErrorLogger() {}
mockErrorLogger.log = function(err) {
    console.error(err.stack);
};
plugin.mockErrorLogger = mockErrorLogger;

{'fraction_non_alphanumeric': 0.15073921658283798, 'fraction_numerical': 0.003353147386069197, 'mean_word_length': 2.9866342648845685, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1037778', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1892, 'n_words': 424}