@@ -8,8 +8,9 @@ module.exports = WebsocketStream;
8
8
function WebsocketStream ( options , res ) {
9
9
Duplex . call ( this ) ;
10
10
11
- this . options = options ;
12
- this . res = res ;
11
+ this . options = options ;
12
+ this . res = res ;
13
+ this . handshakeDone = false ;
13
14
14
15
var self = this ;
15
16
@@ -28,9 +29,13 @@ WebsocketStream.prototype.onPipe = function(req) {
28
29
common . setupOutgoing ( self . options . ssl || { } , self . options , req )
29
30
) ;
30
31
31
- this . proxyReq . once ( 'response ' , function ( proxyRes ) {
32
- self . onResponse ( proxyRes ) ;
32
+ this . proxyReq . once ( 'socket ' , function ( proxySocket ) {
33
+ self . onSocket ( proxySocket ) ;
33
34
} ) ;
35
+ this . proxyReq . on ( 'upgrade' , function ( proxyRes , proxySocket , proxyHead ) {
36
+ self . onUpgrade ( proxyRes , proxySocket , proxyHead ) ;
37
+ } ) ;
38
+
34
39
this . proxyReq . on ( 'error' , function ( e ) {
35
40
self . onError ( e ) ;
36
41
} ) ;
@@ -40,8 +45,25 @@ WebsocketStream.prototype.onFinish = function() {
40
45
this . proxyReq . end ( ) ;
41
46
} ;
42
47
43
- WebsocketStream . prototype . onResponse = function ( proxyRes ) {
44
- this . proxyRes = proxyRes ;
48
+ WebsocketStream . prototype . onSocket = function ( proxySocket ) {
49
+
50
+
51
+ } ;
52
+
53
+ WebsocketStream . prototype . onUpgrade = function ( proxyRes , proxySocket , proxyHead ) {
54
+ this . handshake = {
55
+ headers : proxyRes . headers ,
56
+ statusCode : proxyRes . statusCode
57
+ } ;
58
+
59
+ this . proxyRes = proxyRes ;
60
+ this . proxySocket = proxySocket ;
61
+ this . proxyHead = proxyHead ;
62
+
63
+ proxySocket . setTimeout ( 0 ) ;
64
+ proxySocket . setNoDelay ( true ) ;
65
+
66
+ proxySocket . setKeepAlive ( true , 0 ) ;
45
67
46
68
47
69
} ;
@@ -52,9 +74,42 @@ WebsocketStream.prototype.onError = function(e) {
52
74
53
75
54
76
WebsocketStream . prototype . _write = function ( chunk , encoding , callback ) {
55
-
77
+ this . proxySocket . write ( chunk , encoding , callback ) ;
56
78
} ;
57
79
58
80
WebsocketStream . prototype . _read = function ( size ) {
59
-
60
- } ;
81
+ var chunk = ( this . proxySocket ? this . proxySocket . read ( size ) : '' ) || '' ;
82
+
83
+ if ( chunk && ! this . handshakeDone ) {
84
+ var headers = '' ;
85
+
86
+ if ( this . handshake . statusCode && this . handshake . statusCode == 101 ) {
87
+ headers = [
88
+ 'HTTP/1.1 101 Switching Protocols' ,
89
+ 'Upgrade: websocket' ,
90
+ 'Connection: Upgrade' ,
91
+ 'Sec-WebSocket-Accept: ' + this . handshake . headers [ 'sec-websocket-accept' ]
92
+ ] ;
93
+
94
+ headers = headers . concat ( '' , '' ) . join ( '\r\n' ) ;
95
+ }
96
+
97
+ /*
98
+ * Socket.IO specific code
99
+ */
100
+
101
+ var sdata = chunk . toString ( ) ;
102
+ sdata = sdata . substr ( 0 , sdata . search ( CRLF + CRLF ) ) ;
103
+ chunk = data . slice ( Buffer . byteLength ( sdata ) , data . length ) ;
104
+
105
+ if ( self . source . https && ! self . target . https ) { sdata = sdata . replace ( 'ws:' , 'wss:' ) ; }
106
+
107
+ this . push ( headers + sdata ) ;
108
+ this . push ( data ) ;
109
+
110
+ this . handshakeDone = true ;
111
+ return ;
112
+ }
113
+
114
+ this . push ( chunk ) ;
115
+ } ;
0 commit comments