Skip to content

Commit

Permalink
Merge branch 'master' into release/v4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinehage committed Oct 15, 2023
2 parents 850328d + 8eebe19 commit 6f297e4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
65 changes: 39 additions & 26 deletions mw/gotoService/redirectToService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ const get = (p, o) => p.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, o);
const request = require('request');

module.exports = (configuration) => {

let core = configuration.core;
let isRequestAuthorized = require("./lib/isRequestAuthorized.js");
let preRedirect = require("./lib/preRedirect.js");

/**
*
* @param req
Expand All @@ -36,7 +36,7 @@ module.exports = (configuration) => {
if (obj.config.authorization) {
isRequestAuthorized(req, core, requestOptions);
}

let restServiceParams = req.soajs.controller.serviceParams;
try {
let log_monitor = (doc) => {
Expand All @@ -46,7 +46,7 @@ module.exports = (configuration) => {
port = "4050";
}
let api = "/monitor/item";

req.soajs.awareness.getHost(soamonitor, function (host) {
if (!host) {
req.soajs.log.error('Unable to find any healthy host for service ' + soamonitor);
Expand All @@ -64,7 +64,7 @@ module.exports = (configuration) => {
"json": true
};
if (req.headers && req.headers.soajsinjectobj) {
requestOptions.headers = {"soajsinjectobj": req.headers.soajsinjectobj};
requestOptions.headers = { "soajsinjectobj": req.headers.soajsinjectobj };
}
request.post(requestOptions, (error, response, body) => {
if (error) {
Expand Down Expand Up @@ -115,10 +115,10 @@ module.exports = (configuration) => {
if (monitor && !monitor_service_blacklist && monitor.req_query) {
monitoObj.query = req.query;
}

// Trigger request
req.soajs.controller.redirectedRequest = request(requestOptions);

// Handle error event for both with monitor and without monitor
req.soajs.controller.redirectedRequest.on('error', (err) => {
req.soajs.log.error(err.message + ' with [' + restServiceParams.name + (restServiceParams.version ? ('@' + restServiceParams.version) : '') + ']');
Expand All @@ -137,35 +137,44 @@ module.exports = (configuration) => {
//Handle body
if (req.method === 'POST' || req.method === 'PUT' || req.method === 'PATCH' || req.method === 'DELETE') {
if (monitor && !monitor_service_blacklist && monitor.req_body) {
let allowedContentType = false;
let isStream = false;
req.on("data", (chunk) => {
req.soajs.controller.redirectedRequest.write(chunk);
if (!isStream) {
let resContentType = res.getHeader('content-type');
if (resContentType) {
let resContentType = req.headers['content-type'];
if (resContentType) {
if (!allowedContentType) {
allowedContentType = resContentType.match(/application\/json|text\/plain/i);
}
if (!isStream) {
isStream = resContentType.match(/stream/i);
}
}
if (!isStream) {
if (!isStream && allowedContentType) {
if (!monitoObj.body) {
monitoObj.time.req_body_start = new Date().getTime();
monitoObj.body = chunk;
} else {
monitoObj.body += chunk;
}
} else {
monitoObj.body = "stream";
monitoObj.body = "{\"contentType\": \"is stream or not allowed\", \"value': \"" + resContentType + "\"}";
}
});
req.on("end", () => {
if (!isStream) {
let resContentType = res.getHeader('content-type');
if (resContentType) {
let resContentType = req.headers['content-length'];
if (resContentType) {
if (!allowedContentType) {
allowedContentType = resContentType.match(/application\/json|text\/plain/i);
}
if (!isStream) {
isStream = resContentType.match(/stream/i);
}
}
if (!isStream) {
req.soajs.controller.redirectedRequest.end();
}
if (!isStream && allowedContentType) {
monitoObj.time.req_body_end = new Date().getTime();
}
});
Expand All @@ -175,6 +184,7 @@ module.exports = (configuration) => {
}
// Handle response
if (monitor && !monitor_service_blacklist && monitor.req_response) {
let allowedContentType = false;
let isStream = false;
req.soajs.controller.redirectedRequest.on("response", (response) => {
if (!res.headersSent) {
Expand All @@ -184,34 +194,37 @@ module.exports = (configuration) => {
});
req.soajs.controller.redirectedRequest.on("data", (chunk) => {
res.write(chunk);
if (!isStream) {
let resContentType = res.getHeader('content-type');
if (resContentType) {
let resContentType = res.getHeader('content-type');
if (resContentType) {
if (!allowedContentType) {
allowedContentType = resContentType.match(/application\/json|text\/plain/i);
}
if (!isStream) {
isStream = resContentType.match(/stream/i);
}
}
if (!isStream) {
if (!isStream && allowedContentType) {
if (!monitoObj.response) {
monitoObj.response = chunk;
} else {
monitoObj.response += chunk;
}
} else {
monitoObj.response = "stream";
monitoObj.response = "{\"contentType\": \"is stream or not allowed\", \"value': \"" + resContentType + "\"}";
}
});
req.soajs.controller.redirectedRequest.on("end", () => {
if (!isStream) {
let resContentType = res.getHeader('content-type');
if (resContentType) {
let resContentType = res.getHeader('content-type');
if (resContentType) {
if (!isStream) {
isStream = resContentType.match(/stream/i);
}
}
if (!isStream) {
res.end();
monitoObj.time.res_end = new Date().getTime();
log_monitor(monitoObj);
}
monitoObj.time.res_end = new Date().getTime();
log_monitor(monitoObj);
});
req.soajs.controller.redirectedRequest.on("abort", () => {
if (!req.soajs.controller.monitorEndingReq) {
Expand All @@ -223,7 +236,7 @@ module.exports = (configuration) => {
} else {
req.soajs.controller.redirectedRequest.pipe(res);
}

} catch (e) {
req.soajs.log.error(e.message + ' @catch with [' + restServiceParams.name + (restServiceParams.version ? ('@' + restServiceParams.version) : '') + ']');
if (req.soajs.controller.redirectedRequest) {
Expand Down
3 changes: 3 additions & 0 deletions mw/mt/urac.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ function Urac(param) {
if (!_self.soajs.tenant.roaming && (param.oauth.bearerToken.clientId === _self.soajs.tenant.id)) {
_self.userRecord = param.oauth.bearerToken.user;
}
if (_self.soajs.tenant.roaming) {
_self.userRecord = param.oauth.bearerToken.user;
}
} else {
if (_self.soajs.tenant.roaming) {
_self.userRecord = param.oauth.bearerToken.user;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "soajs.controller",
"description": "soajs multi tenant API gateway",
"version": "4.2.5",
"version": "4.2.6",
"author": {
"name": "soajs team",
"email": "team@soajs.org"
Expand Down

0 comments on commit 6f297e4

Please sign in to comment.