Skip to content

Commit a05fc2d

Browse files
author
Laurent Brucher
committed
Provide a "proxyReq" event also for websocket connections.
1 parent 60baca5 commit a05fc2d

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ http.createServer(function (req, res) {
198198
#### Listening for proxy events
199199

200200
* `error`: The error event is emitted if the request to the target fail.
201+
* `proxyReq`: This event is emitted before the data is sent. It gives you a chance to alter the proxyReq request object. Applies to "web" connections
202+
* `proxyReqWs`: This event is emitted before the data is sent. It gives you a chance to alter the proxyReq request object. Applies to "websocket" connections
201203
* `proxyRes`: This event is emitted if the request to the target got a response.
202204
* `open`: This event is emitted once the proxy websocket was created and piped into the target websocket.
203205
* `close`: This event is emitted once the proxy websocket was closed.

lib/http-proxy/passes/ws-incoming.js

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ var passes = exports;
8787
var proxyReq = (common.isSSL.test(options.target.protocol) ? https : http).request(
8888
common.setupOutgoing(options.ssl || {}, options, req)
8989
);
90+
91+
// Enable developers to modify the proxyReq before headers are sent
92+
if(server) { server.emit('proxyReqWs', proxyReq, req, socket, options, head); }
93+
9094
// Error Handler
9195
proxyReq.on('error', onOutgoingError);
9296
proxyReq.on('response', function (res) {

test/lib-http-proxy-test.js

+43
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,49 @@ describe('lib/http-proxy.js', function() {
444444
headers.push('Set-Cookie: test2=test2');
445445
});
446446
});
447+
448+
it('should detect a proxyReq event and modify headers', function (done) {
449+
var ports = { source: gen.port, proxy: gen.port },
450+
proxy,
451+
proxyServer,
452+
destiny;
453+
454+
proxy = httpProxy.createProxyServer({
455+
target: 'ws://127.0.0.1:' + ports.source,
456+
ws: true
457+
});
458+
459+
proxy.on('proxyReqWs', function(proxyReq, req, res, options) {
460+
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
461+
});
462+
463+
proxyServer = proxy.listen(ports.proxy);
464+
465+
destiny = new ws.Server({ port: ports.source }, function () {
466+
var client = new ws('ws://127.0.0.1:' + ports.proxy);
467+
468+
client.on('open', function () {
469+
client.send('hello there');
470+
});
471+
472+
client.on('message', function (msg) {
473+
expect(msg).to.be('Hello over websockets');
474+
client.close();
475+
proxyServer.close();
476+
destiny.close();
477+
done();
478+
});
479+
});
480+
481+
destiny.on('connection', function (socket) {
482+
expect(socket.upgradeReq.headers['x-special-proxy-header']).to.eql('foobar');
483+
484+
socket.on('message', function (msg) {
485+
expect(msg).to.be('hello there');
486+
socket.send('Hello over websockets');
487+
});
488+
});
489+
});
447490

448491
})
449492
});

0 commit comments

Comments
 (0)