@@ -115,13 +115,15 @@ private static void HandleClient(TcpClient client)
115
115
private static void HandleHttpSessionRequest ( TcpClient client , string httpCmd , Stream clientStream ,
116
116
CustomBinaryReader clientStreamReader , StreamWriter clientStreamWriter , string secureTunnelHostName )
117
117
{
118
+ TcpConnection connection = null ;
119
+ string lastRequestHostName = null ;
118
120
119
121
while ( true )
120
122
{
121
123
if ( string . IsNullOrEmpty ( httpCmd ) )
122
124
{
123
125
Dispose ( client , clientStream , clientStreamReader , clientStreamWriter , null ) ;
124
- return ;
126
+ break ;
125
127
}
126
128
127
129
var args = new SessionEventArgs ( BUFFER_SIZE ) ;
@@ -156,14 +158,14 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
156
158
args . ProxySession . Request . RequestHeaders = new List < HttpHeader > ( ) ;
157
159
158
160
string tmpLine ;
159
-
160
161
while ( ! string . IsNullOrEmpty ( tmpLine = clientStreamReader . ReadLine ( ) ) )
161
162
{
162
163
var header = tmpLine . Split ( new char [ ] { ':' } , 2 ) ;
163
164
args . ProxySession . Request . RequestHeaders . Add ( new HttpHeader ( header [ 0 ] , header [ 1 ] ) ) ;
164
165
}
165
166
166
167
SetRequestHeaders ( args . ProxySession . Request . RequestHeaders , args . ProxySession ) ;
168
+
167
169
if ( args . ProxySession . Request . UpgradeToWebSocket )
168
170
{
169
171
TcpHelper . SendRaw ( clientStreamReader . BaseStream , httpCmd , args . ProxySession . Request . RequestHeaders ,
@@ -179,7 +181,7 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
179
181
args . Client . ClientStream = clientStream ;
180
182
args . Client . ClientStreamReader = clientStreamReader ;
181
183
args . Client . ClientStreamWriter = clientStreamWriter ;
182
- args . ProxySession . Request . RequestHostname = args . ProxySession . Request . RequestUri . Host ;
184
+ args . ProxySession . Request . RequestHost = args . ProxySession . Request . RequestUri . Host ;
183
185
args . ProxySession . Request . RequestUrl = args . ProxySession . Request . RequestUri . OriginalString ;
184
186
args . Client . ClientPort = ( ( IPEndPoint ) client . Client . RemoteEndPoint ) . Port ;
185
187
args . Client . ClientIpAddress = ( ( IPEndPoint ) client . Client . RemoteEndPoint ) . Address ;
@@ -198,14 +200,19 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
198
200
if ( args . ProxySession . Request . CancelRequest )
199
201
{
200
202
Dispose ( client , clientStream , clientStreamReader , clientStreamWriter , args ) ;
201
- return ;
203
+ break ;
202
204
}
203
205
204
206
205
207
//construct the web request that we are going to issue on behalf of the client.
206
- var connection = TcpConnectionManager . GetClient ( args . ProxySession . Request . RequestUri . Host , args . ProxySession . Request . RequestUri . Port , args . IsHttps ) ;
207
- args . ProxySession . SetConnection ( connection ) ;
208
+ connection = connection == null ?
209
+ TcpConnectionManager . GetClient ( args . ProxySession . Request . RequestUri . Host , args . ProxySession . Request . RequestUri . Port , args . IsHttps )
210
+ : lastRequestHostName != args . ProxySession . Request . RequestHost ? TcpConnectionManager . GetClient ( args . ProxySession . Request . RequestUri . Host , args . ProxySession . Request . RequestUri . Port , args . IsHttps )
211
+ : connection ;
212
+
213
+ lastRequestHostName = args . ProxySession . Request . RequestHost ;
208
214
215
+ args . ProxySession . SetConnection ( connection ) ;
209
216
args . ProxySession . SendRequest ( ) ;
210
217
211
218
//If request was modified by user
@@ -233,8 +240,6 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
233
240
Dispose ( client , clientStream , clientStreamReader , clientStreamWriter , args ) ;
234
241
return ;
235
242
}
236
- else
237
- TcpConnectionManager . ReleaseClient ( connection ) ;
238
243
239
244
// read the next request
240
245
httpCmd = clientStreamReader . ReadLine ( ) ;
@@ -245,8 +250,11 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
245
250
Dispose ( client , clientStream , clientStreamReader , clientStreamWriter , args ) ;
246
251
break ;
247
252
}
253
+
248
254
}
249
255
256
+ if ( connection != null )
257
+ TcpConnectionManager . ReleaseClient ( connection ) ;
250
258
}
251
259
252
260
private static void WriteConnectResponse ( StreamWriter clientStreamWriter , string httpVersion )
0 commit comments