Windshaft-cartodb/lib/models/cdb-request.js

27 lines
772 B
JavaScript
Raw Normal View History

'use strict';
2019-10-22 01:07:24 +08:00
function CdbRequest () {
2015-03-30 22:28:37 +08:00
this.RE_USER_FROM_HOST = new RegExp(global.environment.user_from_host ||
'^([^\\.]+)\\.' // would extract "strk" from "strk.cartodb.com"
);
2015-03-24 00:35:09 +08:00
}
module.exports = CdbRequest;
2019-10-22 01:07:24 +08:00
CdbRequest.prototype.userByReq = function (req) {
var host = req.headers.host || '';
2015-03-24 00:35:09 +08:00
if (req.params.user) {
return req.params.user;
}
2015-03-30 22:28:37 +08:00
var mat = host.match(this.RE_USER_FROM_HOST);
2019-10-22 01:07:24 +08:00
if (!mat) {
global.logger.error("Pattern '%s' does not match hostname '%s'", this.RE_USER_FROM_HOST, host);
2015-03-24 00:35:09 +08:00
return;
}
2019-10-22 01:07:24 +08:00
if (mat.length !== 2) {
global.logger.error("Pattern '%s' gave unexpected matches against '%s': %s", this.RE_USER_FROM_HOST, host, mat);
2015-03-24 00:35:09 +08:00
return;
}
return mat[1];
};