Skip to content

HTTP Upgrade IncomingMessage does not parse body #58394

Open
@mscdex

Description

@mscdex

Version

22.15.0

Platform

Linux

Subsystem

http

What steps will reproduce the bug?

'use strict';

const { createServer, request } = require('http');

createServer((req, res) => {
  res.writeHead(200).end();
}).on('upgrade', (req, reqSocket, head) => {
  console.log('Server Upgrade', req.headers);
  let buf = '';
  req.on('data', (str) => buf += str);
  req.on('close', () => {
    console.log(
      'Request close',
      'data', JSON.stringify(buf.trim()),
      'head', Buffer.from(head)
    );
  });
  req.setEncoding('utf8');
}).listen(0, '127.0.0.1', function() {
  const req = request({
    host: '127.0.0.1',
    port: this.address().port,
    method: 'POST',
    path: '/foo',
    headers: {
      Connection: 'Upgrade',
      Upgrade: 'db',
    },
  });
  req.on('upgrade', (res, socket, head) => {
    console.log(
      'Client Upgrade',
      'status', res.statusCode,
      'head', Buffer.from(head)
    );
  });
  setTimeout(() => {
    req.write('2');
    setTimeout(() => req.end(), 1000);
  }, 1000);
});

Output:

Server Upgrade {                                                                                                                                                                                                   
  connection: 'Upgrade',                                                                                                                                                                                           
  upgrade: 'db',                                                                                                                                                                                                   
  host: '127.0.0.1:39511',                                                                                                                                                                                         
  'transfer-encoding': 'chunked'                                                                                                                                                                                   
}                                                                                                                                                                                                                  
Request close data "" head <Buffer 31 0d 0a 32 0d 0a>

How often does it reproduce? Is there a required condition?

Reproduces every time.

What is the expected behavior? Why is that the expected behavior?

The expected behavior is to have node parse the body like it normally would.

What do you see instead?

Node ignores the content or potentially puts some of it (unparsed -- especially evident in the case of a chunked encoded body) in the head argument passed to the 'upgrade' event.

Additional information

No response

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