Skip to content

Commit 07c1803

Browse files
committed
chore(capture): add captureIncomingMessageBody
1 parent 72be759 commit 07c1803

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

bin/proxy/http.route.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ function doRoute(req, res) {
469469
}
470470

471471
// 抓回包
472-
httpUtil.captureBody(this);
472+
httpUtil.captureServerResponseBody(this);
473473
}
474474

475475
logger.debug('response ${statusCode}', {
@@ -488,6 +488,8 @@ function doRoute(req, res) {
488488
logger.getLog().showLineNumber = true;
489489
logger.debug('showLineNumber on');
490490
}
491+
492+
httpUtil.captureIncomingMessageBody(req);
491493
}
492494

493495
logger.debug('node-${version}, name: ${name}, appid: ${appid}', {

bin/tsw/util/auto-report/logReport.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ module.exports = function(req, res) {
189189

190190
logger.debug('\n${headers}${body}\r\nresponse ${statusCode} ${resHeaders}', {
191191
headers: httpUtil.getRequestHeaderStr(req),
192-
body: req.REQUEST.body || '',
192+
body: req.REQUEST.body || (req._body && req._body.toString('UTF-8') || ''),
193193
statusCode: res.statusCode,
194194
resHeaders: JSON.stringify(res._headers, null, 2)
195195
});
@@ -218,7 +218,7 @@ module.exports = function(req, res) {
218218
clientPort: req.socket && req.socket.remotePort,
219219
serverIp: serverInfo.intranetIp,
220220
serverPort: config.httpPort,
221-
requestRaw: httpUtil.getRequestHeaderStr(req) + (req.REQUEST.body || ''),
221+
requestRaw: httpUtil.getRequestHeaderStr(req) + (req._body && req._body.toString('UTF-8') || ''),
222222
responseHeader: httpUtil.getResponseHeaderStr(res),
223223
responseBody: res._body ? res._body.toString('base64') : '',
224224
logText: logText,

bin/tsw/util/http.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const logger = require('logger');
1414
const isInnerIP = require('util/http.isInnerIP.js');
1515
const Deferred = require('util/Deferred');
1616
const more = require('util/http.more.js');
17+
const maxBodySize = 1024 * 1024; // 1MB
1718

1819

1920
this.formatHeader = function(headers) {
@@ -75,7 +76,38 @@ this.filterInvalidHeaderChar = function(val) {
7576
return val;
7677
};
7778

78-
this.captureBody = function(res) {
79+
this.captureIncomingMessageBody = function(req) {
80+
// init...
81+
const result = [];
82+
let bodySize = 0;
83+
84+
if (req._capturing) {
85+
return;
86+
}
87+
88+
req._capturing = true;
89+
90+
const data = function(chunk) {
91+
bodySize += chunk.length;
92+
93+
if (bodySize <= maxBodySize) {
94+
result.push(chunk);
95+
}
96+
};
97+
98+
logger.debug('capture IncomingMessage body on');
99+
100+
req.on('data', data);
101+
req.once('end', function() {
102+
logger.debug('receive end');
103+
this.removeListener('data', data);
104+
const buffer = Buffer.concat(result);
105+
req._bodySize = bodySize;
106+
req._body = buffer;
107+
});
108+
};
109+
110+
this.captureBody = this.captureServerResponseBody = function(res) {
79111

80112
if (res._capturing) {
81113
return;
@@ -85,6 +117,8 @@ this.captureBody = function(res) {
85117
res._body = [];
86118
res._bodySize = 0;
87119

120+
logger.debug('capture ServerResponse body on');
121+
88122
res.captrueBody = function(data, encoding) {
89123
// 大于1M的不抓包
90124
let buffer;
@@ -118,7 +152,7 @@ this.captureBody = function(res) {
118152
this._body.push(Buffer.from(String(size.toString(16)) + '\r\n'));
119153
}
120154

121-
if (this._bodySize < 1024 * 1024) {
155+
if (this._bodySize < maxBodySize) {
122156
this._body.push(buffer);
123157
// chunked
124158
if (this.useChunkedEncodingByDefaultNoNeed) {

0 commit comments

Comments
 (0)