http: add rawPacket in err of `clientError` event #17672

Open
wants to merge 6 commits into
from

Conversation

Projects
None yet
4 participants
Member

XadillaX commented Dec 14, 2017 •

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.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

http

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.
doc/api/http.md
@@ -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:
@addaleax

addaleax Dec 14, 2017

Owner

Why the > blockquote?

doc/api/http.md
@@ -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:
+>
+> + `bytesParsed`: the bytes count of request packet that Node.js may parse correctly;
@addaleax

addaleax Dec 14, 2017

Owner

s/parse correctly/have parsed incorrectly/?

@XadillaX

XadillaX Dec 15, 2017

Member

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

@@ -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();
@addaleax

addaleax Dec 14, 2017

Owner

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

Member

XadillaX commented Dec 15, 2017

@addaleax I've updated the code

doc/api/http.md
+`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.
@addaleax

addaleax Dec 15, 2017

Owner

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

lib/_http_server.js
@@ -475,7 +475,11 @@ function socketOnError(e) {
function onParserExecuteCommon(server, socket, parser, state, ret, d) {
resetSocketTimeout(server, socket, state);
+ if (!d)
+ d = parser.getCurrentBuffer();
@bnoordhuis

bnoordhuis Dec 15, 2017

Member

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.

@XadillaX

XadillaX Dec 15, 2017

Member

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

doc/api/http.md
@@ -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
@addaleax

addaleax Dec 18, 2017

Owner

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

lib/_http_server.js
@@ -475,7 +475,12 @@ function socketOnError(e) {
function onParserExecuteCommon(server, socket, parser, state, ret, d) {
resetSocketTimeout(server, socket, state);
+ if (!d) {
+ d = parser.getCurrentBuffer();
+ }
@addaleax

addaleax Dec 18, 2017

Owner

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

@addaleax addaleax added the ready label Dec 20, 2017

doc/api/http.md
@@ -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;
@bnoordhuis

bnoordhuis Dec 20, 2017

Member

Long line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment