Skip to content

Commit

Permalink
Provide a "proxyReq" event also for websocket connections.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent Brucher committed Oct 22, 2015
1 parent 60baca5 commit a05fc2d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ http.createServer(function (req, res) {
#### Listening for proxy events

* `error`: The error event is emitted if the request to the target fail.
* `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
* `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
* `proxyRes`: This event is emitted if the request to the target got a response.
* `open`: This event is emitted once the proxy websocket was created and piped into the target websocket.
* `close`: This event is emitted once the proxy websocket was closed.
Expand Down
4 changes: 4 additions & 0 deletions lib/http-proxy/passes/ws-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ var passes = exports;
var proxyReq = (common.isSSL.test(options.target.protocol) ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req)
);

// Enable developers to modify the proxyReq before headers are sent
if(server) { server.emit('proxyReqWs', proxyReq, req, socket, options, head); }

// Error Handler
proxyReq.on('error', onOutgoingError);
proxyReq.on('response', function (res) {
Expand Down
43 changes: 43 additions & 0 deletions test/lib-http-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,49 @@ describe('lib/http-proxy.js', function() {
headers.push('Set-Cookie: test2=test2');
});
});

it('should detect a proxyReq event and modify headers', function (done) {
var ports = { source: gen.port, proxy: gen.port },
proxy,
proxyServer,
destiny;

proxy = httpProxy.createProxyServer({
target: 'ws:https://127.0.0.1:' + ports.source,
ws: true
});

proxy.on('proxyReqWs', function(proxyReq, req, res, options) {
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});

proxyServer = proxy.listen(ports.proxy);

destiny = new ws.Server({ port: ports.source }, function () {
var client = new ws('ws:https://127.0.0.1:' + ports.proxy);

client.on('open', function () {
client.send('hello there');
});

client.on('message', function (msg) {
expect(msg).to.be('Hello over websockets');
client.close();
proxyServer.close();
destiny.close();
done();
});
});

destiny.on('connection', function (socket) {
expect(socket.upgradeReq.headers['x-special-proxy-header']).to.eql('foobar');

socket.on('message', function (msg) {
expect(msg).to.be('hello there');
socket.send('Hello over websockets');
});
});
});

})
});

0 comments on commit a05fc2d

Please sign in to comment.