CartoDB-SQL-API/lib/models/cartodb-request.js

42 lines
996 B
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
/**
* this module provides cartodb-specific interpretation
* of request headers
*/
2019-12-24 01:19:08 +08:00
function CartodbRequest () {
}
module.exports = CartodbRequest;
/**
* 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) {
if (req.params.user) {
return req.params.user;
}
return userByHostName(req.headers.host);
};
var re_userFromHost = new RegExp(
global.settings.user_from_host || '^([^\\.]+)\\.' // would extract "strk" from "strk.cartodb.com"
);
2019-12-24 01:19:08 +08:00
function userByHostName (host) {
var mat = host.match(re_userFromHost);
if (!mat) {
console.error("ERROR: user pattern '" + re_userFromHost + "' does not match hostname '" + host + "'");
return;
}
if (mat.length !== 2) {
2015-05-13 18:23:27 +08:00
console.error(
"ERROR: pattern '" + re_userFromHost + "' gave unexpected matches against '" + host + "': " + mat
);
return;
}
return mat[1];
}