Skip to content

Commit 72a089e

Browse files
committed
Correctly 404 upgrade requests for an unknown endpoint
1 parent 57655b0 commit 72a089e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,24 @@ module.exports = function(server, opts) {
152152
});
153153
}
154154

155+
primus.transformer.on('previous::upgrade', function(req, socket, head) {
156+
abortConnection(socket, 404, 'Not Found');
157+
});
158+
155159
return require('./manager')(primus, opts);
156160
};
161+
162+
function abortConnection(socket, code, name) {
163+
try {
164+
var response = [
165+
'HTTP/1.1 ' + code + ' ' + name,
166+
'Content-type: text/html'
167+
];
168+
socket.write(response.concat('', '').join('\r\n'));
169+
}
170+
catch (e) { /* ignore errors - we've aborted this connection */ }
171+
finally {
172+
// ensure that an early aborted connection is shut down completely
173+
try { socket.destroy(); } catch (e) {}
174+
}
175+
}

0 commit comments

Comments
 (0)