Skip to content

Commit e850027

Browse files
committed
Revert "http: remove bodyHead from 'upgrade' events"
This reverts commit a40133d. 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
1 parent 4847627 commit e850027

6 files changed

Lines changed: 18 additions & 28 deletions

File tree

doc/api/http.markdown

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ not be emitted.
9393

9494
### Event: 'connect'
9595

96-
`function (request, socket) { }`
96+
`function (request, socket, head) { }`
9797

9898
Emitted each time a client requests a http CONNECT method. If this event isn't
9999
listened for, then clients requesting a CONNECT method will have their
@@ -102,14 +102,16 @@ connections closed.
102102
* `request` is the arguments for the http request, as it is in the request
103103
event.
104104
* `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.
105107

106108
After this event is emitted, the request's socket will not have a `data`
107109
event listener, meaning you will need to bind to it in order to handle data
108110
sent to the server on that socket.
109111

110112
### Event: 'upgrade'
111113

112-
`function (request, socket) { }`
114+
`function (request, socket, head) { }`
113115

114116
Emitted each time a client requests a http upgrade. If this event isn't
115117
listened for, then clients requesting an upgrade will have their connections
@@ -118,6 +120,8 @@ closed.
118120
* `request` is the arguments for the http request, as it is in the request
119121
event.
120122
* `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+
this may be empty.
121125

122126
After this event is emitted, the request's socket will not have a `data`
123127
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.
591595

592596
### Event: 'connect'
593597

594-
`function (response, socket) { }`
598+
`function (response, socket, head) { }`
595599

