Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: add rawPacket in err of clientError event #17672

Closed
Closed
Show file tree
Hide file tree
Changes from 4 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
10 changes: 10 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,11 @@ changes:
description: The default action of calling `.destroy()` on the `socket`
will no longer take place if there are listeners attached
for `clientError`.
- version: VERSION
Copy link
Member

Choose a reason for hiding this comment

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

REPLACEME is a string that will get picked up by release tooling ):

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}
Expand Down Expand Up @@ -765,6 +770,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:

+ `bytesParsed`: the bytes count of request packet that Node.js may have parsed correctly;
Copy link
Member

Choose a reason for hiding this comment

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

Long line.

+ `rawPacket`: the raw packet of current request.
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, one thing I forgot to mention: Can you list the addition of rawPacket in the changes: section of the YAML block for clientError?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


### Event: 'close'
<!-- YAML
added: v0.1.4
Expand Down
8 changes: 5 additions & 3 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,12 @@ function socketOnError(e) {
function onParserExecuteCommon(server, socket, parser, state, ret, d) {
resetSocketTimeout(server, socket, state);

if (!d) {
d = parser.getCurrentBuffer();
Copy link
Member

Choose a reason for hiding this comment

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

Can you move this into the branches of the if statement? parser.getCurrrentBuffer() creates a copy. Calling it when the result is unused is wasteful and probably has a pretty big performance impact.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But if we don't get the buffer, we cannot pass it to the clientError event.

}
Copy link
Member

Choose a reason for hiding this comment

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

I think @bnoordhuis’ comment was basically about undoing my earlier comment (which is probably better, yes). i.e. move this part back into the branches were d is actually used, not before the if


if (ret instanceof Error) {
ret.rawPacket = d;
debug('parse error', ret);
socketOnError.call(socket, ret);
} else if (parser.incoming && parser.incoming.upgrade) {
Expand All @@ -484,9 +489,6 @@ function onParserExecuteCommon(server, socket, parser, state, ret, d) {
var req = parser.incoming;
debug('SERVER upgrade or connect', req.method);

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

socket.removeListener('data', state.onData);
socket.removeListener('end', state.onEnd);
socket.removeListener('close', state.onClose);
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