Skip to content

HTTP parser premature close #29609

@awwright

Description

@awwright
  • Version: v12.10.0
  • Platform: macOS
  • Subsystem: http, stream

I'm running across an issue where when no response data is written during the "request" event call, the HTTP handling either closes the response without sending any apparent reply, or sends a Connection: close response but doesn't end the stream.

This is a strange behavior that I can't find an explanation for and I can't come up with any workaround; so I think this is a bug.

Case 1

"use strict";
const http = require('http');
const makeDuplexPair = require('./test/common/duplexpair.js');

const message = 'GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n';
const server = http.createServer(function(req, res){
	console.log('Have request: ', req.method, req.url);
	req.resume();
	req.once('end', function(){
		console.log('(request end)');
		res.end('Fin\r\n');
	});
});

const {clientSide, serverSide} = makeDuplexPair();
clientSide.end(message);
clientSide.pipe(process.stdout);
clientSide.once('end', function(){ console.log('(Connection closed)'); });

server.emit('connection', serverSide);

Actual:

Have request:  GET /
(request end)
(Connection closed)

I expect to see the HTTP response output, but the connection simply closes.

Case 2 (using clientSide.write instead of clientSide.end)

"use strict";
const http = require('http');
const makeDuplexPair = require('./test/common/duplexpair.js');

const message = 'GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n';
const server = http.createServer(function(req, res){
	console.log('Have request: ', req.method, req.url);
	req.resume();
	req.once('end', function(){
		console.log('(request end)');
		res.end('Fin\r\n');
	});
});

const {clientSide, serverSide} = makeDuplexPair();
clientSide.write(message);
clientSide.pipe(process.stdout);
clientSide.once('end', function(){ console.log('(Connection closed)'); });

server.emit('connection', serverSide);

I expect to see (Connection closed) at the end, but I do not.

Actual:

Have request:  GET /
(request end)
HTTP/1.1 200 OK
Date: Thu, 19 Sep 2019 01:01:49 GMT
Connection: close
Content-Length: 5

Fin

In one or the other case I expect:

Have request:  GET /
(request end)
HTTP/1.1 200 OK
Date: Thu, 19 Sep 2019 01:01:49 GMT
Connection: close
Content-Length: 5

Fin
(Connection closed)

As far as I can tell, there's no way to do this.

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