Skip to content

http module emits 'end' event after 'error' event being called #31935

@megothub

Description

@megothub

Hello

Update: Found another similar topic:
#2156

Couldn't find solution, but, found similar issue related to TCP sockets
#6083

  • OS: Ubuntu 18.04.3 LTS
  • node v12.16.1

What I need is to receive Github webhook request and handle it within 'End' event as 'Data' Event might not contain all data (as I understood the process). And, if there is a DoS attempt detected from a 3rd party just want to drop that connection to prevent node process to grow.

Code to reproduce:

const http = require('http');

console.log("Webhook Server: STARTING")

http.createServer((request, response) => {
  const { headers, method, url } = request;
  let body = [];

  request.on('error', (err) => {
        console.log("Webhook Server: Request Handler Error event: " + err);
  }).on('data', (chunk) => {
        console.log("Webhook Server: Data event");
        body.push(chunk);

        console.log("Webhook Server: Emitting Error");
        request.emit('error', "Error message");
  }).on('end', () => {
        console.log("Webhook Server: End event");
        response.on('error', (err) => {
             console.log("Webhook Server: Response Error")
             console.error(err);
        });

        response.writeHead(200, {'Content-Type': 'application/json'})
        const responseBody = { headers, method, url, body };

        response.end(JSON.stringify(responseBody))

    });
}).listen(3001, '127.0.0.1', function() {
    console.log("Webhook Server: LISTENING")
});

Getting:
Webhook Server: STARTING
Webhook Server: LISTENING
Webhook Server: Data event
Webhook Server: Emitting Error
Webhook Server: Request Handler Error event: Error message
Webhook Server: End event

Metadata

Metadata

Assignees

No one assigned

    Labels

    httpIssues or PRs related to the http subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions