2015-06-29 08:31:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2015-06-29 08:41:18 +00:00
|
|
|
* @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
|
2015-06-29 08:31:41 +00:00
|
|
|
*/
|
|
|
|
plugin.getBaseUrl = function(req, sourceBaseUrlPattern, targetBaseUrlPattern) {
|
2015-06-29 10:22:32 +00:00
|
|
|
var sourceHost = req.headers && req.headers.host || req.host || req;
|
2015-06-29 08:31:41 +00:00
|
|
|
var sourceHostMatcher = sourceBaseUrlPattern
|
|
|
|
.replace(/^https?:\/\//, "")
|
|
|
|
.replace(/\/.*/, "")
|
|
|
|
.replace("$DOMAIN", "([^/]+)");
|
|
|
|
var hostMatch = sourceHost.match(sourceHostMatcher);
|
|
|
|
if (!hostMatch) {
|
|
|
|
errorLogger.log(new Error("Could not construct URL: request host "
|
|
|
|
+ sourceHost + " should match " + sourceBaseUrlPattern));
|
2015-06-29 10:22:32 +00:00
|
|
|
return replaceDomain(targetBaseUrlPattern || sourceBaseUrlPattern, "c9.io")
|
|
|
|
.replace(/\/$/, "");
|
2015-06-29 08:31:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 10:22:32 +00:00
|
|
|
return replaceDomain(targetBaseUrlPattern || sourceBaseUrlPattern, hostMatch[1])
|
|
|
|
.replace(/\/$/, "");;
|
2015-06-29 08:31:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|