@@ -52,10 +52,21 @@ private static async void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient
52
52
else
53
53
httpRemoteUri = new Uri ( httpCmdSplit [ 1 ] ) ;
54
54
55
- string httpVersion = "HTTP/1.1" ;
56
-
55
+ Version version = new Version ( 1 , 1 ) ;
57
56
if ( httpCmdSplit . Length == 3 )
58
- httpVersion = httpCmdSplit [ 2 ] ;
57
+ {
58
+ string httpVersion = httpCmdSplit [ 1 ] . Trim ( ) ;
59
+
60
+ if ( httpVersion == "http/1.1" )
61
+ {
62
+ version = new Version ( 1 , 1 ) ;
63
+ }
64
+ else
65
+ {
66
+ version = new Version ( 1 , 0 ) ;
67
+ }
68
+
69
+ }
59
70
60
71
var excluded = endPoint . ExcludedHttpsHostNameRegex != null ? endPoint . ExcludedHttpsHostNameRegex . Any ( x => Regex . IsMatch ( httpRemoteUri . Host , x ) ) : false ;
61
72
@@ -65,7 +76,7 @@ private static async void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient
65
76
httpRemoteUri = new Uri ( "https://" + httpCmdSplit [ 1 ] ) ;
66
77
await clientStreamReader . ReadAllLinesAsync ( ) . ConfigureAwait ( false ) ;
67
78
68
- await WriteConnectResponse ( clientStreamWriter , httpVersion ) . ConfigureAwait ( false ) ;
79
+ await WriteConnectResponse ( clientStreamWriter , version ) . ConfigureAwait ( false ) ;
69
80
70
81
var certificate = await CertManager . CreateCertificate ( httpRemoteUri . Host ) ;
71
82
@@ -101,7 +112,7 @@ await sslStream.AuthenticateAsServerAsync(certificate, false,
101
112
else if ( httpVerb . ToUpper ( ) == "CONNECT" )
102
113
{
103
114
await clientStreamReader . ReadAllLinesAsync ( ) . ConfigureAwait ( false ) ;
104
- await WriteConnectResponse ( clientStreamWriter , httpVersion ) . ConfigureAwait ( false ) ;
115
+ await WriteConnectResponse ( clientStreamWriter , version ) . ConfigureAwait ( false ) ;
105
116
106
117
await TcpHelper . SendRaw ( clientStream , null , null , httpRemoteUri . Host , httpRemoteUri . Port ,
107
118
false ) . ConfigureAwait ( false ) ;
@@ -178,7 +189,7 @@ private static async Task HandleHttpSessionRequest(TcpClient client, string http
178
189
CustomBinaryReader clientStreamReader , StreamWriter clientStreamWriter , bool IsHttps )
179
190
{
180
191
TcpConnection connection = null ;
181
- string lastRequestHostName = null ;
192
+ string lastRequest = null ;
182
193
183
194
while ( true )
184
195
{
@@ -197,10 +208,10 @@ private static async Task HandleHttpSessionRequest(TcpClient client, string http
197
208
var httpCmdSplit = httpCmd . Split ( Constants . SpaceSplit , 3 ) ;
198
209
199
210
var httpMethod = httpCmdSplit [ 0 ] ;
200
- var httpVersion = httpCmdSplit [ 2 ] ;
211
+ var httpVersion = httpCmdSplit [ 2 ] . ToLower ( ) . Trim ( ) ;
201
212
202
213
Version version ;
203
- if ( httpVersion == "HTTP /1.1" )
214
+ if ( httpVersion == "http /1.1" )
204
215
{
205
216
version = new Version ( 1 , 1 ) ;
206
217
}
@@ -219,26 +230,18 @@ private static async Task HandleHttpSessionRequest(TcpClient client, string http
219
230
}
220
231
221
232
var httpRemoteUri = new Uri ( ! IsHttps ? httpCmdSplit [ 1 ] : ( string . Concat ( "https://" , args . WebSession . Request . Host , httpCmdSplit [ 1 ] ) ) ) ;
222
- args . IsHttps = IsHttps ;
223
233
224
234
args . WebSession . Request . RequestUri = httpRemoteUri ;
225
235
226
236
args . WebSession . Request . Method = httpMethod ;
227
- args . WebSession . Request . HttpVersion = httpVersion ;
237
+ args . WebSession . Request . HttpVersion = version ;
228
238
args . Client . ClientStream = clientStream ;
229
239
args . Client . ClientStreamReader = clientStreamReader ;
230
240
args . Client . ClientStreamWriter = clientStreamWriter ;
231
241
232
- if ( args . WebSession . Request . UpgradeToWebSocket )
233
- {
234
- await TcpHelper . SendRaw ( clientStream , httpCmd , args . WebSession . Request . RequestHeaders ,
235
- httpRemoteUri . Host , httpRemoteUri . Port , args . IsHttps ) . ConfigureAwait ( false ) ;
236
- Dispose ( client , clientStream , clientStreamReader , clientStreamWriter , args ) ;
237
- return ;
238
- }
239
-
242
+
240
243
PrepareRequestHeaders ( args . WebSession . Request . RequestHeaders , args . WebSession ) ;
241
- args . WebSession . Request . Host = args . WebSession . Request . RequestUri . Host ;
244
+ args . WebSession . Request . Host = args . WebSession . Request . RequestUri . Authority ;
242
245
243
246
//If requested interception
244
247
if ( BeforeRequest != null )
@@ -251,16 +254,26 @@ await TcpHelper.SendRaw(clientStream, httpCmd, args.WebSession.Request.RequestHe
251
254
handlerTasks [ i ] = ( ( Func < object , SessionEventArgs , Task > ) invocationList [ i ] ) ( null , args ) ;
252
255
}
253
256
254
- await Task . WhenAll ( handlerTasks ) . ConfigureAwait ( false ) ;
257
+ await Task . WhenAll ( handlerTasks ) . ConfigureAwait ( false ) ;
258
+ }
259
+
260
+ if ( args . WebSession . Request . UpgradeToWebSocket )
261
+ {
262
+ await TcpHelper . SendRaw ( clientStream , httpCmd , args . WebSession . Request . RequestHeaders ,
263
+ httpRemoteUri . Host , httpRemoteUri . Port , args . IsHttps ) . ConfigureAwait ( false ) ;
264
+ Dispose ( client , clientStream , clientStreamReader , clientStreamWriter , args ) ;
265
+ return ;
255
266
}
256
267
257
268
//construct the web request that we are going to issue on behalf of the client.
258
269
connection = connection == null ?
259
270
await TcpConnectionManager . GetClient ( args , args . WebSession . Request . RequestUri . Host , args . WebSession . Request . RequestUri . Port , args . IsHttps , version ) . ConfigureAwait ( false )
260
- : lastRequestHostName != args . WebSession . Request . RequestUri . Host ? await TcpConnectionManager . GetClient ( args , args . WebSession . Request . RequestUri . Host , args . WebSession . Request . RequestUri . Port , args . IsHttps , version ) . ConfigureAwait ( false )
271
+ : lastRequest != args . WebSession . Request . RequestUri . Host ? await TcpConnectionManager . GetClient ( args , args . WebSession . Request . RequestUri . Host , args . WebSession . Request . RequestUri . Port , args . IsHttps , version ) . ConfigureAwait ( false )
261
272
: connection ;
262
273
263
- lastRequestHostName = args . WebSession . Request . RequestUri . Host ;
274
+ lastRequest = string . Concat ( args . WebSession . Request . RequestUri . Host , ":" ,
275
+ args . WebSession . Request . RequestUri . Port , ":" ,
276
+ args . IsHttps , ":" , version . ToString ( ) ) ;
264
277
265
278
args . WebSession . Request . RequestLocked = true ;
266
279
@@ -347,12 +360,12 @@ await TcpConnectionManager.GetClient(args, args.WebSession.Request.RequestUri.Ho
347
360
}
348
361
349
362
if ( connection != null )
350
- TcpConnectionManager . ReleaseClient ( connection ) ;
363
+ await TcpConnectionManager . ReleaseClient ( connection ) ;
351
364
}
352
365
353
- private static async Task WriteConnectResponse ( StreamWriter clientStreamWriter , string httpVersion )
366
+ private static async Task WriteConnectResponse ( StreamWriter clientStreamWriter , Version httpVersion )
354
367
{
355
- await clientStreamWriter . WriteLineAsync ( httpVersion + " 200 Connection established") . ConfigureAwait ( false ) ;
368
+ await clientStreamWriter . WriteLineAsync ( string . Format ( "HTTP/{0}.{1} {2}" , httpVersion . Major , httpVersion . Minor , " 200 Connection established") ) . ConfigureAwait ( false ) ;
356
369
await clientStreamWriter . WriteLineAsync ( string . Format ( "Timestamp: {0}" , DateTime . Now ) ) . ConfigureAwait ( false ) ;
357
370
await clientStreamWriter . WriteLineAsync ( ) . ConfigureAwait ( false ) ;
358
371
await clientStreamWriter . FlushAsync ( ) . ConfigureAwait ( false ) ;
0 commit comments