596600
Emitted each time a server responds to a request with a CONNECT method. If this
597601
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.
608612
res.writeHead(200, {'Content-Type': 'text/plain'});
609613
res.end('okay');
610614
});
611-
proxy.on('connect', function(req, cltSocket) {
615+
proxy.on('connect', function(req, cltSocket, head) {
612616
// connect to an origin server
613617
var srvUrl = url.parse('http://' + req.url);
614618
var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, function() {
615619
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
616620
'Proxy-agent: Node-Proxy\r\n' +
617621
'\r\n');
622+
srvSocket.write(head);
618623
srvSocket.pipe(cltSocket);
619624
cltSocket.pipe(srvSocket);
620625
});
@@ -634,7 +639,7 @@ A client server pair that show you how to listen for the `connect` event.
634639
var req = http.request(options);
635640
req.end();
636641

637-
req.on('connect', function(res, socket) {
642+
req.on('connect', function(res, socket, head) {
638643
console.log('got connected!');
639644

640645
// 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.
653658

654659
### Event: 'upgrade'
655660

656-
`function (response, socket) { }`
661+
`function (response, socket, head) { }`
657662

658663
Emitted each time a server responds to a request with an upgrade. If this
659664
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.
668673
res.writeHead(200, {'Content-Type': 'text/plain'});
669674
res.end('okay');
670675
});
671-
srv.on('upgrade', function(req, socket) {
676+
srv.on('upgrade', function(req, socket, head) {
672677
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
673678
'Upgrade: WebSocket\r\n' +
674679
'Connection: Upgrade\r\n' +
@@ -693,7 +698,7 @@ A client server pair that show you how to listen for the `upgrade` event.
693698
var req = http.request(options);
694699
req.end();
695700

696-
req.on('upgrade', function(res, socket) {
701+
req.on('upgrade', function(res, socket, upgradeHead) {
697702
console.log('got upgraded!');
698703
socket.end();
699704
process.exit(0);

lib/http.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ var FreeList = require('freelist').FreeList;
2828
var HTTPParser = process.binding('http_parser').HTTPParser;
2929
var assert = require('assert').ok;
3030

31-
// an empty buffer for UPGRADE/CONNECT bodyHead compatibility
32-
var emptyBuffer = new Buffer(0);
33-
3431
var debug;
3532
if (process.env.NODE_DEBUG && /http/.test(process.env.NODE_DEBUG)) {
3633
debug = function(x) { console.error('HTTP: %s', x); };
@@ -1582,9 +1579,7 @@ function socketOnData(d, start, end) {
15821579
socket.removeListener('close', socketCloseListener);
15831580
socket.removeListener('error', socketErrorListener);
15841581

1585-
socket.unshift(bodyHead);
1586-
1587-
req.emit(eventName, res, socket, emptyBuffer);
1582+
req.emit(eventName, res, socket, bodyHead);
15881583
req.emit('close');
15891584
} else {
15901585
// Got Upgrade header or CONNECT method, but have no handler.
@@ -1957,8 +1952,7 @@ function connectionListener(socket) {
19571952

19581953
var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
19591954
if (EventEmitter.listenerCount(self, eventName) > 0) {
1960-
socket.unshift(bodyHead);
1961-
self.emit(eventName, req, req.socket, emptyBuffer);
1955+
self.emit(eventName, req, req.socket, bodyHead);
19621956
} else {
19631957
// Got upgrade header or CONNECT method, but have no handler.
19641958
socket.destroy();

test/simple/test-http-upgrade-agent.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ srv.listen(common.PORT, '127.0.0.1', function() {
6767
req.on('upgrade', function(res, socket, upgradeHead) {
6868
// XXX: This test isn't fantastic, as it assumes that the entire response
6969
// from the server will arrive in a single data callback
70-
assert.equal(upgradeHead, '');
70+
assert.equal(upgradeHead, 'nurtzo');
7171

7272
console.log(res.headers);
7373
var expectedHeaders = { 'hello': 'world',
@@ -78,8 +78,6 @@ srv.listen(common.PORT, '127.0.0.1', function() {
7878
// Make sure this request got removed from the pool.
7979
assert(!http.globalAgent.sockets.hasOwnProperty(name));
8080

81-
assert.equal(socket.read(), 'nurtzo');
82-
8381
req.on('close', function() {
8482
socket.end();
8583
srv.close();

test/simple/test-http-upgrade-client.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,14 @@ srv.listen(common.PORT, '127.0.0.1', function() {
5656
req.on('upgrade', function(res, socket, upgradeHead) {
5757
// XXX: This test isn't fantastic, as it assumes that the entire response
5858
// from the server will arrive in a single data callback
59-
assert.equal(upgradeHead, '');
59+
assert.equal(upgradeHead, 'nurtzo');
6060

6161
console.log(res.headers);
6262
var expectedHeaders = {'hello': 'world',
6363
'connection': 'upgrade',
6464
'upgrade': 'websocket' };
6565
assert.deepEqual(expectedHeaders, res.headers);
6666

67-
assert.equal(socket.read(), 'nurtzo');
68-
6967
socket.end();
7068
srv.close();
7169

test/simple/test-http-upgrade-client2.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ server.on('upgrade', function(req, socket, head) {
3030
socket.write('HTTP/1.1 101 Ok' + CRLF +
3131
'Connection: Upgrade' + CRLF +
3232
'Upgrade: Test' + CRLF + CRLF + 'head');
33-
socket.on('readable', function() {
34-
socket.read();
35-
});
3633
socket.on('end', function() {
3734
socket.end();
3835
});
@@ -53,7 +50,6 @@ server.listen(common.PORT, function() {
5350
wasUpgrade = true;
5451

5552
request.removeListener('upgrade', onUpgrade);
56-
socket.unref();
5753
socket.end();
5854
}
5955
request.on('upgrade', onUpgrade);
@@ -79,7 +75,6 @@ server.listen(common.PORT, function() {
7975
successCount++;
8076
// Test pass
8177
console.log('Pass!');
82-
server.unref();
8378
server.close();
8479
});
8580
});

test/simple/test-http-upgrade-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function testServer() {
5555
'Connection: Upgrade\r\n' +
5656
'\r\n\r\n');
5757

58-
request_upgradeHead = socket.read();
58+
request_upgradeHead = upgradeHead;
5959

6060
socket.ondata = function(d, start, end) {
6161
var data = d.toString('utf8', start, end);

0 commit comments

Comments
 (0)