2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2013-12-18 18:57:46 +08:00
|
|
|
/**
|
|
|
|
* this module provides cartodb-specific interpretation
|
|
|
|
* of request headers
|
|
|
|
*/
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function CartodbRequest () {
|
2013-12-18 18:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = CartodbRequest;
|
|
|
|
|
2015-02-02 19:15:53 +08:00
|
|
|
/**
|
|
|
|
* If the request contains the user use it, if not guess from the host
|
|
|
|
*/
|
2019-12-24 01:19:08 +08:00
|
|
|
CartodbRequest.prototype.userByReq = function (req) {
|
2015-02-02 19:09:34 +08:00
|
|
|
if (req.params.user) {
|
|
|
|
return req.params.user;
|
|
|
|
}
|
|
|
|
return userByHostName(req.headers.host);
|
2013-12-18 18:57:46 +08:00
|
|
|
};
|
|
|
|
|
2019-12-27 01:12:47 +08:00
|
|
|
var userFromHostRegex = new RegExp(
|
2014-08-05 08:29:07 +08:00
|
|
|
global.settings.user_from_host || '^([^\\.]+)\\.' // would extract "strk" from "strk.cartodb.com"
|
|
|
|
);
|
2015-02-02 19:09:34 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function userByHostName (host) {
|
2019-12-27 01:12:47 +08:00
|
|
|
var mat = host.match(userFromHostRegex);
|
2015-02-02 19:09:34 +08:00
|
|
|
if (!mat) {
|
2019-12-27 01:12:47 +08:00
|
|
|
console.error("ERROR: user pattern '" + userFromHostRegex + "' does not match hostname '" + host + "'");
|
2015-02-02 19:09:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mat.length !== 2) {
|
2015-05-13 18:23:27 +08:00
|
|
|
console.error(
|
2019-12-27 01:12:47 +08:00
|
|
|
"ERROR: pattern '" + userFromHostRegex + "' gave unexpected matches against '" + host + "': " + mat
|
2015-05-13 18:23:27 +08:00
|
|
|
);
|
2015-02-02 19:09:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return mat[1];
|
|
|
|
}
|