[fix] make more functional

This commit is contained in:
Jarrett Cruger 2015-08-30 17:28:05 -04:00
parent 3d2350c54f
commit cea0e8676b

View File

@ -1,6 +1,5 @@
var http = require('http'),
https = require('https'),
util = require('util'),
common = require('../common'),
passes = exports;
@ -114,24 +113,27 @@ var passes = exports;
if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
var writeHead = [
'HTTP/1.1 101 Switching Protocols'
];
//
// Remark: Handle writing the headers to the socket when switching protocols
// Also handles when a header is an array
//
socket.write(
Object.keys(proxyRes.headers).reduce(function (head, key) {
var value = proxyRes.headers[key];
for(var i in proxyRes.headers) {
if (util.isArray(proxyRes.headers[i])) {
var a = proxyRes.headers[i];
var len = a.length;
if (!Array.isArray(value)) {
head.push(key + ': ' + value);
return head;
}
for (var x = 0; x < len; x++) {
writeHead.push(i + ": " + a[x]);
}
} else {
writeHead.push(i + ": " + proxyRes.headers[i]);
}
}
for (var i = 0; i < value.length; i++) {
head.push(key + ': ' + value[i]);
}
return head;
}, ['HTTP/1.1 101 Switching Protocols'])
.join('\r\n') + '\r\n\r\n'
);
socket.write(writeHead.join('\r\n') + '\r\n\r\n');
proxySocket.pipe(socket).pipe(proxySocket);
server.emit('open', proxySocket);