c9-core/node_modules/c9/urls.js

137 wiersze
4.3 KiB
JavaScript
Czysty Zwykły widok Historia

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 assert = require("assert");
2015-10-19 22:30:12 +00:00
function getHost(req) {
2015-10-23 09:14:35 +00:00
var host =
req.headers && req.headers.host ||
req.host ||
req.url ||
req;
return host.replace(/^https?:\/\/([^/]*).*/, "$1");
}
2015-10-19 22:30:12 +00:00
2015-10-23 09:14:35 +00:00
function splitDomain(req, domains) {
var host = getHost(req);
var allDomains = domains.join("|");
var domainRe = new RegExp("^(.*?)\\.?(" + allDomains.replace(".", "\\.") + ")(?::(\\d+))?$");
var m = host.match(domainRe);
if (m) {
return {
subDomains: m[1],
domainName: m[2],
port: m[3] || ""
};
} else {
return {
subDomains: "",
domainName: host.split(":")[0],
port: host.split(":")[1] || ""
};
}
2015-06-29 08:31:41 +00:00
}
/**
* Get a desired base URL, given some context.
*
* Example for a request coming into the IDE service:
*
* ```
* getBaseUrl(req, "https://ide.$DOMAIN", "https://preview.$DOMAIN");
* ```
*
* The above example will determine the domain name from the request,
* by stripping off the "https://ide." part. So, for a domain like
* "https://ide.c9.io" we'll get "c9.io". For a domain like
* "https://ide.dogfooding-lennartcl.c9.io" we'll get dogfooding-lennartcl.c9.io.
* If there is no match, a warning is shown. The target pattern
* is used to construct the resulting URL, e.g. https://preview.c9.io.
*
* @param req
* The current request object or URL
* @param {String} [sourceUrlPattern]
* The URL pattern of the current service, e.g. https://ide.$DOMAIN if
* we are getting an incoming request for the IDE service
* @param {String} targetBaseUrlPattern
* The URL pattern of the target service. E.g., if we want to
* construct the base URL of the API service, this might be https://api.$DOMAIN.
2015-06-29 08:31:41 +00:00
*/
2015-10-19 22:30:12 +00:00
function getBaseUrl(req, sourceBaseUrlPattern, targetBaseUrlPattern) {
var sourceHost = getHost(req);
2015-07-01 08:44:36 +00:00
if (typeof sourceHost !== "string")
throw new Error("Not a valid request object: " + req);
2015-10-19 22:30:12 +00:00
2015-07-02 12:07:50 +00:00
if (!sourceBaseUrlPattern)
throw new Error("getBaseUrl() requires at least two arguments");
2015-11-23 16:19:46 +00:00
if (!targetBaseUrlPattern)
targetBaseUrlPattern = sourceBaseUrlPattern;
2015-06-29 08:31:41 +00:00
var sourceHostMatcher = sourceBaseUrlPattern
.replace(/^https?:\/\//, "")
.replace(/\/.*/, "")
2015-06-30 10:38:15 +00:00
.replace(/\./, "\\.")
2015-06-29 08:31:41 +00:00
.replace("$DOMAIN", "([^/]+)");
var hostMatch = sourceHost.match(sourceHostMatcher);
var targetHost;
2015-10-19 22:30:12 +00:00
if (hostMatch) {
targetHost = hostMatch[1];
}
else {
2015-10-19 22:30:12 +00:00
console.error(new Error("Could not construct URL: request host " + sourceHost + " should match " + sourceBaseUrlPattern));
targetHost = "c9.io";
2015-06-29 08:31:41 +00:00
}
2015-10-19 22:30:12 +00:00
2015-10-29 20:50:02 +00:00
if (/^(ide|vfs)./.test(targetHost))
2015-10-19 22:30:12 +00:00
console.trace("Warning: possibly incorrect baseUrl constructed, with 'ide.' in the hostname: " + targetHost);
2015-11-23 16:19:46 +00:00
return replaceDomain(targetBaseUrlPattern, targetHost)
.replace(/\/$/, "");
2015-10-19 22:30:12 +00:00
}
2015-06-29 08:31:41 +00:00
2015-10-19 22:30:12 +00:00
function replaceDomains(settings, domains) {
2015-06-29 08:31:41 +00:00
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].internalBaseUrl)
settings[s].internalBaseUrl = replaceDomain(settings[s].internalBaseUrl, primaryDomain);
2015-06-29 08:31:41 +00:00
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);
});
}
}
2015-10-19 22:30:12 +00:00
}
2015-06-29 08:31:41 +00:00
function replaceDomain(url, domain) {
return url.replace("$DOMAIN", domain);
}
2015-10-19 22:30:12 +00:00
module.exports = {
replaceDomains: replaceDomains,
getBaseUrl: getBaseUrl,
2015-10-23 09:14:35 +00:00
splitDomain: splitDomain,
getHost: getHost
2015-10-19 22:30:12 +00:00
};