From e8500274e04bab2eb630b4000cbd35be8d23d3fd Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 12 Jun 2013 17:45:30 -0700 Subject: [PATCH] Revert "http: remove bodyHead from 'upgrade' events" This reverts commit a40133d10cdb911b27fe8d46d67a835b0103bbf1. Unfortunately, this breaks socket.io. Even though it's not strictly an API change, it is too subtle and in too brittle an area of node, to be done in a stable branch. Conflicts: doc/api/http.markdown --- doc/api/http.markdown | 21 +++++++++++++-------- lib/http.js | 10 ++-------- test/simple/test-http-upgrade-agent.js | 4 +--- test/simple/test-http-upgrade-client.js | 4 +--- test/simple/test-http-upgrade-client2.js | 5 ----- test/simple/test-http-upgrade-server.js | 2 +- 6 files changed, 18 insertions(+), 28 deletions(-) diff --git a/doc/api/http.markdown b/doc/api/http.markdown index 438c7a326ff..e93aa2d2ddf 100644 --- a/doc/api/http.markdown +++ b/doc/api/http.markdown @@ -93,7 +93,7 @@ not be emitted. ### Event: 'connect' -`function (request, socket) { }` +`function (request, socket, head) { }` Emitted each time a client requests a http CONNECT method. If this event isn't listened for, then clients requesting a CONNECT method will have their @@ -102,6 +102,8 @@ connections closed. * `request` is the arguments for the http request, as it is in the request event. * `socket` is the network socket between the server and client. +* `head` is an instance of Buffer, the first packet of the tunneling stream, + this may be empty. After this event is emitted, the request's socket will not have a `data` event listener, meaning you will need to bind to it in order to handle data @@ -109,7 +111,7 @@ sent to the server on that socket. ### Event: 'upgrade' -`function (request, socket) { }` +`function (request, socket, head) { }` Emitted each time a client requests a http upgrade. If this event isn't listened for, then clients requesting an upgrade will have their connections @@ -118,6 +120,8 @@ closed. * `request` is the arguments for the http request, as it is in the request event. * `socket` is the network socket between the server and client. +* `head` is an instance of Buffer, the first packet of the upgraded stream, + this may be empty. After this event is emitted, the request's socket will not have a `data` event listener, meaning you will need to bind to it in order to handle data @@ -591,7 +595,7 @@ Emitted after a socket is assigned to this request. ### Event: 'connect' -`function (response, socket) { }` +`function (response, socket, head) { }` Emitted each time a server responds to a request with a CONNECT method. If this event isn't being listened for, clients receiving a CONNECT method will have @@ -608,13 +612,14 @@ A client server pair that show you how to listen for the `connect` event. res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('okay'); }); - proxy.on('connect', function(req, cltSocket) { + proxy.on('connect', function(req, cltSocket, head) { // connect to an origin server var srvUrl = url.parse('http://' + req.url); var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, function() { cltSocket.write('HTTP/1.1 200 Connection Established\r\n' + 'Proxy-agent: Node-Proxy\r\n' + '\r\n'); + srvSocket.write(head); srvSocket.pipe(cltSocket); cltSocket.pipe(srvSocket); }); @@ -634,7 +639,7 @@ A client server pair that show you how to listen for the `connect` event. var req = http.request(options); req.end(); - req.on('connect', function(res, socket) { + req.on('connect', function(res, socket, head) { console.log('got connected!'); // make a request over an HTTP tunnel @@ -653,7 +658,7 @@ A client server pair that show you how to listen for the `connect` event. ### Event: 'upgrade' -`function (response, socket) { }` +`function (response, socket, head) { }` Emitted each time a server responds to a request with an upgrade. If this event isn't being listened for, clients receiving an upgrade header will have @@ -668,7 +673,7 @@ A client server pair that show you how to listen for the `upgrade` event. res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('okay'); }); - srv.on('upgrade', function(req, socket) { + srv.on('upgrade', function(req, socket, head) { socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' + 'Upgrade: WebSocket\r\n' + 'Connection: Upgrade\r\n' + @@ -693,7 +698,7 @@ A client server pair that show you how to listen for the `upgrade` event. var req = http.request(options); req.end(); - req.on('upgrade', function(res, socket) { + req.on('upgrade', function(res, socket, upgradeHead) { console.log('got upgraded!'); socket.end(); process.exit(0); diff --git a/lib/http.js b/lib/http.js index 3c3a56c50b0..ab36cd74914 100644 --- a/lib/http.js +++ b/lib/http.js @@ -28,9 +28,6 @@ var FreeList = require('freelist').FreeList; var HTTPParser = process.binding('http_parser').HTTPParser; var assert = require('assert').ok; -// an empty buffer for UPGRADE/CONNECT bodyHead compatibility -var emptyBuffer = new Buffer(0); - var debug; if (process.env.NODE_DEBUG && /http/.test(process.env.NODE_DEBUG)) { debug = function(x) { console.error('HTTP: %s', x); }; @@ -1582,9 +1579,7 @@ function socketOnData(d, start, end) { socket.removeListener('close', socketCloseListener); socket.removeListener('error', socketErrorListener); - socket.unshift(bodyHead); - - req.emit(eventName, res, socket, emptyBuffer); + req.emit(eventName, res, socket, bodyHead); req.emit('close'); } else { // Got Upgrade header or CONNECT method, but have no handler. @@ -1957,8 +1952,7 @@ function connectionListener(socket) { var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade'; if (EventEmitter.listenerCount(self, eventName) > 0) { - socket.unshift(bodyHead); - self.emit(eventName, req, req.socket, emptyBuffer); + self.emit(eventName, req, req.socket, bodyHead); } else { // Got upgrade header or CONNECT method, but have no handler. socket.destroy(); diff --git a/test/simple/test-http-upgrade-agent.js b/test/simple/test-http-upgrade-agent.js index ff8e5405564..1077a983dcb 100644 --- a/test/simple/test-http-upgrade-agent.js +++ b/test/simple/test-http-upgrade-agent.js @@ -67,7 +67,7 @@ srv.listen(common.PORT, '127.0.0.1', function() { req.on('upgrade', function(res, socket, upgradeHead) { // XXX: This test isn't fantastic, as it assumes that the entire response // from the server will arrive in a single data callback - assert.equal(upgradeHead, ''); + assert.equal(upgradeHead, 'nurtzo'); console.log(res.headers); var expectedHeaders = { 'hello': 'world', @@ -78,8 +78,6 @@ srv.listen(common.PORT, '127.0.0.1', function() { // Make sure this request got removed from the pool. assert(!http.globalAgent.sockets.hasOwnProperty(name)); - assert.equal(socket.read(), 'nurtzo'); - req.on('close', function() { socket.end(); srv.close(); diff --git a/test/simple/test-http-upgrade-client.js b/test/simple/test-http-upgrade-client.js index 9361e0b503f..3bf5beccf5b 100644 --- a/test/simple/test-http-upgrade-client.js +++ b/test/simple/test-http-upgrade-client.js @@ -56,7 +56,7 @@ srv.listen(common.PORT, '127.0.0.1', function() { req.on('upgrade', function(res, socket, upgradeHead) { // XXX: This test isn't fantastic, as it assumes that the entire response // from the server will arrive in a single data callback - assert.equal(upgradeHead, ''); + assert.equal(upgradeHead, 'nurtzo'); console.log(res.headers); var expectedHeaders = {'hello': 'world', @@ -64,8 +64,6 @@ srv.listen(common.PORT, '127.0.0.1', function() { 'upgrade': 'websocket' }; assert.deepEqual(expectedHeaders, res.headers); - assert.equal(socket.read(), 'nurtzo'); - socket.end(); srv.close(); diff --git a/test/simple/test-http-upgrade-client2.js b/test/simple/test-http-upgrade-client2.js index 53f15d29392..fa39f2a572c 100644 --- a/test/simple/test-http-upgrade-client2.js +++ b/test/simple/test-http-upgrade-client2.js @@ -30,9 +30,6 @@ server.on('upgrade', function(req, socket, head) { socket.write('HTTP/1.1 101 Ok' + CRLF + 'Connection: Upgrade' + CRLF + 'Upgrade: Test' + CRLF + CRLF + 'head'); - socket.on('readable', function() { - socket.read(); - }); socket.on('end', function() { socket.end(); }); @@ -53,7 +50,6 @@ server.listen(common.PORT, function() { wasUpgrade = true; request.removeListener('upgrade', onUpgrade); - socket.unref(); socket.end(); } request.on('upgrade', onUpgrade); @@ -79,7 +75,6 @@ server.listen(common.PORT, function() { successCount++; // Test pass console.log('Pass!'); - server.unref(); server.close(); }); }); diff --git a/test/simple/test-http-upgrade-server.js b/test/simple/test-http-upgrade-server.js index 88b83ca85e8..6a14d39aa5b 100644 --- a/test/simple/test-http-upgrade-server.js +++ b/test/simple/test-http-upgrade-server.js @@ -55,7 +55,7 @@ function testServer() { 'Connection: Upgrade\r\n' + '\r\n\r\n'); - request_upgradeHead = socket.read(); + request_upgradeHead = upgradeHead; socket.ondata = function(d, start, end) { var data = d.toString('utf8', start, end);