Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use emit() result in 'upgrade' event in http.js

  • Loading branch information...
commit d9fbe1fda3d6ca9dc5baf122b531b6c55a6aec6b 1 parent f1e3f32
@indutny authored
View
20 lib/http.js
@@ -1,4 +1,5 @@
-var util = require('util');
+var util = require('util'),
+ events = require('events');
var debug;
var debugLevel = parseInt(process.env.NODE_DEBUG, 16);
@@ -794,9 +795,11 @@ function connectionListener (socket) {
// in the upgradeHead from the closing lines of the headers
var upgradeHead = d.slice(start + bytesParsed + 1, end);
- if (self.listeners("upgrade").length) {
- self.emit('upgrade', req, req.socket, upgradeHead);
- } else {
+ var result = self.emit('upgrade', req, req.socket, upgradeHead);
+
+ // Event listener should stop propagation (return false)
+ // To get us notified that upgrade connection was used
+ if (result !== events.STOPPED_PROPAGATION) {
// Got upgrade header, but have no handler.
socket.destroy();
}
@@ -889,9 +892,12 @@ function Client ( ) {
var upgradeHead = d.slice(start + bytesParsed + 1, end);
- if (self.listeners('upgrade').length) {
- self.emit('upgrade', req, self, upgradeHead);
- } else {
+ var result = self.emit('upgrade', req, self, upgradeHead);
+
+ // Event listener should stop propagation (return false)
+ // To get us notified that upgrade connection was used
+ if (result !== events.STOPPED_PROPAGATION) {
+ // Got upgrade header, but have no handler.
self.destroy();
}
}
View
4 test/simple/test-http-upgrade-client.js
@@ -48,6 +48,10 @@ srv.listen(common.PORT, '127.0.0.1', function () {
srv.close();
gotUpgrade = true;
+
+ // Stop propagation
+ // So 'http' module will know, that it should not destroy socket
+ return false;
});
hc.request('GET', '/').end();
});
View
9 test/simple/test-http-upgrade-client2.js
@@ -11,6 +11,9 @@ server.on('upgrade', function(req, socket, head) {
socket.on('end', function () {
socket.end();
});
+ // Stop propagation
+ // So 'http' module will know, that it should not destroy socket
+ return false;
});
@@ -30,7 +33,11 @@ server.listen(common.PORT, function () {
wasUpgrade = true;
client.removeListener('upgrade', onUpgrade);
- socket.end();
+ socket.end();
+
+ // Stop propagation
+ // So 'http' module will know, that it should not destroy socket
+ return false;
}
client.on('upgrade', onUpgrade);
View
4 test/simple/test-http-upgrade-server.js
@@ -45,6 +45,10 @@ function testServer(){
socket.write(data, "utf8");
}
};
+
+ // Stop propagation
+ // So 'http' module will know, that it should not destroy socket
+ return false;
});
};
View
6 test/simple/test-http-upgrade-server2.js
@@ -14,6 +14,12 @@ server.addListener('upgrade', function (req, socket, upgradeHead) {
// test that throwing an error from upgrade gets
// is uncaught
throw new Error('upgrade error');
+
+ // Stop propagation
+ // So 'http' module will know, that it should not destroy socket
+ // Actually, this line wouldn't be called
+ // But anyway return false
+ return false;
});
gotError = false;
View
45 test/simple/test-http-upgrade-server3.js
@@ -0,0 +1,45 @@
+common = require("../common");
+assert = common.assert
+
+http = require('http');
+net = require('net');
+
+server = http.createServer();
+
+server.addListener('upgrade', function (req, socket, upgradeHead) {
+ common.error('server got upgrade event');
+ // We won't step propagation
+ // (not return false)
+ // And see - what will happen
+
+ process.nextTick(function() {
+ // Socket should be destroyed here
+ // Because upgrade event wasn't catched
+ // (Propagation was not stopped)
+
+ assert.ok(socket.fd === null);
+ server.close();
+ });
+});
+
+server.listen(common.PORT, function () {
+ var c = net.createConnection(common.PORT);
+
+ c.addListener('connect', function () {
+ common.error('client wrote message');
+ c.write( "GET /blah HTTP/1.1\r\n"
+ + "Upgrade: WebSocket\r\n"
+ + "Connection: Upgrade\r\n"
+ + "\r\n\r\nhello world"
+ );
+ });
+
+ c.addListener('end', function () {
+ c.end();
+ });
+
+ c.addListener('close', function () {
+ common.error('client close');
+ });
+});
+
Please sign in to comment.
Something went wrong with that request. Please try again.