Skip to content
This repository
Browse code

http: remove bodyHead from 'upgrade' events

Streams2 makes this unnecessary.
An empty buffer is provided for compatibility.
  • Loading branch information...
commit a40133d10cdb911b27fe8d46d67a835b0103bbf1 1 parent 007e63b
Nathan Zadoks authored isaacs committed
20  doc/api/http.markdown
Source Rendered
@@ -93,7 +93,7 @@ not be emitted.
93 93
 
94 94
 ### Event: 'connect'
95 95
 
96  
-`function (request, socket, head) { }`
  96
+`function (request, socket) { }`
97 97
 
98 98
 Emitted each time a client requests a http CONNECT method. If this event isn't
99 99
 listened for, then clients requesting a CONNECT method will have their
@@ -102,8 +102,6 @@ connections closed.
102 102
 * `request` is the arguments for the http request, as it is in the request
103 103
   event.
104 104
 * `socket` is the network socket between the server and client.
105  
-* `head` is an instance of Buffer, the first packet of the tunneling stream,
106  
-  this may be empty.
107 105
 
108 106
 After this event is emitted, the request's socket will not have a `data`
109 107
 event listener, meaning you will need to bind to it in order to handle data
@@ -111,7 +109,7 @@ sent to the server on that socket.
111 109
 
112 110
 ### Event: 'upgrade'
113 111
 
114  
-`function (request, socket, head) { }`
  112
+`function (request, socket) { }`
115 113
 
116 114
 Emitted each time a client requests a http upgrade. If this event isn't
117 115
 listened for, then clients requesting an upgrade will have their connections
@@ -120,7 +118,6 @@ closed.
120 118
 * `request` is the arguments for the http request, as it is in the request
121 119
   event.
122 120
 * `socket` is the network socket between the server and client.
123  
-* `head` is an instance of Buffer, the first packet of the upgraded stream,
124 121
   this may be empty.
125 122
 
126 123
 After this event is emitted, the request's socket will not have a `data`
@@ -595,7 +592,7 @@ Emitted after a socket is assigned to this request.
595 592
 
596 593
 ### Event: 'connect'
597 594
 
598  
-`function (response, socket, head) { }`
  595
+`function (response, socket) { }`
599 596
 
600 597
 Emitted each time a server responds to a request with a CONNECT method. If this
601 598
 event isn't being listened for, clients receiving a CONNECT method will have
@@ -612,14 +609,13 @@ A client server pair that show you how to listen for the `connect` event.
612 609
       res.writeHead(200, {'Content-Type': 'text/plain'});
613 610
       res.end('okay');
614 611
     });
