Skip to content

Commit

Permalink
chore(capture): add captureIncomingMessageBody
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyoukun committed Jun 13, 2018
1 parent 72be759 commit 07c1803
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
4 changes: 3 additions & 1 deletion bin/proxy/http.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ function doRoute(req, res) {
}

// 抓回包
httpUtil.captureBody(this);
httpUtil.captureServerResponseBody(this);
}

logger.debug('response ${statusCode}', {
Expand All @@ -488,6 +488,8 @@ function doRoute(req, res) {
logger.getLog().showLineNumber = true;
logger.debug('showLineNumber on');
}

httpUtil.captureIncomingMessageBody(req);
}

logger.debug('node-${version}, name: ${name}, appid: ${appid}', {
Expand Down
4 changes: 2 additions & 2 deletions bin/tsw/util/auto-report/logReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ module.exports = function(req, res) {

logger.debug('\n${headers}${body}\r\nresponse ${statusCode} ${resHeaders}', {
headers: httpUtil.getRequestHeaderStr(req),
body: req.REQUEST.body || '',
body: req.REQUEST.body || (req._body && req._body.toString('UTF-8') || ''),
statusCode: res.statusCode,
resHeaders: JSON.stringify(res._headers, null, 2)
});
Expand Down Expand Up @@ -218,7 +218,7 @@ module.exports = function(req, res) {
clientPort: req.socket && req.socket.remotePort,
serverIp: serverInfo.intranetIp,
serverPort: config.httpPort,
requestRaw: httpUtil.getRequestHeaderStr(req) + (req.REQUEST.body || ''),
requestRaw: httpUtil.getRequestHeaderStr(req) + (req._body && req._body.toString('UTF-8') || ''),
responseHeader: httpUtil.getResponseHeaderStr(res),
responseBody: res._body ? res._body.toString('base64') : '',
logText: logText,
Expand Down
38 changes: 36 additions & 2 deletions bin/tsw/util/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const logger = require('logger');
const isInnerIP = require('util/http.isInnerIP.js');
const Deferred = require('util/Deferred');
const more = require('util/http.more.js');
const maxBodySize = 1024 * 1024; // 1MB


this.formatHeader = function(headers) {
Expand Down Expand Up @@ -75,7 +76,38 @@ this.filterInvalidHeaderChar = function(val) {
return val;
};

this.captureBody = function(res) {
this.captureIncomingMessageBody = function(req) {
// init...
const result = [];
let bodySize = 0;

if (req._capturing) {
return;
}

req._capturing = true;

const data = function(chunk) {
bodySize += chunk.length;

if (bodySize <= maxBodySize) {
result.push(chunk);
}
};

logger.debug('capture IncomingMessage body on');

req.on('data', data);
req.once('end', function() {
logger.debug('receive end');
this.removeListener('data', data);
const buffer = Buffer.concat(result);
req._bodySize = bodySize;
req._body = buffer;
});
};

this.captureBody = this.captureServerResponseBody = function(res) {

if (res._capturing) {
return;
Expand All @@ -85,6 +117,8 @@ this.captureBody = function(res) {
res._body = [];
res._bodySize = 0;

logger.debug('capture ServerResponse body on');

res.captrueBody = function(data, encoding) {
// 大于1M的不抓包
let buffer;
Expand Down Expand Up @@ -118,7 +152,7 @@ this.captureBody = function(res) {
this._body.push(Buffer.from(String(size.toString(16)) + '\r\n'));
}

if (this._bodySize < 1024 * 1024) {
if (this._bodySize < maxBodySize) {
this._body.push(buffer);
// chunked
if (this.useChunkedEncodingByDefaultNoNeed) {
Expand Down

0 comments on commit 07c1803

Please sign in to comment.