Skip to content

Commit bab02e9

Browse files
Tiggeindexzero
authored andcommitted
Include websocket non-upgrade response
When the server do not accept the upgrade request for websockets the server's response was previously not included and sent back. Now the proxy will include the response in these cases. Fixes #890.
1 parent c9a556c commit bab02e9

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

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

+23-17
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ module.exports = {
7777
* @api private
7878
*/
7979
stream : function stream(req, socket, options, head, server, clb) {
80+
81+
var createHttpHeader = function(line, headers) {
82+
return Object.keys(headers).reduce(function (head, key) {
83+
var value = headers[key];
84+
85+
if (!Array.isArray(value)) {
86+
head.push(key + ': ' + value);
87+
return head;
88+
}
89+
90+
for (var i = 0; i < value.length; i++) {
91+
head.push(key + ': ' + value[i]);
92+
}
93+
return head;
94+
}, [line])
95+
.join('\r\n') + '\r\n\r\n';
96+
}
97+
8098
common.setupSocket(socket);
8199

82100
if (head && head.length) socket.unshift(head);
@@ -93,7 +111,10 @@ module.exports = {
93111
proxyReq.on('error', onOutgoingError);
94112
proxyReq.on('response', function (res) {
95113
// if upgrade event isn't going to happen, close the socket
96-
if (!res.upgrade) socket.end();
114+
if (!res.upgrade) {
115+
socket.write(createHttpHeader('HTTP/' + res.httpVersion + ' ' + res.statusCode + ' ' + res.statusMessage, res.headers));
116+
res.pipe(socket);
117+
}
97118
});
98119

99120
proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
@@ -119,22 +140,7 @@ module.exports = {
119140
// Remark: Handle writing the headers to the socket when switching protocols
120141
// Also handles when a header is an array
121142
//
122-
socket.write(
123-
Object.keys(proxyRes.headers).reduce(function (head, key) {
124-
var value = proxyRes.headers[key];
125-
126-
if (!Array.isArray(value)) {
127-
head.push(key + ': ' + value);
128-
return head;
129-
}
130-
131-
for (var i = 0; i < value.length; i++) {
132-
head.push(key + ': ' + value[i]);
133-
}
134-
return head;
135-
}, ['HTTP/1.1 101 Switching Protocols'])
136-
.join('\r\n') + '\r\n\r\n'
137-
);
143+
socket.write(createHttpHeader('HTTP/1.1 101 Switching Protocols', proxyRes.headers));
138144

139145
proxySocket.pipe(socket).pipe(proxySocket);
140146

Diff for: test/lib-http-proxy-test.js

-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,6 @@ describe('lib/http-proxy.js', function() {
415415

416416
client.on('error', function (err) {
417417
expect(err).to.be.an(Error);
418-
expect(err.code).to.be('ECONNRESET');
419418
proxyServer.close();
420419
done();
421420
});

0 commit comments

Comments
 (0)