Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {

const kIncomingMessage = Symbol('IncomingMessage');
const kSkipPendingData = Symbol('SkipPendingData');
const kOnBodyChunk = Symbol('onBodyChunk');
const kOnMessageBegin = HTTPParser.kOnMessageBegin | 0;
const kOnHeaders = HTTPParser.kOnHeaders | 0;
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
Expand Down Expand Up @@ -134,6 +135,7 @@ function parserOnBody(b) {

// Pretend this was the result of a stream._read call.
if (!stream._dumped) {
stream[kOnBodyChunk]?.(b);
const ret = stream.push(b);
if (!ret)
readStop(this.socket);
Expand Down Expand Up @@ -337,6 +339,7 @@ module.exports = {
methods,
parsers,
kIncomingMessage,
kOnBodyChunk,
HTTPParser,
isLenient,
calculateLenientFlags,
Expand Down
8 changes: 4 additions & 4 deletions lib/internal/inspector/network_http.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const {
sniffMimeType,
} = require('internal/inspector/network');
const { Network } = require('inspector');
const EventEmitter = require('events');
const { kOnBodyChunk } = require('_http_common');
const { kEmptyObject } = require('internal/util');

const kRequestUrl = Symbol('kRequestUrl');
Expand Down Expand Up @@ -136,19 +136,19 @@ function onClientResponseFinish({ request, response }) {
},
});

// Unlike response.on('data', ...), this does not put the stream into flowing mode.
EventEmitter.prototype.on.call(response, 'data', (chunk) => {
response[kOnBodyChunk] = (chunk) => {
Network.dataReceived({
requestId: request[kInspectorRequestId],
timestamp: getMonotonicTime(),
dataLength: chunk.byteLength,
encodedDataLength: chunk.byteLength,
data: chunk,
});
});
};

// Wait until the response body is consumed by user code.
response.once('end', () => {
response[kOnBodyChunk] = undefined;
Network.loadingFinished({
requestId: request[kInspectorRequestId],
timestamp: getMonotonicTime(),
Expand Down
40 changes: 40 additions & 0 deletions test/parallel/test-inspector-network-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,23 @@ function verifyHttpResponse(response) {
}));
}

function verifyHttpResponseWithEncoding(response) {
assert.strictEqual(response.statusCode, 200);
const chunks = [];

response.setEncoding('hex');
response.on('data', (chunk) => {
chunks.push(chunk);
});

response.on('end', common.mustCall(() => {
assert.strictEqual(
chunks.join(''),
Buffer.from('\nhello world\n').toString('hex'),
);
}));
}

function drainHttpResponse(response) {
response.resume();
}
Expand Down Expand Up @@ -342,6 +359,27 @@ async function testHttpsGet() {
await assertResponseBody(responseReceived, '\nhello world\n');
}

async function testHttpResponseBodyWithEncoding() {
const url = `http://127.0.0.1:${httpServer.address().port}/hello-world`;
const {
requestWillBeSentFuture,
responseReceivedFuture,
loadingFinishedFuture,
} = createRequestTracker(url, getDefaultResponseExpect(url));

http.get({
host: '127.0.0.1',
port: httpServer.address().port,
path: '/hello-world',
headers: requestHeaders,
}, common.mustCall(verifyHttpResponseWithEncoding));

await requestWillBeSentFuture;
const responseReceived = await responseReceivedFuture;
await loadingFinishedFuture;
await assertResponseBody(responseReceived, '\nhello world\n');
}

async function testHttpError() {
const url = `http://${addresses.INVALID_HOST}/`;
const requestWillBeSentFuture = once(session, 'Network.requestWillBeSent')
Expand Down Expand Up @@ -389,6 +427,8 @@ const testNetworkInspection = async () => {
session.removeAllListeners();
await testHttpsGet();
session.removeAllListeners();
await testHttpResponseBodyWithEncoding();
session.removeAllListeners();
await testHttpError();
session.removeAllListeners();
await testHttpsError();
Expand Down
Loading