Skip to content

Commit cea0e86

Browse files
committed
[fix] make more functional
1 parent 3d2350c commit cea0e86

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

Diff for: lib/http-proxy/passes/ws-incoming.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var http = require('http'),
22
https = require('https'),
3-
util = require('util'),
43
common = require('../common'),
54
passes = exports;
65

@@ -114,24 +113,27 @@ var passes = exports;
114113

115114
if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
116115

117-
var writeHead = [
118-
'HTTP/1.1 101 Switching Protocols'
119-
];
120-
121-
for(var i in proxyRes.headers) {
122-
if (util.isArray(proxyRes.headers[i])) {
123-
var a = proxyRes.headers[i];
124-
var len = a.length;
125-
126-
for (var x = 0; x < len; x++) {
127-
writeHead.push(i + ": " + a[x]);
128-
}
129-
} else {
130-
writeHead.push(i + ": " + proxyRes.headers[i]);
131-
}
132-
}
116+
//
117+
// Remark: Handle writing the headers to the socket when switching protocols
118+
// Also handles when a header is an array
119+
//
120+
socket.write(
121+
Object.keys(proxyRes.headers).reduce(function (head, key) {
122+
var value = proxyRes.headers[key];
123+
124+
if (!Array.isArray(value)) {
125+
head.push(key + ': ' + value);
126+
return head;
127+
}
128+
129+
for (var i = 0; i < value.length; i++) {
130+
head.push(key + ': ' + value[i]);
131+
}
132+
return head;
133+
}, ['HTTP/1.1 101 Switching Protocols'])
134+
.join('\r\n') + '\r\n\r\n'
135+
);
133136

134-
socket.write(writeHead.join('\r\n') + '\r\n\r\n');
135137
proxySocket.pipe(socket).pipe(proxySocket);
136138

137139
server.emit('open', proxySocket);

0 commit comments

Comments
 (0)