Windshaft-cartodb/lib/cartodb/models/cdb_request.js

26 lines
761 B
JavaScript
Raw Normal View History

2015-03-24 00:35:09 +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;
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);
2015-03-24 00:35:09 +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;
}
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];
};