diff --git a/lib/server.js b/lib/server.js index e2f379f34..791b80fe1 100644 --- a/lib/server.js +++ b/lib/server.js @@ -329,8 +329,8 @@ Server.prototype.handleUpgrade = function (req, socket, upgradeHead) { var self = this; this.verify(req, true, function (err, success) { - if (err) { - socket.end(); + if (!success) { + abortConnection(socket, err); return; } @@ -463,3 +463,27 @@ Server.prototype.attach = function (server, options) { }); } }; + +/** + * Closes the connection + * + * @param {net.Socket} socket + * @param {code} error code + * @api private + */ + +function abortConnection (socket, code) { + if (socket.writable) { + var message = Server.errorMessages.hasOwnProperty(code) ? Server.errorMessages[code] : code; + var length = Buffer.byteLength(message); + socket.write( + 'HTTP/1.1 400 Bad Request\r\n' + + 'Connection: close\r\n' + + 'Content-type: text/html\r\n' + + 'Content-Length: ' + length + '\r\n' + + '\r\n' + + message + ); + } + socket.destroy(); +}