Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
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.
  • Loading branch information
XadillaX committed Dec 14, 2017
commit e9eb79283dd866cf69f46c6849ae631c63a76322
5 changes: 5 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,11 @@ object, so any HTTP response sent, including response headers and payload,
*must* be written directly to the `socket` object. Care must be taken to
ensure the response is a properly formatted HTTP response message.

> `err` is an instance of `Error` with two extra columns:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the > blockquote?

>
> + `bytesParsed`: the bytes count of request packet that Node.js may parse correctly;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/parse correctly/have parsed incorrectly/?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the count of 'correctly', not 'incorrectly'. Means Node.js have parsed bytesParsed correctly and the left are incorrectly.

> + `rawPacket`: the raw packet of current request.

### Event: 'close'
<!-- YAML
added: v0.1.4
Expand Down
1 change: 1 addition & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ function onParserExecuteCommon(server, socket, parser, state, ret, d) {
resetSocketTimeout(server, socket, state);

if (ret instanceof Error) {
ret.rawPacket = d || parser.getCurrentBuffer();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, but can you maybe move the

     if (!d)
       d = parser.getCurrentBuffer();

block in front of the if (ret instanceof Error) part? Then you can always just use d inside of this function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug('parse error', ret);
socketOnError.call(socket, ret);
} else if (parser.incoming && parser.incoming.upgrade) {
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-http-server-client-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ const server = http.createServer(common.mustCall(function(req, res) {
}));

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');

server.close();
Expand Down