@@ -235,11 +235,22 @@ var HttpProxy = exports.HttpProxy = function (options) {
235
235
236
236
var self = this ;
237
237
options = options || { } ;
238
- options . target = options . target || { } ;
239
238
239
+ //
240
+ // Setup basic proxying options
241
+ //
240
242
this . https = options . https ;
241
243
this . forward = options . forward ;
242
- this . target = options . target ;
244
+ this . target = options . target || { } ;
245
+
246
+ //
247
+ // Setup additional options for WebSocket proxying. When forcing
248
+ // the WebSocket handshake to change the `sec-websocket-location`
249
+ // and `sec-websocket-origin` headers `options.source` **MUST**
250
+ // be provided or the operation will fail with an `origin mismatch`
251
+ // by definition.
252
+ //
253
+ this . source = options . source || { host : 'localhost' , port : 8000 } ;
243
254
this . changeOrigin = options . changeOrigin || false ;
244
255
245
256
if ( options . router ) {
@@ -684,12 +695,18 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
684
695
// Setup the incoming client socket.
685
696
_socket ( socket ) ;
686
697
698
+ function getPort ( port ) {
699
+ port = port || 80 ;
700
+ return port - 80 === 0 ? '' : ':' + port
701
+ }
702
+
687
703
//
688
704
// Get the protocol, and host for this request and create an instance
689
705
// of `http.Agent` or `https.Agent` from the pool managed by `node-http-proxy`.
690
706
//
691
707
var protocolName = options . https || this . target . https ? 'https' : 'http' ,
692
- remoteHost = options . host + ( options . port - 80 === 0 ? '' : ':' + options . port ) ,
708
+ portUri = getPort ( this . source . port ) ,
709
+ remoteHost = options . host + portUri ,
693
710
agent = _getAgent ( options . host , options . port , options . https || this . target . https ) ;
694
711
695
712
// Change headers (if requested).
@@ -777,12 +794,6 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
777
794
778
795
// Get the Non-Printable data
779
796
data = data . slice ( Buffer . byteLength ( sdata ) , data . length ) ;
780
-
781
- //
782
- // Replace the host and origin headers in the Printable data
783
- //
784
- sdata = sdata . replace ( remoteHost , options . host )
785
- . replace ( remoteHost , options . host ) ;
786
797
787
798
if ( self . https && ! self . target . https ) {
788
799
//
0 commit comments