615  
-    proxy.on('connect', function(req, cltSocket, head) {
  612
+    proxy.on('connect', function(req, cltSocket) {
616 613
       // connect to an origin server
617 614
       var srvUrl = url.parse('http://' + req.url);
618 615
       var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, function() {
619 616
         cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
620 617
                         'Proxy-agent: Node-Proxy\r\n' +
621 618
                         '\r\n');
622  
-        srvSocket.write(head);
623 619
         srvSocket.pipe(cltSocket);
624 620
         cltSocket.pipe(srvSocket);
625 621
       });
@@ -639,7 +635,7 @@ A client server pair that show you how to listen for the `connect` event.
639 635
       var req = http.request(options);
640 636
       req.end();
641 637
 
642  
-      req.on('connect', function(res, socket, head) {
  638
+      req.on('connect', function(res, socket) {
643 639
         console.log('got connected!');
644 640
 
645 641
         // make a request over an HTTP tunnel
@@ -658,7 +654,7 @@ A client server pair that show you how to listen for the `connect` event.
658 654
 
659 655
 ### Event: 'upgrade'
660 656
 
661  
-`function (response, socket, head) { }`
  657
+`function (response, socket) { }`
662 658
 
663 659
 Emitted each time a server responds to a request with an upgrade. If this
664 660
 event isn't being listened for, clients receiving an upgrade header will have
@@ -673,7 +669,7 @@ A client server pair that show you how to listen for the `upgrade` event.
673 669
       res.writeHead(200, {'Content-Type': 'text/plain'});
674 670
       res.end('okay');
675 671
     });
676  
-    srv.on('upgrade', function(req, socket, head) {
  672
+    srv.on('upgrade', function(req, socket) {
677 673
       socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
678 674
                    'Upgrade: WebSocket\r\n' +
679 675
                    'Connection: Upgrade\r\n' +
@@ -698,7 +694,7 @@ A client server pair that show you how to listen for the `upgrade` event.
698 694
       var req = http.request(options);
699 695
       req.end();
700 696
 
701  
-      req.on('upgrade', function(res, socket, upgradeHead) {
  697
+      req.on('upgrade', function(res, socket) {
702 698
         console.log('got upgraded!');
703 699
         socket.end();
704 700
         process.exit(0);
10  lib/http.js
@@ -28,6 +28,9 @@ var FreeList = require('freelist').FreeList;
28 28
 var HTTPParser = process.binding('http_parser').HTTPParser;
29 29
 var assert = require('assert').ok;
30 30
 
  31
+// an empty buffer for UPGRADE/CONNECT bodyHead compatibility
  32
+var emptyBuffer = new Buffer(0);
  33
+
31 34
 var debug;
32 35
 if (process.env.NODE_DEBUG && /http/.test(process.env.NODE_DEBUG)) {
33 36
   debug = function(x) { console.error('HTTP: %s', x); };
@@ -1579,7 +1582,9 @@ function socketOnData(d, start, end) {
1579 1582
       socket.removeListener('close', socketCloseListener);
1580 1583
       socket.removeListener('error', socketErrorListener);
1581 1584
 
1582  
-      req.emit(eventName, res, socket, bodyHead);
  1585
+      socket.unshift(bodyHead);
  1586
+
  1587
+      req.emit(eventName, res, socket, emptyBuffer);
1583 1588
       req.emit('close');
1584 1589
     } else {
1585 1590
       // Got Upgrade header or CONNECT method, but have no handler.
@@ -1952,7 +1957,8 @@ function connectionListener(socket) {
1952 1957
 
1953 1958
       var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
1954 1959
       if (EventEmitter.listenerCount(self, eventName) > 0) {
1955  
-        self.emit(eventName, req, req.socket, bodyHead);
  1960
+        socket.unshift(bodyHead);
  1961
+        self.emit(eventName, req, req.socket, emptyBuffer);
1956 1962
       } else {
1957 1963
         // Got upgrade header or CONNECT method, but have no handler.
1958 1964
         socket.destroy();
4  test/simple/test-http-upgrade-agent.js
@@ -67,7 +67,7 @@ srv.listen(common.PORT, '127.0.0.1', function() {
67 67
   req.on('upgrade', function(res, socket, upgradeHead) {
68 68
     // XXX: This test isn't fantastic, as it assumes that the entire response
69 69
     //      from the server will arrive in a single data callback
70  
-    assert.equal(upgradeHead, 'nurtzo');
  70
+    assert.equal(upgradeHead, '');
71 71
 
72 72
     console.log(res.headers);
73 73
     var expectedHeaders = { 'hello': 'world',
@@ -78,6 +78,8 @@ srv.listen(common.PORT, '127.0.0.1', function() {
78 78
     // Make sure this request got removed from the pool.
79 79
     assert(!http.globalAgent.sockets.hasOwnProperty(name));
80 80
 
  81
+    assert.equal(socket.read(), 'nurtzo');
  82
+
81 83
     req.on('close', function() {
82 84
       socket.end();
83 85
       srv.close();
4  test/simple/test-http-upgrade-client.js
@@ -56,7 +56,7 @@ srv.listen(common.PORT, '127.0.0.1', function() {
56 56
   req.on('upgrade', function(res, socket, upgradeHead) {
57 57
     // XXX: This test isn't fantastic, as it assumes that the entire response
58 58
     //      from the server will arrive in a single data callback
59  
-    assert.equal(upgradeHead, 'nurtzo');
  59
+    assert.equal(upgradeHead, '');
60 60
 
61 61
     console.log(res.headers);
62 62
     var expectedHeaders = {'hello': 'world',
@@ -64,6 +64,8 @@ srv.listen(common.PORT, '127.0.0.1', function() {
64 64
                             'upgrade': 'websocket' };
65 65
     assert.deepEqual(expectedHeaders, res.headers);
66 66
 
  67
+    assert.equal(socket.read(), 'nurtzo');
  68
+
67 69
     socket.end();
68 70
     srv.close();
69 71
 
5  test/simple/test-http-upgrade-client2.js
@@ -30,6 +30,9 @@ server.on('upgrade', function(req, socket, head) {
30 30
   socket.write('HTTP/1.1 101 Ok' + CRLF +
31 31
                'Connection: Upgrade' + CRLF +
32 32
                'Upgrade: Test' + CRLF + CRLF + 'head');
  33
+  socket.on('readable', function() {
  34
+    socket.read();
  35
+  });
33 36
   socket.on('end', function() {
34 37
     socket.end();
35 38
   });
@@ -50,6 +53,7 @@ server.listen(common.PORT, function() {
50 53
       wasUpgrade = true;
51 54
 
52 55
       request.removeListener('upgrade', onUpgrade);
  56
+      socket.unref();
53 57
       socket.end();
54 58
     }
55 59
     request.on('upgrade', onUpgrade);
@@ -75,6 +79,7 @@ server.listen(common.PORT, function() {
75 79
       successCount++;
76 80
       // Test pass
77 81
       console.log('Pass!');
  82
+      server.unref();
78 83
       server.close();
79 84
     });
80 85
   });
2  test/simple/test-http-upgrade-server.js
@@ -55,7 +55,7 @@ function testServer() {
55 55
                  'Connection: Upgrade\r\n' +
56 56
                  '\r\n\r\n');
57 57
 
58  
-    request_upgradeHead = upgradeHead;
  58
+    request_upgradeHead = socket.read();
59 59
 
60 60
     socket.ondata = function(d, start, end) {
61 61
       var data = d.toString('utf8', start, end);

0 notes on commit a40133d

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