Skip to content

Commit a40133d

Browse files
edef1cisaacs
authored andcommitted
http: remove bodyHead from 'upgrade' events
Streams2 makes this unnecessary. An empty buffer is provided for compatibility.
1 parent 007e63b commit a40133d

6 files changed

Lines changed: 28 additions & 17 deletions

File tree

doc/api/http.markdown

Lines changed: 8 additions & 12 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, head) { }`
96+
`function (request, socket) { }`
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,16 +102,14 @@ 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.
107105

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

112110
### Event: 'upgrade'
113111

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

116114
Emitted each time a client requests a http upgrade. If this event isn't
117115
listened for, then clients requesting an upgrade will have their connections
@@ -120,7 +118,6 @@ closed.
120118
* `request` is the arguments for the http request, as it is in the request
121119
event.
122120
* `socket` is the network socket between the server and client.
123-
* `head` is an instance of Buffer, the first packet of the upgraded stream,
124121
this may be empty.
125122

126123
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.
595592

596593
### Event: 'connect'
597594

598-
`function (response, socket, head) { }`
595+
`function (response, socket) { }`
599596

600597
Emitted each time a server responds to a request with a CONNECT method. If this
601598
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.
612609
res.writeHead(200, {'Content-Type': 'text/plain'});
613610
res.end('okay');
614611
});
615-
proxy.on('connect', function(req, cltSocket, head) {
612+
proxy.on('connect', function(req, cltSocket) {
616613
// connect to an origin server
617614
var srvUrl = url.parse('http://' + req.url);
618615
var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, function() {
619616
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
620617
'Proxy-agent: Node-Proxy\r\n' +
621618
'\r\n');
622-
srvSocket.write(head);
623619
srvSocket.pipe(cltSocket);
624620
cltSocket.pipe(srvSocket);
625621
});
@@ -639,7 +635,7 @@ A client server pair that show you how to listen for the `connect` event.
639635
var req = http.request(options);
640636
req.end();
641637

642-
req.on('connect', function(res, socket, head) {
638+
req.on('connect', function(res, socket) {
643639
console.log('got connected!');
644640

645641
// 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.
658654

659655
### Event: 'upgrade'
660656

661-
`function (response, socket, head) { }`
657+
`function (response, socket) { }`
662658

663659
Emitted each time a server responds to a request with an upgrade. If this
664660
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.
673669
res.writeHead(200, {'Content-Type': 'text/plain'});
674670
res.end('okay');
675671
});
676-
srv.on('upgrade', function(req, socket, head) {
672+
srv.on('upgrade', function(req, socket) {
677673
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
678674
'Upgrade: WebSocket\r\n' +
679675
'Connection: Upgrade\r\n' +
@@ -698,7 +694,7 @@ A client server pair that show you how to listen for the `upgrade` event.
698694
var req = http.request(options);
699695
req.end();
700696

701-
req.on('upgrade', function(res, socket, upgradeHead) {
697+
req.on('upgrade', function(res, socket) {
702698
console.log('got upgraded!');
703699
socket.end();
704700
process.exit(0);

lib/http.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ 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+
3134
var debug;
3235
if (process.env.NODE_DEBUG && /http/.test(process.env.NODE_DEBUG)) {
3336
debug = function(x) { console.error('HTTP: %s', x); };
@@ -1579,7 +1582,9 @@ function socketOnData(d, start, end) {
15791582
socket.removeListener('close', socketCloseListener);
15801583
socket.removeListener('error', socketErrorListener);
15811584

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

19531958
var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
19541959
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);
19561962
} else {
19571963
// Got upgrade header or CONNECT method, but have no handler.
19581964
socket.destroy();

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

Lines changed: 3 additions & 1 deletion
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, 'nurtzo');
70+
assert.equal(upgradeHead, '');
7171

7272
console.log(res.headers);
7373
var expectedHeaders = { 'hello': 'world',
@@ -78,6 +78,8 @@ 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+
8183
req.on('close', function() {
8284
socket.end();
8385
srv.close();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ 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, 'nurtzo');
59+
assert.equal(upgradeHead, '');
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+
6769
socket.end();
6870
srv.close();
6971

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ 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+
});
3336
socket.on('end', function() {
3437
socket.end();
3538
});
@@ -50,6 +53,7 @@ server.listen(common.PORT, function() {
5053
wasUpgrade = true;
5154

5255
request.removeListener('upgrade', onUpgrade);
56+
socket.unref();
5357
socket.end();
5458
}
5559
request.on('upgrade', onUpgrade);
@@ -75,6 +79,7 @@ server.listen(common.PORT, function() {
7579
successCount++;
7680
// Test pass
7781
console.log('Pass!');
82+
server.unref();
7883
server.close();
7984
});
8085
});

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 = upgradeHead;
58+
request_upgradeHead = socket.read();
5959

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

0 commit comments

Comments
 (0)