Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit d9fbe1fda3d6ca9dc5baf122b531b6c55a6aec6b 1 parent f1e3f32
Fedor Indutny authored
20 lib/http.js
... ... @@ -1,4 +1,5 @@
1   -var util = require('util');
  1 +var util = require('util'),
  2 + events = require('events');
2 3
3 4 var debug;
4 5 var debugLevel = parseInt(process.env.NODE_DEBUG, 16);
@@ -794,9 +795,11 @@ function connectionListener (socket) {
794 795 // in the upgradeHead from the closing lines of the headers
795 796 var upgradeHead = d.slice(start + bytesParsed + 1, end);
796 797
797   - if (self.listeners("upgrade").length) {
798   - self.emit('upgrade', req, req.socket, upgradeHead);
799   - } else {
  798 + var result = self.emit('upgrade', req, req.socket, upgradeHead);
  799 +
  800 + // Event listener should stop propagation (return false)
  801 + // To get us notified that upgrade connection was used
  802 + if (result !== events.STOPPED_PROPAGATION) {
800 803 // Got upgrade header, but have no handler.
801 804 socket.destroy();
802 805 }
@@ -889,9 +892,12 @@ function Client ( ) {
889 892
890 893 var upgradeHead = d.slice(start + bytesParsed + 1, end);
891 894
892   - if (self.listeners('upgrade').length) {
893   - self.emit('upgrade', req, self, upgradeHead);
894   - } else {
  895 + var result = self.emit('upgrade', req, self, upgradeHead);
  896 +
  897 + // Event listener should stop propagation (return false)
  898 + // To get us notified that upgrade connection was used
  899 + if (result !== events.STOPPED_PROPAGATION) {
  900 + // Got upgrade header, but have no handler.
895 901 self.destroy();
896 902 }
897 903 }
4 test/simple/test-http-upgrade-client.js
@@ -48,6 +48,10 @@ srv.listen(common.PORT, '127.0.0.1', function () {
48 48 srv.close();
49 49
50 50 gotUpgrade = true;
  51 +
  52 + // Stop propagation
  53 + // So 'http' module will know, that it should not destroy socket
  54 + return false;
51 55 });
52 56 hc.request('GET', '/').end();
53 57 });
9 test/simple/test-http-upgrade-client2.js
@@ -11,6 +11,9 @@ server.on('upgrade', function(req, socket, head) {
11 11 socket.on('end', function () {
12 12 socket.end();
13 13 });
  14 + // Stop propagation
  15 + // So 'http' module will know, that it should not destroy socket
  16 + return false;
14 17 });
15 18
16 19
@@ -30,7 +33,11 @@ server.listen(common.PORT, function () {
30 33 wasUpgrade = true;
31 34
32 35 client.removeListener('upgrade', onUpgrade);
33   - socket.end();
  36 + socket.end();
  37 +
  38 + // Stop propagation
  39 + // So 'http' module will know, that it should not destroy socket
  40 + return false;
34 41 }
35 42 client.on('upgrade', onUpgrade);
36 43
4 test/simple/test-http-upgrade-server.js
@@ -45,6 +45,10 @@ function testServer(){
45 45 socket.write(data, "utf8");
46 46 }
47 47 };
  48 +
  49 + // Stop propagation
  50 + // So 'http' module will know, that it should not destroy socket
  51 + return false;
48 52 });
49 53 };
50 54
6 test/simple/test-http-upgrade-server2.js
@@ -14,6 +14,12 @@ server.addListener('upgrade', function (req, socket, upgradeHead) {
14 14 // test that throwing an error from upgrade gets
15 15 // is uncaught
16 16 throw new Error('upgrade error');
  17 +
  18 + // Stop propagation
  19 + // So 'http' module will know, that it should not destroy socket
  20 + // Actually, this line wouldn't be called
  21 + // But anyway return false
  22 + return false;
17 23 });
18 24
19 25 gotError = false;
45 test/simple/test-http-upgrade-server3.js
... ... @@ -0,0 +1,45 @@
  1 +common = require("../common");
  2 +assert = common.assert
  3 +
  4 +http = require('http');
  5 +net = require('net');
  6 +
  7 +server = http.createServer();
  8 +
  9 +server.addListener('upgrade', function (req, socket, upgradeHead) {
  10 + common.error('server got upgrade event');
  11 + // We won't step propagation
  12 + // (not return false)
  13 + // And see - what will happen
  14 +
  15 + process.nextTick(function() {
  16 + // Socket should be destroyed here
  17 + // Because upgrade event wasn't catched
  18 + // (Propagation was not stopped)
  19 +
  20 + assert.ok(socket.fd === null);
  21 + server.close();
  22 + });
  23 +});
  24 +
  25 +server.listen(common.PORT, function () {
  26 + var c = net.createConnection(common.PORT);
  27 +
  28 + c.addListener('connect', function () {
  29 + common.error('client wrote message');
  30 + c.write( "GET /blah HTTP/1.1\r\n"
  31 + + "Upgrade: WebSocket\r\n"
  32 + + "Connection: Upgrade\r\n"
  33 + + "\r\n\r\nhello world"
  34 + );
  35 + });
  36 +
  37 + c.addListener('end', function () {
  38 + c.end();
  39 + });
  40 +
  41 + c.addListener('close', function () {
  42 + common.error('client close');
  43 + });
  44 +});
  45 +

0 comments on commit d9fbe1f

Please sign in to comment.
Something went wrong with that request. Please try again.