Skip to content

Commit e9eb792

Browse files
committed
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.
1 parent 9f61c70 commit e9eb792

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

doc/api/http.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,11 @@ object, so any HTTP response sent, including response headers and payload,
765765
*must* be written directly to the `socket` object. Care must be taken to
766766
ensure the response is a properly formatted HTTP response message.
767767

768+
> `err` is an instance of `Error` with two extra columns:
769+
>
770+
> + `bytesParsed`: the bytes count of request packet that Node.js may parse correctly;
771+
> + `rawPacket`: the raw packet of current request.
772+
768773
### Event: 'close'
769774
<!-- YAML
770775
added: v0.1.4

lib/_http_server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ function onParserExecuteCommon(server, socket, parser, state, ret, d) {
476476
resetSocketTimeout(server, socket, state);
477477

478478
if (ret instanceof Error) {
479+
ret.rawPacket = d || parser.getCurrentBuffer();
479480
debug('parse error', ret);
480481
socketOnError.call(socket, ret);
481482
} else if (parser.incoming && parser.incoming.upgrade) {

test/parallel/test-http-server-client-error.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ const server = http.createServer(common.mustCall(function(req, res) {
1010
}));
1111

1212
server.on('clientError', common.mustCall(function(err, socket) {
13+
assert.strictEqual(err instanceof Error, true);
14+
assert.strictEqual(err.code, 'HPE_INVALID_METHOD');
15+
assert.strictEqual(err.bytesParsed, 1);
16+
assert.strictEqual(err.message, 'Parse Error');
17+
assert.strictEqual(err.rawPacket.toString(), 'Oopsie-doopsie\r\n');
18+
1319
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
1420

1521
server.close();

0 commit comments

Comments
 (0)