Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

Commit

Permalink
http: send 400 bad request on parse error
Browse files Browse the repository at this point in the history
A web server such as nginx assumes that upstream is dead
if upstream closes the socket without any response.

PR-URL: nodejs/node#15324
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
mog422 authored and addaleax committed Oct 26, 2017
1 parent 0727916 commit 1cbc3b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const {
const { OutgoingMessage } = require('_http_outgoing');
const { outHeadersKey, ondrain } = require('internal/http');
const errors = require('internal/errors');
const Buffer = require('buffer').Buffer;

const STATUS_CODES = {
100: 'Continue',
Expand Down Expand Up @@ -451,13 +452,21 @@ function onParserExecute(server, socket, parser, state, ret, d) {
onParserExecuteCommon(server, socket, parser, state, ret, undefined);
}

const badRequestResponse = Buffer.from(
'HTTP/1.1 400 ' + STATUS_CODES[400] + CRLF + CRLF, 'ascii'
);
function socketOnError(e) {
// Ignore further errors
this.removeListener('error', socketOnError);
this.on('error', () => {});

if (!this.server.emit('clientError', e, this))
if (!this.server.emit('clientError', e, this)) {
if (this.writable) {
this.end(badRequestResponse);
return;
}
this.destroy(e);
}
}

function onParserExecuteCommon(server, socket, parser, state, ret, d) {
Expand Down
10 changes: 8 additions & 2 deletions test/parallel/test-http-blank-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const server = http.createServer(common.mustCall((req, res) => {

server.listen(0, common.mustCall(() => {
const c = net.createConnection(server.address().port);
let received = '';

c.on('connect', common.mustCall(() => {
c.write('GET /blah HTTP/1.1\r\n' +
Expand All @@ -47,7 +48,12 @@ server.listen(0, common.mustCall(() => {
'\r\n\r\nhello world'
);
}));

c.on('end', common.mustCall(() => c.end()));
c.on('data', common.mustCall((data) => {
received += data.toString();
}));
c.on('end', common.mustCall(() => {
assert.strictEqual('HTTP/1.1 400 Bad Request\r\n\r\n', received);
c.end();
}));
c.on('close', common.mustCall(() => server.close()));
}));

0 comments on commit 1cbc3b3

Please sign in to comment.