From cea0e8676b3e609828320bb03051eaf78cc43b54 Mon Sep 17 00:00:00 2001 From: Jarrett Cruger Date: Sun, 30 Aug 2015 17:28:05 -0400 Subject: [PATCH] [fix] make more functional --- lib/http-proxy/passes/ws-incoming.js | 34 +++++++++++++++------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/http-proxy/passes/ws-incoming.js b/lib/http-proxy/passes/ws-incoming.js index 1d57b78..f517857 100644 --- a/lib/http-proxy/passes/ws-incoming.js +++ b/lib/http-proxy/passes/ws-incoming.js @@ -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);