http: add rawPacket in err of clientError
event
The `rawPacket` is the current buffer that just parsed. Adding this buffer to the error object of `clientError` event is to make it possible that developers can log the broken packet. PR-URL: https://github.com/nodejs/node/pull/17672 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
b0dd43cd63
commit
6c0da34905
@ -734,6 +734,11 @@ changes:
|
|||||||
description: The default action of calling `.destroy()` on the `socket`
|
description: The default action of calling `.destroy()` on the `socket`
|
||||||
will no longer take place if there are listeners attached
|
will no longer take place if there are listeners attached
|
||||||
for `clientError`.
|
for `clientError`.
|
||||||
|
- version: REPLACEME
|
||||||
|
pr-url: https://github.com/nodejs/node/pull/17672
|
||||||
|
description: The rawPacket is the current buffer that just parsed. Adding
|
||||||
|
this buffer to the error object of clientError event is to make
|
||||||
|
it possible that developers can log the broken packet.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
* `exception` {Error}
|
* `exception` {Error}
|
||||||
@ -765,6 +770,12 @@ object, so any HTTP response sent, including response headers and payload,
|
|||||||
*must* be written directly to the `socket` object. Care must be taken to
|
*must* be written directly to the `socket` object. Care must be taken to
|
||||||
ensure the response is a properly formatted HTTP response message.
|
ensure the response is a properly formatted HTTP response message.
|
||||||
|
|
||||||
|
`err` is an instance of `Error` with two extra columns:
|
||||||
|
|
||||||
|
+ `bytesParsed`: the bytes count of request packet that Node.js may have parsed
|
||||||
|
correctly;
|
||||||
|
+ `rawPacket`: the raw packet of current request.
|
||||||
|
|
||||||
### Event: 'close'
|
### Event: 'close'
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
added: v0.1.4
|
added: v0.1.4
|
||||||
|
@ -476,6 +476,7 @@ function onParserExecuteCommon(server, socket, parser, state, ret, d) {
|
|||||||
resetSocketTimeout(server, socket, state);
|
resetSocketTimeout(server, socket, state);
|
||||||
|
|
||||||
if (ret instanceof Error) {
|
if (ret instanceof Error) {
|
||||||
|
ret.rawPacket = d || parser.getCurrentBuffer();
|
||||||
debug('parse error', ret);
|
debug('parse error', ret);
|
||||||
socketOnError.call(socket, ret);
|
socketOnError.call(socket, ret);
|
||||||
} else if (parser.incoming && parser.incoming.upgrade) {
|
} else if (parser.incoming && parser.incoming.upgrade) {
|
||||||
|
@ -10,6 +10,12 @@ const server = http.createServer(common.mustCall(function(req, res) {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
server.on('clientError', common.mustCall(function(err, socket) {
|
server.on('clientError', common.mustCall(function(err, socket) {
|
||||||
|
assert.strictEqual(err instanceof Error, true);
|
||||||
|
assert.strictEqual(err.code, 'HPE_INVALID_METHOD');
|
||||||
|
assert.strictEqual(err.bytesParsed, 1);
|
||||||
|
assert.strictEqual(err.message, 'Parse Error');
|
||||||
|
assert.strictEqual(err.rawPacket.toString(), 'Oopsie-doopsie\r\n');
|
||||||
|
|
||||||
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
|
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
|
||||||
|
|
||||||
server.close();
|
server.close();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user