Skip to content

Commit

Permalink
fixed OpenROV/openrov-software#402; parallel ssl connections now work
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianAdams committed May 14, 2015
1 parent 6229b43 commit 7b3d30c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
32 changes: 28 additions & 4 deletions middleware/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var app = express();
var server = http.createServer(app);
var bs = BinaryServer({ port: 3011 });
var io = require('socket.io')(server);
var connectionNumber = 0;
http.globalAgent.maxSockets=20;

bs.on('connection', function (client) {
console.log('Connection to client');
// Incoming stream from browsers
Expand Down Expand Up @@ -100,23 +103,44 @@ function handleHttp(requestData, stream) {
});
}
function handleSsl(requestData, stream) {

var conn_num = connectionNumber;
connectionNumber++;
console.log(conn_num+ ':Stream requested, url: ' + requestData.url);
var requestUrl = requestData.url;
var srvUrl = url.parse(requestUrl);
var thestream = stream;
//console.dir(stream);
var d = new Date();
var client = net.connect({
port: srvUrl.port,
host: srvUrl.hostname
}, function () {
stream.write('HTTP/1.1 200 Connection Established\r\n' + 'Proxy-agent: Node-Proxy\r\n' + '\r\n');
var connectiondelay= (new Date() - d);
console.log(conn_num + ":net Connection delay:" + connectiondelay);
thestream.write('HTTP/1.1 200 Connection Established\r\n' + 'Proxy-agent: Node-Proxy\r\n' + '\r\n');
thestream.pipe(client);
client.pipe(thestream);
});
client.on('end', function () {
stream.end();
thestream.end();
console.log (conn_num+ ":done... " + requestData.url + ' ' + (new Date() - d) + 'ms');
});
client.on('close', function() {
thestream.end();
console.log (conn_num + ":close recieved " + requestData.url + ' ' + (new Date() - d) + 'ms');
})
client.on('error', function(error){
stream.end();
console.log('SSL Net error:');
thestream.end();
console.log(conn_num + ':SSL Net error:');
console.dir(error);
});
thestream.on('close', function(){
console.log(conn_num + ':BinaryJS stream hung up.');
client.end();
});
thestream.on('end', function(){
console.log(conn_num + ':BinaryJS stream hung up.');
client.end();
});
}
12 changes: 12 additions & 0 deletions proxy-via-browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ function proxyReq(req, res, head) {
stream.pipe(res);
req.socket.pipe(stream);
}
req.on('close', function () {
console.log("detected requester closed socket");
stream.close();
});
req.on('end', function () {
console.log("detected requester ended socket");
stream.end();
});
stream.on('data', function (data) {
if (!headers_set) {
if (data == '\r\n' || data == '\n') {
Expand Down Expand Up @@ -136,6 +144,10 @@ function proxyReq(req, res, head) {
// When the proxy tells us everything was sent, we end the response to the client.
res.end();
});
stream.on('close', function () {
// When the proxy tells us everything was sent, we end the response to the client.
res.end();
});
} else {
console.log('No client connected!');
res.statusCode = 500;
Expand Down
13 changes: 12 additions & 1 deletion proxy-via-browser/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
if (proxy.client) {
// create a stream to the proxy on the server
// this will trigger the download of the requested file
proxyStream = proxy.client.createStream(url);
var proxyStream = proxy.client.createStream(url);
// we pipe the incoming data to the rovStream

proxyStream.on('data', function(data) {
Expand All @@ -64,12 +64,23 @@
proxyStream.write(data);
console.log('@');
});
rovStream.on('close', function(){
proxyStream.close();
console.log('closing stream to proxy');
});
rovStream.on('error', function(err){
proxyStream.close();
rovStream.destory();
console.log('closing stream to proxy due to rovstream error');
console.dir(err);
});

// when the server is done downloading it will end the stream
proxyStream.on('end', function() {
$('#messages').append('<li>End of stream ' + url + '</li>');
// end the stream to the rov. This will cause the proxy to end the response
rovStream.end();
rovStream.destroy();
});
}
});
Expand Down

0 comments on commit 7b3d30c

Please sign in to comment.