Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Response object socket & connection properties fix #878

Closed
wants to merge 6 commits into
from
View
@@ -376,19 +376,25 @@ OutgoingMessage.prototype._writeRaw = function(data, encoding) {
if (this.connection &&
this.connection._httpMessage === this &&
this.connection.writable) {
+
// There might be pending data in the this.output buffer.
while (this.output.length) {
+
+ var c = this.output.shift();
+ var e = this.outputEncodings.shift();
+ this.connection.write(c, e);
+
if (!this.connection.writable) {
this._buffer(data, encoding);
return false;
}
- var c = this.output.shift();
- var e = this.outputEncodings.shift();
- this.connection.write(c, e);
+
}
// Directly write to socket.
return this.connection.write(data, encoding);
+
+
} else {
this._buffer(data, encoding);
return false;
@@ -1072,10 +1078,15 @@ function connectionListener(socket) {
if (socket._httpMessage) {
// There are already pending outgoing res, append.
outgoing.push(res);
+
+ res.socket = socket;
+ res.connection = socket;
} else {
res.assignSocket(socket);
}
+
+
// When we're finished writing the response, check if this is the last
// respose, if so destroy the socket.
res.on('finish', function() {
@@ -7,6 +7,7 @@ var server = net.createServer();
//test unix sockets
var fds = process.binding('net').socketpair();
var unixsocket = new net.Socket(fds[0]);
+
assert.ok(unixsocket.type == 'unix', 'Should be UNIX');
//test that stdin is default file
@@ -0,0 +1,52 @@
+var net = require('net'), http = require('http'), assert = require('assert');
+var gotError = false;
+var count = 0;
+
+var server = http.createServer(function(req, res) {
+ var id = ++count;
+ assert.equal(typeof res.socket, 'object');
+ assert.equal(typeof res.connection, 'object');
+ setTimeout(function() {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end('id: ' + id);
+ }, (1 - count) * 1000);
+});
+
+server.on('clientError', function(e) {
+ gotError = true;
+});
+
+server.listen(8080, '127.0.0.1', function() {
+ var socket = net.createConnection(8080, '127.0.0.1');
+ socket.setEncoding('utf8');
+ socket.on('connect', function() {
+ socket.write('GET / HTTP/1.1\r\n');
+ socket.write('Host: localhost\r\n\r\n');
+ socket.flush();
+ socket.write('GET / HTTP/1.1\r\n');
+ socket.write('Host: localhost\r\n\r\n');
+ socket.flush();
+
+ var buf = '';
+ socket.on('data', function(data) {
+ buf += data;
+ });
+ socket.on('end', function() {
+ server.close();
+ var lines = buf.split('\r\n').filter(function(s) {
+ return s.indexOf('id: ') === 0;
+ });
+ assert.equal(lines.length, 2);
+ assert.equal(lines[0], 'id: 1');
+ assert.equal(lines[1], 'id: 2');
+ });
+
+ setTimeout(function() {
+ socket.end();
+ }, 1000);
+ });
+});
+
+process.on('exit', function() {
+ assert.ok(!gotError);
+});