Skip to content

Commit 6ad9cb9

Browse files
committed
refactor(capturer): support requestHeader and requestBody
1 parent 398bf63 commit 6ad9cb9

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

bin/tsw/runtime/capturer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ process.nextTick(function() {
112112
clientPort: localPort || '',
113113
serverIp: remoteAddress || opt.host,
114114
serverPort: remotePort || opt.port,
115-
requestRaw: httpUtil.getClientRequestHeaderStr(request) + (request._body.toString('UTF-8') || ''),
115+
requestHeader: httpUtil.getClientRequestHeaderStr(request),
116+
requestBody: request._body ? request._body.toString('base64') : '',
116117
responseHeader: httpUtil.getClientResponseHeaderStr(response, bodySize),
117118
responseBody: (buffer.toString('base64')) || '',
118119
timestamps: {

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,14 @@ const downloadHaz = function (request, response, opt) {
417417
});
418418

419419
viewData.forEach(function(tmp, i) {
420+
if (tmp.requestHeader) {
421+
tmp.requestRaw = Buffer.concat([
422+
Buffer.from(tmp.curr.requestHeader || '', 'utf-8'),
423+
Buffer.from(tmp.curr.requestBody || '', 'base64')
424+
]).toString('UTF-8');
425+
}
420426

421427
hazJson.log.entries.push(initRequestHar(tmp));
422-
423-
424428
});
425429
const buf = Buffer.from(JSON.stringify(hazJson), 'UTF-8');
426430
gzipResponse.write(buf);
@@ -511,7 +515,8 @@ const download = function(request, response, opt) {
511515
clientPort: '',
512516
serverIp: '',
513517
serverPort: '',
514-
requestRaw: 'GET log/' + logSNKey + ' HTTP/1.1\r\n\r\n',
518+
requestHeader: 'GET log/' + logSNKey + ' HTTP/1.1\r\n\r\n',
519+
requestBody: '',
515520
responseHeader: 'HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=UTF-8\r\nConnection: close\r\n\r\n',
516521
responseBody: Buffer.from(tmp.curr.logText || '', 'UTF-8').toString('base64'),
517522
timestamps: tmp.curr.timestamps
@@ -539,8 +544,19 @@ const download = function(request, response, opt) {
539544
});
540545

541546
viewData.forEach(function(tmp, i) {
547+
let requestRaw;
548+
549+
if (tmp.curr.requestHeader) {
550+
requestRaw = Buffer.concat([
551+
Buffer.from(tmp.curr.requestHeader || '', 'utf-8'),
552+
Buffer.from(tmp.curr.requestBody || '', 'base64')
553+
]);
554+
} else {
555+
requestRaw = tmp.curr.requestRaw || '';
556+
}
557+
542558
archiver.append(
543-
tmp.curr.requestRaw || '',
559+
requestRaw,
544560
{
545561
name: 'raw/' + (tmp.curr.sid) + '_c.txt'
546562
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,22 @@ module.exports = function(req, res) {
187187
req.REQUEST.body = format.formatBuffer(req.REQUEST.body);
188188
}
189189

190+
let requestBodyText = req.REQUEST.body;
191+
192+
if (!requestBodyText) {
193+
if (req._body) {
194+
requestBodyText = req._body.toString('UTF-8');
195+
}
196+
}
197+
198+
// limit 64KB
199+
if (requestBodyText && requestBodyText.length >= 64 * 1024) {
200+
requestBodyText = `[Large than 64KB]: ${requestBodyText.length}`;
201+
}
202+
190203
logger.debug('\n${headers}${body}\r\nresponse ${statusCode} ${resHeaders}', {
191204
headers: httpUtil.getRequestHeaderStr(req),
192-
body: req.REQUEST.body || (req._body && req._body.toString('UTF-8') || ''),
205+
body: requestBodyText,
193206
statusCode: res.statusCode,
194207
resHeaders: JSON.stringify(res._headers, null, 2)
195208
});
@@ -218,7 +231,8 @@ module.exports = function(req, res) {
218231
clientPort: req.socket && req.socket.remotePort,
219232
serverIp: serverInfo.intranetIp,
220233
serverPort: config.httpPort,
221-
requestRaw: httpUtil.getRequestHeaderStr(req) + (req._body && req._body.toString('UTF-8') || ''),
234+
requestHeader: httpUtil.getRequestHeaderStr(req),
235+
requestBody: req._body ? req._body.toString('base64') : '',
222236
responseHeader: httpUtil.getResponseHeaderStr(res),
223237
responseBody: res._body ? res._body.toString('base64') : '',
224238
logText: logText,

0 commit comments

Comments
 (0)